1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-29 01:50:06 +02:00

Rename <Slate> component value prop to initialValue (#5421)

* Rename `slate-react` Slate component `value` prop to `initialValue`

Fixes #4992

* Update documentation: `value` -> `initialValue`

* Add a changeset record

* Make props order consistent
This commit is contained in:
Ivan Voskoboinyk
2023-05-26 17:53:39 +03:00
committed by GitHub
parent 0b1799091a
commit 91e388ecd9
34 changed files with 71 additions and 56 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': minor
---
Rename `<Slate>` component prop from `value` to `initialValue` to emphasize uncontrolled nature of it

View File

@@ -53,7 +53,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable /> <Editable />
</Slate> </Slate>
) )

View File

@@ -309,7 +309,7 @@ const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null) const [selection, setSelection] = useState(null)
<Slate <Slate
value={value} initialValue={initialValue}
selection={selection} selection={selection}
onChange={(value, selection) => { onChange={(value, selection) => {
setValue(value) setValue(value)

View File

@@ -81,7 +81,7 @@ const initialValue = [
const App = () => { const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
// Render the Slate context. // Render the Slate context.
return <Slate editor={editor} value={initialValue} /> return <Slate editor={editor} initialValue={initialValue} />
} }
``` ```
@@ -107,7 +107,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
// Add the editable component inside the context. // Add the editable component inside the context.
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable /> <Editable />
</Slate> </Slate>
) )

View File

@@ -20,7 +20,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable /> <Editable />
</Slate> </Slate>
) )
@@ -41,7 +41,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
// Define a new handler which prints the key that was pressed. // Define a new handler which prints the key that was pressed.
onKeyDown={event => { onKeyDown={event => {
@@ -71,7 +71,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
onKeyDown={event => { onKeyDown={event => {
if (event.key === '&') { if (event.key === '&') {

View File

@@ -18,7 +18,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
onKeyDown={event => { onKeyDown={event => {
if (event.key === '&') { if (event.key === '&') {
@@ -88,7 +88,7 @@ const App = () => {
}, []) }, [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
// Pass in the `renderElement` function. // Pass in the `renderElement` function.
renderElement={renderElement} renderElement={renderElement}
@@ -142,7 +142,7 @@ const App = () => {
}, []) }, [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
onKeyDown={event => { onKeyDown={event => {
@@ -200,7 +200,7 @@ const App = () => {
}, []) }, [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
onKeyDown={event => { onKeyDown={event => {

View File

@@ -27,7 +27,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
onKeyDown={event => { onKeyDown={event => {
@@ -72,7 +72,7 @@ const App = () => {
}, []) }, [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
onKeyDown={event => { onKeyDown={event => {
@@ -163,7 +163,7 @@ const App = () => {
}, []) }, [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
// Pass in the `renderLeaf` function. // Pass in the `renderLeaf` function.

View File

@@ -35,7 +35,7 @@ const App = () => {
}, []) }, [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
renderLeaf={renderLeaf} renderLeaf={renderLeaf}
@@ -142,7 +142,7 @@ const App = () => {
}, []) }, [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
renderLeaf={renderLeaf} renderLeaf={renderLeaf}
@@ -200,7 +200,7 @@ const App = () => {
return ( return (
// Add a toolbar with buttons that call the same methods. // Add a toolbar with buttons that call the same methods.
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<div> <div>
<button <button
onMouseDown={event => { onMouseDown={event => {

View File

@@ -18,7 +18,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable /> <Editable />
</Slate> </Slate>
) )
@@ -45,7 +45,7 @@ const App = () => {
return ( return (
<Slate <Slate
editor={editor} editor={editor}
value={initialValue} initialValue={initialValue}
onChange={value => { onChange={value => {
const isAstChange = editor.operations.some( const isAstChange = editor.operations.some(
op => 'set_selection' !== op.type op => 'set_selection' !== op.type
@@ -85,7 +85,7 @@ const App = () => {
return ( return (
<Slate <Slate
editor={editor} editor={editor}
value={initialValue} initialValue={initialValue}
onChange={value => { onChange={value => {
const isAstChange = editor.operations.some( const isAstChange = editor.operations.some(
op => 'set_selection' !== op.type op => 'set_selection' !== op.type
@@ -145,7 +145,7 @@ const App = () => {
return ( return (
<Slate <Slate
editor={editor} editor={editor}
value={initialValue} initialValue={initialValue}
onChange={value => { onChange={value => {
const isAstChange = editor.operations.some( const isAstChange = editor.operations.some(
op => 'set_selection' !== op.type op => 'set_selection' !== op.type

View File

@@ -19,18 +19,18 @@ import { EDITOR_TO_ON_CHANGE } from '../utils/weak-maps'
export const Slate = (props: { export const Slate = (props: {
editor: ReactEditor editor: ReactEditor
value: Descendant[] initialValue: Descendant[]
children: React.ReactNode children: React.ReactNode
onChange?: (value: Descendant[]) => void onChange?: (value: Descendant[]) => void
}) => { }) => {
const { editor, children, onChange, value, ...rest } = props const { editor, children, onChange, initialValue, ...rest } = props
const unmountRef = useRef(false) const unmountRef = useRef(false)
const [context, setContext] = React.useState<SlateContextValue>(() => { const [context, setContext] = React.useState<SlateContextValue>(() => {
if (!Node.isNodeList(value)) { if (!Node.isNodeList(initialValue)) {
throw new Error( throw new Error(
`[Slate] value is invalid! Expected a list of elements but got: ${Scrubber.stringify( `[Slate] initialValue is invalid! Expected a list of elements but got: ${Scrubber.stringify(
value initialValue
)}` )}`
) )
} }
@@ -39,7 +39,7 @@ export const Slate = (props: {
`[Slate] editor is invalid! You passed: ${Scrubber.stringify(editor)}` `[Slate] editor is invalid! You passed: ${Scrubber.stringify(editor)}`
) )
} }
editor.children = value editor.children = initialValue
Object.assign(editor, rest) Object.assign(editor, rest)
return { v: 0, editor } return { v: 0, editor }
}) })

View File

@@ -10,7 +10,9 @@ const createNodeMock = () => ({
class MockResizeObserver { class MockResizeObserver {
observe() {} observe() {}
unobserve() {} unobserve() {}
disconnect() {} disconnect() {}
} }
@@ -21,14 +23,18 @@ describe('slate-react', () => {
describe('NODE_TO_KEY logic', () => { describe('NODE_TO_KEY logic', () => {
test('should not unmount the node that gets split on a split_node operation', async () => { test('should not unmount the node that gets split on a split_node operation', async () => {
const editor = withReact(createEditor()) const editor = withReact(createEditor())
const value = [{ type: 'block', children: [{ text: 'test' }] }] const initialValue = [{ type: 'block', children: [{ text: 'test' }] }]
const mounts = jest.fn() const mounts = jest.fn()
let el: ReactTestRenderer let el: ReactTestRenderer
act(() => { act(() => {
el = create( el = create(
<Slate editor={editor} value={value} onChange={() => {}}> <Slate
editor={editor}
initialValue={initialValue}
onChange={() => {}}
>
<Editable <Editable
renderElement={({ children }) => { renderElement={({ children }) => {
useEffect(() => mounts(), []) useEffect(() => mounts(), [])
@@ -52,7 +58,7 @@ describe('slate-react', () => {
test('should not unmount the node that gets merged into on a merge_node operation', async () => { test('should not unmount the node that gets merged into on a merge_node operation', async () => {
const editor = withReact(createEditor()) const editor = withReact(createEditor())
const value = [ const initialValue = [
{ type: 'block', children: [{ text: 'te' }] }, { type: 'block', children: [{ text: 'te' }] },
{ type: 'block', children: [{ text: 'st' }] }, { type: 'block', children: [{ text: 'st' }] },
] ]
@@ -62,7 +68,11 @@ describe('slate-react', () => {
act(() => { act(() => {
el = create( el = create(
<Slate editor={editor} value={value} onChange={() => {}}> <Slate
editor={editor}
initialValue={initialValue}
onChange={() => {}}
>
<Editable <Editable
renderElement={({ children }) => { renderElement={({ children }) => {
useEffect(() => mounts(), []) useEffect(() => mounts(), [])

View File

@@ -73,7 +73,7 @@ const CheckListsExample = () => {
) )
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
placeholder="Get to work…" placeholder="Get to work…"

View File

@@ -46,7 +46,7 @@ const CodeHighlightingExample = () => {
const onKeyDown = useOnKeydown(editor) const onKeyDown = useOnKeydown(editor)
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<ExampleToolbar /> <ExampleToolbar />
<SetNodeToDecorations /> <SetNodeToDecorations />
<Editable <Editable
@@ -324,7 +324,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable /> <Editable />
</Slate> </Slate>
) )

View File

@@ -13,7 +13,7 @@ const initialValue: Descendant[] = [
const PlainTextExample = () => { const PlainTextExample = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), []) const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
placeholder="Type something" placeholder="Type something"
renderPlaceholder={({ children, attributes }) => ( renderPlaceholder={({ children, attributes }) => (

View File

@@ -15,7 +15,7 @@ const EditableVoidsExample = () => {
) )
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Toolbar> <Toolbar>
<InsertEditableVoidButton /> <InsertEditableVoidButton />
</Toolbar> </Toolbar>

View File

@@ -16,7 +16,7 @@ import {
const EmbedsExample = () => { const EmbedsExample = () => {
const editor = useMemo(() => withEmbeds(withReact(createEditor())), []) const editor = useMemo(() => withEmbeds(withReact(createEditor())), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={props => <Element {...props} />} renderElement={props => <Element {...props} />}
placeholder="Enter some text..." placeholder="Enter some text..."

View File

@@ -74,7 +74,7 @@ const ForcedLayoutExample = () => {
[] []
) )
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
placeholder="Enter a title…" placeholder="Enter a title…"

View File

@@ -17,7 +17,7 @@ const HoveringMenuExample = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), []) const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<HoveringToolbar /> <HoveringToolbar />
<Editable <Editable
renderLeaf={props => <Leaf {...props} />} renderLeaf={props => <Leaf {...props} />}

View File

@@ -25,7 +25,7 @@ const HugeDocumentExample = () => {
const renderElement = useCallback(props => <Element {...props} />, []) const renderElement = useCallback(props => <Element {...props} />, [])
const editor = useMemo(() => withReact(createEditor()), []) const editor = useMemo(() => withReact(createEditor()), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable renderElement={renderElement} spellCheck autoFocus /> <Editable renderElement={renderElement} spellCheck autoFocus />
</Slate> </Slate>
) )

View File

@@ -25,7 +25,7 @@ const IFrameExample = () => {
const handleBlur = useCallback(() => ReactEditor.deselect(editor), [editor]) const handleBlur = useCallback(() => ReactEditor.deselect(editor), [editor])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Toolbar> <Toolbar>
<MarkButton format="bold" icon="format_bold" /> <MarkButton format="bold" icon="format_bold" />
<MarkButton format="italic" icon="format_italic" /> <MarkButton format="italic" icon="format_italic" />

View File

@@ -25,7 +25,7 @@ const ImagesExample = () => {
) )
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Toolbar> <Toolbar>
<InsertImageButton /> <InsertImageButton />
</Toolbar> </Toolbar>

View File

@@ -98,7 +98,7 @@ const InlinesExample = () => {
} }
return ( return (
<SlateReact.Slate editor={editor} value={initialValue}> <SlateReact.Slate editor={editor} initialValue={initialValue}>
<Toolbar> <Toolbar>
<AddLinkButton /> <AddLinkButton />
<RemoveLinkButton /> <RemoveLinkButton />

View File

@@ -48,7 +48,7 @@ const MarkdownPreviewExample = () => {
}, []) }, [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
decorate={decorate} decorate={decorate}
renderLeaf={renderLeaf} renderLeaf={renderLeaf}

View File

@@ -70,7 +70,7 @@ const MarkdownShortcutsExample = () => {
) )
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
onDOMBeforeInput={handleDOMBeforeInput} onDOMBeforeInput={handleDOMBeforeInput}
renderElement={renderElement} renderElement={renderElement}

View File

@@ -73,7 +73,7 @@ const MentionExample = () => {
return ( return (
<Slate <Slate
editor={editor} editor={editor}
value={initialValue} initialValue={initialValue}
onChange={() => { onChange={() => {
const { selection } = editor const { selection } = editor

View File

@@ -91,7 +91,7 @@ const PasteHtmlExample = () => {
[] []
) )
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable <Editable
renderElement={renderElement} renderElement={renderElement}
renderLeaf={renderLeaf} renderLeaf={renderLeaf}

View File

@@ -6,7 +6,7 @@ import { withHistory } from 'slate-history'
const PlainTextExample = () => { const PlainTextExample = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), []) const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable placeholder="Enter some plain text..." /> <Editable placeholder="Enter some plain text..." />
</Slate> </Slate>
) )

View File

@@ -5,7 +5,7 @@ import { Slate, Editable, withReact } from 'slate-react'
const ReadOnlyExample = () => { const ReadOnlyExample = () => {
const editor = useMemo(() => withReact(createEditor()), []) const editor = useMemo(() => withReact(createEditor()), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable readOnly placeholder="Enter some plain text..." /> <Editable readOnly placeholder="Enter some plain text..." />
</Slate> </Slate>
) )

View File

@@ -28,7 +28,7 @@ const RichTextExample = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), []) const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Toolbar> <Toolbar>
<MarkButton format="bold" icon="format_bold" /> <MarkButton format="bold" icon="format_bold" />
<MarkButton format="italic" icon="format_italic" /> <MarkButton format="italic" icon="format_italic" />

View File

@@ -51,7 +51,7 @@ const ScrollIntoViewExample = () => {
const PlainTextEditor = () => { const PlainTextEditor = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), []) const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable placeholder="Enter some plain text..." /> <Editable placeholder="Enter some plain text..." />
</Slate> </Slate>
) )

View File

@@ -37,7 +37,7 @@ const SearchHighlightingExample = () => {
) )
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Toolbar> <Toolbar>
<div <div
className={css` className={css`

View File

@@ -31,7 +31,7 @@ const ShadowEditor = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), []) const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable placeholder="Enter some plain text..." /> <Editable placeholder="Enter some plain text..." />
</Slate> </Slate>
) )

View File

@@ -11,7 +11,7 @@ const StylingExample = () => {
<div style={{ display: 'flex', flexDirection: 'column', gap: '40px' }}> <div style={{ display: 'flex', flexDirection: 'column', gap: '40px' }}>
<Slate <Slate
editor={editor1} editor={editor1}
value={[ initialValue={[
{ {
type: 'paragraph', type: 'paragraph',
children: [{ text: 'This editor is styled using the style prop.' }], children: [{ text: 'This editor is styled using the style prop.' }],
@@ -29,7 +29,7 @@ const StylingExample = () => {
<Slate <Slate
editor={editor2} editor={editor2}
value={[ initialValue={[
{ {
type: 'paragraph', type: 'paragraph',
children: [ children: [

View File

@@ -18,7 +18,7 @@ const TablesExample = () => {
[] []
) )
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} initialValue={initialValue}>
<Editable renderElement={renderElement} renderLeaf={renderLeaf} /> <Editable renderElement={renderElement} renderLeaf={renderLeaf} />
</Slate> </Slate>
) )