forked from 10up/block-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (54 loc) · 1.81 KB
/
index.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
import { RichText } from '@wordpress/block-editor';
import { __experimentalNumberControl as NumberControl, ToggleControl } from '@wordpress/components';
import PropTypes from 'prop-types';
import { usePostMetaValue } from '../../hooks';
import { toSentence } from './utilities';
export const PostMeta = (props) => {
const { metaKey, children } = props;
const [metaValue, setMetaValue] = usePostMetaValue(metaKey);
const metaValueType = typeof metaValue;
if (typeof children === 'function') {
return children(metaValue, setMetaValue);
}
if (metaValueType === 'number') {
return <MetaNumber {...props} />;
}
if (metaValueType === 'boolean') {
return <MetaBoolean {...props} label={toSentence(metaKey)} />;
}
return <MetaString {...props} />;
};
PostMeta.propTypes = {
metaKey: PropTypes.string.isRequired,
};
const MetaString = (props) => {
const { metaKey, tagName = 'p' } = props;
const [metaValue, setMetaValue] = usePostMetaValue(metaKey);
return <RichText value={metaValue} onChange={setMetaValue} tagName={tagName} {...props} />;
};
MetaString.propTypes = {
metaKey: PropTypes.string.isRequired,
tagName: PropTypes.string,
};
MetaString.defaultProps = {
tagName: 'p',
};
const MetaNumber = (props) => {
const { metaKey } = props;
const [metaValue, setMetaValue] = usePostMetaValue(metaKey);
return <NumberControl value={metaValue} onChange={setMetaValue} {...props} />;
};
MetaNumber.propTypes = {
metaKey: PropTypes.string.isRequired,
};
const MetaBoolean = (props) => {
const { metaKey } = props;
const [metaValue, setMetaValue] = usePostMetaValue(metaKey);
return <ToggleControl checked={metaValue} onChange={setMetaValue} {...props} />;
};
MetaBoolean.propTypes = {
metaKey: PropTypes.string.isRequired,
};
PostMeta.String = MetaString;
PostMeta.Number = MetaNumber;
PostMeta.Boolean = MetaBoolean;