From 9892cf0ffbd741cc2880d1f0bd0d7c1b36145bbd Mon Sep 17 00:00:00 2001 From: Eric Meier Date: Sun, 3 Apr 2022 16:52:32 +0100 Subject: [PATCH] Make onChange prop optional, update examples and docs to treat slate as uncontrolled (#4922) * Make onChange prop optional, update examples and docs to treat slate as uncontrolled * Add changeset --- .changeset/red-bottles-peel.md | 5 ++ docs/walkthroughs/01-installing-slate.md | 70 +++++-------------- docs/walkthroughs/02-adding-event-handlers.md | 45 ++++++------ .../03-defining-custom-elements.md | 60 ++++++++-------- .../04-applying-custom-formatting.md | 49 +++++++------ docs/walkthroughs/05-executing-commands.md | 45 ++++++------ docs/walkthroughs/06-saving-to-a-database.md | 50 ++++++------- packages/slate-react/src/components/slate.tsx | 7 +- site/examples/check-lists.tsx | 5 +- site/examples/code-highlighting.tsx | 3 +- site/examples/custom-placeholder.tsx | 19 +++-- site/examples/embeds.tsx | 5 +- site/examples/forced-layout.tsx | 5 +- site/examples/hovering-toolbar.tsx | 5 +- site/examples/huge-document.tsx | 5 +- site/examples/iframe.tsx | 3 +- site/examples/images.tsx | 5 +- site/examples/inlines.tsx | 9 +-- site/examples/markdown-preview.tsx | 7 +- site/examples/markdown-shortcuts.tsx | 5 +- site/examples/mentions.tsx | 6 +- site/examples/paste-html.tsx | 5 +- site/examples/plaintext.tsx | 5 +- site/examples/read-only.tsx | 7 +- site/examples/richtext.tsx | 5 +- site/examples/scroll-into-view.tsx | 5 +- site/examples/search-highlighting.tsx | 3 +- site/examples/shadow-dom.tsx | 5 +- site/examples/tables.tsx | 5 +- 29 files changed, 207 insertions(+), 246 deletions(-) create mode 100644 .changeset/red-bottles-peel.md diff --git a/.changeset/red-bottles-peel.md b/.changeset/red-bottles-peel.md new file mode 100644 index 0000000000..a087a3bddc --- /dev/null +++ b/.changeset/red-bottles-peel.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Make Slate component onChange optional diff --git a/docs/walkthroughs/01-installing-slate.md b/docs/walkthroughs/01-installing-slate.md index 7f4ea02aa1..75987f503c 100644 --- a/docs/walkthroughs/01-installing-slate.md +++ b/docs/walkthroughs/01-installing-slate.md @@ -66,53 +66,23 @@ declare module 'slate' { } ``` -```typescript -// Also you must annotate `useState` and the editor's initial value. -const App = () => { - const initialValue: CustomElement[] = [] - const [value, setValue] = useState(initialValue) - return ( - - ... - - ) -} -``` - -Next we want to create state for `value`: - -```jsx -const App = () => { - const [editor] = useState(() => withReact(createEditor())) - - // Keep track of state for the value of the editor. - const [value, setValue] = useState([]) - return null -} -``` - Next up is to render a `` context provider. The provider component keeps track of your Slate editor, its plugins, its value, its selection, and any changes that occur. It **must** be rendered above any `` components. But it can also provide the editor state to other components like toolbars, menus, etc. using the `useSlate` hook. ```jsx +const initialValue = [] + const App = () => { const [editor] = useState(() => withReact(createEditor())) - const [value, setValue] = useState([]) // Render the Slate context. - return ( - setValue(newValue)} - /> - ) + return } ``` You can think of the `` component as providing a context to every component underneath it. -> As of v0.67 the Slate Provider's "value" prop is now only used as initial state for editor.children. If your code relies on replacing editor.children you should do so by replacing it directly instead of relying on the "value" prop to do this for you. See [Slate PR 4540](https://github.com/ianstormtaylor/slate/pull/4540) for a more in-depth discussion. +> Slate Provider's "value" prop is only used as initial state for editor.children. If your code relies on replacing editor.children you should do so by replacing it directly instead of relying on the "value" prop to do this for you. See [Slate PR 4540](https://github.com/ianstormtaylor/slate/pull/4540) for a more in-depth discussion. This is a slightly different mental model than things like `` or `