Skip to content

Commit bc4b0db

Browse files
authored
fix(fields): force MarkdownEditor re-rendering on default value change (#87)
1 parent 2eefc23 commit bc4b0db

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

fields/MarkdownEditor/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Editable, Slate, withReact } from 'slate-react'
66
import styled from 'styled-components'
77

88
import { SIZE } from '../../common/constants'
9+
import { generateKeyFromValue } from '../../helpers/generateKeyFromValue'
910
import { Error, Helper, Label } from '../shared'
1011
import { MarkdownEditorFormat } from './constants'
1112
import { Element } from './Element'
@@ -76,6 +77,7 @@ export const MarkdownEditor: FunctionComponent<MarkdownEditorProps> = ({
7677

7778
const defaultValueAsAst = React.useMemo(() => deserialize(defaultValue), [defaultValue])
7879
const hasError = typeof error === 'string' && error.length > 0
80+
const key = React.useMemo(() => generateKeyFromValue(defaultValue), [defaultValue])
7981

8082
const handleChange = React.useCallback((newValueAsAst: Descendant[]) => {
8183
if (onChange === undefined) {
@@ -96,7 +98,7 @@ export const MarkdownEditor: FunctionComponent<MarkdownEditorProps> = ({
9698
)}
9799

98100
<EditorBox className="EditorBox" hasError={hasError}>
99-
<Slate editor={editor} onChange={handleChange} value={defaultValueAsAst}>
101+
<Slate key={key} editor={editor} onChange={handleChange} value={defaultValueAsAst}>
100102
<Toolbar />
101103

102104
<EditableBox className="EditableBox">

helpers/generateKeyFromValue.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sha1 from 'sha1'
2+
3+
export function generateKeyFromValue(value: any): string {
4+
return sha1(JSON.stringify(value))
5+
}

0 commit comments

Comments
 (0)