mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-17 20:51:20 +02:00
add guard checks for value and editor in <Slate /> (#3326)
* add guard checks for value and editor in <Slate /> * adding tiny-invariant to slate-react; refactoring errors * import Editor for guard check * Create fuzzy-yaks-drive.md Co-authored-by: Cameron Ackerman <cameron_ackerman@selinc.com> Co-authored-by: Ian Storm Taylor <ian@ianstormtaylor.com>
This commit is contained in:
5
.changeset/fuzzy-yaks-drive.md
Normal file
5
.changeset/fuzzy-yaks-drive.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Added invariants when passing invalud `value` or `editor` props to `<Editable>`.
|
@@ -20,7 +20,8 @@
|
|||||||
"is-hotkey": "^0.1.6",
|
"is-hotkey": "^0.1.6",
|
||||||
"is-plain-object": "^3.0.0",
|
"is-plain-object": "^3.0.0",
|
||||||
"lodash": "^4.17.4",
|
"lodash": "^4.17.4",
|
||||||
"scroll-into-view-if-needed": "^2.2.20"
|
"scroll-into-view-if-needed": "^2.2.20",
|
||||||
|
"tiny-invariant": "1.0.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"slate": "^0.61.3",
|
"slate": "^0.61.3",
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
import React, { useMemo, useState, useCallback, useEffect } from 'react'
|
import React, { useMemo, useState, useCallback, useEffect } from 'react'
|
||||||
import { Node, Element, Descendant } from 'slate'
|
import { Editor, Node, Element, Descendant } from 'slate'
|
||||||
|
import invariant from 'tiny-invariant'
|
||||||
|
|
||||||
import { ReactEditor } from '../plugin/react-editor'
|
import { ReactEditor } from '../plugin/react-editor'
|
||||||
import { FocusedContext } from '../hooks/use-focused'
|
import { FocusedContext } from '../hooks/use-focused'
|
||||||
@@ -21,6 +22,17 @@ export const Slate = (props: {
|
|||||||
const { editor, children, onChange, value, ...rest } = props
|
const { editor, children, onChange, value, ...rest } = props
|
||||||
const [key, setKey] = useState(0)
|
const [key, setKey] = useState(0)
|
||||||
const context: [ReactEditor] = useMemo(() => {
|
const context: [ReactEditor] = useMemo(() => {
|
||||||
|
invariant(
|
||||||
|
Node.isNodeList(value),
|
||||||
|
`[Slate] value is invalid! Expected a list of elements but got: ${JSON.stringify(
|
||||||
|
value
|
||||||
|
)}`
|
||||||
|
)
|
||||||
|
invariant(
|
||||||
|
Editor.isEditor(editor),
|
||||||
|
`[Slate] editor is invalid! you passed: ${JSON.stringify(editor)}`
|
||||||
|
)
|
||||||
|
|
||||||
editor.children = value
|
editor.children = value
|
||||||
Object.assign(editor, rest)
|
Object.assign(editor, rest)
|
||||||
return [editor]
|
return [editor]
|
||||||
|
Reference in New Issue
Block a user