-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.tsx
More file actions
148 lines (137 loc) · 3.24 KB
/
index.tsx
File metadata and controls
148 lines (137 loc) · 3.24 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { useContext, useMemo } from '@wordpress/element';
import {
__experimentalHStack as HStack,
__experimentalVStack as VStack,
__experimentalHeading as Heading,
__experimentalSpacer as Spacer,
BaseControl,
} from '@wordpress/components';
/**
* Internal dependencies
*/
import type {
Form,
FieldLayoutProps,
NormalizedRegularLayout,
} from '../../types';
import DataFormContext from '../../components/dataform-context';
import { DataFormLayout } from '../data-form-layout';
import { isCombinedField } from '../is-combined-field';
import { DEFAULT_LAYOUT, normalizeLayout } from '../../normalize-form-fields';
function Header( { title }: { title: string } ) {
return (
<VStack className="dataforms-layouts-regular__header" spacing={ 4 }>
<HStack alignment="center">
<Heading level={ 2 } size={ 13 }>
{ title }
</Heading>
<Spacer />
</HStack>
</VStack>
);
}
export default function FormRegularField< Item >( {
data,
field,
onChange,
hideLabelFromVision,
}: FieldLayoutProps< Item > ) {
const { fields } = useContext( DataFormContext );
const form: Form = useMemo(
(): Form => ( {
layout: DEFAULT_LAYOUT,
fields: isCombinedField( field ) ? field.children : [],
} ),
[ field ]
);
if ( isCombinedField( field ) ) {
return (
<>
{ ! hideLabelFromVision && field.label && (
<Header title={ field.label } />
) }
<DataFormLayout
data={ data }
form={ form }
onChange={ onChange }
/>
</>
);
}
const layout: NormalizedRegularLayout = normalizeLayout( {
...field.layout,
type: 'regular',
} ) as NormalizedRegularLayout;
const labelPosition = layout.labelPosition;
const fieldDefinition = fields.find(
( fieldDef ) => fieldDef.id === field.id
);
if ( ! fieldDefinition || ! fieldDefinition.Edit ) {
return null;
}
if ( labelPosition === 'side' ) {
return (
<HStack className="dataforms-layouts-regular__field">
<div
className={ clsx(
'dataforms-layouts-regular__field-label',
`dataforms-layouts-regular__field-label--label-position-${ labelPosition }`
) }
>
{ fieldDefinition.label }
</div>
<div className="dataforms-layouts-regular__field-control">
{ fieldDefinition.readOnly === true ? (
<fieldDefinition.render
item={ data }
field={ fieldDefinition }
/>
) : (
<fieldDefinition.Edit
key={ fieldDefinition.id }
data={ data }
field={ fieldDefinition }
onChange={ onChange }
hideLabelFromVision
/>
) }
</div>
</HStack>
);
}
return (
<div className="dataforms-layouts-regular__field">
{ fieldDefinition.readOnly === true ? (
<>
<>
{ ! hideLabelFromVision && labelPosition !== 'none' && (
<BaseControl.VisualLabel>
{ fieldDefinition.label }
</BaseControl.VisualLabel>
) }
<fieldDefinition.render
item={ data }
field={ fieldDefinition }
/>
</>
</>
) : (
<fieldDefinition.Edit
data={ data }
field={ fieldDefinition }
onChange={ onChange }
hideLabelFromVision={
labelPosition === 'none' ? true : hideLabelFromVision
}
/>
) }
</div>
);
}