Skip to content
This repository was archived by the owner on May 31, 2020. It is now read-only.

Commit 6586bfe

Browse files
committed
fix toggle-checked in checkable-list example
1 parent a553e48 commit 6586bfe

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

examples/checkable-list-example/src/CheckableList.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// @flow
22

33
import React, { Component, Fragment } from 'react'
4-
import { ContentBlock, RichUtils } from 'draft-js'
4+
import { EditorState, ContentBlock, DefaultDraftBlockRenderMap } from 'draft-js'
55
import { Plugin, withPluginContext } from '@djsp/core'
6-
import { insertEntityBlock } from '@djsp/utils'
7-
import AtomicBlock from '@djsp/atomic-block'
8-
import { CheckableListItem, CheckableListItemBlock, CHECKABLE_LIST_ITEM, CheckableListItemUtils } from 'draft-js-checkable-list-item'
6+
import { insertEntityBlock, mergeEntityData } from '@djsp/utils'
7+
import { CheckableListItem, CheckableListItemBlock, CHECKABLE_LIST_ITEM, blockRenderMap } from 'draft-js-checkable-list-item'
98

109

1110
type Props = {
@@ -24,20 +23,25 @@ class CheckableList extends Component<Props> {
2423

2524
const { setEditorState, editorState } = this.props
2625
setEditorState(insertEntityBlock(editorState, CHECKABLE_LIST_ITEM, {
27-
checked: true
26+
checked: false
2827
}))
2928
}
3029

3130
toggleChecked = (block) => {
3231
const { setEditorState, editorState } = this.props
33-
console.log(CheckableListItemUtils.toggleChecked(editorState, block))
34-
setEditorState(
35-
CheckableListItemUtils.toggleChecked(editorState, block)
36-
);
32+
const content = editorState.getCurrentContent();
33+
const entityKey = block.getEntityAt(0);
34+
const data = content.getEntity(entityKey).getData();
35+
36+
let newEditorState = mergeEntityData(editorState, entityKey, { checked: !data.checked })
37+
setEditorState(EditorState.forceSelection(
38+
newEditorState,
39+
editorState.getSelection()
40+
));
3741
}
3842

3943
blockRendererFn = (block: ContentBlock): ?CheckableListItemBlock => {
40-
const { setEditorState, editorState } = this.props;
44+
const { editorState } = this.props;
4145
var content = editorState.getCurrentContent();
4246

4347
if (block.getType() === 'atomic') {
@@ -51,28 +55,25 @@ class CheckableList extends Component<Props> {
5155
component: CheckableListItem,
5256
props: {
5357
onChangeChecked: () => this.toggleChecked(block),
54-
checked: !!block.getData().get('checked'),
58+
checked: !!data.checked,
5559
},
5660
}
5761
}
5862
}
5963
}
6064

65+
blockStyleFn(block: ContentBlock): ?string {
66+
if (block.getType() === CHECKABLE_LIST_ITEM) {
67+
return CHECKABLE_LIST_ITEM
68+
}
69+
}
70+
6171
render() {
6272
return (<Fragment>
63-
{/* <AtomicBlock type={CHECKABLE_LIST_ITEM}>
64-
{({ isFocused, blockProps: { checked }, ...otherProps }) => {
65-
const props = {
66-
checked,
67-
onChangeChecked: () => this.toggleChecked(otherProps.block)
68-
}
69-
70-
return (
71-
<CheckableListItem blockProps={props} {...otherProps} />
72-
)
73-
}}
74-
</AtomicBlock> */}
75-
<Plugin blockRendererFn={this.blockRendererFn}/>
73+
<Plugin
74+
blockRendererFn={this.blockRendererFn}
75+
blockRenderMap={DefaultDraftBlockRenderMap.merge(blockRenderMap)}
76+
blockStyleFn={this.blockStyleFn} />
7677
<button type="button" onClick={this.onClick}>
7778
Insert checkable-list
7879
</button>

0 commit comments

Comments
 (0)