gform-react 2.6.3 → 2.7.0
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/dist/cjs/gform-react.development.js +54 -47
- package/dist/cjs/gform-react.development.js.map +1 -1
- package/dist/cjs/gform-react.production.js +1 -1
- package/dist/cjs/gform-react.production.js.map +1 -1
- package/dist/esm/GForm.development.js +14 -43
- package/dist/esm/GForm.development.js.map +1 -1
- package/dist/esm/GForm.production.js +1 -1
- package/dist/esm/GForm.production.js.map +1 -1
- package/dist/esm/GInput.development.js +3 -4
- package/dist/esm/GInput.development.js.map +1 -1
- package/dist/esm/GInput.production.js +1 -1
- package/dist/esm/GInput.production.js.map +1 -1
- package/dist/esm/shared.development.js +41 -4
- package/dist/esm/shared.development.js.map +1 -1
- package/dist/esm/shared.production.js +1 -1
- package/dist/esm/shared.production.js.map +1 -1
- package/dist/esm/useFormSelector.development.js +1 -1
- package/dist/esm/useFormSelector.production.js +1 -1
- package/native/dist/cjs/gform-react.development.js +8 -4
- package/native/dist/cjs/gform-react.development.js.map +1 -1
- package/native/dist/cjs/gform-react.production.js +1 -1
- package/native/dist/cjs/gform-react.production.js.map +1 -1
- package/native/dist/esm/shared.development.js +8 -4
- package/native/dist/esm/shared.development.js.map +1 -1
- package/native/dist/esm/shared.production.js +1 -1
- package/native/dist/esm/shared.production.js.map +1 -1
- package/package.json +1 -1
|
@@ -233,6 +233,40 @@ const _merge = (target = {}, ...nodes) => {
|
|
|
233
233
|
}
|
|
234
234
|
return _merge(target, ...nodes);
|
|
235
235
|
};
|
|
236
|
+
const _mergeRefs = (refA, refB) => {
|
|
237
|
+
return value => {
|
|
238
|
+
if (typeof refA === 'function') {
|
|
239
|
+
refA(value);
|
|
240
|
+
} else if (refA) {
|
|
241
|
+
refA.current = value;
|
|
242
|
+
}
|
|
243
|
+
if (typeof refB === 'function') {
|
|
244
|
+
refB(value);
|
|
245
|
+
} else if (refB) {
|
|
246
|
+
refB.current = value;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
const _buildFormState = (fields, formElement, dispatchChanges) => {
|
|
251
|
+
const isFormValid = _checkIfFormIsValid(fields);
|
|
252
|
+
const formState = {
|
|
253
|
+
...fields,
|
|
254
|
+
isValid: isFormValid,
|
|
255
|
+
isInvalid: !isFormValid,
|
|
256
|
+
toRawData: options => _toRawData(fields, options),
|
|
257
|
+
toFormData: () => _toFormData(formElement),
|
|
258
|
+
toURLSearchParams: _toURLSearchParams,
|
|
259
|
+
checkValidity: function () {
|
|
260
|
+
this.isValid = formElement && formElement.checkValidity() || false;
|
|
261
|
+
this.isInvalid = !this.isValid;
|
|
262
|
+
return this.isValid;
|
|
263
|
+
},
|
|
264
|
+
dispatchChanges: changes => dispatchChanges({
|
|
265
|
+
fields: _merge({}, fields, changes)
|
|
266
|
+
})
|
|
267
|
+
};
|
|
268
|
+
return formState;
|
|
269
|
+
};
|
|
236
270
|
|
|
237
271
|
let handlersMap;
|
|
238
272
|
let validityMap;
|
|
@@ -352,10 +386,9 @@ const useFormHandlers = (getState, setState, validators = {}, optimized = false)
|
|
|
352
386
|
const _viHandler = (input, e) => {
|
|
353
387
|
if (!input) return;
|
|
354
388
|
const element = e && e.target;
|
|
355
|
-
const hasInitialValue = !input.dirty && input.value && !input.touched;
|
|
356
|
-
if (!element && !hasInitialValue) return;
|
|
357
389
|
if (typeof document !== 'undefined' && (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement)) {
|
|
358
390
|
if (!input.checkValidity) input.checkValidity = () => element.checkValidity();
|
|
391
|
+
const hasInitialValue = !input.dirty && input.value && !input.touched;
|
|
359
392
|
if (hasInitialValue) {
|
|
360
393
|
_checkInputManually(input);
|
|
361
394
|
_dispatchChanges(input, input.formKey);
|
|
@@ -484,7 +517,12 @@ const GFormContextProvider = ({
|
|
|
484
517
|
const stateRef = React.useRef(initialState);
|
|
485
518
|
const listeners = React.useRef(null);
|
|
486
519
|
const setState = React.useCallback(updater => {
|
|
487
|
-
|
|
520
|
+
const prevState = stateRef.current;
|
|
521
|
+
const nextState = typeof updater === "function" ? updater(prevState) : updater;
|
|
522
|
+
if (Object.is(prevState, nextState)) {
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
stateRef.current = nextState;
|
|
488
526
|
listeners.current.forEach(l => l());
|
|
489
527
|
}, []);
|
|
490
528
|
const handlers = useFormHandlers(() => stateRef.current, setState, validators, optimized);
|
|
@@ -549,46 +587,15 @@ const FormRenderer = React.forwardRef(({
|
|
|
549
587
|
}, ref) => {
|
|
550
588
|
const formRef = React.useRef(null);
|
|
551
589
|
const {
|
|
552
|
-
|
|
553
|
-
|
|
590
|
+
handlers,
|
|
591
|
+
getState
|
|
554
592
|
} = useFormStore();
|
|
555
593
|
const fields = useFormSelector(state => state.fields);
|
|
556
|
-
const refHandler = React.useCallback(element => {
|
|
557
|
-
if (ref) {
|
|
558
|
-
if (typeof ref === 'function') {
|
|
559
|
-
ref(element);
|
|
560
|
-
} else {
|
|
561
|
-
ref.current = element;
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
formRef.current = element;
|
|
565
|
-
}, [ref]);
|
|
566
|
-
const getFormState = React.useCallback(() => {
|
|
567
|
-
const isFormValid = _checkIfFormIsValid(fields);
|
|
568
|
-
const formState = {
|
|
569
|
-
...fields,
|
|
570
|
-
isValid: isFormValid,
|
|
571
|
-
isInvalid: !isFormValid,
|
|
572
|
-
toRawData: options => _toRawData(fields, options),
|
|
573
|
-
toFormData: () => _toFormData(formRef.current),
|
|
574
|
-
toURLSearchParams: _toURLSearchParams,
|
|
575
|
-
checkValidity: function () {
|
|
576
|
-
this.isValid = formRef.current && formRef.current.checkValidity() || false;
|
|
577
|
-
this.isInvalid = !this.isValid;
|
|
578
|
-
return this.isValid;
|
|
579
|
-
},
|
|
580
|
-
dispatchChanges: changes => handlers._dispatchChanges({
|
|
581
|
-
fields: _merge({}, fields, changes)
|
|
582
|
-
})
|
|
583
|
-
};
|
|
584
|
-
if (stateRef) stateRef.current = formState;
|
|
585
|
-
return formState;
|
|
586
|
-
}, [fields]);
|
|
587
594
|
const formComponent = React.useMemo(() => {
|
|
588
|
-
const state =
|
|
595
|
+
const state = _buildFormState(fields, formRef.current, handlers._dispatchChanges);
|
|
589
596
|
const formChildren = typeof children === 'function' ? children(state) : children;
|
|
590
597
|
const _onSubmit = e => {
|
|
591
|
-
const state =
|
|
598
|
+
const state = _buildFormState(fields, formRef.current, handlers._dispatchChanges);
|
|
592
599
|
if (state.isValid && onSubmit) {
|
|
593
600
|
onSubmit(state, e);
|
|
594
601
|
}
|
|
@@ -603,6 +610,7 @@ const FormRenderer = React.forwardRef(({
|
|
|
603
610
|
if (onKeyUp) {
|
|
604
611
|
_onKeyUp = e => onKeyUp(state, e);
|
|
605
612
|
}
|
|
613
|
+
if (stateRef) stateRef.current = state;
|
|
606
614
|
if (handlers.optimized) {
|
|
607
615
|
if (onChange) {
|
|
608
616
|
_onChange = (e, unknown) => {
|
|
@@ -615,7 +623,7 @@ const FormRenderer = React.forwardRef(({
|
|
|
615
623
|
};
|
|
616
624
|
}
|
|
617
625
|
return React.createElement("form", _extends({}, rest, {
|
|
618
|
-
ref:
|
|
626
|
+
ref: _mergeRefs(ref, formRef),
|
|
619
627
|
onPaste: _onPaste,
|
|
620
628
|
onKeyDown: _onKeyDown,
|
|
621
629
|
onKeyUp: _onKeyUp,
|
|
@@ -632,16 +640,17 @@ const FormRenderer = React.forwardRef(({
|
|
|
632
640
|
_onChange = e => onChange(state, e);
|
|
633
641
|
}
|
|
634
642
|
return React.createElement("form", _extends({}, rest, {
|
|
635
|
-
ref:
|
|
643
|
+
ref: _mergeRefs(ref, formRef),
|
|
636
644
|
onSubmit: _onSubmit,
|
|
637
645
|
onChange: _onChange,
|
|
638
646
|
onPaste: _onPaste,
|
|
639
647
|
onKeyDown: _onKeyDown,
|
|
640
648
|
onKeyUp: _onKeyUp
|
|
641
649
|
}), formChildren);
|
|
642
|
-
}, [children,
|
|
650
|
+
}, [children, fields]);
|
|
643
651
|
React.useEffect(() => {
|
|
644
|
-
const
|
|
652
|
+
const initialStateFields = getState().fields;
|
|
653
|
+
const state = _buildFormState(initialStateFields, formRef.current, handlers._dispatchChanges);
|
|
645
654
|
if (!_hasSubmitter(formRef.current)) {
|
|
646
655
|
console.warn(`DEV ONLY - [No Submit Button] - you have created a form without a button type=submit, this will prevent the onSubmit event from being fired.\nif you have a button with onClick event that handle the submission of the form then ignore this warning\nbut don't forget to manually invoke the checkValidity() function to check if the form is valid before perfoming any action, for example:\nif (formState.checkValidity()) { \n\t//do somthing\n}\n`);
|
|
647
656
|
}
|
|
@@ -656,13 +665,12 @@ const FormRenderer = React.forwardRef(({
|
|
|
656
665
|
} else _handler(changes);
|
|
657
666
|
}
|
|
658
667
|
}
|
|
659
|
-
const fields = getState().fields;
|
|
660
668
|
for (const fieldKey in fields) {
|
|
661
669
|
const field = fields[fieldKey];
|
|
662
670
|
if (!field.value) continue;
|
|
663
671
|
handlers._viHandler(field);
|
|
664
672
|
}
|
|
665
|
-
}, [
|
|
673
|
+
}, []);
|
|
666
674
|
return formComponent;
|
|
667
675
|
});
|
|
668
676
|
const GForm = React.forwardRef(({
|
|
@@ -685,7 +693,7 @@ const GForm = React.forwardRef(({
|
|
|
685
693
|
|
|
686
694
|
const selectFields = [state => state.fields];
|
|
687
695
|
const makeSelectFields = (keys = []) => createSelector(selectFields, fields => {
|
|
688
|
-
const selected = keys.map(key => fields[key]).
|
|
696
|
+
const selected = keys.map(key => JSON.stringify(fields[key].value)).join(', ');
|
|
689
697
|
return selected.length ? selected : null;
|
|
690
698
|
});
|
|
691
699
|
|
|
@@ -761,7 +769,6 @@ const _GInput = React.forwardRef(({
|
|
|
761
769
|
return React.createElement("input", _props);
|
|
762
770
|
}, [inputState, element]);
|
|
763
771
|
const _fetchDeps = useFormSelector(makeSelectFields(fetchDeps));
|
|
764
|
-
const stableFetchDeps = React.useMemo(() => JSON.stringify(_fetchDeps), [_fetchDeps]);
|
|
765
772
|
React.useEffect(() => {
|
|
766
773
|
if (fetch) {
|
|
767
774
|
_debounce(debounce, `${inputState.gid}-fetch`).then(() => {
|
|
@@ -773,7 +780,7 @@ const _GInput = React.forwardRef(({
|
|
|
773
780
|
}
|
|
774
781
|
});
|
|
775
782
|
}
|
|
776
|
-
}, [
|
|
783
|
+
}, [_fetchDeps]);
|
|
777
784
|
return _element;
|
|
778
785
|
});
|
|
779
786
|
const GInput = React.memo(_GInput);
|