forked from keystonejs/keystone
/
Field.js
74 lines (67 loc) 路 2.11 KB
/
Field.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/** @jsx jsx */
import { Component } from 'react';
import { jsx } from '@emotion/core';
import { colors } from '@arch-ui/theme';
import Editor from './editor';
import { FieldContainer, FieldLabel, FieldInput } from '@arch-ui/fields';
import { inputStyles } from '@arch-ui/input';
class ErrorBoundary extends Component {
state = {
hasError: false,
};
static getDerivedStateFromError() {
return { hasError: true };
}
render() {
if (this.state.hasError) {
return <span css={{ color: colors.danger }}>Unable to render content</span>;
}
return this.props.children;
}
}
let ContentField = ({ field, value, onChange, autoFocus, errors, isDisabled }) => {
const htmlID = `ks-content-editor-${field.path}`;
return (
<FieldContainer>
<FieldLabel htmlFor={htmlID} field={field} errors={errors} />
<FieldInput
css={{ cursor: 'text', tabIndex: 0 }}
onClick={() => {
const elm = document.getElementById(htmlID).querySelector('[data-slate-editor]');
if (elm) {
elm.focus();
}
}}
>
<ErrorBoundary>
{Object.values(field.getBlocks())
.filter(({ Provider, options }) => Provider && options)
.reduce(
(children, { Provider, options }, index) => (
// Using index within key is ok here as the blocks never change
// across renders
<Provider value={options} key={`${htmlID}-provider-${index}`}>
{children}
</Provider>
),
<Editor
key={htmlID}
blocks={field.getBlocks()}
value={value}
onChange={onChange}
autoFocus={autoFocus}
id={htmlID}
css={{
...inputStyles({ isMultiline: true }),
padding: '16px 32px',
minHeight: 200,
}}
isDisabled={isDisabled}
/>
)}
</ErrorBoundary>
</FieldInput>
</FieldContainer>
);
};
export default ContentField;