-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathmodal.tsx
More file actions
214 lines (200 loc) · 4.84 KB
/
modal.tsx
File metadata and controls
214 lines (200 loc) · 4.84 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* External dependencies
*/
import deepMerge from 'deepmerge';
/**
* WordPress dependencies
*/
import {
__experimentalSpacer as Spacer,
Button,
Modal,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useContext, useState, useMemo } from '@wordpress/element';
import { useFocusOnMount } from '@wordpress/compose';
import { Stack } from '@wordpress/ui';
/**
* Internal dependencies
*/
import type {
Field,
NormalizedForm,
NormalizedFormField,
NormalizedField,
} from '../../../types';
import { DataFormLayout } from '../data-form-layout';
import { DEFAULT_LAYOUT } from '../normalize-form';
import SummaryButton from './summary-button';
import useFormValidity from '../../../hooks/use-form-validity';
import DataFormContext from '../../dataform-context';
function ModalContent< Item >( {
data,
field,
onChange,
fieldLabel,
onClose,
}: {
data: Item;
field: NormalizedFormField;
onChange: ( data: Partial< Item > ) => void;
onClose: () => void;
fieldLabel: string;
} ) {
const { fields } = useContext( DataFormContext );
const [ changes, setChanges ] = useState< Partial< Item > >( {} );
const modalData = useMemo( () => {
return deepMerge( data, changes, {
arrayMerge: ( target, source ) => source,
} );
}, [ data, changes ] );
const form: NormalizedForm = useMemo(
() => ( {
layout: DEFAULT_LAYOUT,
fields: !! field.children
? field.children
: // If not explicit children return the field id itself.
[ { id: field.id, layout: DEFAULT_LAYOUT } ],
} ),
[ field ]
);
function denormalizeFields< T >(
normalizedFields: NormalizedField< T >[]
): Field< T >[] {
return normalizedFields.map( ( f ) => ( {
...f,
properties: f.properties
? Object.fromEntries(
Object.entries( f.properties ).map(
( [ key, property ] ) => [
key,
denormalizeFields( [ property ] )[ 0 ],
]
)
)
: {},
Edit: f.Edit === null ? undefined : f.Edit,
isValid: {
required: f.isValid.required?.constraint,
elements: f.isValid.elements?.constraint,
min: f.isValid.min?.constraint,
max: f.isValid.max?.constraint,
pattern: f.isValid.pattern?.constraint,
minLength: f.isValid.minLength?.constraint,
maxLength: f.isValid.maxLength?.constraint,
},
} ) ) as Field< T >[];
}
const fieldsAsFieldType: Field< Item >[] = denormalizeFields( fields );
const { validity } = useFormValidity( modalData, fieldsAsFieldType, form );
const onApply = () => {
onChange( changes );
onClose();
};
const handleOnChange = ( newValue: Partial< Item > ) => {
setChanges( ( prev ) =>
deepMerge( prev, newValue, {
arrayMerge: ( target, source ) => source,
} )
);
};
const focusOnMountRef = useFocusOnMount( 'firstInputElement' );
return (
<Modal
className="dataforms-layouts-panel__modal"
onRequestClose={ onClose }
isFullScreen={ false }
title={ fieldLabel }
size="medium"
>
<div ref={ focusOnMountRef }>
<DataFormLayout
data={ modalData }
form={ form }
onChange={ handleOnChange }
validity={ validity }
>
{ ( FieldLayout, childField, childFieldValidity ) => (
<FieldLayout
key={ childField.id }
data={ modalData }
field={ childField }
onChange={ handleOnChange }
hideLabelFromVision={ form.fields.length < 2 }
validity={ childFieldValidity }
/>
) }
</DataFormLayout>
</div>
<Stack
direction="row"
className="dataforms-layouts-panel__modal-footer"
gap="sm"
>
<Spacer style={ { flex: 1 } } />
<Button
variant="tertiary"
onClick={ onClose }
__next40pxDefaultSize
>
{ __( 'Cancel' ) }
</Button>
<Button
variant="primary"
onClick={ onApply }
__next40pxDefaultSize
>
{ __( 'Apply' ) }
</Button>
</Stack>
</Modal>
);
}
function PanelModal< Item >( {
data,
field,
onChange,
labelPosition,
summaryFields,
fieldDefinition,
onOpen,
}: {
data: Item;
field: NormalizedFormField;
onChange: ( value: any ) => void;
labelPosition: 'side' | 'top' | 'none';
summaryFields: NormalizedField< Item >[];
fieldDefinition: NormalizedField< Item >;
onOpen?: () => void;
} ) {
const [ isOpen, setIsOpen ] = useState( false );
const fieldLabel = !! field.children ? field.label : fieldDefinition?.label;
return (
<>
<SummaryButton
summaryFields={ summaryFields }
data={ data }
labelPosition={ labelPosition }
fieldLabel={ fieldLabel }
disabled={ fieldDefinition.readOnly === true }
onClick={ () => {
if ( onOpen ) {
onOpen();
}
setIsOpen( true );
} }
aria-expanded={ isOpen }
/>
{ isOpen && (
<ModalContent
data={ data }
field={ field }
onChange={ onChange }
fieldLabel={ fieldLabel ?? '' }
onClose={ () => setIsOpen( false ) }
/>
) }
</>
);
}
export default PanelModal;