1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-02 03:32:36 +02:00

Update examples in docs, editable voids example (#4925)

* Update examples in docs, editable voids example

* Update typescript docs
This commit is contained in:
Eric Meier
2022-04-03 17:57:38 +01:00
committed by GitHub
parent aff67312cb
commit f17413086e
4 changed files with 12 additions and 14 deletions

View File

@@ -35,24 +35,25 @@ declare module 'slate' {
## Annotations in the Editor ## Annotations in the Editor
Annotate `useState` w/ `<Descendant[]>` and the editor's initial value w/ your custom Element type. Annotate the editor's initial value w/ `Descendant[]`.
```tsx ```tsx
import React, { useMemo, useState } from 'react' import React, { useMemo, useState } from 'react'
import { createEditor, Descendant } from 'slate' import { createEditor, Descendant } from 'slate'
import { Slate, Editable, withReact } from 'slate-react' import { Slate, Editable, withReact } from 'slate-react'
const initialValue: Descendant[] = [
{
type: 'paragraph',
children: [{ text: 'A line of text in a paragraph.' }],
},
]
const App = () => { const App = () => {
const initialValue: Descendant[] = [
{
type: 'paragraph',
children: [{ text: 'A line of text in a paragraph.' }],
},
]
const editor = useMemo(() => withReact(createEditor()), []) const editor = useMemo(() => withReact(createEditor()), [])
const [value, setValue] = useState<Descendant[]>(initialValue)
return ( return (
<Slate editor={editor} value={value} onChange={setValue}> <Slate editor={editor} value={initialValue}>
<Editable /> <Editable />
</Slate> </Slate>
) )

View File

@@ -95,7 +95,6 @@ const initialValue = []
const App = () => { const App = () => {
const [editor] = useState(() => withReact(createEditor())) const [editor] = useState(() => withReact(createEditor()))
const [value, setValue] = useState([])
return ( return (
// Add the editable component inside the context. // Add the editable component inside the context.
<Slate editor={editor} value={initialValue}> <Slate editor={editor} value={initialValue}>
@@ -122,7 +121,6 @@ const initialValue = [
const App = () => { const App = () => {
const [editor] = useState(() => withReact(createEditor()) const [editor] = useState(() => withReact(createEditor())
const [value, setValue] = useState()
return ( return (
<Slate editor={editor} value={initialValue}> <Slate editor={editor} value={initialValue}>

View File

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

View File

@@ -9,14 +9,13 @@ import { Button, Icon, Toolbar } from '../components'
import { EditableVoidElement } from './custom-types' import { EditableVoidElement } from './custom-types'
const EditableVoidsExample = () => { const EditableVoidsExample = () => {
const [value, setValue] = useState<Descendant[]>(initialValue)
const editor = useMemo( const editor = useMemo(
() => withEditableVoids(withHistory(withReact(createEditor()))), () => withEditableVoids(withHistory(withReact(createEditor()))),
[] []
) )
return ( return (
<Slate editor={editor} value={value} onChange={setValue}> <Slate editor={editor} value={initialValue}>
<Toolbar> <Toolbar>
<InsertEditableVoidButton /> <InsertEditableVoidButton />
</Toolbar> </Toolbar>