@homecode/ui 4.27.16 → 4.27.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
22
|
|
@@ -63,6 +63,8 @@ function Form(props) {
|
|
|
63
63
|
const touchedRef = useRef(touched);
|
|
64
64
|
const errorsRef = useRef(errors);
|
|
65
65
|
const onChangeRef = useRef(onChange);
|
|
66
|
+
const onChangeHandlerRef = useRef(null); // Add this
|
|
67
|
+
const onBlurHandlerRef = useRef(null); // Add this
|
|
66
68
|
const setValues = (newValues) => {
|
|
67
69
|
_setValues(newValues);
|
|
68
70
|
valuesRef.current = newValues;
|
|
@@ -182,14 +184,18 @@ function Form(props) {
|
|
|
182
184
|
const newValues = { ...valuesRef.current, [field]: val };
|
|
183
185
|
if (onChangeRef.current && !onChangeRef.current(newValues))
|
|
184
186
|
return;
|
|
187
|
+
const isTouched = !compare(val, initialValues[field]);
|
|
185
188
|
setValue(field, val);
|
|
186
|
-
setFieldTouched(field,
|
|
189
|
+
setFieldTouched(field, isTouched);
|
|
187
190
|
calcChanged(field, val);
|
|
188
191
|
validate();
|
|
189
|
-
}, [calcChanged, validate]);
|
|
192
|
+
}, [calcChanged, validate, initialValues]);
|
|
190
193
|
const onBlurHandler = useCallback((name) => {
|
|
191
194
|
setFieldTouched(name, true);
|
|
192
195
|
}, []);
|
|
196
|
+
// Update refs to always point to latest handlers
|
|
197
|
+
onChangeHandlerRef.current = onChangeHandler;
|
|
198
|
+
onBlurHandlerRef.current = onBlurHandler;
|
|
193
199
|
const onSubmitHandler = async (e) => {
|
|
194
200
|
e?.preventDefault();
|
|
195
201
|
dropFocusFromSubmit();
|
|
@@ -199,7 +205,6 @@ function Form(props) {
|
|
|
199
205
|
await onSubmit({ ...values });
|
|
200
206
|
setIsLoading(false);
|
|
201
207
|
};
|
|
202
|
-
// Simple field component - let memo on Field handle optimization
|
|
203
208
|
const FieldComponent = useRef((fieldProps) => {
|
|
204
209
|
const { name } = fieldProps;
|
|
205
210
|
const fullProps = {
|
|
@@ -209,8 +214,8 @@ function Form(props) {
|
|
|
209
214
|
markEdited,
|
|
210
215
|
isChanged: changedRef.current[name],
|
|
211
216
|
isTouched: touchedRef.current[name],
|
|
212
|
-
handleChange:
|
|
213
|
-
handleBlur:
|
|
217
|
+
handleChange: (...args) => onChangeHandlerRef.current(...args),
|
|
218
|
+
handleBlur: (...args) => onBlurHandlerRef.current(...args),
|
|
214
219
|
};
|
|
215
220
|
if (validationSchemaRef.current?.[name]?.empty === false) {
|
|
216
221
|
fullProps.required = true;
|
|
@@ -238,41 +243,29 @@ function Form(props) {
|
|
|
238
243
|
// Effects
|
|
239
244
|
useEffect(() => {
|
|
240
245
|
validationSchemaRef.current = validationSchema;
|
|
246
|
+
validate();
|
|
241
247
|
}, [validationSchema]);
|
|
248
|
+
useEffect(() => {
|
|
249
|
+
defaultValuesRef.current = updateDefaultValues();
|
|
250
|
+
calcChangedAll(initialValues);
|
|
251
|
+
}, [defaultValues]);
|
|
252
|
+
useEffect(() => {
|
|
253
|
+
setValues(cloneValues(initialValues));
|
|
254
|
+
setTouched({});
|
|
255
|
+
setChanged({});
|
|
256
|
+
setIsDirty(false);
|
|
257
|
+
updateIsEmpty();
|
|
258
|
+
validate();
|
|
259
|
+
if (onInit)
|
|
260
|
+
onInit(formAPI);
|
|
261
|
+
}, [initialValues]);
|
|
242
262
|
useEffect(() => {
|
|
243
263
|
calcChangedAll();
|
|
244
264
|
validate();
|
|
245
265
|
if (onInit)
|
|
246
266
|
onInit(formAPI);
|
|
247
267
|
}, []);
|
|
248
|
-
|
|
249
|
-
const validationChanged = !compare(validationSchema, validationSchemaRef.current);
|
|
250
|
-
const initialValsChanged = !compare(initialValues, props.initialValues);
|
|
251
|
-
const defaultValsChanged = !compare(defaultValues, props.defaultValues);
|
|
252
|
-
if (initialValsChanged) {
|
|
253
|
-
setValues(cloneValues(initialValues));
|
|
254
|
-
validate();
|
|
255
|
-
if (onInit)
|
|
256
|
-
onInit(formAPI);
|
|
257
|
-
}
|
|
258
|
-
if (defaultValsChanged) {
|
|
259
|
-
defaultValuesRef.current = updateDefaultValues();
|
|
260
|
-
}
|
|
261
|
-
if (initialValsChanged || defaultValsChanged) {
|
|
262
|
-
calcChangedAll(initialValues);
|
|
263
|
-
}
|
|
264
|
-
if (initialValsChanged || validationChanged) {
|
|
265
|
-
validate();
|
|
266
|
-
}
|
|
267
|
-
}, [
|
|
268
|
-
initialValues,
|
|
269
|
-
validationSchema,
|
|
270
|
-
defaultValues,
|
|
271
|
-
onInit,
|
|
272
|
-
formAPI,
|
|
273
|
-
calcChangedAll,
|
|
274
|
-
validate,
|
|
275
|
-
]);
|
|
268
|
+
console.log('FORM: initialValues', initialValues);
|
|
276
269
|
const classes = cn(S.root, className, isLoading && S.isLoading);
|
|
277
270
|
return (jsx("form", { className: classes, ...restProps, onSubmit: onSubmitHandler, children: children(formAPI) }));
|
|
278
271
|
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { Size } from 'uilib/types';
|
|
2
2
|
import { ButtonProps } from '../Button/Button';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
3
4
|
type ID = string | number;
|
|
4
|
-
export type Item = ButtonProps & {
|
|
5
|
+
export type Item = Omit<ButtonProps, 'children'> & {
|
|
5
6
|
id: ID;
|
|
6
7
|
label: string;
|
|
7
|
-
content:
|
|
8
|
+
content: ReactNode | (() => ReactNode);
|
|
8
9
|
contentClassName?: string;
|
|
9
10
|
forceRender?: boolean;
|
|
10
11
|
onClick?: (e: MouseEvent) => boolean | void;
|
|
12
|
+
children?: ButtonProps['children'];
|
|
11
13
|
};
|
|
12
14
|
export type RenderProps = {
|
|
13
15
|
tabs: React.ReactNode;
|