mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 19:01:54 +02:00
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
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useMemo, useCallback } from 'react'
|
||||
import React, { useMemo, useCallback } from 'react'
|
||||
import {
|
||||
Slate,
|
||||
Editable,
|
||||
@@ -66,7 +66,6 @@ const initialValue: Descendant[] = [
|
||||
]
|
||||
|
||||
const CheckListsExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderElement = useCallback(props => <Element {...props} />, [])
|
||||
const editor = useMemo(
|
||||
() => withChecklists(withHistory(withReact(createEditor()))),
|
||||
@@ -74,7 +73,7 @@ const CheckListsExample = () => {
|
||||
)
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable
|
||||
renderElement={renderElement}
|
||||
placeholder="Get to work…"
|
||||
|
@@ -10,7 +10,6 @@ import { withHistory } from 'slate-history'
|
||||
import { css } from '@emotion/css'
|
||||
|
||||
const CodeHighlightingExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const [language, setLanguage] = useState('html')
|
||||
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
@@ -46,7 +45,7 @@ const CodeHighlightingExample = () => {
|
||||
)
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<div
|
||||
contentEditable={false}
|
||||
style={{ position: 'relative', top: '5px', right: '5px' }}
|
||||
|
@@ -1,13 +1,19 @@
|
||||
import React, { useState, useMemo } from 'react'
|
||||
import React, { useMemo } from 'react'
|
||||
import { createEditor, Descendant } from 'slate'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
import { withHistory } from 'slate-history'
|
||||
|
||||
const initialValue: Descendant[] = [
|
||||
{
|
||||
type: 'paragraph',
|
||||
children: [{ text: '' }],
|
||||
},
|
||||
]
|
||||
|
||||
const PlainTextExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable
|
||||
placeholder="Type something"
|
||||
renderPlaceholder={({ children, attributes }) => (
|
||||
@@ -24,11 +30,4 @@ const PlainTextExample = () => {
|
||||
)
|
||||
}
|
||||
|
||||
const initialValue: Descendant[] = [
|
||||
{
|
||||
type: 'paragraph',
|
||||
children: [{ text: '' }],
|
||||
},
|
||||
]
|
||||
|
||||
export default PlainTextExample
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useMemo } from 'react'
|
||||
import React, { useMemo } from 'react'
|
||||
import {
|
||||
Transforms,
|
||||
createEditor,
|
||||
@@ -14,10 +14,9 @@ import {
|
||||
} from 'slate-react'
|
||||
|
||||
const EmbedsExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(() => withEmbeds(withReact(createEditor())), [])
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable
|
||||
renderElement={props => <Element {...props} />}
|
||||
placeholder="Enter some text..."
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useCallback, useMemo } from 'react'
|
||||
import React, { useCallback, useMemo } from 'react'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
import {
|
||||
Transforms,
|
||||
@@ -64,14 +64,13 @@ const withLayout = editor => {
|
||||
}
|
||||
|
||||
const ForcedLayoutExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderElement = useCallback(props => <Element {...props} />, [])
|
||||
const editor = useMemo(
|
||||
() => withLayout(withHistory(withReact(createEditor()))),
|
||||
[]
|
||||
)
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable
|
||||
renderElement={renderElement}
|
||||
placeholder="Enter a title…"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useMemo, useRef, useEffect } from 'react'
|
||||
import React, { useMemo, useRef, useEffect } from 'react'
|
||||
import { Slate, Editable, withReact, useSlate, useFocused } from 'slate-react'
|
||||
import {
|
||||
Editor,
|
||||
@@ -14,11 +14,10 @@ import { withHistory } from 'slate-history'
|
||||
import { Button, Icon, Menu, Portal } from '../components'
|
||||
|
||||
const HoveringMenuExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<HoveringToolbar />
|
||||
<Editable
|
||||
renderLeaf={props => <Leaf {...props} />}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useMemo, useCallback } from 'react'
|
||||
import React, { useMemo, useCallback } from 'react'
|
||||
import faker from 'faker'
|
||||
import { createEditor, Descendant } from 'slate'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
@@ -22,11 +22,10 @@ for (let h = 0; h < HEADINGS; h++) {
|
||||
}
|
||||
|
||||
const HugeDocumentExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderElement = useCallback(props => <Element {...props} />, [])
|
||||
const editor = useMemo(() => withReact(createEditor()), [])
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable renderElement={renderElement} spellCheck autoFocus />
|
||||
</Slate>
|
||||
)
|
||||
|
@@ -15,7 +15,6 @@ const HOTKEYS = {
|
||||
}
|
||||
|
||||
const IFrameExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderElement = useCallback(
|
||||
({ attributes, children }) => <p {...attributes}>{children}</p>,
|
||||
[]
|
||||
@@ -26,7 +25,7 @@ const IFrameExample = () => {
|
||||
const handleBlur = useCallback(() => ReactEditor.deselect(editor), [editor])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Toolbar>
|
||||
<MarkButton format="bold" icon="format_bold" />
|
||||
<MarkButton format="italic" icon="format_italic" />
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useMemo } from 'react'
|
||||
import React, { useMemo } from 'react'
|
||||
import imageExtensions from 'image-extensions'
|
||||
import isUrl from 'is-url'
|
||||
import { Transforms, createEditor, Descendant } from 'slate'
|
||||
@@ -18,14 +18,13 @@ import { Button, Icon, Toolbar } from '../components'
|
||||
import { ImageElement } from './custom-types'
|
||||
|
||||
const ImagesExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(
|
||||
() => withImages(withHistory(withReact(createEditor()))),
|
||||
[]
|
||||
)
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Toolbar>
|
||||
<InsertImageButton />
|
||||
</Toolbar>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useMemo } from 'react'
|
||||
import React, { useMemo } from 'react'
|
||||
import isUrl from 'is-url'
|
||||
import { isKeyHotkey } from 'is-hotkey'
|
||||
import { css } from '@emotion/css'
|
||||
@@ -61,7 +61,6 @@ const initialValue: Descendant[] = [
|
||||
},
|
||||
]
|
||||
const InlinesExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(
|
||||
() => withInlines(withHistory(withReact(createEditor()))),
|
||||
[]
|
||||
@@ -92,11 +91,7 @@ const InlinesExample = () => {
|
||||
}
|
||||
|
||||
return (
|
||||
<SlateReact.Slate
|
||||
editor={editor}
|
||||
value={value}
|
||||
onChange={value => setValue(value)}
|
||||
>
|
||||
<SlateReact.Slate editor={editor} value={initialValue}>
|
||||
<Toolbar>
|
||||
<AddLinkButton />
|
||||
<RemoveLinkButton />
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import Prism from 'prismjs'
|
||||
import React, { useState, useCallback, useMemo } from 'react'
|
||||
import React, { useCallback, useMemo } from 'react'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
import { Text, createEditor, Element, Descendant } from 'slate'
|
||||
import { Text, createEditor, Descendant } from 'slate'
|
||||
import { withHistory } from 'slate-history'
|
||||
import { css } from '@emotion/css'
|
||||
|
||||
@@ -9,7 +9,6 @@ import { css } from '@emotion/css'
|
||||
;Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/^>(?:[\t ]*>)*/m,alias:"punctuation"},code:[{pattern:/^(?: {4}|\t).+/m,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*(?:\r?\n|\r)(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/(^\s*)#+.+/m,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/(^\s*)([*-])([\t ]*\2){2,}(?=\s*$)/m,lookbehind:!0,alias:"punctuation"},list:{pattern:/(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,punctuation:/^[\[\]!:]|[<>]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^\*\*|^__|\*\*$|__$/}},italic:{pattern:/(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^[*_]|[*_]$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:\\.|[^"\\])*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.italic.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.bold.inside.italic=Prism.util.clone(Prism.languages.markdown.italic),Prism.languages.markdown.italic.inside.bold=Prism.util.clone(Prism.languages.markdown.bold); // prettier-ignore
|
||||
|
||||
const MarkdownPreviewExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
const decorate = useCallback(([node, path]) => {
|
||||
@@ -51,7 +50,7 @@ const MarkdownPreviewExample = () => {
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable
|
||||
decorate={decorate}
|
||||
renderLeaf={renderLeaf}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useCallback, useMemo } from 'react'
|
||||
import React, { useCallback, useMemo } from 'react'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
import {
|
||||
Editor,
|
||||
@@ -26,14 +26,13 @@ const SHORTCUTS = {
|
||||
}
|
||||
|
||||
const MarkdownShortcutsExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderElement = useCallback(props => <Element {...props} />, [])
|
||||
const editor = useMemo(
|
||||
() => withShortcuts(withReact(withHistory(createEditor()))),
|
||||
[]
|
||||
)
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable
|
||||
renderElement={renderElement}
|
||||
placeholder="Write some markdown..."
|
||||
|
@@ -15,7 +15,6 @@ import { MentionElement } from './custom-types'
|
||||
|
||||
const MentionExample = () => {
|
||||
const ref = useRef<HTMLDivElement | null>()
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const [target, setTarget] = useState<Range | undefined>()
|
||||
const [index, setIndex] = useState(0)
|
||||
const [search, setSearch] = useState('')
|
||||
@@ -73,9 +72,8 @@ const MentionExample = () => {
|
||||
return (
|
||||
<Slate
|
||||
editor={editor}
|
||||
value={value}
|
||||
onChange={value => {
|
||||
setValue(value)
|
||||
value={initialValue}
|
||||
onChange={() => {
|
||||
const { selection } = editor
|
||||
|
||||
if (selection && Range.isCollapsed(selection)) {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useCallback, useMemo } from 'react'
|
||||
import React, { useCallback, useMemo } from 'react'
|
||||
import { jsx } from 'slate-hyperscript'
|
||||
import { Transforms, createEditor, Descendant } from 'slate'
|
||||
import { withHistory } from 'slate-history'
|
||||
@@ -84,7 +84,6 @@ export const deserialize = el => {
|
||||
}
|
||||
|
||||
const PasteHtmlExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderElement = useCallback(props => <Element {...props} />, [])
|
||||
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
|
||||
const editor = useMemo(
|
||||
@@ -92,7 +91,7 @@ const PasteHtmlExample = () => {
|
||||
[]
|
||||
)
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable
|
||||
renderElement={renderElement}
|
||||
renderLeaf={renderLeaf}
|
||||
|
@@ -1,13 +1,12 @@
|
||||
import React, { useState, useMemo } from 'react'
|
||||
import React, { useMemo } from 'react'
|
||||
import { createEditor, Descendant } from 'slate'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
import { withHistory } from 'slate-history'
|
||||
|
||||
const PlainTextExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable placeholder="Enter some plain text..." />
|
||||
</Slate>
|
||||
)
|
||||
|
@@ -1,12 +1,11 @@
|
||||
import React, { useState, useMemo } from 'react'
|
||||
import { createEditor, Descendant, Element } from 'slate'
|
||||
import React, { useMemo } from 'react'
|
||||
import { createEditor, Descendant } from 'slate'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
|
||||
const ReadOnlyExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(() => withReact(createEditor()), [])
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable readOnly placeholder="Enter some plain text..." />
|
||||
</Slate>
|
||||
)
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useCallback, useMemo, useState } from 'react'
|
||||
import React, { useCallback, useMemo } from 'react'
|
||||
import isHotkey from 'is-hotkey'
|
||||
import { Editable, withReact, useSlate, Slate } from 'slate-react'
|
||||
import {
|
||||
@@ -23,13 +23,12 @@ const LIST_TYPES = ['numbered-list', 'bulleted-list']
|
||||
const TEXT_ALIGN_TYPES = ['left', 'center', 'right', 'justify']
|
||||
|
||||
const RichTextExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderElement = useCallback(props => <Element {...props} />, [])
|
||||
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Toolbar>
|
||||
<MarkButton format="bold" icon="format_bold" />
|
||||
<MarkButton format="italic" icon="format_italic" />
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useMemo } from 'react'
|
||||
import React, { useMemo } from 'react'
|
||||
import { createEditor, Descendant } from 'slate'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
import { withHistory } from 'slate-history'
|
||||
@@ -49,10 +49,9 @@ const ScrollIntoViewExample = () => {
|
||||
}
|
||||
|
||||
const PlainTextEditor = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable placeholder="Enter some plain text..." />
|
||||
</Slate>
|
||||
)
|
||||
|
@@ -7,7 +7,6 @@ import { withHistory } from 'slate-history'
|
||||
import { Icon, Toolbar } from '../components'
|
||||
|
||||
const SearchHighlightingExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const [search, setSearch] = useState<string | undefined>()
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
const decorate = useCallback(
|
||||
@@ -38,7 +37,7 @@ const SearchHighlightingExample = () => {
|
||||
)
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Toolbar>
|
||||
<div
|
||||
className={css`
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import ReactDOM from 'react-dom'
|
||||
import React, { useState, useMemo, useRef, useEffect } from 'react'
|
||||
import React, { useMemo, useRef, useEffect } from 'react'
|
||||
import { createEditor, Descendant } from 'slate'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
import { withHistory } from 'slate-history'
|
||||
@@ -28,11 +28,10 @@ const ShadowDOM = () => {
|
||||
}
|
||||
|
||||
const ShadowEditor = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
|
||||
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable placeholder="Enter some plain text..." />
|
||||
</Slate>
|
||||
)
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useCallback, useMemo } from 'react'
|
||||
import React, { useCallback, useMemo } from 'react'
|
||||
import { Slate, Editable, withReact } from 'slate-react'
|
||||
import {
|
||||
Editor,
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
import { withHistory } from 'slate-history'
|
||||
|
||||
const TablesExample = () => {
|
||||
const [value, setValue] = useState<Descendant[]>(initialValue)
|
||||
const renderElement = useCallback(props => <Element {...props} />, [])
|
||||
const renderLeaf = useCallback(props => <Leaf {...props} />, [])
|
||||
const editor = useMemo(
|
||||
@@ -19,7 +18,7 @@ const TablesExample = () => {
|
||||
[]
|
||||
)
|
||||
return (
|
||||
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
|
||||
<Slate editor={editor} value={initialValue}>
|
||||
<Editable renderElement={renderElement} renderLeaf={renderLeaf} />
|
||||
</Slate>
|
||||
)
|
||||
|
Reference in New Issue
Block a user