Jake Vanderwerf
2026-09-15 bdf344f19158a980a99dcca7c82c6e34e84533c0
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
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { PanelBody, SelectControl, Placeholder } from '@wordpress/components';
import { useEntityProp } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import Fields from '../_meta/Fields';
 
const Edit = ({ attributes, setAttributes }) => {
    const { fieldName } = attributes;
    const { fields } = window.jvbFieldConfig || {};
 
    const postType = useSelect(
        (select) => select('core/editor').getCurrentPostType(),
        []
    );
    const [meta, setMeta] = useEntityProp('postType', postType, 'meta');
 
    const field = (fields || []).find((f) => f.name === fieldName);
 
    const blockProps = useBlockProps();
 
    return (
        <div {...blockProps}>
            <InspectorControls>
                <PanelBody title="Field">
                    <SelectControl
                        label="Meta field"
                        value={fieldName}
                        options={[
                            { label: 'Select a field…', value: '' },
                            ...(fields || []).map((f) => ({ label: f.label || f.name, value: f.name })),
                        ]}
                        onChange={(value) => setAttributes({ fieldName: value })}
                    />
                </PanelBody>
            </InspectorControls>
 
            { !field && <Placeholder label="Meta Field" instructions="Select a field in the block settings." /> }
 
            { field && (
                <Fields
                    field={field}
                    value={meta?.[fieldName]}
                    onChange={(value) => setMeta({ ...meta, [fieldName]: value })}
                    meta={meta}
                />
            ) }
        </div>
    );
};
 
export default Edit;