Skip to content

Commit

Permalink
Rename <Slate> component value prop to initialValue (#5421)
Browse files Browse the repository at this point in the history
* Rename `slate-react` Slate component `value` prop to `initialValue`

Fixes #4992

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

* Add a changeset record

* Make props order consistent
  • Loading branch information
e1himself committed May 26, 2023
1 parent 0b17990 commit 91e388e
Show file tree
Hide file tree
Showing 34 changed files with 71 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-suns-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': minor
---

Rename `<Slate>` component prop from `value` to `initialValue` to emphasize uncontrolled nature of it
2 changes: 1 addition & 1 deletion docs/concepts/12-typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor()))

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable />
</Slate>
)
Expand Down
2 changes: 1 addition & 1 deletion docs/general/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ const [value, setValue] = useState(initialValue)
const [selection, setSelection] = useState(null)

<Slate
value={value}
initialValue={initialValue}
selection={selection}
onChange={(value, selection) => {
setValue(value)
Expand Down
4 changes: 2 additions & 2 deletions docs/walkthroughs/01-installing-slate.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const initialValue = [
const App = () => {
const [editor] = useState(() => withReact(createEditor()))
// Render the Slate context.
return <Slate editor={editor} value={initialValue} />
return <Slate editor={editor} initialValue={initialValue} />
}
```

Expand All @@ -107,7 +107,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor()))
return (
// Add the editable component inside the context.
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable />
</Slate>
)
Expand Down
6 changes: 3 additions & 3 deletions docs/walkthroughs/02-adding-event-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor()))

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

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

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
onKeyDown={event => {
if (event.key === '&') {
Expand Down
8 changes: 4 additions & 4 deletions docs/walkthroughs/03-defining-custom-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor()))

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
onKeyDown={event => {
if (event.key === '&') {
Expand Down Expand Up @@ -88,7 +88,7 @@ const App = () => {
}, [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
// Pass in the `renderElement` function.
renderElement={renderElement}
Expand Down Expand Up @@ -142,7 +142,7 @@ const App = () => {
}, [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
onKeyDown={event => {
Expand Down Expand Up @@ -200,7 +200,7 @@ const App = () => {
}, [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
onKeyDown={event => {
Expand Down
6 changes: 3 additions & 3 deletions docs/walkthroughs/04-applying-custom-formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor()))

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
onKeyDown={event => {
Expand Down Expand Up @@ -72,7 +72,7 @@ const App = () => {
}, [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
onKeyDown={event => {
Expand Down Expand Up @@ -163,7 +163,7 @@ const App = () => {
}, [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
// Pass in the `renderLeaf` function.
Expand Down
6 changes: 3 additions & 3 deletions docs/walkthroughs/05-executing-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const App = () => {
}, [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
renderLeaf={renderLeaf}
Expand Down Expand Up @@ -142,7 +142,7 @@ const App = () => {
}, [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
renderLeaf={renderLeaf}
Expand Down Expand Up @@ -200,7 +200,7 @@ const App = () => {

return (
// Add a toolbar with buttons that call the same methods.
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<div>
<button
onMouseDown={event => {
Expand Down
8 changes: 4 additions & 4 deletions docs/walkthroughs/06-saving-to-a-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor()))

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable />
</Slate>
)
Expand All @@ -45,7 +45,7 @@ const App = () => {
return (
<Slate
editor={editor}
value={initialValue}
initialValue={initialValue}
onChange={value => {
const isAstChange = editor.operations.some(
op => 'set_selection' !== op.type
Expand Down Expand Up @@ -85,7 +85,7 @@ const App = () => {
return (
<Slate
editor={editor}
value={initialValue}
initialValue={initialValue}
onChange={value => {
const isAstChange = editor.operations.some(
op => 'set_selection' !== op.type
Expand Down Expand Up @@ -145,7 +145,7 @@ const App = () => {
return (
<Slate
editor={editor}
value={initialValue}
initialValue={initialValue}
onChange={value => {
const isAstChange = editor.operations.some(
op => 'set_selection' !== op.type
Expand Down
12 changes: 6 additions & 6 deletions packages/slate-react/src/components/slate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import { EDITOR_TO_ON_CHANGE } from '../utils/weak-maps'

export const Slate = (props: {
editor: ReactEditor
value: Descendant[]
initialValue: Descendant[]
children: React.ReactNode
onChange?: (value: Descendant[]) => void
}) => {
const { editor, children, onChange, value, ...rest } = props
const { editor, children, onChange, initialValue, ...rest } = props
const unmountRef = useRef(false)

const [context, setContext] = React.useState<SlateContextValue>(() => {
if (!Node.isNodeList(value)) {
if (!Node.isNodeList(initialValue)) {
throw new Error(
`[Slate] value is invalid! Expected a list of elements but got: ${Scrubber.stringify(
value
`[Slate] initialValue is invalid! Expected a list of elements but got: ${Scrubber.stringify(
initialValue
)}`
)
}
Expand All @@ -39,7 +39,7 @@ export const Slate = (props: {
`[Slate] editor is invalid! You passed: ${Scrubber.stringify(editor)}`
)
}
editor.children = value
editor.children = initialValue
Object.assign(editor, rest)
return { v: 0, editor }
})
Expand Down
18 changes: 14 additions & 4 deletions packages/slate-react/test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const createNodeMock = () => ({

class MockResizeObserver {
observe() {}

unobserve() {}

disconnect() {}
}

Expand All @@ -21,14 +23,18 @@ describe('slate-react', () => {
describe('NODE_TO_KEY logic', () => {
test('should not unmount the node that gets split on a split_node operation', async () => {
const editor = withReact(createEditor())
const value = [{ type: 'block', children: [{ text: 'test' }] }]
const initialValue = [{ type: 'block', children: [{ text: 'test' }] }]
const mounts = jest.fn()

let el: ReactTestRenderer

act(() => {
el = create(
<Slate editor={editor} value={value} onChange={() => {}}>
<Slate
editor={editor}
initialValue={initialValue}
onChange={() => {}}
>
<Editable
renderElement={({ children }) => {
useEffect(() => mounts(), [])
Expand All @@ -52,7 +58,7 @@ describe('slate-react', () => {

test('should not unmount the node that gets merged into on a merge_node operation', async () => {
const editor = withReact(createEditor())
const value = [
const initialValue = [
{ type: 'block', children: [{ text: 'te' }] },
{ type: 'block', children: [{ text: 'st' }] },
]
Expand All @@ -62,7 +68,11 @@ describe('slate-react', () => {

act(() => {
el = create(
<Slate editor={editor} value={value} onChange={() => {}}>
<Slate
editor={editor}
initialValue={initialValue}
onChange={() => {}}
>
<Editable
renderElement={({ children }) => {
useEffect(() => mounts(), [])
Expand Down
2 changes: 1 addition & 1 deletion site/examples/check-lists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const CheckListsExample = () => {
)

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
placeholder="Get to work…"
Expand Down
4 changes: 2 additions & 2 deletions site/examples/code-highlighting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const CodeHighlightingExample = () => {
const onKeyDown = useOnKeydown(editor)

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<ExampleToolbar />
<SetNodeToDecorations />
<Editable
Expand Down Expand Up @@ -324,7 +324,7 @@ const App = () => {
const [editor] = useState(() => withReact(createEditor()))
return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable />
</Slate>
)
Expand Down
2 changes: 1 addition & 1 deletion site/examples/custom-placeholder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const initialValue: Descendant[] = [
const PlainTextExample = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
placeholder="Type something"
renderPlaceholder={({ children, attributes }) => (
Expand Down
2 changes: 1 addition & 1 deletion site/examples/editable-voids.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const EditableVoidsExample = () => {
)

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Toolbar>
<InsertEditableVoidButton />
</Toolbar>
Expand Down
2 changes: 1 addition & 1 deletion site/examples/embeds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
const EmbedsExample = () => {
const editor = useMemo(() => withEmbeds(withReact(createEditor())), [])
return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={props => <Element {...props} />}
placeholder="Enter some text..."
Expand Down
2 changes: 1 addition & 1 deletion site/examples/forced-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const ForcedLayoutExample = () => {
[]
)
return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
renderElement={renderElement}
placeholder="Enter a title…"
Expand Down
2 changes: 1 addition & 1 deletion site/examples/hovering-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const HoveringMenuExample = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<HoveringToolbar />
<Editable
renderLeaf={props => <Leaf {...props} />}
Expand Down
2 changes: 1 addition & 1 deletion site/examples/huge-document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const HugeDocumentExample = () => {
const renderElement = useCallback(props => <Element {...props} />, [])
const editor = useMemo(() => withReact(createEditor()), [])
return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable renderElement={renderElement} spellCheck autoFocus />
</Slate>
)
Expand Down
2 changes: 1 addition & 1 deletion site/examples/iframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const IFrameExample = () => {
const handleBlur = useCallback(() => ReactEditor.deselect(editor), [editor])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Toolbar>
<MarkButton format="bold" icon="format_bold" />
<MarkButton format="italic" icon="format_italic" />
Expand Down
2 changes: 1 addition & 1 deletion site/examples/images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const ImagesExample = () => {
)

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Toolbar>
<InsertImageButton />
</Toolbar>
Expand Down
2 changes: 1 addition & 1 deletion site/examples/inlines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const InlinesExample = () => {
}

return (
<SlateReact.Slate editor={editor} value={initialValue}>
<SlateReact.Slate editor={editor} initialValue={initialValue}>
<Toolbar>
<AddLinkButton />
<RemoveLinkButton />
Expand Down
2 changes: 1 addition & 1 deletion site/examples/markdown-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const MarkdownPreviewExample = () => {
}, [])

return (
<Slate editor={editor} value={initialValue}>
<Slate editor={editor} initialValue={initialValue}>
<Editable
decorate={decorate}
renderLeaf={renderLeaf}
Expand Down

0 comments on commit 91e388e

Please sign in to comment.