ivt 0.5.1 → 0.5.2
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/accordion/index.mjs +15 -15
- package/dist/alert/index.mjs +2 -2
- package/dist/alert-dialog/index.mjs +17 -17
- package/dist/aspect-ratio/index.mjs +1 -1
- package/dist/aspect-ratio/index.mjs.map +1 -1
- package/dist/avatar/index.mjs +7 -7
- package/dist/badge/index.mjs +3 -3
- package/dist/base/index.d.ts +1 -7
- package/dist/base/index.mjs +45 -85
- package/dist/base/index.mjs.map +1 -1
- package/dist/breadcrumb/index.mjs +5 -5
- package/dist/button/index.mjs +3 -3
- package/dist/button-group/index.mjs +3 -3
- package/dist/calendar/index.mjs +9 -9
- package/dist/card/index.mjs +3 -3
- package/dist/carousel/index.mjs +4 -4
- package/dist/chart/index.mjs +58 -27
- package/dist/chart/index.mjs.map +1 -1
- package/dist/checkbox/index.mjs +16 -221
- package/dist/checkbox/index.mjs.map +1 -1
- package/dist/chunks/CalendarRange-cGKHXdiT.mjs +45 -0
- package/dist/chunks/CalendarRange-cGKHXdiT.mjs.map +1 -0
- package/dist/chunks/ComboboxField-BTKXl9y7.mjs +67 -0
- package/dist/chunks/ComboboxField-BTKXl9y7.mjs.map +1 -0
- package/dist/chunks/{Container-BrdBMmR0.mjs → Container-BUT39dlc.mjs} +2 -2
- package/dist/chunks/{Container-BrdBMmR0.mjs.map → Container-BUT39dlc.mjs.map} +1 -1
- package/dist/chunks/{TooltipIndicator-BRnOXovZ.mjs → TooltipIndicator-EfXM1ogO.mjs} +4 -4
- package/dist/chunks/{TooltipIndicator-BRnOXovZ.mjs.map → TooltipIndicator-EfXM1ogO.mjs.map} +1 -1
- package/dist/chunks/{_commonjsHelpers-BMPzsb8G.mjs → _commonjsHelpers-DCzDIER3.mjs} +1 -1
- package/dist/chunks/_commonjsHelpers-DCzDIER3.mjs.map +1 -0
- package/dist/chunks/{accordion-D2idHhKo.mjs → accordion-BXurL6Pv.mjs} +11 -11
- package/dist/chunks/{accordion-D2idHhKo.mjs.map → accordion-BXurL6Pv.mjs.map} +1 -1
- package/dist/chunks/{badge-DBJfSSib.mjs → badge-CpWQDnGV.mjs} +2 -2
- package/dist/chunks/{badge-DBJfSSib.mjs.map → badge-CpWQDnGV.mjs.map} +1 -1
- package/dist/chunks/{bundle-mjs-hDzYn9Rn.mjs → bundle-mjs-C9OlPgmH.mjs} +1 -1
- package/dist/chunks/{bundle-mjs-hDzYn9Rn.mjs.map → bundle-mjs-C9OlPgmH.mjs.map} +1 -1
- package/dist/chunks/{button-D-AuR380.mjs → button-BrmYbiY8.mjs} +2 -2
- package/dist/chunks/{button-D-AuR380.mjs.map → button-BrmYbiY8.mjs.map} +1 -1
- package/dist/chunks/{calendar-UmDqBCDE.mjs → calendar-BQgywKTj.mjs} +7 -7
- package/dist/chunks/{calendar-UmDqBCDE.mjs.map → calendar-BQgywKTj.mjs.map} +1 -1
- package/dist/chunks/{calendar-days-DwMabC50.mjs → calendar-days-C_RzSweF.mjs} +2 -2
- package/dist/chunks/{calendar-days-DwMabC50.mjs.map → calendar-days-C_RzSweF.mjs.map} +1 -1
- package/dist/chunks/{card-BvvM14Gb.mjs → card-DeaeODxs.mjs} +2 -2
- package/dist/chunks/{card-BvvM14Gb.mjs.map → card-DeaeODxs.mjs.map} +1 -1
- package/dist/chunks/{check-BRfyRdfr.mjs → check-CP6BCwvU.mjs} +2 -2
- package/dist/chunks/{check-BRfyRdfr.mjs.map → check-CP6BCwvU.mjs.map} +1 -1
- package/dist/chunks/checkbox-BvnI-3Q6.mjs +219 -0
- package/dist/chunks/checkbox-BvnI-3Q6.mjs.map +1 -0
- package/dist/chunks/{chevron-down-Da1qAjZn.mjs → chevron-down-cCIwKwmA.mjs} +2 -2
- package/dist/chunks/{chevron-down-Da1qAjZn.mjs.map → chevron-down-cCIwKwmA.mjs.map} +1 -1
- package/dist/chunks/{chevron-left-COHT-3Eq.mjs → chevron-left-BPWGf-Mt.mjs} +2 -2
- package/dist/chunks/{chevron-left-COHT-3Eq.mjs.map → chevron-left-BPWGf-Mt.mjs.map} +1 -1
- package/dist/chunks/{chevron-right-DuI9c_Gd.mjs → chevron-right-Bk4Jys-R.mjs} +2 -2
- package/dist/chunks/{chevron-right-DuI9c_Gd.mjs.map → chevron-right-Bk4Jys-R.mjs.map} +1 -1
- package/dist/chunks/{chevrons-up-down-CPNoTxmJ.mjs → chevrons-up-down-BDmpiJmb.mjs} +2 -2
- package/dist/chunks/{chevrons-up-down-CPNoTxmJ.mjs.map → chevrons-up-down-BDmpiJmb.mjs.map} +1 -1
- package/dist/chunks/{command-Qqchwo0s.mjs → command-CTTPLJSd.mjs} +8 -8
- package/dist/chunks/{command-Qqchwo0s.mjs.map → command-CTTPLJSd.mjs.map} +1 -1
- package/dist/chunks/{createLucideIcon-7URJZQ6y.mjs → createLucideIcon-rdK7ReAV.mjs} +1 -1
- package/dist/chunks/{createLucideIcon-7URJZQ6y.mjs.map → createLucideIcon-rdK7ReAV.mjs.map} +1 -1
- package/dist/chunks/{date-OwUlKCdN.mjs → date-BF0VPeD6.mjs} +13 -3
- package/dist/chunks/{date-OwUlKCdN.mjs.map → date-BF0VPeD6.mjs.map} +1 -1
- package/dist/chunks/{dialog-COoPeq2Z.mjs → dialog-BZ1Bem1X.mjs} +5 -5
- package/dist/chunks/dialog-BZ1Bem1X.mjs.map +1 -0
- package/dist/chunks/{ellipsis-D4Xfhncn.mjs → ellipsis-CvvEpdNm.mjs} +2 -2
- package/dist/chunks/{ellipsis-D4Xfhncn.mjs.map → ellipsis-CvvEpdNm.mjs.map} +1 -1
- package/dist/chunks/form-CLuajH9A.mjs +2223 -0
- package/dist/chunks/form-CLuajH9A.mjs.map +1 -0
- package/dist/chunks/{format-D4e-OzO4.mjs → format-DvB2IToG.mjs} +1 -1
- package/dist/chunks/{format-D4e-OzO4.mjs.map → format-DvB2IToG.mjs.map} +1 -1
- package/dist/chunks/format-numbers-DtAwKNYo.mjs +55 -0
- package/dist/chunks/format-numbers-DtAwKNYo.mjs.map +1 -0
- package/dist/chunks/{hover-card-DWwA1Yuy.mjs → hover-card-Cwtai5wK.mjs} +11 -11
- package/dist/chunks/{hover-card-DWwA1Yuy.mjs.map → hover-card-Cwtai5wK.mjs.map} +1 -1
- package/dist/chunks/{index-DtKExqnn.mjs → index-2wUt7vgM.mjs} +5 -5
- package/dist/chunks/{index-DtKExqnn.mjs.map → index-2wUt7vgM.mjs.map} +1 -1
- package/dist/chunks/{index-5faT867a.mjs → index-9Ejb37i6.mjs} +2 -2
- package/dist/chunks/{index-5faT867a.mjs.map → index-9Ejb37i6.mjs.map} +1 -1
- package/dist/chunks/{index-BkSq4h61.mjs → index-B0fljwsr.mjs} +1 -1
- package/dist/chunks/{index-BkSq4h61.mjs.map → index-B0fljwsr.mjs.map} +1 -1
- package/dist/chunks/{index-Ddd4CdXg.mjs → index-B1HovDby.mjs} +1 -1
- package/dist/chunks/{index-Ddd4CdXg.mjs.map → index-B1HovDby.mjs.map} +1 -1
- package/dist/chunks/{index-v6afswDN.mjs → index-BpfkkunI.mjs} +3 -3
- package/dist/chunks/{index-v6afswDN.mjs.map → index-BpfkkunI.mjs.map} +1 -1
- package/dist/chunks/{index-NklcsNwH.mjs → index-C5dQnmlR.mjs} +9 -9
- package/dist/chunks/{index-NklcsNwH.mjs.map → index-C5dQnmlR.mjs.map} +1 -1
- package/dist/chunks/{index-5Pzed4rM.mjs → index-C7WDAsMc.mjs} +2 -2
- package/dist/chunks/{index-5Pzed4rM.mjs.map → index-C7WDAsMc.mjs.map} +1 -1
- package/dist/chunks/{index-nZoGoNPf.mjs → index-C8YG6ZVI.mjs} +1 -1
- package/dist/chunks/{index-nZoGoNPf.mjs.map → index-C8YG6ZVI.mjs.map} +1 -1
- package/dist/chunks/{index-B8Xjv2ZH.mjs → index-CJIjOsdz.mjs} +1 -1
- package/dist/chunks/{index-B8Xjv2ZH.mjs.map → index-CJIjOsdz.mjs.map} +1 -1
- package/dist/chunks/{index-DBCfgozJ.mjs → index-CN-1P6oe.mjs} +9 -9
- package/dist/chunks/{index-DBCfgozJ.mjs.map → index-CN-1P6oe.mjs.map} +1 -1
- package/dist/chunks/{index-B8-uTAUO.mjs → index-CcfhA3fh.mjs} +11 -11
- package/dist/chunks/{index-B8-uTAUO.mjs.map → index-CcfhA3fh.mjs.map} +1 -1
- package/dist/chunks/{index-C9o4LjP_.mjs → index-CdbLHZ-v.mjs} +2 -2
- package/dist/chunks/{index-C9o4LjP_.mjs.map → index-CdbLHZ-v.mjs.map} +1 -1
- package/dist/chunks/{index-CN5_KnJA.mjs → index-D4Y6_Jit.mjs} +5 -5
- package/dist/chunks/{index-CN5_KnJA.mjs.map → index-D4Y6_Jit.mjs.map} +1 -1
- package/dist/chunks/{index-BtarojiO.mjs → index-DEFfOyUE.mjs} +1 -1
- package/dist/chunks/{index-BtarojiO.mjs.map → index-DEFfOyUE.mjs.map} +1 -1
- package/dist/chunks/{index-B2yIIBCu.mjs → index-DFMyqwc8.mjs} +2 -2
- package/dist/chunks/{index-B2yIIBCu.mjs.map → index-DFMyqwc8.mjs.map} +1 -1
- package/dist/chunks/{index-ZHgU3iXJ.mjs → index-DNPAHxPV.mjs} +1 -1
- package/dist/chunks/{index-ZHgU3iXJ.mjs.map → index-DNPAHxPV.mjs.map} +1 -1
- package/dist/chunks/{index-DLJUwQi9.mjs → index-DkhtbUNc.mjs} +1 -1
- package/dist/chunks/{index-DLJUwQi9.mjs.map → index-DkhtbUNc.mjs.map} +1 -1
- package/dist/chunks/{index-B-6Ckys4.mjs → index-Dpe5NTKJ.mjs} +7 -7
- package/dist/chunks/{index-B-6Ckys4.mjs.map → index-Dpe5NTKJ.mjs.map} +1 -1
- package/dist/chunks/{index-B6brYbxC.mjs → index-DymROPqb.mjs} +1 -1
- package/dist/chunks/{index-B6brYbxC.mjs.map → index-DymROPqb.mjs.map} +1 -1
- package/dist/chunks/{index-CfDMge0f.mjs → index-LOIUbp1N.mjs} +3 -3
- package/dist/chunks/{index-CfDMge0f.mjs.map → index-LOIUbp1N.mjs.map} +1 -1
- package/dist/chunks/{index-B87WmpPj.mjs → index-N54PlKgO.mjs} +1 -1
- package/dist/chunks/{index-B87WmpPj.mjs.map → index-N54PlKgO.mjs.map} +1 -1
- package/dist/chunks/{index-C9PsqjPs.mjs → index-SHhYuecD.mjs} +1 -1
- package/dist/chunks/{index-C9PsqjPs.mjs.map → index-SHhYuecD.mjs.map} +1 -1
- package/dist/chunks/{index-C67E33Wg.mjs → index-eIYpJRu_.mjs} +15 -15
- package/dist/chunks/{index-C67E33Wg.mjs.map → index-eIYpJRu_.mjs.map} +1 -1
- package/dist/chunks/{input-P6tpQoZI.mjs → input-BRRi8JbN.mjs} +2 -2
- package/dist/chunks/{input-P6tpQoZI.mjs.map → input-BRRi8JbN.mjs.map} +1 -1
- package/dist/chunks/{label-DO-vk0C1.mjs → label-DyMdyPfF.mjs} +3 -3
- package/dist/chunks/{label-DO-vk0C1.mjs.map → label-DyMdyPfF.mjs.map} +1 -1
- package/dist/chunks/{loader-circle-ByrzCrlm.mjs → loader-circle-DlDf3jDF.mjs} +2 -2
- package/dist/chunks/{loader-circle-ByrzCrlm.mjs.map → loader-circle-DlDf3jDF.mjs.map} +1 -1
- package/dist/chunks/multi-select-BAp66njF.mjs +277 -0
- package/dist/chunks/multi-select-BAp66njF.mjs.map +1 -0
- package/dist/chunks/{pagination-BokeG2NE.mjs → pagination-CoQF8H-y.mjs} +6 -6
- package/dist/chunks/{pagination-BokeG2NE.mjs.map → pagination-CoQF8H-y.mjs.map} +1 -1
- package/dist/chunks/{popover-BHGG1pi4.mjs → popover-Bx3Ak-qS.mjs} +13 -13
- package/dist/chunks/{popover-BHGG1pi4.mjs.map → popover-Bx3Ak-qS.mjs.map} +1 -1
- package/dist/chunks/{progress-tTjKFb3n.mjs → progress-Unv1Ywou.mjs} +4 -4
- package/dist/chunks/{progress-tTjKFb3n.mjs.map → progress-Unv1Ywou.mjs.map} +1 -1
- package/dist/chunks/{scroll-area-DEPEjRwi.mjs → scroll-area-Bu_eg6tq.mjs} +11 -11
- package/dist/chunks/{scroll-area-DEPEjRwi.mjs.map → scroll-area-Bu_eg6tq.mjs.map} +1 -1
- package/dist/chunks/{select-D65oWCP_.mjs → select-VgTkuC8i.mjs} +5 -5
- package/dist/chunks/{select-D65oWCP_.mjs.map → select-VgTkuC8i.mjs.map} +1 -1
- package/dist/chunks/{separator-DGk34I19.mjs → separator-ChTsmHyH.mjs} +3 -3
- package/dist/chunks/{separator-DGk34I19.mjs.map → separator-ChTsmHyH.mjs.map} +1 -1
- package/dist/chunks/{sheet-DzqTwvfR.mjs → sheet-CX9dlZuk.mjs} +4 -4
- package/dist/chunks/{sheet-DzqTwvfR.mjs.map → sheet-CX9dlZuk.mjs.map} +1 -1
- package/dist/chunks/{skeleton-BKAZlhUa.mjs → skeleton-TvPSLZ-7.mjs} +2 -2
- package/dist/chunks/{skeleton-BKAZlhUa.mjs.map → skeleton-TvPSLZ-7.mjs.map} +1 -1
- package/dist/chunks/{sortable-h2HFN8nu.mjs → sortable-DQ0ESvYM.mjs} +3 -3
- package/dist/chunks/{sortable-h2HFN8nu.mjs.map → sortable-DQ0ESvYM.mjs.map} +1 -1
- package/dist/chunks/{table-CU2pn-A3.mjs → table-5dae4DKj.mjs} +2 -2
- package/dist/chunks/{table-CU2pn-A3.mjs.map → table-5dae4DKj.mjs.map} +1 -1
- package/dist/chunks/{textarea-BwN5bQKw.mjs → textarea-C6sc9FNI.mjs} +2 -2
- package/dist/chunks/{textarea-BwN5bQKw.mjs.map → textarea-C6sc9FNI.mjs.map} +1 -1
- package/dist/chunks/{toggle-D4Zey3Qh.mjs → toggle-SS3ISGud.mjs} +5 -5
- package/dist/chunks/{toggle-D4Zey3Qh.mjs.map → toggle-SS3ISGud.mjs.map} +1 -1
- package/dist/chunks/{tooltip-DzraytiQ.mjs → tooltip-WyVePJR7.mjs} +13 -13
- package/dist/chunks/{tooltip-DzraytiQ.mjs.map → tooltip-WyVePJR7.mjs.map} +1 -1
- package/dist/chunks/{trash-2-CP_u4s-9.mjs → trash-2-Dp8LY8vw.mjs} +2 -2
- package/dist/chunks/{trash-2-CP_u4s-9.mjs.map → trash-2-Dp8LY8vw.mjs.map} +1 -1
- package/dist/chunks/{triangle-alert-BWSa1Gx0.mjs → triangle-alert-CpLMma6K.mjs} +2 -2
- package/dist/chunks/{triangle-alert-BWSa1Gx0.mjs.map → triangle-alert-CpLMma6K.mjs.map} +1 -1
- package/dist/chunks/{tslib.es6-OCi7qCQ4.mjs → tslib.es6-BgXxJBc3.mjs} +1 -1
- package/dist/chunks/{tslib.es6-OCi7qCQ4.mjs.map → tslib.es6-BgXxJBc3.mjs.map} +1 -1
- package/dist/chunks/{utils-D0vBZFbG.mjs → utils-DfaTa-5f.mjs} +2 -2
- package/dist/chunks/{utils-D0vBZFbG.mjs.map → utils-DfaTa-5f.mjs.map} +1 -1
- package/dist/chunks/{x-Cpa5dTvp.mjs → x-BtRqeVV7.mjs} +2 -2
- package/dist/chunks/{x-Cpa5dTvp.mjs.map → x-BtRqeVV7.mjs.map} +1 -1
- package/dist/collapsible/index.mjs +9 -9
- package/dist/combobox/index.d.ts +8 -1
- package/dist/combobox/index.mjs +121 -39
- package/dist/combobox/index.mjs.map +1 -1
- package/dist/command/index.mjs +20 -20
- package/dist/context-menu/index.mjs +23 -23
- package/dist/dash/index.mjs +25 -75
- package/dist/dash/index.mjs.map +1 -1
- package/dist/data-table/index.mjs +35 -35
- package/dist/date-picker/index.mjs +27 -27
- package/dist/dialog/index.mjs +19 -19
- package/dist/drawer/index.mjs +16 -16
- package/dist/dropdown-menu/index.mjs +23 -23
- package/dist/dropzone/index.d.ts +6 -0
- package/dist/dropzone/index.mjs +40 -28
- package/dist/dropzone/index.mjs.map +1 -1
- package/dist/empty/index.mjs +2 -2
- package/dist/field/index.mjs +5 -5
- package/dist/form/index.mjs +7 -653
- package/dist/form/index.mjs.map +1 -1
- package/dist/form-fields/index.mjs +29 -85
- package/dist/form-fields/index.mjs.map +1 -1
- package/dist/hover-card/index.mjs +15 -15
- package/dist/icon/index.mjs +1 -1
- package/dist/index.css +1 -1
- package/dist/input/index.mjs +3 -3
- package/dist/input-group/index.mjs +5 -5
- package/dist/input-otp/index.mjs +3 -3
- package/dist/item/index.mjs +4 -4
- package/dist/ivt.css +1 -1
- package/dist/kbd/index.mjs +2 -2
- package/dist/label/index.mjs +4 -4
- package/dist/layout/index.mjs +6 -6
- package/dist/menubar/index.mjs +23 -23
- package/dist/multi-select/index.mjs +33 -295
- package/dist/multi-select/index.mjs.map +1 -1
- package/dist/navigation-menu/index.mjs +17 -17
- package/dist/pagination/index.mjs +8 -8
- package/dist/popover/index.mjs +18 -18
- package/dist/progress/index.mjs +5 -5
- package/dist/radio-group/index.mjs +17 -17
- package/dist/resizable/index.mjs +3 -3
- package/dist/scroll-area/index.mjs +12 -12
- package/dist/select/index.mjs +6 -6
- package/dist/separator/index.mjs +4 -4
- package/dist/shared/index.mjs +5 -5
- package/dist/sheet/index.mjs +19 -19
- package/dist/sidebar/index.mjs +27 -27
- package/dist/skeleton/index.mjs +3 -3
- package/dist/skeleton-component/index.mjs +4 -4
- package/dist/slider/index.mjs +12 -12
- package/dist/slider/index.mjs.map +1 -1
- package/dist/sortable/index.mjs +4 -4
- package/dist/spinner/index.mjs +4 -4
- package/dist/switch/index.mjs +10 -10
- package/dist/table/index.mjs +3 -3
- package/dist/table-filter/index.d.ts +49 -0
- package/dist/table-filter/index.mjs +2068 -0
- package/dist/table-filter/index.mjs.map +1 -0
- package/dist/tabs/index.mjs +13 -13
- package/dist/textarea/index.mjs +3 -3
- package/dist/toast/index.mjs +15 -15
- package/dist/toggle/index.mjs +7 -7
- package/dist/toggle-group/index.mjs +13 -13
- package/dist/tooltip/index.mjs +17 -17
- package/package.json +6 -1
- package/dist/chunks/_commonjsHelpers-BMPzsb8G.mjs.map +0 -1
- package/dist/chunks/dialog-COoPeq2Z.mjs.map +0 -1
|
@@ -0,0 +1,2223 @@
|
|
|
1
|
+
import { Slot } from '@radix-ui/react-slot';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import React__default from 'react';
|
|
4
|
+
import { c as cn } from './utils-DfaTa-5f.mjs';
|
|
5
|
+
import { L as Label } from './label-DyMdyPfF.mjs';
|
|
6
|
+
|
|
7
|
+
var isCheckBoxInput = (element)=>element.type === 'checkbox';
|
|
8
|
+
var isDateObject = (value1)=>value1 instanceof Date;
|
|
9
|
+
var isNullOrUndefined = (value1)=>value1 == null;
|
|
10
|
+
const isObjectType = (value1)=>typeof value1 === 'object';
|
|
11
|
+
var isObject = (value1)=>!isNullOrUndefined(value1) && !Array.isArray(value1) && isObjectType(value1) && !isDateObject(value1);
|
|
12
|
+
var getEventValue = (event)=>isObject(event) && event.target ? isCheckBoxInput(event.target) ? event.target.checked : event.target.value : event;
|
|
13
|
+
var getNodeParentName = (name)=>name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
|
14
|
+
var isNameInFieldArray = (names, name)=>names.has(getNodeParentName(name));
|
|
15
|
+
var isPlainObject = (tempObject)=>{
|
|
16
|
+
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
17
|
+
return isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf');
|
|
18
|
+
};
|
|
19
|
+
var isWeb = typeof window !== 'undefined' && typeof window.HTMLElement !== 'undefined' && typeof document !== 'undefined';
|
|
20
|
+
function cloneObject(data) {
|
|
21
|
+
let copy;
|
|
22
|
+
const isArray = Array.isArray(data);
|
|
23
|
+
const isFileListInstance = typeof FileList !== 'undefined' ? data instanceof FileList : false;
|
|
24
|
+
if (data instanceof Date) {
|
|
25
|
+
copy = new Date(data);
|
|
26
|
+
} else if (!(isWeb && (data instanceof Blob || isFileListInstance)) && (isArray || isObject(data))) {
|
|
27
|
+
copy = isArray ? [] : Object.create(Object.getPrototypeOf(data));
|
|
28
|
+
if (!isArray && !isPlainObject(data)) {
|
|
29
|
+
copy = data;
|
|
30
|
+
} else {
|
|
31
|
+
for(const key in data){
|
|
32
|
+
if (data.hasOwnProperty(key)) {
|
|
33
|
+
copy[key] = cloneObject(data[key]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
return data;
|
|
39
|
+
}
|
|
40
|
+
return copy;
|
|
41
|
+
}
|
|
42
|
+
var isKey = (value1)=>/^\w*$/.test(value1);
|
|
43
|
+
var isUndefined = (val)=>val === undefined;
|
|
44
|
+
var compact = (value1)=>Array.isArray(value1) ? value1.filter(Boolean) : [];
|
|
45
|
+
var stringToPath = (input)=>compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
|
46
|
+
var get = (object, path, defaultValue)=>{
|
|
47
|
+
if (!path || !isObject(object)) {
|
|
48
|
+
return defaultValue;
|
|
49
|
+
}
|
|
50
|
+
const result = (isKey(path) ? [
|
|
51
|
+
path
|
|
52
|
+
] : stringToPath(path)).reduce((result, key)=>isNullOrUndefined(result) ? result : result[key], object);
|
|
53
|
+
return isUndefined(result) || result === object ? isUndefined(object[path]) ? defaultValue : object[path] : result;
|
|
54
|
+
};
|
|
55
|
+
var isBoolean = (value1)=>typeof value1 === 'boolean';
|
|
56
|
+
var set = (object, path, value1)=>{
|
|
57
|
+
let index = -1;
|
|
58
|
+
const tempPath = isKey(path) ? [
|
|
59
|
+
path
|
|
60
|
+
] : stringToPath(path);
|
|
61
|
+
const length = tempPath.length;
|
|
62
|
+
const lastIndex = length - 1;
|
|
63
|
+
while(++index < length){
|
|
64
|
+
const key = tempPath[index];
|
|
65
|
+
let newValue = value1;
|
|
66
|
+
if (index !== lastIndex) {
|
|
67
|
+
const objValue = object[key];
|
|
68
|
+
newValue = isObject(objValue) || Array.isArray(objValue) ? objValue : !isNaN(+tempPath[index + 1]) ? [] : {};
|
|
69
|
+
}
|
|
70
|
+
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
object[key] = newValue;
|
|
74
|
+
object = object[key];
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const EVENTS = {
|
|
78
|
+
BLUR: 'blur',
|
|
79
|
+
FOCUS_OUT: 'focusout',
|
|
80
|
+
CHANGE: 'change'
|
|
81
|
+
};
|
|
82
|
+
const VALIDATION_MODE = {
|
|
83
|
+
onBlur: 'onBlur',
|
|
84
|
+
onChange: 'onChange',
|
|
85
|
+
onSubmit: 'onSubmit',
|
|
86
|
+
onTouched: 'onTouched',
|
|
87
|
+
all: 'all'
|
|
88
|
+
};
|
|
89
|
+
const INPUT_VALIDATION_RULES = {
|
|
90
|
+
max: 'max',
|
|
91
|
+
min: 'min',
|
|
92
|
+
maxLength: 'maxLength',
|
|
93
|
+
minLength: 'minLength',
|
|
94
|
+
pattern: 'pattern',
|
|
95
|
+
required: 'required',
|
|
96
|
+
validate: 'validate'
|
|
97
|
+
};
|
|
98
|
+
const HookFormContext = React__default.createContext(null);
|
|
99
|
+
HookFormContext.displayName = 'HookFormContext';
|
|
100
|
+
/**
|
|
101
|
+
* This custom hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the context as a prop. To be used with {@link FormProvider}.
|
|
102
|
+
*
|
|
103
|
+
* @remarks
|
|
104
|
+
* [API](https://react-hook-form.com/docs/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
|
105
|
+
*
|
|
106
|
+
* @returns return all useForm methods
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```tsx
|
|
110
|
+
* function App() {
|
|
111
|
+
* const methods = useForm();
|
|
112
|
+
* const onSubmit = data => console.log(data);
|
|
113
|
+
*
|
|
114
|
+
* return (
|
|
115
|
+
* <FormProvider {...methods} >
|
|
116
|
+
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
|
117
|
+
* <NestedInput />
|
|
118
|
+
* <input type="submit" />
|
|
119
|
+
* </form>
|
|
120
|
+
* </FormProvider>
|
|
121
|
+
* );
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* function NestedInput() {
|
|
125
|
+
* const { register } = useFormContext(); // retrieve all hook methods
|
|
126
|
+
* return <input {...register("test")} />;
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/ const useFormContext = ()=>React__default.useContext(HookFormContext);
|
|
130
|
+
/**
|
|
131
|
+
* A provider component that propagates the `useForm` methods to all children components via [React Context](https://reactjs.org/docs/context.html) API. To be used with {@link useFormContext}.
|
|
132
|
+
*
|
|
133
|
+
* @remarks
|
|
134
|
+
* [API](https://react-hook-form.com/docs/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
|
135
|
+
*
|
|
136
|
+
* @param props - all useForm methods
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```tsx
|
|
140
|
+
* function App() {
|
|
141
|
+
* const methods = useForm();
|
|
142
|
+
* const onSubmit = data => console.log(data);
|
|
143
|
+
*
|
|
144
|
+
* return (
|
|
145
|
+
* <FormProvider {...methods} >
|
|
146
|
+
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
|
147
|
+
* <NestedInput />
|
|
148
|
+
* <input type="submit" />
|
|
149
|
+
* </form>
|
|
150
|
+
* </FormProvider>
|
|
151
|
+
* );
|
|
152
|
+
* }
|
|
153
|
+
*
|
|
154
|
+
* function NestedInput() {
|
|
155
|
+
* const { register } = useFormContext(); // retrieve all hook methods
|
|
156
|
+
* return <input {...register("test")} />;
|
|
157
|
+
* }
|
|
158
|
+
* ```
|
|
159
|
+
*/ const FormProvider = (props)=>{
|
|
160
|
+
const { children, ...data } = props;
|
|
161
|
+
return React__default.createElement(HookFormContext.Provider, {
|
|
162
|
+
value: data
|
|
163
|
+
}, children);
|
|
164
|
+
};
|
|
165
|
+
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true)=>{
|
|
166
|
+
const result = {
|
|
167
|
+
defaultValues: control._defaultValues
|
|
168
|
+
};
|
|
169
|
+
for(const key in formState){
|
|
170
|
+
Object.defineProperty(result, key, {
|
|
171
|
+
get: ()=>{
|
|
172
|
+
const _key = key;
|
|
173
|
+
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
|
174
|
+
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
|
175
|
+
}
|
|
176
|
+
localProxyFormState && (localProxyFormState[_key] = true);
|
|
177
|
+
return formState[_key];
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
};
|
|
183
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__default.useLayoutEffect : React__default.useEffect;
|
|
184
|
+
/**
|
|
185
|
+
* This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
|
|
186
|
+
*
|
|
187
|
+
* @remarks
|
|
188
|
+
* [API](https://react-hook-form.com/docs/useformstate) • [Demo](https://codesandbox.io/s/useformstate-75xly)
|
|
189
|
+
*
|
|
190
|
+
* @param props - include options on specify fields to subscribe. {@link UseFormStateReturn}
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```tsx
|
|
194
|
+
* function App() {
|
|
195
|
+
* const { register, handleSubmit, control } = useForm({
|
|
196
|
+
* defaultValues: {
|
|
197
|
+
* firstName: "firstName"
|
|
198
|
+
* }});
|
|
199
|
+
* const { dirtyFields } = useFormState({
|
|
200
|
+
* control
|
|
201
|
+
* });
|
|
202
|
+
* const onSubmit = (data) => console.log(data);
|
|
203
|
+
*
|
|
204
|
+
* return (
|
|
205
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
|
206
|
+
* <input {...register("firstName")} placeholder="First Name" />
|
|
207
|
+
* {dirtyFields.firstName && <p>Field is dirty.</p>}
|
|
208
|
+
* <input type="submit" />
|
|
209
|
+
* </form>
|
|
210
|
+
* );
|
|
211
|
+
* }
|
|
212
|
+
* ```
|
|
213
|
+
*/ function useFormState(props) {
|
|
214
|
+
const methods = useFormContext();
|
|
215
|
+
const { control = methods.control, disabled, name, exact } = props || {};
|
|
216
|
+
const [formState, updateFormState] = React__default.useState(control._formState);
|
|
217
|
+
const _localProxyFormState = React__default.useRef({
|
|
218
|
+
isDirty: false,
|
|
219
|
+
isLoading: false,
|
|
220
|
+
dirtyFields: false,
|
|
221
|
+
touchedFields: false,
|
|
222
|
+
validatingFields: false,
|
|
223
|
+
isValidating: false,
|
|
224
|
+
isValid: false,
|
|
225
|
+
errors: false
|
|
226
|
+
});
|
|
227
|
+
useIsomorphicLayoutEffect(()=>control._subscribe({
|
|
228
|
+
name,
|
|
229
|
+
formState: _localProxyFormState.current,
|
|
230
|
+
exact,
|
|
231
|
+
callback: (formState)=>{
|
|
232
|
+
!disabled && updateFormState({
|
|
233
|
+
...control._formState,
|
|
234
|
+
...formState
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}), [
|
|
238
|
+
name,
|
|
239
|
+
disabled,
|
|
240
|
+
exact
|
|
241
|
+
]);
|
|
242
|
+
React__default.useEffect(()=>{
|
|
243
|
+
_localProxyFormState.current.isValid && control._setValid(true);
|
|
244
|
+
}, [
|
|
245
|
+
control
|
|
246
|
+
]);
|
|
247
|
+
return React__default.useMemo(()=>getProxyFormState(formState, control, _localProxyFormState.current, false), [
|
|
248
|
+
formState,
|
|
249
|
+
control
|
|
250
|
+
]);
|
|
251
|
+
}
|
|
252
|
+
var isString = (value1)=>typeof value1 === 'string';
|
|
253
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue)=>{
|
|
254
|
+
if (isString(names)) {
|
|
255
|
+
isGlobal && _names.watch.add(names);
|
|
256
|
+
return get(formValues, names, defaultValue);
|
|
257
|
+
}
|
|
258
|
+
if (Array.isArray(names)) {
|
|
259
|
+
return names.map((fieldName)=>(isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
|
260
|
+
}
|
|
261
|
+
isGlobal && (_names.watchAll = true);
|
|
262
|
+
return formValues;
|
|
263
|
+
};
|
|
264
|
+
var isPrimitive = (value1)=>isNullOrUndefined(value1) || !isObjectType(value1);
|
|
265
|
+
function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
|
|
266
|
+
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
267
|
+
return object1 === object2;
|
|
268
|
+
}
|
|
269
|
+
if (isDateObject(object1) && isDateObject(object2)) {
|
|
270
|
+
return object1.getTime() === object2.getTime();
|
|
271
|
+
}
|
|
272
|
+
const keys1 = Object.keys(object1);
|
|
273
|
+
const keys2 = Object.keys(object2);
|
|
274
|
+
if (keys1.length !== keys2.length) {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
if (_internal_visited.has(object1) || _internal_visited.has(object2)) {
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
_internal_visited.add(object1);
|
|
281
|
+
_internal_visited.add(object2);
|
|
282
|
+
for (const key of keys1){
|
|
283
|
+
const val1 = object1[key];
|
|
284
|
+
if (!keys2.includes(key)) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
if (key !== 'ref') {
|
|
288
|
+
const val2 = object2[key];
|
|
289
|
+
if (isDateObject(val1) && isDateObject(val2) || isObject(val1) && isObject(val2) || Array.isArray(val1) && Array.isArray(val2) ? !deepEqual(val1, val2, _internal_visited) : val1 !== val2) {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
|
298
|
+
*
|
|
299
|
+
* @remarks
|
|
300
|
+
*
|
|
301
|
+
* [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e)
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```tsx
|
|
305
|
+
* const { control } = useForm();
|
|
306
|
+
* const values = useWatch({
|
|
307
|
+
* name: "fieldName"
|
|
308
|
+
* control,
|
|
309
|
+
* })
|
|
310
|
+
* ```
|
|
311
|
+
*/ function useWatch(props) {
|
|
312
|
+
const methods = useFormContext();
|
|
313
|
+
const { control = methods.control, name, defaultValue, disabled, exact, compute } = props || {};
|
|
314
|
+
const _defaultValue = React__default.useRef(defaultValue);
|
|
315
|
+
const _compute = React__default.useRef(compute);
|
|
316
|
+
const _computeFormValues = React__default.useRef(undefined);
|
|
317
|
+
_compute.current = compute;
|
|
318
|
+
const defaultValueMemo = React__default.useMemo(()=>control._getWatch(name, _defaultValue.current), [
|
|
319
|
+
control,
|
|
320
|
+
name
|
|
321
|
+
]);
|
|
322
|
+
const [value1, updateValue] = React__default.useState(_compute.current ? _compute.current(defaultValueMemo) : defaultValueMemo);
|
|
323
|
+
useIsomorphicLayoutEffect(()=>control._subscribe({
|
|
324
|
+
name,
|
|
325
|
+
formState: {
|
|
326
|
+
values: true
|
|
327
|
+
},
|
|
328
|
+
exact,
|
|
329
|
+
callback: (formState)=>{
|
|
330
|
+
if (!disabled) {
|
|
331
|
+
const formValues = generateWatchOutput(name, control._names, formState.values || control._formValues, false, _defaultValue.current);
|
|
332
|
+
if (_compute.current) {
|
|
333
|
+
const computedFormValues = _compute.current(formValues);
|
|
334
|
+
if (!deepEqual(computedFormValues, _computeFormValues.current)) {
|
|
335
|
+
updateValue(computedFormValues);
|
|
336
|
+
_computeFormValues.current = computedFormValues;
|
|
337
|
+
}
|
|
338
|
+
} else {
|
|
339
|
+
updateValue(formValues);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}), [
|
|
344
|
+
control,
|
|
345
|
+
disabled,
|
|
346
|
+
name,
|
|
347
|
+
exact
|
|
348
|
+
]);
|
|
349
|
+
React__default.useEffect(()=>control._removeUnmounted());
|
|
350
|
+
return value1;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
|
|
354
|
+
*
|
|
355
|
+
* @remarks
|
|
356
|
+
* [API](https://react-hook-form.com/docs/usecontroller) • [Demo](https://codesandbox.io/s/usecontroller-0o8px)
|
|
357
|
+
*
|
|
358
|
+
* @param props - the path name to the form field value, and validation rules.
|
|
359
|
+
*
|
|
360
|
+
* @returns field properties, field and form state. {@link UseControllerReturn}
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```tsx
|
|
364
|
+
* function Input(props) {
|
|
365
|
+
* const { field, fieldState, formState } = useController(props);
|
|
366
|
+
* return (
|
|
367
|
+
* <div>
|
|
368
|
+
* <input {...field} placeholder={props.name} />
|
|
369
|
+
* <p>{fieldState.isTouched && "Touched"}</p>
|
|
370
|
+
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
|
371
|
+
* </div>
|
|
372
|
+
* );
|
|
373
|
+
* }
|
|
374
|
+
* ```
|
|
375
|
+
*/ function useController(props) {
|
|
376
|
+
const methods = useFormContext();
|
|
377
|
+
const { name, disabled, control = methods.control, shouldUnregister, defaultValue } = props;
|
|
378
|
+
const isArrayField = isNameInFieldArray(control._names.array, name);
|
|
379
|
+
const defaultValueMemo = React__default.useMemo(()=>get(control._formValues, name, get(control._defaultValues, name, defaultValue)), [
|
|
380
|
+
control,
|
|
381
|
+
name,
|
|
382
|
+
defaultValue
|
|
383
|
+
]);
|
|
384
|
+
const value1 = useWatch({
|
|
385
|
+
control,
|
|
386
|
+
name,
|
|
387
|
+
defaultValue: defaultValueMemo,
|
|
388
|
+
exact: true
|
|
389
|
+
});
|
|
390
|
+
const formState = useFormState({
|
|
391
|
+
control,
|
|
392
|
+
name,
|
|
393
|
+
exact: true
|
|
394
|
+
});
|
|
395
|
+
const _props = React__default.useRef(props);
|
|
396
|
+
const _previousNameRef = React__default.useRef(undefined);
|
|
397
|
+
const _registerProps = React__default.useRef(control.register(name, {
|
|
398
|
+
...props.rules,
|
|
399
|
+
value: value1,
|
|
400
|
+
...isBoolean(props.disabled) ? {
|
|
401
|
+
disabled: props.disabled
|
|
402
|
+
} : {}
|
|
403
|
+
}));
|
|
404
|
+
_props.current = props;
|
|
405
|
+
const fieldState = React__default.useMemo(()=>Object.defineProperties({}, {
|
|
406
|
+
invalid: {
|
|
407
|
+
enumerable: true,
|
|
408
|
+
get: ()=>!!get(formState.errors, name)
|
|
409
|
+
},
|
|
410
|
+
isDirty: {
|
|
411
|
+
enumerable: true,
|
|
412
|
+
get: ()=>!!get(formState.dirtyFields, name)
|
|
413
|
+
},
|
|
414
|
+
isTouched: {
|
|
415
|
+
enumerable: true,
|
|
416
|
+
get: ()=>!!get(formState.touchedFields, name)
|
|
417
|
+
},
|
|
418
|
+
isValidating: {
|
|
419
|
+
enumerable: true,
|
|
420
|
+
get: ()=>!!get(formState.validatingFields, name)
|
|
421
|
+
},
|
|
422
|
+
error: {
|
|
423
|
+
enumerable: true,
|
|
424
|
+
get: ()=>get(formState.errors, name)
|
|
425
|
+
}
|
|
426
|
+
}), [
|
|
427
|
+
formState,
|
|
428
|
+
name
|
|
429
|
+
]);
|
|
430
|
+
const onChange = React__default.useCallback((event)=>_registerProps.current.onChange({
|
|
431
|
+
target: {
|
|
432
|
+
value: getEventValue(event),
|
|
433
|
+
name: name
|
|
434
|
+
},
|
|
435
|
+
type: EVENTS.CHANGE
|
|
436
|
+
}), [
|
|
437
|
+
name
|
|
438
|
+
]);
|
|
439
|
+
const onBlur = React__default.useCallback(()=>_registerProps.current.onBlur({
|
|
440
|
+
target: {
|
|
441
|
+
value: get(control._formValues, name),
|
|
442
|
+
name: name
|
|
443
|
+
},
|
|
444
|
+
type: EVENTS.BLUR
|
|
445
|
+
}), [
|
|
446
|
+
name,
|
|
447
|
+
control._formValues
|
|
448
|
+
]);
|
|
449
|
+
const ref = React__default.useCallback((elm)=>{
|
|
450
|
+
const field = get(control._fields, name);
|
|
451
|
+
if (field && elm) {
|
|
452
|
+
field._f.ref = {
|
|
453
|
+
focus: ()=>elm.focus && elm.focus(),
|
|
454
|
+
select: ()=>elm.select && elm.select(),
|
|
455
|
+
setCustomValidity: (message)=>elm.setCustomValidity(message),
|
|
456
|
+
reportValidity: ()=>elm.reportValidity()
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
}, [
|
|
460
|
+
control._fields,
|
|
461
|
+
name
|
|
462
|
+
]);
|
|
463
|
+
const field = React__default.useMemo(()=>({
|
|
464
|
+
name,
|
|
465
|
+
value: value1,
|
|
466
|
+
...isBoolean(disabled) || formState.disabled ? {
|
|
467
|
+
disabled: formState.disabled || disabled
|
|
468
|
+
} : {},
|
|
469
|
+
onChange,
|
|
470
|
+
onBlur,
|
|
471
|
+
ref
|
|
472
|
+
}), [
|
|
473
|
+
name,
|
|
474
|
+
disabled,
|
|
475
|
+
formState.disabled,
|
|
476
|
+
onChange,
|
|
477
|
+
onBlur,
|
|
478
|
+
ref,
|
|
479
|
+
value1
|
|
480
|
+
]);
|
|
481
|
+
React__default.useEffect(()=>{
|
|
482
|
+
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
483
|
+
const previousName = _previousNameRef.current;
|
|
484
|
+
if (previousName && previousName !== name && !isArrayField) {
|
|
485
|
+
control.unregister(previousName);
|
|
486
|
+
}
|
|
487
|
+
control.register(name, {
|
|
488
|
+
..._props.current.rules,
|
|
489
|
+
...isBoolean(_props.current.disabled) ? {
|
|
490
|
+
disabled: _props.current.disabled
|
|
491
|
+
} : {}
|
|
492
|
+
});
|
|
493
|
+
const updateMounted = (name, value1)=>{
|
|
494
|
+
const field = get(control._fields, name);
|
|
495
|
+
if (field && field._f) {
|
|
496
|
+
field._f.mount = value1;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
updateMounted(name, true);
|
|
500
|
+
if (_shouldUnregisterField) {
|
|
501
|
+
const value1 = cloneObject(get(control._options.defaultValues, name, _props.current.defaultValue));
|
|
502
|
+
set(control._defaultValues, name, value1);
|
|
503
|
+
if (isUndefined(get(control._formValues, name))) {
|
|
504
|
+
set(control._formValues, name, value1);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
!isArrayField && control.register(name);
|
|
508
|
+
_previousNameRef.current = name;
|
|
509
|
+
return ()=>{
|
|
510
|
+
(isArrayField ? _shouldUnregisterField && !control._state.action : _shouldUnregisterField) ? control.unregister(name) : updateMounted(name, false);
|
|
511
|
+
};
|
|
512
|
+
}, [
|
|
513
|
+
name,
|
|
514
|
+
control,
|
|
515
|
+
isArrayField,
|
|
516
|
+
shouldUnregister
|
|
517
|
+
]);
|
|
518
|
+
React__default.useEffect(()=>{
|
|
519
|
+
control._setDisabledField({
|
|
520
|
+
disabled,
|
|
521
|
+
name
|
|
522
|
+
});
|
|
523
|
+
}, [
|
|
524
|
+
disabled,
|
|
525
|
+
name,
|
|
526
|
+
control
|
|
527
|
+
]);
|
|
528
|
+
return React__default.useMemo(()=>({
|
|
529
|
+
field,
|
|
530
|
+
formState,
|
|
531
|
+
fieldState
|
|
532
|
+
}), [
|
|
533
|
+
field,
|
|
534
|
+
formState,
|
|
535
|
+
fieldState
|
|
536
|
+
]);
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Component based on `useController` hook to work with controlled component.
|
|
540
|
+
*
|
|
541
|
+
* @remarks
|
|
542
|
+
* [API](https://react-hook-form.com/docs/usecontroller/controller) • [Demo](https://codesandbox.io/s/react-hook-form-v6-controller-ts-jwyzw) • [Video](https://www.youtube.com/watch?v=N2UNk_UCVyA)
|
|
543
|
+
*
|
|
544
|
+
* @param props - the path name to the form field value, and validation rules.
|
|
545
|
+
*
|
|
546
|
+
* @returns provide field handler functions, field and form state.
|
|
547
|
+
*
|
|
548
|
+
* @example
|
|
549
|
+
* ```tsx
|
|
550
|
+
* function App() {
|
|
551
|
+
* const { control } = useForm<FormValues>({
|
|
552
|
+
* defaultValues: {
|
|
553
|
+
* test: ""
|
|
554
|
+
* }
|
|
555
|
+
* });
|
|
556
|
+
*
|
|
557
|
+
* return (
|
|
558
|
+
* <form>
|
|
559
|
+
* <Controller
|
|
560
|
+
* control={control}
|
|
561
|
+
* name="test"
|
|
562
|
+
* render={({ field: { onChange, onBlur, value, ref }, formState, fieldState }) => (
|
|
563
|
+
* <>
|
|
564
|
+
* <input
|
|
565
|
+
* onChange={onChange} // send value to hook form
|
|
566
|
+
* onBlur={onBlur} // notify when input is touched
|
|
567
|
+
* value={value} // return updated value
|
|
568
|
+
* ref={ref} // set ref for focus management
|
|
569
|
+
* />
|
|
570
|
+
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
|
571
|
+
* <p>{fieldState.isTouched ? "touched" : ""}</p>
|
|
572
|
+
* </>
|
|
573
|
+
* )}
|
|
574
|
+
* />
|
|
575
|
+
* </form>
|
|
576
|
+
* );
|
|
577
|
+
* }
|
|
578
|
+
* ```
|
|
579
|
+
*/ const Controller = (props)=>props.render(useController(props));
|
|
580
|
+
var appendErrors = (name, validateAllFieldCriteria, errors, type, message)=>validateAllFieldCriteria ? {
|
|
581
|
+
...errors[name],
|
|
582
|
+
types: {
|
|
583
|
+
...errors[name] && errors[name].types ? errors[name].types : {},
|
|
584
|
+
[type]: message || true
|
|
585
|
+
}
|
|
586
|
+
} : {};
|
|
587
|
+
var convertToArrayPayload = (value1)=>Array.isArray(value1) ? value1 : [
|
|
588
|
+
value1
|
|
589
|
+
];
|
|
590
|
+
var createSubject = ()=>{
|
|
591
|
+
let _observers = [];
|
|
592
|
+
const next = (value1)=>{
|
|
593
|
+
for (const observer of _observers){
|
|
594
|
+
observer.next && observer.next(value1);
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
const subscribe = (observer)=>{
|
|
598
|
+
_observers.push(observer);
|
|
599
|
+
return {
|
|
600
|
+
unsubscribe: ()=>{
|
|
601
|
+
_observers = _observers.filter((o)=>o !== observer);
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
};
|
|
605
|
+
const unsubscribe = ()=>{
|
|
606
|
+
_observers = [];
|
|
607
|
+
};
|
|
608
|
+
return {
|
|
609
|
+
get observers () {
|
|
610
|
+
return _observers;
|
|
611
|
+
},
|
|
612
|
+
next,
|
|
613
|
+
subscribe,
|
|
614
|
+
unsubscribe
|
|
615
|
+
};
|
|
616
|
+
};
|
|
617
|
+
function extractFormValues(fieldsState, formValues) {
|
|
618
|
+
const values = {};
|
|
619
|
+
for(const key in fieldsState){
|
|
620
|
+
if (fieldsState.hasOwnProperty(key)) {
|
|
621
|
+
const fieldState = fieldsState[key];
|
|
622
|
+
const fieldValue = formValues[key];
|
|
623
|
+
if (fieldState && isObject(fieldState) && fieldValue) {
|
|
624
|
+
const nestedFieldsState = extractFormValues(fieldState, fieldValue);
|
|
625
|
+
if (isObject(nestedFieldsState)) {
|
|
626
|
+
values[key] = nestedFieldsState;
|
|
627
|
+
}
|
|
628
|
+
} else if (fieldsState[key]) {
|
|
629
|
+
values[key] = fieldValue;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
return values;
|
|
634
|
+
}
|
|
635
|
+
var isEmptyObject = (value1)=>isObject(value1) && !Object.keys(value1).length;
|
|
636
|
+
var isFileInput = (element)=>element.type === 'file';
|
|
637
|
+
var isFunction = (value1)=>typeof value1 === 'function';
|
|
638
|
+
var isHTMLElement = (value1)=>{
|
|
639
|
+
if (!isWeb) {
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
const owner = value1 ? value1.ownerDocument : 0;
|
|
643
|
+
return value1 instanceof (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement);
|
|
644
|
+
};
|
|
645
|
+
var isMultipleSelect = (element)=>element.type === `select-multiple`;
|
|
646
|
+
var isRadioInput = (element)=>element.type === 'radio';
|
|
647
|
+
var isRadioOrCheckbox = (ref)=>isRadioInput(ref) || isCheckBoxInput(ref);
|
|
648
|
+
var live = (ref)=>isHTMLElement(ref) && ref.isConnected;
|
|
649
|
+
function baseGet(object, updatePath) {
|
|
650
|
+
const length = updatePath.slice(0, -1).length;
|
|
651
|
+
let index = 0;
|
|
652
|
+
while(index < length){
|
|
653
|
+
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
654
|
+
}
|
|
655
|
+
return object;
|
|
656
|
+
}
|
|
657
|
+
function isEmptyArray(obj) {
|
|
658
|
+
for(const key in obj){
|
|
659
|
+
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
return true;
|
|
664
|
+
}
|
|
665
|
+
function unset(object, path) {
|
|
666
|
+
const paths = Array.isArray(path) ? path : isKey(path) ? [
|
|
667
|
+
path
|
|
668
|
+
] : stringToPath(path);
|
|
669
|
+
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
670
|
+
const index = paths.length - 1;
|
|
671
|
+
const key = paths[index];
|
|
672
|
+
if (childObject) {
|
|
673
|
+
delete childObject[key];
|
|
674
|
+
}
|
|
675
|
+
if (index !== 0 && (isObject(childObject) && isEmptyObject(childObject) || Array.isArray(childObject) && isEmptyArray(childObject))) {
|
|
676
|
+
unset(object, paths.slice(0, -1));
|
|
677
|
+
}
|
|
678
|
+
return object;
|
|
679
|
+
}
|
|
680
|
+
var objectHasFunction = (data)=>{
|
|
681
|
+
for(const key in data){
|
|
682
|
+
if (isFunction(data[key])) {
|
|
683
|
+
return true;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return false;
|
|
687
|
+
};
|
|
688
|
+
function isTraversable(value1) {
|
|
689
|
+
return Array.isArray(value1) || isObject(value1) && !objectHasFunction(value1);
|
|
690
|
+
}
|
|
691
|
+
function markFieldsDirty(data, fields = {}) {
|
|
692
|
+
for(const key in data){
|
|
693
|
+
if (isTraversable(data[key])) {
|
|
694
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
|
695
|
+
markFieldsDirty(data[key], fields[key]);
|
|
696
|
+
} else if (!isUndefined(data[key])) {
|
|
697
|
+
fields[key] = true;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return fields;
|
|
701
|
+
}
|
|
702
|
+
function getDirtyFields(data, formValues, dirtyFieldsFromValues) {
|
|
703
|
+
if (!dirtyFieldsFromValues) {
|
|
704
|
+
dirtyFieldsFromValues = markFieldsDirty(formValues);
|
|
705
|
+
}
|
|
706
|
+
for(const key in data){
|
|
707
|
+
if (isTraversable(data[key])) {
|
|
708
|
+
if (isUndefined(formValues) || isPrimitive(dirtyFieldsFromValues[key])) {
|
|
709
|
+
dirtyFieldsFromValues[key] = markFieldsDirty(data[key], Array.isArray(data[key]) ? [] : {});
|
|
710
|
+
} else {
|
|
711
|
+
getDirtyFields(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
|
712
|
+
}
|
|
713
|
+
} else {
|
|
714
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return dirtyFieldsFromValues;
|
|
718
|
+
}
|
|
719
|
+
const defaultResult = {
|
|
720
|
+
value: false,
|
|
721
|
+
isValid: false
|
|
722
|
+
};
|
|
723
|
+
const validResult = {
|
|
724
|
+
value: true,
|
|
725
|
+
isValid: true
|
|
726
|
+
};
|
|
727
|
+
var getCheckboxValue = (options)=>{
|
|
728
|
+
if (Array.isArray(options)) {
|
|
729
|
+
if (options.length > 1) {
|
|
730
|
+
const values = options.filter((option)=>option && option.checked && !option.disabled).map((option)=>option.value);
|
|
731
|
+
return {
|
|
732
|
+
value: values,
|
|
733
|
+
isValid: !!values.length
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
return options[0].checked && !options[0].disabled ? options[0].attributes && !isUndefined(options[0].attributes.value) ? isUndefined(options[0].value) || options[0].value === '' ? validResult : {
|
|
737
|
+
value: options[0].value,
|
|
738
|
+
isValid: true
|
|
739
|
+
} : validResult : defaultResult;
|
|
740
|
+
}
|
|
741
|
+
return defaultResult;
|
|
742
|
+
};
|
|
743
|
+
var getFieldValueAs = (value1, { valueAsNumber, valueAsDate, setValueAs })=>isUndefined(value1) ? value1 : valueAsNumber ? value1 === '' ? NaN : value1 ? +value1 : value1 : valueAsDate && isString(value1) ? new Date(value1) : setValueAs ? setValueAs(value1) : value1;
|
|
744
|
+
const defaultReturn = {
|
|
745
|
+
isValid: false,
|
|
746
|
+
value: null
|
|
747
|
+
};
|
|
748
|
+
var getRadioValue = (options)=>Array.isArray(options) ? options.reduce((previous, option)=>option && option.checked && !option.disabled ? {
|
|
749
|
+
isValid: true,
|
|
750
|
+
value: option.value
|
|
751
|
+
} : previous, defaultReturn) : defaultReturn;
|
|
752
|
+
function getFieldValue(_f) {
|
|
753
|
+
const ref = _f.ref;
|
|
754
|
+
if (isFileInput(ref)) {
|
|
755
|
+
return ref.files;
|
|
756
|
+
}
|
|
757
|
+
if (isRadioInput(ref)) {
|
|
758
|
+
return getRadioValue(_f.refs).value;
|
|
759
|
+
}
|
|
760
|
+
if (isMultipleSelect(ref)) {
|
|
761
|
+
return [
|
|
762
|
+
...ref.selectedOptions
|
|
763
|
+
].map(({ value: value1 })=>value1);
|
|
764
|
+
}
|
|
765
|
+
if (isCheckBoxInput(ref)) {
|
|
766
|
+
return getCheckboxValue(_f.refs).value;
|
|
767
|
+
}
|
|
768
|
+
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
|
769
|
+
}
|
|
770
|
+
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation)=>{
|
|
771
|
+
const fields = {};
|
|
772
|
+
for (const name of fieldsNames){
|
|
773
|
+
const field = get(_fields, name);
|
|
774
|
+
field && set(fields, name, field._f);
|
|
775
|
+
}
|
|
776
|
+
return {
|
|
777
|
+
criteriaMode,
|
|
778
|
+
names: [
|
|
779
|
+
...fieldsNames
|
|
780
|
+
],
|
|
781
|
+
fields,
|
|
782
|
+
shouldUseNativeValidation
|
|
783
|
+
};
|
|
784
|
+
};
|
|
785
|
+
var isRegex = (value1)=>value1 instanceof RegExp;
|
|
786
|
+
var getRuleValue = (rule)=>isUndefined(rule) ? rule : isRegex(rule) ? rule.source : isObject(rule) ? isRegex(rule.value) ? rule.value.source : rule.value : rule;
|
|
787
|
+
var getValidationModes = (mode)=>({
|
|
788
|
+
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
789
|
+
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
790
|
+
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
791
|
+
isOnAll: mode === VALIDATION_MODE.all,
|
|
792
|
+
isOnTouch: mode === VALIDATION_MODE.onTouched
|
|
793
|
+
});
|
|
794
|
+
const ASYNC_FUNCTION = 'AsyncFunction';
|
|
795
|
+
var hasPromiseValidation = (fieldReference)=>!!fieldReference && !!fieldReference.validate && !!(isFunction(fieldReference.validate) && fieldReference.validate.constructor.name === ASYNC_FUNCTION || isObject(fieldReference.validate) && Object.values(fieldReference.validate).find((validateFunction)=>validateFunction.constructor.name === ASYNC_FUNCTION));
|
|
796
|
+
var hasValidation = (options)=>options.mount && (options.required || options.min || options.max || options.maxLength || options.minLength || options.pattern || options.validate);
|
|
797
|
+
var isWatched = (name, _names, isBlurEvent)=>!isBlurEvent && (_names.watchAll || _names.watch.has(name) || [
|
|
798
|
+
..._names.watch
|
|
799
|
+
].some((watchName)=>name.startsWith(watchName) && /^\.\w+/.test(name.slice(watchName.length))));
|
|
800
|
+
const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly)=>{
|
|
801
|
+
for (const key of fieldsNames || Object.keys(fields)){
|
|
802
|
+
const field = get(fields, key);
|
|
803
|
+
if (field) {
|
|
804
|
+
const { _f, ...currentField } = field;
|
|
805
|
+
if (_f) {
|
|
806
|
+
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
|
|
807
|
+
return true;
|
|
808
|
+
} else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
|
|
809
|
+
return true;
|
|
810
|
+
} else {
|
|
811
|
+
if (iterateFieldsByAction(currentField, action)) {
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
} else if (isObject(currentField)) {
|
|
816
|
+
if (iterateFieldsByAction(currentField, action)) {
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
return;
|
|
823
|
+
};
|
|
824
|
+
function schemaErrorLookup(errors, _fields, name) {
|
|
825
|
+
const error = get(errors, name);
|
|
826
|
+
if (error || isKey(name)) {
|
|
827
|
+
return {
|
|
828
|
+
error,
|
|
829
|
+
name
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
const names = name.split('.');
|
|
833
|
+
while(names.length){
|
|
834
|
+
const fieldName = names.join('.');
|
|
835
|
+
const field = get(_fields, fieldName);
|
|
836
|
+
const foundError = get(errors, fieldName);
|
|
837
|
+
if (field && !Array.isArray(field) && name !== fieldName) {
|
|
838
|
+
return {
|
|
839
|
+
name
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
if (foundError && foundError.type) {
|
|
843
|
+
return {
|
|
844
|
+
name: fieldName,
|
|
845
|
+
error: foundError
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
if (foundError && foundError.root && foundError.root.type) {
|
|
849
|
+
return {
|
|
850
|
+
name: `${fieldName}.root`,
|
|
851
|
+
error: foundError.root
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
names.pop();
|
|
855
|
+
}
|
|
856
|
+
return {
|
|
857
|
+
name
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot)=>{
|
|
861
|
+
updateFormState(formStateData);
|
|
862
|
+
const { name, ...formState } = formStateData;
|
|
863
|
+
return isEmptyObject(formState) || Object.keys(formState).length >= Object.keys(_proxyFormState).length || Object.keys(formState).find((key)=>_proxyFormState[key] === (!isRoot || VALIDATION_MODE.all));
|
|
864
|
+
};
|
|
865
|
+
var shouldSubscribeByName = (name, signalName, exact)=>!name || !signalName || name === signalName || convertToArrayPayload(name).some((currentName)=>currentName && (exact ? currentName === signalName : currentName.startsWith(signalName) || signalName.startsWith(currentName)));
|
|
866
|
+
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode)=>{
|
|
867
|
+
if (mode.isOnAll) {
|
|
868
|
+
return false;
|
|
869
|
+
} else if (!isSubmitted && mode.isOnTouch) {
|
|
870
|
+
return !(isTouched || isBlurEvent);
|
|
871
|
+
} else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
|
872
|
+
return !isBlurEvent;
|
|
873
|
+
} else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
|
874
|
+
return isBlurEvent;
|
|
875
|
+
}
|
|
876
|
+
return true;
|
|
877
|
+
};
|
|
878
|
+
var unsetEmptyArray = (ref, name)=>!compact(get(ref, name)).length && unset(ref, name);
|
|
879
|
+
var updateFieldArrayRootError = (errors, error, name)=>{
|
|
880
|
+
const fieldArrayErrors = convertToArrayPayload(get(errors, name));
|
|
881
|
+
set(fieldArrayErrors, 'root', error[name]);
|
|
882
|
+
set(errors, name, fieldArrayErrors);
|
|
883
|
+
return errors;
|
|
884
|
+
};
|
|
885
|
+
function getValidateError(result, ref, type = 'validate') {
|
|
886
|
+
if (isString(result) || Array.isArray(result) && result.every(isString) || isBoolean(result) && !result) {
|
|
887
|
+
return {
|
|
888
|
+
type,
|
|
889
|
+
message: isString(result) ? result : '',
|
|
890
|
+
ref
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
var getValueAndMessage = (validationData)=>isObject(validationData) && !isRegex(validationData) ? validationData : {
|
|
895
|
+
value: validationData,
|
|
896
|
+
message: ''
|
|
897
|
+
};
|
|
898
|
+
var validateField = async (field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray)=>{
|
|
899
|
+
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount } = field._f;
|
|
900
|
+
const inputValue = get(formValues, name);
|
|
901
|
+
if (!mount || disabledFieldNames.has(name)) {
|
|
902
|
+
return {};
|
|
903
|
+
}
|
|
904
|
+
const inputRef = refs ? refs[0] : ref;
|
|
905
|
+
const setCustomValidity = (message)=>{
|
|
906
|
+
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
|
907
|
+
inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
|
|
908
|
+
inputRef.reportValidity();
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
const error = {};
|
|
912
|
+
const isRadio = isRadioInput(ref);
|
|
913
|
+
const isCheckBox = isCheckBoxInput(ref);
|
|
914
|
+
const isRadioOrCheckbox = isRadio || isCheckBox;
|
|
915
|
+
const isEmpty = (valueAsNumber || isFileInput(ref)) && isUndefined(ref.value) && isUndefined(inputValue) || isHTMLElement(ref) && ref.value === '' || inputValue === '' || Array.isArray(inputValue) && !inputValue.length;
|
|
916
|
+
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
917
|
+
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength)=>{
|
|
918
|
+
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
919
|
+
error[name] = {
|
|
920
|
+
type: exceedMax ? maxType : minType,
|
|
921
|
+
message,
|
|
922
|
+
ref,
|
|
923
|
+
...appendErrorsCurry(exceedMax ? maxType : minType, message)
|
|
924
|
+
};
|
|
925
|
+
};
|
|
926
|
+
if (isFieldArray ? !Array.isArray(inputValue) || !inputValue.length : required && (!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue)) || isBoolean(inputValue) && !inputValue || isCheckBox && !getCheckboxValue(refs).isValid || isRadio && !getRadioValue(refs).isValid)) {
|
|
927
|
+
const { value: value1, message } = isString(required) ? {
|
|
928
|
+
value: !!required,
|
|
929
|
+
message: required
|
|
930
|
+
} : getValueAndMessage(required);
|
|
931
|
+
if (value1) {
|
|
932
|
+
error[name] = {
|
|
933
|
+
type: INPUT_VALIDATION_RULES.required,
|
|
934
|
+
message,
|
|
935
|
+
ref: inputRef,
|
|
936
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message)
|
|
937
|
+
};
|
|
938
|
+
if (!validateAllFieldCriteria) {
|
|
939
|
+
setCustomValidity(message);
|
|
940
|
+
return error;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
|
945
|
+
let exceedMax;
|
|
946
|
+
let exceedMin;
|
|
947
|
+
const maxOutput = getValueAndMessage(max);
|
|
948
|
+
const minOutput = getValueAndMessage(min);
|
|
949
|
+
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
|
950
|
+
const valueNumber = ref.valueAsNumber || (inputValue ? +inputValue : inputValue);
|
|
951
|
+
if (!isNullOrUndefined(maxOutput.value)) {
|
|
952
|
+
exceedMax = valueNumber > maxOutput.value;
|
|
953
|
+
}
|
|
954
|
+
if (!isNullOrUndefined(minOutput.value)) {
|
|
955
|
+
exceedMin = valueNumber < minOutput.value;
|
|
956
|
+
}
|
|
957
|
+
} else {
|
|
958
|
+
const valueDate = ref.valueAsDate || new Date(inputValue);
|
|
959
|
+
const convertTimeToDate = (time)=>new Date(new Date().toDateString() + ' ' + time);
|
|
960
|
+
const isTime = ref.type == 'time';
|
|
961
|
+
const isWeek = ref.type == 'week';
|
|
962
|
+
if (isString(maxOutput.value) && inputValue) {
|
|
963
|
+
exceedMax = isTime ? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value) : isWeek ? inputValue > maxOutput.value : valueDate > new Date(maxOutput.value);
|
|
964
|
+
}
|
|
965
|
+
if (isString(minOutput.value) && inputValue) {
|
|
966
|
+
exceedMin = isTime ? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value) : isWeek ? inputValue < minOutput.value : valueDate < new Date(minOutput.value);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
if (exceedMax || exceedMin) {
|
|
970
|
+
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
971
|
+
if (!validateAllFieldCriteria) {
|
|
972
|
+
setCustomValidity(error[name].message);
|
|
973
|
+
return error;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
if ((maxLength || minLength) && !isEmpty && (isString(inputValue) || isFieldArray && Array.isArray(inputValue))) {
|
|
978
|
+
const maxLengthOutput = getValueAndMessage(maxLength);
|
|
979
|
+
const minLengthOutput = getValueAndMessage(minLength);
|
|
980
|
+
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) && inputValue.length > +maxLengthOutput.value;
|
|
981
|
+
const exceedMin = !isNullOrUndefined(minLengthOutput.value) && inputValue.length < +minLengthOutput.value;
|
|
982
|
+
if (exceedMax || exceedMin) {
|
|
983
|
+
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
|
984
|
+
if (!validateAllFieldCriteria) {
|
|
985
|
+
setCustomValidity(error[name].message);
|
|
986
|
+
return error;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
if (pattern && !isEmpty && isString(inputValue)) {
|
|
991
|
+
const { value: patternValue, message } = getValueAndMessage(pattern);
|
|
992
|
+
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
|
993
|
+
error[name] = {
|
|
994
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
|
995
|
+
message,
|
|
996
|
+
ref,
|
|
997
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message)
|
|
998
|
+
};
|
|
999
|
+
if (!validateAllFieldCriteria) {
|
|
1000
|
+
setCustomValidity(message);
|
|
1001
|
+
return error;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
if (validate) {
|
|
1006
|
+
if (isFunction(validate)) {
|
|
1007
|
+
const result = await validate(inputValue, formValues);
|
|
1008
|
+
const validateError = getValidateError(result, inputRef);
|
|
1009
|
+
if (validateError) {
|
|
1010
|
+
error[name] = {
|
|
1011
|
+
...validateError,
|
|
1012
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message)
|
|
1013
|
+
};
|
|
1014
|
+
if (!validateAllFieldCriteria) {
|
|
1015
|
+
setCustomValidity(validateError.message);
|
|
1016
|
+
return error;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
} else if (isObject(validate)) {
|
|
1020
|
+
let validationResult = {};
|
|
1021
|
+
for(const key in validate){
|
|
1022
|
+
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
1023
|
+
break;
|
|
1024
|
+
}
|
|
1025
|
+
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
|
1026
|
+
if (validateError) {
|
|
1027
|
+
validationResult = {
|
|
1028
|
+
...validateError,
|
|
1029
|
+
...appendErrorsCurry(key, validateError.message)
|
|
1030
|
+
};
|
|
1031
|
+
setCustomValidity(validateError.message);
|
|
1032
|
+
if (validateAllFieldCriteria) {
|
|
1033
|
+
error[name] = validationResult;
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
if (!isEmptyObject(validationResult)) {
|
|
1038
|
+
error[name] = {
|
|
1039
|
+
ref: inputRef,
|
|
1040
|
+
...validationResult
|
|
1041
|
+
};
|
|
1042
|
+
if (!validateAllFieldCriteria) {
|
|
1043
|
+
return error;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
setCustomValidity(true);
|
|
1049
|
+
return error;
|
|
1050
|
+
};
|
|
1051
|
+
const defaultOptions = {
|
|
1052
|
+
mode: VALIDATION_MODE.onSubmit,
|
|
1053
|
+
reValidateMode: VALIDATION_MODE.onChange,
|
|
1054
|
+
shouldFocusError: true
|
|
1055
|
+
};
|
|
1056
|
+
function createFormControl(props = {}) {
|
|
1057
|
+
let _options = {
|
|
1058
|
+
...defaultOptions,
|
|
1059
|
+
...props
|
|
1060
|
+
};
|
|
1061
|
+
let _formState = {
|
|
1062
|
+
submitCount: 0,
|
|
1063
|
+
isDirty: false,
|
|
1064
|
+
isReady: false,
|
|
1065
|
+
isLoading: isFunction(_options.defaultValues),
|
|
1066
|
+
isValidating: false,
|
|
1067
|
+
isSubmitted: false,
|
|
1068
|
+
isSubmitting: false,
|
|
1069
|
+
isSubmitSuccessful: false,
|
|
1070
|
+
isValid: false,
|
|
1071
|
+
touchedFields: {},
|
|
1072
|
+
dirtyFields: {},
|
|
1073
|
+
validatingFields: {},
|
|
1074
|
+
errors: _options.errors || {},
|
|
1075
|
+
disabled: _options.disabled || false
|
|
1076
|
+
};
|
|
1077
|
+
let _fields = {};
|
|
1078
|
+
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values) ? cloneObject(_options.defaultValues || _options.values) || {} : {};
|
|
1079
|
+
let _formValues = _options.shouldUnregister ? {} : cloneObject(_defaultValues);
|
|
1080
|
+
let _state = {
|
|
1081
|
+
action: false,
|
|
1082
|
+
mount: false,
|
|
1083
|
+
watch: false
|
|
1084
|
+
};
|
|
1085
|
+
let _names = {
|
|
1086
|
+
mount: new Set(),
|
|
1087
|
+
disabled: new Set(),
|
|
1088
|
+
unMount: new Set(),
|
|
1089
|
+
array: new Set(),
|
|
1090
|
+
watch: new Set()
|
|
1091
|
+
};
|
|
1092
|
+
let delayErrorCallback;
|
|
1093
|
+
let timer = 0;
|
|
1094
|
+
const _proxyFormState = {
|
|
1095
|
+
isDirty: false,
|
|
1096
|
+
dirtyFields: false,
|
|
1097
|
+
validatingFields: false,
|
|
1098
|
+
touchedFields: false,
|
|
1099
|
+
isValidating: false,
|
|
1100
|
+
isValid: false,
|
|
1101
|
+
errors: false
|
|
1102
|
+
};
|
|
1103
|
+
let _proxySubscribeFormState = {
|
|
1104
|
+
..._proxyFormState
|
|
1105
|
+
};
|
|
1106
|
+
const _subjects = {
|
|
1107
|
+
array: createSubject(),
|
|
1108
|
+
state: createSubject()
|
|
1109
|
+
};
|
|
1110
|
+
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
|
1111
|
+
const debounce = (callback)=>(wait)=>{
|
|
1112
|
+
clearTimeout(timer);
|
|
1113
|
+
timer = setTimeout(callback, wait);
|
|
1114
|
+
};
|
|
1115
|
+
const _setValid = async (shouldUpdateValid)=>{
|
|
1116
|
+
if (!_options.disabled && (_proxyFormState.isValid || _proxySubscribeFormState.isValid || shouldUpdateValid)) {
|
|
1117
|
+
const isValid = _options.resolver ? isEmptyObject((await _runSchema()).errors) : await executeBuiltInValidation(_fields, true);
|
|
1118
|
+
if (isValid !== _formState.isValid) {
|
|
1119
|
+
_subjects.state.next({
|
|
1120
|
+
isValid
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
1125
|
+
const _updateIsValidating = (names, isValidating)=>{
|
|
1126
|
+
if (!_options.disabled && (_proxyFormState.isValidating || _proxyFormState.validatingFields || _proxySubscribeFormState.isValidating || _proxySubscribeFormState.validatingFields)) {
|
|
1127
|
+
(names || Array.from(_names.mount)).forEach((name)=>{
|
|
1128
|
+
if (name) {
|
|
1129
|
+
isValidating ? set(_formState.validatingFields, name, isValidating) : unset(_formState.validatingFields, name);
|
|
1130
|
+
}
|
|
1131
|
+
});
|
|
1132
|
+
_subjects.state.next({
|
|
1133
|
+
validatingFields: _formState.validatingFields,
|
|
1134
|
+
isValidating: !isEmptyObject(_formState.validatingFields)
|
|
1135
|
+
});
|
|
1136
|
+
}
|
|
1137
|
+
};
|
|
1138
|
+
const _setFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true)=>{
|
|
1139
|
+
if (args && method && !_options.disabled) {
|
|
1140
|
+
_state.action = true;
|
|
1141
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
|
1142
|
+
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
|
1143
|
+
shouldSetValues && set(_fields, name, fieldValues);
|
|
1144
|
+
}
|
|
1145
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_formState.errors, name))) {
|
|
1146
|
+
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
|
1147
|
+
shouldSetValues && set(_formState.errors, name, errors);
|
|
1148
|
+
unsetEmptyArray(_formState.errors, name);
|
|
1149
|
+
}
|
|
1150
|
+
if ((_proxyFormState.touchedFields || _proxySubscribeFormState.touchedFields) && shouldUpdateFieldsAndState && Array.isArray(get(_formState.touchedFields, name))) {
|
|
1151
|
+
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
|
1152
|
+
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
|
1153
|
+
}
|
|
1154
|
+
if (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) {
|
|
1155
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
|
1156
|
+
}
|
|
1157
|
+
_subjects.state.next({
|
|
1158
|
+
name,
|
|
1159
|
+
isDirty: _getDirty(name, values),
|
|
1160
|
+
dirtyFields: _formState.dirtyFields,
|
|
1161
|
+
errors: _formState.errors,
|
|
1162
|
+
isValid: _formState.isValid
|
|
1163
|
+
});
|
|
1164
|
+
} else {
|
|
1165
|
+
set(_formValues, name, values);
|
|
1166
|
+
}
|
|
1167
|
+
};
|
|
1168
|
+
const updateErrors = (name, error)=>{
|
|
1169
|
+
set(_formState.errors, name, error);
|
|
1170
|
+
_subjects.state.next({
|
|
1171
|
+
errors: _formState.errors
|
|
1172
|
+
});
|
|
1173
|
+
};
|
|
1174
|
+
const _setErrors = (errors)=>{
|
|
1175
|
+
_formState.errors = errors;
|
|
1176
|
+
_subjects.state.next({
|
|
1177
|
+
errors: _formState.errors,
|
|
1178
|
+
isValid: false
|
|
1179
|
+
});
|
|
1180
|
+
};
|
|
1181
|
+
const updateValidAndValue = (name, shouldSkipSetValueAs, value1, ref)=>{
|
|
1182
|
+
const field = get(_fields, name);
|
|
1183
|
+
if (field) {
|
|
1184
|
+
const defaultValue = get(_formValues, name, isUndefined(value1) ? get(_defaultValues, name) : value1);
|
|
1185
|
+
isUndefined(defaultValue) || ref && ref.defaultChecked || shouldSkipSetValueAs ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f)) : setFieldValue(name, defaultValue);
|
|
1186
|
+
_state.mount && _setValid();
|
|
1187
|
+
}
|
|
1188
|
+
};
|
|
1189
|
+
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender)=>{
|
|
1190
|
+
let shouldUpdateField = false;
|
|
1191
|
+
let isPreviousDirty = false;
|
|
1192
|
+
const output = {
|
|
1193
|
+
name
|
|
1194
|
+
};
|
|
1195
|
+
if (!_options.disabled) {
|
|
1196
|
+
if (!isBlurEvent || shouldDirty) {
|
|
1197
|
+
if (_proxyFormState.isDirty || _proxySubscribeFormState.isDirty) {
|
|
1198
|
+
isPreviousDirty = _formState.isDirty;
|
|
1199
|
+
_formState.isDirty = output.isDirty = _getDirty();
|
|
1200
|
+
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
|
1201
|
+
}
|
|
1202
|
+
const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
|
|
1203
|
+
isPreviousDirty = !!get(_formState.dirtyFields, name);
|
|
1204
|
+
isCurrentFieldPristine ? unset(_formState.dirtyFields, name) : set(_formState.dirtyFields, name, true);
|
|
1205
|
+
output.dirtyFields = _formState.dirtyFields;
|
|
1206
|
+
shouldUpdateField = shouldUpdateField || (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) && isPreviousDirty !== !isCurrentFieldPristine;
|
|
1207
|
+
}
|
|
1208
|
+
if (isBlurEvent) {
|
|
1209
|
+
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
|
1210
|
+
if (!isPreviousFieldTouched) {
|
|
1211
|
+
set(_formState.touchedFields, name, isBlurEvent);
|
|
1212
|
+
output.touchedFields = _formState.touchedFields;
|
|
1213
|
+
shouldUpdateField = shouldUpdateField || (_proxyFormState.touchedFields || _proxySubscribeFormState.touchedFields) && isPreviousFieldTouched !== isBlurEvent;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
|
1217
|
+
}
|
|
1218
|
+
return shouldUpdateField ? output : {};
|
|
1219
|
+
};
|
|
1220
|
+
const shouldRenderByError = (name, isValid, error, fieldState)=>{
|
|
1221
|
+
const previousFieldError = get(_formState.errors, name);
|
|
1222
|
+
const shouldUpdateValid = (_proxyFormState.isValid || _proxySubscribeFormState.isValid) && isBoolean(isValid) && _formState.isValid !== isValid;
|
|
1223
|
+
if (_options.delayError && error) {
|
|
1224
|
+
delayErrorCallback = debounce(()=>updateErrors(name, error));
|
|
1225
|
+
delayErrorCallback(_options.delayError);
|
|
1226
|
+
} else {
|
|
1227
|
+
clearTimeout(timer);
|
|
1228
|
+
delayErrorCallback = null;
|
|
1229
|
+
error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
|
|
1230
|
+
}
|
|
1231
|
+
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) || !isEmptyObject(fieldState) || shouldUpdateValid) {
|
|
1232
|
+
const updatedFormState = {
|
|
1233
|
+
...fieldState,
|
|
1234
|
+
...shouldUpdateValid && isBoolean(isValid) ? {
|
|
1235
|
+
isValid
|
|
1236
|
+
} : {},
|
|
1237
|
+
errors: _formState.errors,
|
|
1238
|
+
name
|
|
1239
|
+
};
|
|
1240
|
+
_formState = {
|
|
1241
|
+
..._formState,
|
|
1242
|
+
...updatedFormState
|
|
1243
|
+
};
|
|
1244
|
+
_subjects.state.next(updatedFormState);
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
const _runSchema = async (name)=>{
|
|
1248
|
+
_updateIsValidating(name, true);
|
|
1249
|
+
const result = await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
|
1250
|
+
_updateIsValidating(name);
|
|
1251
|
+
return result;
|
|
1252
|
+
};
|
|
1253
|
+
const executeSchemaAndUpdateState = async (names)=>{
|
|
1254
|
+
const { errors } = await _runSchema(names);
|
|
1255
|
+
if (names) {
|
|
1256
|
+
for (const name of names){
|
|
1257
|
+
const error = get(errors, name);
|
|
1258
|
+
error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
|
|
1259
|
+
}
|
|
1260
|
+
} else {
|
|
1261
|
+
_formState.errors = errors;
|
|
1262
|
+
}
|
|
1263
|
+
return errors;
|
|
1264
|
+
};
|
|
1265
|
+
const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
|
|
1266
|
+
valid: true
|
|
1267
|
+
})=>{
|
|
1268
|
+
for(const name in fields){
|
|
1269
|
+
const field = fields[name];
|
|
1270
|
+
if (field) {
|
|
1271
|
+
const { _f, ...fieldValue } = field;
|
|
1272
|
+
if (_f) {
|
|
1273
|
+
const isFieldArrayRoot = _names.array.has(_f.name);
|
|
1274
|
+
const isPromiseFunction = field._f && hasPromiseValidation(field._f);
|
|
1275
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
|
1276
|
+
_updateIsValidating([
|
|
1277
|
+
_f.name
|
|
1278
|
+
], true);
|
|
1279
|
+
}
|
|
1280
|
+
const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
|
1281
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
|
1282
|
+
_updateIsValidating([
|
|
1283
|
+
_f.name
|
|
1284
|
+
]);
|
|
1285
|
+
}
|
|
1286
|
+
if (fieldError[_f.name]) {
|
|
1287
|
+
context.valid = false;
|
|
1288
|
+
if (shouldOnlyCheckValid) {
|
|
1289
|
+
break;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
!shouldOnlyCheckValid && (get(fieldError, _f.name) ? isFieldArrayRoot ? updateFieldArrayRootError(_formState.errors, fieldError, _f.name) : set(_formState.errors, _f.name, fieldError[_f.name]) : unset(_formState.errors, _f.name));
|
|
1293
|
+
}
|
|
1294
|
+
!isEmptyObject(fieldValue) && await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
return context.valid;
|
|
1298
|
+
};
|
|
1299
|
+
const _removeUnmounted = ()=>{
|
|
1300
|
+
for (const name of _names.unMount){
|
|
1301
|
+
const field = get(_fields, name);
|
|
1302
|
+
field && (field._f.refs ? field._f.refs.every((ref)=>!live(ref)) : !live(field._f.ref)) && unregister(name);
|
|
1303
|
+
}
|
|
1304
|
+
_names.unMount = new Set();
|
|
1305
|
+
};
|
|
1306
|
+
const _getDirty = (name, data)=>!_options.disabled && (name && data && set(_formValues, name, data), !deepEqual(getValues(), _defaultValues));
|
|
1307
|
+
const _getWatch = (names, defaultValue, isGlobal)=>generateWatchOutput(names, _names, {
|
|
1308
|
+
..._state.mount ? _formValues : isUndefined(defaultValue) ? _defaultValues : isString(names) ? {
|
|
1309
|
+
[names]: defaultValue
|
|
1310
|
+
} : defaultValue
|
|
1311
|
+
}, isGlobal, defaultValue);
|
|
1312
|
+
const _getFieldArray = (name)=>compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
|
|
1313
|
+
const setFieldValue = (name, value1, options = {})=>{
|
|
1314
|
+
const field = get(_fields, name);
|
|
1315
|
+
let fieldValue = value1;
|
|
1316
|
+
if (field) {
|
|
1317
|
+
const fieldReference = field._f;
|
|
1318
|
+
if (fieldReference) {
|
|
1319
|
+
!fieldReference.disabled && set(_formValues, name, getFieldValueAs(value1, fieldReference));
|
|
1320
|
+
fieldValue = isHTMLElement(fieldReference.ref) && isNullOrUndefined(value1) ? '' : value1;
|
|
1321
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
|
1322
|
+
[
|
|
1323
|
+
...fieldReference.ref.options
|
|
1324
|
+
].forEach((optionRef)=>optionRef.selected = fieldValue.includes(optionRef.value));
|
|
1325
|
+
} else if (fieldReference.refs) {
|
|
1326
|
+
if (isCheckBoxInput(fieldReference.ref)) {
|
|
1327
|
+
fieldReference.refs.forEach((checkboxRef)=>{
|
|
1328
|
+
if (!checkboxRef.defaultChecked || !checkboxRef.disabled) {
|
|
1329
|
+
if (Array.isArray(fieldValue)) {
|
|
1330
|
+
checkboxRef.checked = !!fieldValue.find((data)=>data === checkboxRef.value);
|
|
1331
|
+
} else {
|
|
1332
|
+
checkboxRef.checked = fieldValue === checkboxRef.value || !!fieldValue;
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
});
|
|
1336
|
+
} else {
|
|
1337
|
+
fieldReference.refs.forEach((radioRef)=>radioRef.checked = radioRef.value === fieldValue);
|
|
1338
|
+
}
|
|
1339
|
+
} else if (isFileInput(fieldReference.ref)) {
|
|
1340
|
+
fieldReference.ref.value = '';
|
|
1341
|
+
} else {
|
|
1342
|
+
fieldReference.ref.value = fieldValue;
|
|
1343
|
+
if (!fieldReference.ref.type) {
|
|
1344
|
+
_subjects.state.next({
|
|
1345
|
+
name,
|
|
1346
|
+
values: cloneObject(_formValues)
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
(options.shouldDirty || options.shouldTouch) && updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
|
1353
|
+
options.shouldValidate && trigger(name);
|
|
1354
|
+
};
|
|
1355
|
+
const setValues = (name, value1, options)=>{
|
|
1356
|
+
for(const fieldKey in value1){
|
|
1357
|
+
if (!value1.hasOwnProperty(fieldKey)) {
|
|
1358
|
+
return;
|
|
1359
|
+
}
|
|
1360
|
+
const fieldValue = value1[fieldKey];
|
|
1361
|
+
const fieldName = name + '.' + fieldKey;
|
|
1362
|
+
const field = get(_fields, fieldName);
|
|
1363
|
+
(_names.array.has(name) || isObject(fieldValue) || field && !field._f) && !isDateObject(fieldValue) ? setValues(fieldName, fieldValue, options) : setFieldValue(fieldName, fieldValue, options);
|
|
1364
|
+
}
|
|
1365
|
+
};
|
|
1366
|
+
const setValue = (name, value1, options = {})=>{
|
|
1367
|
+
const field = get(_fields, name);
|
|
1368
|
+
const isFieldArray = _names.array.has(name);
|
|
1369
|
+
const cloneValue = cloneObject(value1);
|
|
1370
|
+
set(_formValues, name, cloneValue);
|
|
1371
|
+
if (isFieldArray) {
|
|
1372
|
+
_subjects.array.next({
|
|
1373
|
+
name,
|
|
1374
|
+
values: cloneObject(_formValues)
|
|
1375
|
+
});
|
|
1376
|
+
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields || _proxySubscribeFormState.isDirty || _proxySubscribeFormState.dirtyFields) && options.shouldDirty) {
|
|
1377
|
+
_subjects.state.next({
|
|
1378
|
+
name,
|
|
1379
|
+
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
|
1380
|
+
isDirty: _getDirty(name, cloneValue)
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
} else {
|
|
1384
|
+
field && !field._f && !isNullOrUndefined(cloneValue) ? setValues(name, cloneValue, options) : setFieldValue(name, cloneValue, options);
|
|
1385
|
+
}
|
|
1386
|
+
isWatched(name, _names) && _subjects.state.next({
|
|
1387
|
+
..._formState,
|
|
1388
|
+
name
|
|
1389
|
+
});
|
|
1390
|
+
_subjects.state.next({
|
|
1391
|
+
name: _state.mount ? name : undefined,
|
|
1392
|
+
values: cloneObject(_formValues)
|
|
1393
|
+
});
|
|
1394
|
+
};
|
|
1395
|
+
const onChange = async (event)=>{
|
|
1396
|
+
_state.mount = true;
|
|
1397
|
+
const target = event.target;
|
|
1398
|
+
let name = target.name;
|
|
1399
|
+
let isFieldValueUpdated = true;
|
|
1400
|
+
const field = get(_fields, name);
|
|
1401
|
+
const _updateIsFieldValueUpdated = (fieldValue)=>{
|
|
1402
|
+
isFieldValueUpdated = Number.isNaN(fieldValue) || isDateObject(fieldValue) && isNaN(fieldValue.getTime()) || deepEqual(fieldValue, get(_formValues, name, fieldValue));
|
|
1403
|
+
};
|
|
1404
|
+
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
|
1405
|
+
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
|
1406
|
+
if (field) {
|
|
1407
|
+
let error;
|
|
1408
|
+
let isValid;
|
|
1409
|
+
const fieldValue = target.type ? getFieldValue(field._f) : getEventValue(event);
|
|
1410
|
+
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
|
1411
|
+
const shouldSkipValidation = !hasValidation(field._f) && !_options.resolver && !get(_formState.errors, name) && !field._f.deps || skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
|
1412
|
+
const watched = isWatched(name, _names, isBlurEvent);
|
|
1413
|
+
set(_formValues, name, fieldValue);
|
|
1414
|
+
if (isBlurEvent) {
|
|
1415
|
+
if (!target || !target.readOnly) {
|
|
1416
|
+
field._f.onBlur && field._f.onBlur(event);
|
|
1417
|
+
delayErrorCallback && delayErrorCallback(0);
|
|
1418
|
+
}
|
|
1419
|
+
} else if (field._f.onChange) {
|
|
1420
|
+
field._f.onChange(event);
|
|
1421
|
+
}
|
|
1422
|
+
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent);
|
|
1423
|
+
const shouldRender = !isEmptyObject(fieldState) || watched;
|
|
1424
|
+
!isBlurEvent && _subjects.state.next({
|
|
1425
|
+
name,
|
|
1426
|
+
type: event.type,
|
|
1427
|
+
values: cloneObject(_formValues)
|
|
1428
|
+
});
|
|
1429
|
+
if (shouldSkipValidation) {
|
|
1430
|
+
if (_proxyFormState.isValid || _proxySubscribeFormState.isValid) {
|
|
1431
|
+
if (_options.mode === 'onBlur') {
|
|
1432
|
+
if (isBlurEvent) {
|
|
1433
|
+
_setValid();
|
|
1434
|
+
}
|
|
1435
|
+
} else if (!isBlurEvent) {
|
|
1436
|
+
_setValid();
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
return shouldRender && _subjects.state.next({
|
|
1440
|
+
name,
|
|
1441
|
+
...watched ? {} : fieldState
|
|
1442
|
+
});
|
|
1443
|
+
}
|
|
1444
|
+
!isBlurEvent && watched && _subjects.state.next({
|
|
1445
|
+
..._formState
|
|
1446
|
+
});
|
|
1447
|
+
if (_options.resolver) {
|
|
1448
|
+
const { errors } = await _runSchema([
|
|
1449
|
+
name
|
|
1450
|
+
]);
|
|
1451
|
+
_updateIsFieldValueUpdated(fieldValue);
|
|
1452
|
+
if (isFieldValueUpdated) {
|
|
1453
|
+
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
|
1454
|
+
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
|
1455
|
+
error = errorLookupResult.error;
|
|
1456
|
+
name = errorLookupResult.name;
|
|
1457
|
+
isValid = isEmptyObject(errors);
|
|
1458
|
+
}
|
|
1459
|
+
} else {
|
|
1460
|
+
_updateIsValidating([
|
|
1461
|
+
name
|
|
1462
|
+
], true);
|
|
1463
|
+
error = (await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
|
1464
|
+
_updateIsValidating([
|
|
1465
|
+
name
|
|
1466
|
+
]);
|
|
1467
|
+
_updateIsFieldValueUpdated(fieldValue);
|
|
1468
|
+
if (isFieldValueUpdated) {
|
|
1469
|
+
if (error) {
|
|
1470
|
+
isValid = false;
|
|
1471
|
+
} else if (_proxyFormState.isValid || _proxySubscribeFormState.isValid) {
|
|
1472
|
+
isValid = await executeBuiltInValidation(_fields, true);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
if (isFieldValueUpdated) {
|
|
1477
|
+
field._f.deps && (!Array.isArray(field._f.deps) || field._f.deps.length > 0) && trigger(field._f.deps);
|
|
1478
|
+
shouldRenderByError(name, isValid, error, fieldState);
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
};
|
|
1482
|
+
const _focusInput = (ref, key)=>{
|
|
1483
|
+
if (get(_formState.errors, key) && ref.focus) {
|
|
1484
|
+
ref.focus();
|
|
1485
|
+
return 1;
|
|
1486
|
+
}
|
|
1487
|
+
return;
|
|
1488
|
+
};
|
|
1489
|
+
const trigger = async (name, options = {})=>{
|
|
1490
|
+
let isValid;
|
|
1491
|
+
let validationResult;
|
|
1492
|
+
const fieldNames = convertToArrayPayload(name);
|
|
1493
|
+
if (_options.resolver) {
|
|
1494
|
+
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
|
1495
|
+
isValid = isEmptyObject(errors);
|
|
1496
|
+
validationResult = name ? !fieldNames.some((name)=>get(errors, name)) : isValid;
|
|
1497
|
+
} else if (name) {
|
|
1498
|
+
validationResult = (await Promise.all(fieldNames.map(async (fieldName)=>{
|
|
1499
|
+
const field = get(_fields, fieldName);
|
|
1500
|
+
return await executeBuiltInValidation(field && field._f ? {
|
|
1501
|
+
[fieldName]: field
|
|
1502
|
+
} : field);
|
|
1503
|
+
}))).every(Boolean);
|
|
1504
|
+
!(!validationResult && !_formState.isValid) && _setValid();
|
|
1505
|
+
} else {
|
|
1506
|
+
validationResult = isValid = await executeBuiltInValidation(_fields);
|
|
1507
|
+
}
|
|
1508
|
+
_subjects.state.next({
|
|
1509
|
+
...!isString(name) || (_proxyFormState.isValid || _proxySubscribeFormState.isValid) && isValid !== _formState.isValid ? {} : {
|
|
1510
|
+
name
|
|
1511
|
+
},
|
|
1512
|
+
..._options.resolver || !name ? {
|
|
1513
|
+
isValid
|
|
1514
|
+
} : {},
|
|
1515
|
+
errors: _formState.errors
|
|
1516
|
+
});
|
|
1517
|
+
options.shouldFocus && !validationResult && iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
|
|
1518
|
+
return validationResult;
|
|
1519
|
+
};
|
|
1520
|
+
const getValues = (fieldNames, config)=>{
|
|
1521
|
+
let values = {
|
|
1522
|
+
..._state.mount ? _formValues : _defaultValues
|
|
1523
|
+
};
|
|
1524
|
+
if (config) {
|
|
1525
|
+
values = extractFormValues(config.dirtyFields ? _formState.dirtyFields : _formState.touchedFields, values);
|
|
1526
|
+
}
|
|
1527
|
+
return isUndefined(fieldNames) ? values : isString(fieldNames) ? get(values, fieldNames) : fieldNames.map((name)=>get(values, name));
|
|
1528
|
+
};
|
|
1529
|
+
const getFieldState = (name, formState)=>({
|
|
1530
|
+
invalid: !!get((formState || _formState).errors, name),
|
|
1531
|
+
isDirty: !!get((formState || _formState).dirtyFields, name),
|
|
1532
|
+
error: get((formState || _formState).errors, name),
|
|
1533
|
+
isValidating: !!get(_formState.validatingFields, name),
|
|
1534
|
+
isTouched: !!get((formState || _formState).touchedFields, name)
|
|
1535
|
+
});
|
|
1536
|
+
const clearErrors = (name)=>{
|
|
1537
|
+
name && convertToArrayPayload(name).forEach((inputName)=>unset(_formState.errors, inputName));
|
|
1538
|
+
_subjects.state.next({
|
|
1539
|
+
errors: name ? _formState.errors : {}
|
|
1540
|
+
});
|
|
1541
|
+
};
|
|
1542
|
+
const setError = (name, error, options)=>{
|
|
1543
|
+
const ref = (get(_fields, name, {
|
|
1544
|
+
_f: {}
|
|
1545
|
+
})._f || {}).ref;
|
|
1546
|
+
const currentError = get(_formState.errors, name) || {};
|
|
1547
|
+
// Don't override existing error messages elsewhere in the object tree.
|
|
1548
|
+
const { ref: currentRef, message, type, ...restOfErrorTree } = currentError;
|
|
1549
|
+
set(_formState.errors, name, {
|
|
1550
|
+
...restOfErrorTree,
|
|
1551
|
+
...error,
|
|
1552
|
+
ref
|
|
1553
|
+
});
|
|
1554
|
+
_subjects.state.next({
|
|
1555
|
+
name,
|
|
1556
|
+
errors: _formState.errors,
|
|
1557
|
+
isValid: false
|
|
1558
|
+
});
|
|
1559
|
+
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
|
1560
|
+
};
|
|
1561
|
+
const watch = (name, defaultValue)=>isFunction(name) ? _subjects.state.subscribe({
|
|
1562
|
+
next: (payload)=>'values' in payload && name(_getWatch(undefined, defaultValue), payload)
|
|
1563
|
+
}) : _getWatch(name, defaultValue, true);
|
|
1564
|
+
const _subscribe = (props)=>_subjects.state.subscribe({
|
|
1565
|
+
next: (formState)=>{
|
|
1566
|
+
if (shouldSubscribeByName(props.name, formState.name, props.exact) && shouldRenderFormState(formState, props.formState || _proxyFormState, _setFormState, props.reRenderRoot)) {
|
|
1567
|
+
props.callback({
|
|
1568
|
+
values: {
|
|
1569
|
+
..._formValues
|
|
1570
|
+
},
|
|
1571
|
+
..._formState,
|
|
1572
|
+
...formState,
|
|
1573
|
+
defaultValues: _defaultValues
|
|
1574
|
+
});
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
}).unsubscribe;
|
|
1578
|
+
const subscribe = (props)=>{
|
|
1579
|
+
_state.mount = true;
|
|
1580
|
+
_proxySubscribeFormState = {
|
|
1581
|
+
..._proxySubscribeFormState,
|
|
1582
|
+
...props.formState
|
|
1583
|
+
};
|
|
1584
|
+
return _subscribe({
|
|
1585
|
+
...props,
|
|
1586
|
+
formState: _proxySubscribeFormState
|
|
1587
|
+
});
|
|
1588
|
+
};
|
|
1589
|
+
const unregister = (name, options = {})=>{
|
|
1590
|
+
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount){
|
|
1591
|
+
_names.mount.delete(fieldName);
|
|
1592
|
+
_names.array.delete(fieldName);
|
|
1593
|
+
if (!options.keepValue) {
|
|
1594
|
+
unset(_fields, fieldName);
|
|
1595
|
+
unset(_formValues, fieldName);
|
|
1596
|
+
}
|
|
1597
|
+
!options.keepError && unset(_formState.errors, fieldName);
|
|
1598
|
+
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
|
1599
|
+
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
|
1600
|
+
!options.keepIsValidating && unset(_formState.validatingFields, fieldName);
|
|
1601
|
+
!_options.shouldUnregister && !options.keepDefaultValue && unset(_defaultValues, fieldName);
|
|
1602
|
+
}
|
|
1603
|
+
_subjects.state.next({
|
|
1604
|
+
values: cloneObject(_formValues)
|
|
1605
|
+
});
|
|
1606
|
+
_subjects.state.next({
|
|
1607
|
+
..._formState,
|
|
1608
|
+
...!options.keepDirty ? {} : {
|
|
1609
|
+
isDirty: _getDirty()
|
|
1610
|
+
}
|
|
1611
|
+
});
|
|
1612
|
+
!options.keepIsValid && _setValid();
|
|
1613
|
+
};
|
|
1614
|
+
const _setDisabledField = ({ disabled, name })=>{
|
|
1615
|
+
if (isBoolean(disabled) && _state.mount || !!disabled || _names.disabled.has(name)) {
|
|
1616
|
+
disabled ? _names.disabled.add(name) : _names.disabled.delete(name);
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1619
|
+
const register = (name, options = {})=>{
|
|
1620
|
+
let field = get(_fields, name);
|
|
1621
|
+
const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
|
|
1622
|
+
set(_fields, name, {
|
|
1623
|
+
...field || {},
|
|
1624
|
+
_f: {
|
|
1625
|
+
...field && field._f ? field._f : {
|
|
1626
|
+
ref: {
|
|
1627
|
+
name
|
|
1628
|
+
}
|
|
1629
|
+
},
|
|
1630
|
+
name,
|
|
1631
|
+
mount: true,
|
|
1632
|
+
...options
|
|
1633
|
+
}
|
|
1634
|
+
});
|
|
1635
|
+
_names.mount.add(name);
|
|
1636
|
+
if (field) {
|
|
1637
|
+
_setDisabledField({
|
|
1638
|
+
disabled: isBoolean(options.disabled) ? options.disabled : _options.disabled,
|
|
1639
|
+
name
|
|
1640
|
+
});
|
|
1641
|
+
} else {
|
|
1642
|
+
updateValidAndValue(name, true, options.value);
|
|
1643
|
+
}
|
|
1644
|
+
return {
|
|
1645
|
+
...disabledIsDefined ? {
|
|
1646
|
+
disabled: options.disabled || _options.disabled
|
|
1647
|
+
} : {},
|
|
1648
|
+
..._options.progressive ? {
|
|
1649
|
+
required: !!options.required,
|
|
1650
|
+
min: getRuleValue(options.min),
|
|
1651
|
+
max: getRuleValue(options.max),
|
|
1652
|
+
minLength: getRuleValue(options.minLength),
|
|
1653
|
+
maxLength: getRuleValue(options.maxLength),
|
|
1654
|
+
pattern: getRuleValue(options.pattern)
|
|
1655
|
+
} : {},
|
|
1656
|
+
name,
|
|
1657
|
+
onChange,
|
|
1658
|
+
onBlur: onChange,
|
|
1659
|
+
ref: (ref)=>{
|
|
1660
|
+
if (ref) {
|
|
1661
|
+
register(name, options);
|
|
1662
|
+
field = get(_fields, name);
|
|
1663
|
+
const fieldRef = isUndefined(ref.value) ? ref.querySelectorAll ? ref.querySelectorAll('input,select,textarea')[0] || ref : ref : ref;
|
|
1664
|
+
const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
|
|
1665
|
+
const refs = field._f.refs || [];
|
|
1666
|
+
if (radioOrCheckbox ? refs.find((option)=>option === fieldRef) : fieldRef === field._f.ref) {
|
|
1667
|
+
return;
|
|
1668
|
+
}
|
|
1669
|
+
set(_fields, name, {
|
|
1670
|
+
_f: {
|
|
1671
|
+
...field._f,
|
|
1672
|
+
...radioOrCheckbox ? {
|
|
1673
|
+
refs: [
|
|
1674
|
+
...refs.filter(live),
|
|
1675
|
+
fieldRef,
|
|
1676
|
+
...Array.isArray(get(_defaultValues, name)) ? [
|
|
1677
|
+
{}
|
|
1678
|
+
] : []
|
|
1679
|
+
],
|
|
1680
|
+
ref: {
|
|
1681
|
+
type: fieldRef.type,
|
|
1682
|
+
name
|
|
1683
|
+
}
|
|
1684
|
+
} : {
|
|
1685
|
+
ref: fieldRef
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
});
|
|
1689
|
+
updateValidAndValue(name, false, undefined, fieldRef);
|
|
1690
|
+
} else {
|
|
1691
|
+
field = get(_fields, name, {});
|
|
1692
|
+
if (field._f) {
|
|
1693
|
+
field._f.mount = false;
|
|
1694
|
+
}
|
|
1695
|
+
(_options.shouldUnregister || options.shouldUnregister) && !(isNameInFieldArray(_names.array, name) && _state.action) && _names.unMount.add(name);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
};
|
|
1699
|
+
};
|
|
1700
|
+
const _focusError = ()=>_options.shouldFocusError && iterateFieldsByAction(_fields, _focusInput, _names.mount);
|
|
1701
|
+
const _disableForm = (disabled)=>{
|
|
1702
|
+
if (isBoolean(disabled)) {
|
|
1703
|
+
_subjects.state.next({
|
|
1704
|
+
disabled
|
|
1705
|
+
});
|
|
1706
|
+
iterateFieldsByAction(_fields, (ref, name)=>{
|
|
1707
|
+
const currentField = get(_fields, name);
|
|
1708
|
+
if (currentField) {
|
|
1709
|
+
ref.disabled = currentField._f.disabled || disabled;
|
|
1710
|
+
if (Array.isArray(currentField._f.refs)) {
|
|
1711
|
+
currentField._f.refs.forEach((inputRef)=>{
|
|
1712
|
+
inputRef.disabled = currentField._f.disabled || disabled;
|
|
1713
|
+
});
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
}, 0, false);
|
|
1717
|
+
}
|
|
1718
|
+
};
|
|
1719
|
+
const handleSubmit = (onValid, onInvalid)=>async (e)=>{
|
|
1720
|
+
let onValidError = undefined;
|
|
1721
|
+
if (e) {
|
|
1722
|
+
e.preventDefault && e.preventDefault();
|
|
1723
|
+
e.persist && e.persist();
|
|
1724
|
+
}
|
|
1725
|
+
let fieldValues = cloneObject(_formValues);
|
|
1726
|
+
_subjects.state.next({
|
|
1727
|
+
isSubmitting: true
|
|
1728
|
+
});
|
|
1729
|
+
if (_options.resolver) {
|
|
1730
|
+
const { errors, values } = await _runSchema();
|
|
1731
|
+
_formState.errors = errors;
|
|
1732
|
+
fieldValues = cloneObject(values);
|
|
1733
|
+
} else {
|
|
1734
|
+
await executeBuiltInValidation(_fields);
|
|
1735
|
+
}
|
|
1736
|
+
if (_names.disabled.size) {
|
|
1737
|
+
for (const name of _names.disabled){
|
|
1738
|
+
unset(fieldValues, name);
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
unset(_formState.errors, 'root');
|
|
1742
|
+
if (isEmptyObject(_formState.errors)) {
|
|
1743
|
+
_subjects.state.next({
|
|
1744
|
+
errors: {}
|
|
1745
|
+
});
|
|
1746
|
+
try {
|
|
1747
|
+
await onValid(fieldValues, e);
|
|
1748
|
+
} catch (error) {
|
|
1749
|
+
onValidError = error;
|
|
1750
|
+
}
|
|
1751
|
+
} else {
|
|
1752
|
+
if (onInvalid) {
|
|
1753
|
+
await onInvalid({
|
|
1754
|
+
..._formState.errors
|
|
1755
|
+
}, e);
|
|
1756
|
+
}
|
|
1757
|
+
_focusError();
|
|
1758
|
+
setTimeout(_focusError);
|
|
1759
|
+
}
|
|
1760
|
+
_subjects.state.next({
|
|
1761
|
+
isSubmitted: true,
|
|
1762
|
+
isSubmitting: false,
|
|
1763
|
+
isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
|
|
1764
|
+
submitCount: _formState.submitCount + 1,
|
|
1765
|
+
errors: _formState.errors
|
|
1766
|
+
});
|
|
1767
|
+
if (onValidError) {
|
|
1768
|
+
throw onValidError;
|
|
1769
|
+
}
|
|
1770
|
+
};
|
|
1771
|
+
const resetField = (name, options = {})=>{
|
|
1772
|
+
if (get(_fields, name)) {
|
|
1773
|
+
if (isUndefined(options.defaultValue)) {
|
|
1774
|
+
setValue(name, cloneObject(get(_defaultValues, name)));
|
|
1775
|
+
} else {
|
|
1776
|
+
setValue(name, options.defaultValue);
|
|
1777
|
+
set(_defaultValues, name, cloneObject(options.defaultValue));
|
|
1778
|
+
}
|
|
1779
|
+
if (!options.keepTouched) {
|
|
1780
|
+
unset(_formState.touchedFields, name);
|
|
1781
|
+
}
|
|
1782
|
+
if (!options.keepDirty) {
|
|
1783
|
+
unset(_formState.dirtyFields, name);
|
|
1784
|
+
_formState.isDirty = options.defaultValue ? _getDirty(name, cloneObject(get(_defaultValues, name))) : _getDirty();
|
|
1785
|
+
}
|
|
1786
|
+
if (!options.keepError) {
|
|
1787
|
+
unset(_formState.errors, name);
|
|
1788
|
+
_proxyFormState.isValid && _setValid();
|
|
1789
|
+
}
|
|
1790
|
+
_subjects.state.next({
|
|
1791
|
+
..._formState
|
|
1792
|
+
});
|
|
1793
|
+
}
|
|
1794
|
+
};
|
|
1795
|
+
const _reset = (formValues, keepStateOptions = {})=>{
|
|
1796
|
+
const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
|
|
1797
|
+
const cloneUpdatedValues = cloneObject(updatedValues);
|
|
1798
|
+
const isEmptyResetValues = isEmptyObject(formValues);
|
|
1799
|
+
const values = isEmptyResetValues ? _defaultValues : cloneUpdatedValues;
|
|
1800
|
+
if (!keepStateOptions.keepDefaultValues) {
|
|
1801
|
+
_defaultValues = updatedValues;
|
|
1802
|
+
}
|
|
1803
|
+
if (!keepStateOptions.keepValues) {
|
|
1804
|
+
if (keepStateOptions.keepDirtyValues) {
|
|
1805
|
+
const fieldsToCheck = new Set([
|
|
1806
|
+
..._names.mount,
|
|
1807
|
+
...Object.keys(getDirtyFields(_defaultValues, _formValues))
|
|
1808
|
+
]);
|
|
1809
|
+
for (const fieldName of Array.from(fieldsToCheck)){
|
|
1810
|
+
get(_formState.dirtyFields, fieldName) ? set(values, fieldName, get(_formValues, fieldName)) : setValue(fieldName, get(values, fieldName));
|
|
1811
|
+
}
|
|
1812
|
+
} else {
|
|
1813
|
+
if (isWeb && isUndefined(formValues)) {
|
|
1814
|
+
for (const name of _names.mount){
|
|
1815
|
+
const field = get(_fields, name);
|
|
1816
|
+
if (field && field._f) {
|
|
1817
|
+
const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
|
|
1818
|
+
if (isHTMLElement(fieldReference)) {
|
|
1819
|
+
const form = fieldReference.closest('form');
|
|
1820
|
+
if (form) {
|
|
1821
|
+
form.reset();
|
|
1822
|
+
break;
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
if (keepStateOptions.keepFieldsRef) {
|
|
1829
|
+
for (const fieldName of _names.mount){
|
|
1830
|
+
setValue(fieldName, get(values, fieldName));
|
|
1831
|
+
}
|
|
1832
|
+
} else {
|
|
1833
|
+
_fields = {};
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? cloneObject(_defaultValues) : {} : cloneObject(values);
|
|
1837
|
+
_subjects.array.next({
|
|
1838
|
+
values: {
|
|
1839
|
+
...values
|
|
1840
|
+
}
|
|
1841
|
+
});
|
|
1842
|
+
_subjects.state.next({
|
|
1843
|
+
values: {
|
|
1844
|
+
...values
|
|
1845
|
+
}
|
|
1846
|
+
});
|
|
1847
|
+
}
|
|
1848
|
+
_names = {
|
|
1849
|
+
mount: keepStateOptions.keepDirtyValues ? _names.mount : new Set(),
|
|
1850
|
+
unMount: new Set(),
|
|
1851
|
+
array: new Set(),
|
|
1852
|
+
disabled: new Set(),
|
|
1853
|
+
watch: new Set(),
|
|
1854
|
+
watchAll: false,
|
|
1855
|
+
focus: ''
|
|
1856
|
+
};
|
|
1857
|
+
_state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid || !!keepStateOptions.keepDirtyValues;
|
|
1858
|
+
_state.watch = !!_options.shouldUnregister;
|
|
1859
|
+
_subjects.state.next({
|
|
1860
|
+
submitCount: keepStateOptions.keepSubmitCount ? _formState.submitCount : 0,
|
|
1861
|
+
isDirty: isEmptyResetValues ? false : keepStateOptions.keepDirty ? _formState.isDirty : !!(keepStateOptions.keepDefaultValues && !deepEqual(formValues, _defaultValues)),
|
|
1862
|
+
isSubmitted: keepStateOptions.keepIsSubmitted ? _formState.isSubmitted : false,
|
|
1863
|
+
dirtyFields: isEmptyResetValues ? {} : keepStateOptions.keepDirtyValues ? keepStateOptions.keepDefaultValues && _formValues ? getDirtyFields(_defaultValues, _formValues) : _formState.dirtyFields : keepStateOptions.keepDefaultValues && formValues ? getDirtyFields(_defaultValues, formValues) : keepStateOptions.keepDirty ? _formState.dirtyFields : {},
|
|
1864
|
+
touchedFields: keepStateOptions.keepTouched ? _formState.touchedFields : {},
|
|
1865
|
+
errors: keepStateOptions.keepErrors ? _formState.errors : {},
|
|
1866
|
+
isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful ? _formState.isSubmitSuccessful : false,
|
|
1867
|
+
isSubmitting: false,
|
|
1868
|
+
defaultValues: _defaultValues
|
|
1869
|
+
});
|
|
1870
|
+
};
|
|
1871
|
+
const reset = (formValues, keepStateOptions)=>_reset(isFunction(formValues) ? formValues(_formValues) : formValues, keepStateOptions);
|
|
1872
|
+
const setFocus = (name, options = {})=>{
|
|
1873
|
+
const field = get(_fields, name);
|
|
1874
|
+
const fieldReference = field && field._f;
|
|
1875
|
+
if (fieldReference) {
|
|
1876
|
+
const fieldRef = fieldReference.refs ? fieldReference.refs[0] : fieldReference.ref;
|
|
1877
|
+
if (fieldRef.focus) {
|
|
1878
|
+
fieldRef.focus();
|
|
1879
|
+
options.shouldSelect && isFunction(fieldRef.select) && fieldRef.select();
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
};
|
|
1883
|
+
const _setFormState = (updatedFormState)=>{
|
|
1884
|
+
_formState = {
|
|
1885
|
+
..._formState,
|
|
1886
|
+
...updatedFormState
|
|
1887
|
+
};
|
|
1888
|
+
};
|
|
1889
|
+
const _resetDefaultValues = ()=>isFunction(_options.defaultValues) && _options.defaultValues().then((values)=>{
|
|
1890
|
+
reset(values, _options.resetOptions);
|
|
1891
|
+
_subjects.state.next({
|
|
1892
|
+
isLoading: false
|
|
1893
|
+
});
|
|
1894
|
+
});
|
|
1895
|
+
const methods = {
|
|
1896
|
+
control: {
|
|
1897
|
+
register,
|
|
1898
|
+
unregister,
|
|
1899
|
+
getFieldState,
|
|
1900
|
+
handleSubmit,
|
|
1901
|
+
setError,
|
|
1902
|
+
_subscribe,
|
|
1903
|
+
_runSchema,
|
|
1904
|
+
_focusError,
|
|
1905
|
+
_getWatch,
|
|
1906
|
+
_getDirty,
|
|
1907
|
+
_setValid,
|
|
1908
|
+
_setFieldArray,
|
|
1909
|
+
_setDisabledField,
|
|
1910
|
+
_setErrors,
|
|
1911
|
+
_getFieldArray,
|
|
1912
|
+
_reset,
|
|
1913
|
+
_resetDefaultValues,
|
|
1914
|
+
_removeUnmounted,
|
|
1915
|
+
_disableForm,
|
|
1916
|
+
_subjects,
|
|
1917
|
+
_proxyFormState,
|
|
1918
|
+
get _fields () {
|
|
1919
|
+
return _fields;
|
|
1920
|
+
},
|
|
1921
|
+
get _formValues () {
|
|
1922
|
+
return _formValues;
|
|
1923
|
+
},
|
|
1924
|
+
get _state () {
|
|
1925
|
+
return _state;
|
|
1926
|
+
},
|
|
1927
|
+
set _state (value){
|
|
1928
|
+
_state = value;
|
|
1929
|
+
},
|
|
1930
|
+
get _defaultValues () {
|
|
1931
|
+
return _defaultValues;
|
|
1932
|
+
},
|
|
1933
|
+
get _names () {
|
|
1934
|
+
return _names;
|
|
1935
|
+
},
|
|
1936
|
+
set _names (value){
|
|
1937
|
+
_names = value;
|
|
1938
|
+
},
|
|
1939
|
+
get _formState () {
|
|
1940
|
+
return _formState;
|
|
1941
|
+
},
|
|
1942
|
+
get _options () {
|
|
1943
|
+
return _options;
|
|
1944
|
+
},
|
|
1945
|
+
set _options (value){
|
|
1946
|
+
_options = {
|
|
1947
|
+
..._options,
|
|
1948
|
+
...value
|
|
1949
|
+
};
|
|
1950
|
+
}
|
|
1951
|
+
},
|
|
1952
|
+
subscribe,
|
|
1953
|
+
trigger,
|
|
1954
|
+
register,
|
|
1955
|
+
handleSubmit,
|
|
1956
|
+
watch,
|
|
1957
|
+
setValue,
|
|
1958
|
+
getValues,
|
|
1959
|
+
reset,
|
|
1960
|
+
resetField,
|
|
1961
|
+
clearErrors,
|
|
1962
|
+
unregister,
|
|
1963
|
+
setError,
|
|
1964
|
+
setFocus,
|
|
1965
|
+
getFieldState
|
|
1966
|
+
};
|
|
1967
|
+
return {
|
|
1968
|
+
...methods,
|
|
1969
|
+
formControl: methods
|
|
1970
|
+
};
|
|
1971
|
+
}
|
|
1972
|
+
/**
|
|
1973
|
+
* Custom hook to manage the entire form.
|
|
1974
|
+
*
|
|
1975
|
+
* @remarks
|
|
1976
|
+
* [API](https://react-hook-form.com/docs/useform) • [Demo](https://codesandbox.io/s/react-hook-form-get-started-ts-5ksmm) • [Video](https://www.youtube.com/watch?v=RkXv4AXXC_4)
|
|
1977
|
+
*
|
|
1978
|
+
* @param props - form configuration and validation parameters.
|
|
1979
|
+
*
|
|
1980
|
+
* @returns methods - individual functions to manage the form state. {@link UseFormReturn}
|
|
1981
|
+
*
|
|
1982
|
+
* @example
|
|
1983
|
+
* ```tsx
|
|
1984
|
+
* function App() {
|
|
1985
|
+
* const { register, handleSubmit, watch, formState: { errors } } = useForm();
|
|
1986
|
+
* const onSubmit = data => console.log(data);
|
|
1987
|
+
*
|
|
1988
|
+
* console.log(watch("example"));
|
|
1989
|
+
*
|
|
1990
|
+
* return (
|
|
1991
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
|
1992
|
+
* <input defaultValue="test" {...register("example")} />
|
|
1993
|
+
* <input {...register("exampleRequired", { required: true })} />
|
|
1994
|
+
* {errors.exampleRequired && <span>This field is required</span>}
|
|
1995
|
+
* <button>Submit</button>
|
|
1996
|
+
* </form>
|
|
1997
|
+
* );
|
|
1998
|
+
* }
|
|
1999
|
+
* ```
|
|
2000
|
+
*/ function useForm(props = {}) {
|
|
2001
|
+
const _formControl = React__default.useRef(undefined);
|
|
2002
|
+
const _values = React__default.useRef(undefined);
|
|
2003
|
+
const [formState, updateFormState] = React__default.useState({
|
|
2004
|
+
isDirty: false,
|
|
2005
|
+
isValidating: false,
|
|
2006
|
+
isLoading: isFunction(props.defaultValues),
|
|
2007
|
+
isSubmitted: false,
|
|
2008
|
+
isSubmitting: false,
|
|
2009
|
+
isSubmitSuccessful: false,
|
|
2010
|
+
isValid: false,
|
|
2011
|
+
submitCount: 0,
|
|
2012
|
+
dirtyFields: {},
|
|
2013
|
+
touchedFields: {},
|
|
2014
|
+
validatingFields: {},
|
|
2015
|
+
errors: props.errors || {},
|
|
2016
|
+
disabled: props.disabled || false,
|
|
2017
|
+
isReady: false,
|
|
2018
|
+
defaultValues: isFunction(props.defaultValues) ? undefined : props.defaultValues
|
|
2019
|
+
});
|
|
2020
|
+
if (!_formControl.current) {
|
|
2021
|
+
if (props.formControl) {
|
|
2022
|
+
_formControl.current = {
|
|
2023
|
+
...props.formControl,
|
|
2024
|
+
formState
|
|
2025
|
+
};
|
|
2026
|
+
if (props.defaultValues && !isFunction(props.defaultValues)) {
|
|
2027
|
+
props.formControl.reset(props.defaultValues, props.resetOptions);
|
|
2028
|
+
}
|
|
2029
|
+
} else {
|
|
2030
|
+
const { formControl, ...rest } = createFormControl(props);
|
|
2031
|
+
_formControl.current = {
|
|
2032
|
+
...rest,
|
|
2033
|
+
formState
|
|
2034
|
+
};
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
const control = _formControl.current.control;
|
|
2038
|
+
control._options = props;
|
|
2039
|
+
useIsomorphicLayoutEffect(()=>{
|
|
2040
|
+
const sub = control._subscribe({
|
|
2041
|
+
formState: control._proxyFormState,
|
|
2042
|
+
callback: ()=>updateFormState({
|
|
2043
|
+
...control._formState
|
|
2044
|
+
}),
|
|
2045
|
+
reRenderRoot: true
|
|
2046
|
+
});
|
|
2047
|
+
updateFormState((data)=>({
|
|
2048
|
+
...data,
|
|
2049
|
+
isReady: true
|
|
2050
|
+
}));
|
|
2051
|
+
control._formState.isReady = true;
|
|
2052
|
+
return sub;
|
|
2053
|
+
}, [
|
|
2054
|
+
control
|
|
2055
|
+
]);
|
|
2056
|
+
React__default.useEffect(()=>control._disableForm(props.disabled), [
|
|
2057
|
+
control,
|
|
2058
|
+
props.disabled
|
|
2059
|
+
]);
|
|
2060
|
+
React__default.useEffect(()=>{
|
|
2061
|
+
if (props.mode) {
|
|
2062
|
+
control._options.mode = props.mode;
|
|
2063
|
+
}
|
|
2064
|
+
if (props.reValidateMode) {
|
|
2065
|
+
control._options.reValidateMode = props.reValidateMode;
|
|
2066
|
+
}
|
|
2067
|
+
}, [
|
|
2068
|
+
control,
|
|
2069
|
+
props.mode,
|
|
2070
|
+
props.reValidateMode
|
|
2071
|
+
]);
|
|
2072
|
+
React__default.useEffect(()=>{
|
|
2073
|
+
if (props.errors) {
|
|
2074
|
+
control._setErrors(props.errors);
|
|
2075
|
+
control._focusError();
|
|
2076
|
+
}
|
|
2077
|
+
}, [
|
|
2078
|
+
control,
|
|
2079
|
+
props.errors
|
|
2080
|
+
]);
|
|
2081
|
+
React__default.useEffect(()=>{
|
|
2082
|
+
props.shouldUnregister && control._subjects.state.next({
|
|
2083
|
+
values: control._getWatch()
|
|
2084
|
+
});
|
|
2085
|
+
}, [
|
|
2086
|
+
control,
|
|
2087
|
+
props.shouldUnregister
|
|
2088
|
+
]);
|
|
2089
|
+
React__default.useEffect(()=>{
|
|
2090
|
+
if (control._proxyFormState.isDirty) {
|
|
2091
|
+
const isDirty = control._getDirty();
|
|
2092
|
+
if (isDirty !== formState.isDirty) {
|
|
2093
|
+
control._subjects.state.next({
|
|
2094
|
+
isDirty
|
|
2095
|
+
});
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
}, [
|
|
2099
|
+
control,
|
|
2100
|
+
formState.isDirty
|
|
2101
|
+
]);
|
|
2102
|
+
React__default.useEffect(()=>{
|
|
2103
|
+
if (props.values && !deepEqual(props.values, _values.current)) {
|
|
2104
|
+
control._reset(props.values, {
|
|
2105
|
+
keepFieldsRef: true,
|
|
2106
|
+
...control._options.resetOptions
|
|
2107
|
+
});
|
|
2108
|
+
_values.current = props.values;
|
|
2109
|
+
updateFormState((state)=>({
|
|
2110
|
+
...state
|
|
2111
|
+
}));
|
|
2112
|
+
} else {
|
|
2113
|
+
control._resetDefaultValues();
|
|
2114
|
+
}
|
|
2115
|
+
}, [
|
|
2116
|
+
control,
|
|
2117
|
+
props.values
|
|
2118
|
+
]);
|
|
2119
|
+
React__default.useEffect(()=>{
|
|
2120
|
+
if (!control._state.mount) {
|
|
2121
|
+
control._setValid();
|
|
2122
|
+
control._state.mount = true;
|
|
2123
|
+
}
|
|
2124
|
+
if (control._state.watch) {
|
|
2125
|
+
control._state.watch = false;
|
|
2126
|
+
control._subjects.state.next({
|
|
2127
|
+
...control._formState
|
|
2128
|
+
});
|
|
2129
|
+
}
|
|
2130
|
+
control._removeUnmounted();
|
|
2131
|
+
});
|
|
2132
|
+
_formControl.current.formState = getProxyFormState(formState, control);
|
|
2133
|
+
return _formControl.current;
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
const Form = FormProvider;
|
|
2137
|
+
const FormFieldContext = /*#__PURE__*/ React.createContext({});
|
|
2138
|
+
const FormField = ({ ...props })=>{
|
|
2139
|
+
return /*#__PURE__*/ React.createElement(FormFieldContext.Provider, {
|
|
2140
|
+
value: {
|
|
2141
|
+
name: props.name
|
|
2142
|
+
}
|
|
2143
|
+
}, /*#__PURE__*/ React.createElement(Controller, props));
|
|
2144
|
+
};
|
|
2145
|
+
const useFormField = ()=>{
|
|
2146
|
+
const fieldContext = React.useContext(FormFieldContext);
|
|
2147
|
+
const itemContext = React.useContext(FormItemContext);
|
|
2148
|
+
const { getFieldState } = useFormContext();
|
|
2149
|
+
const formState = useFormState({
|
|
2150
|
+
name: fieldContext.name
|
|
2151
|
+
});
|
|
2152
|
+
const fieldState = getFieldState(fieldContext.name, formState);
|
|
2153
|
+
if (!fieldContext) {
|
|
2154
|
+
throw new Error("useFormField should be used within <FormField>");
|
|
2155
|
+
}
|
|
2156
|
+
const { id } = itemContext;
|
|
2157
|
+
return {
|
|
2158
|
+
id,
|
|
2159
|
+
name: fieldContext.name,
|
|
2160
|
+
formItemId: `${id}-form-item`,
|
|
2161
|
+
formDescriptionId: `${id}-form-item-description`,
|
|
2162
|
+
formMessageId: `${id}-form-item-message`,
|
|
2163
|
+
...fieldState
|
|
2164
|
+
};
|
|
2165
|
+
};
|
|
2166
|
+
const FormItemContext = /*#__PURE__*/ React.createContext({});
|
|
2167
|
+
function FormItem({ className, ...props }) {
|
|
2168
|
+
const id = React.useId();
|
|
2169
|
+
return /*#__PURE__*/ React.createElement(FormItemContext.Provider, {
|
|
2170
|
+
value: {
|
|
2171
|
+
id
|
|
2172
|
+
}
|
|
2173
|
+
}, /*#__PURE__*/ React.createElement("div", {
|
|
2174
|
+
"data-slot": "form-item",
|
|
2175
|
+
className: cn("grid gap-2", className),
|
|
2176
|
+
...props
|
|
2177
|
+
}));
|
|
2178
|
+
}
|
|
2179
|
+
function FormLabel({ className, ...props }) {
|
|
2180
|
+
const { error, formItemId } = useFormField();
|
|
2181
|
+
return /*#__PURE__*/ React.createElement(Label, {
|
|
2182
|
+
"data-slot": "form-label",
|
|
2183
|
+
"data-error": !!error,
|
|
2184
|
+
className: cn("data-[error=true]:text-destructive", className),
|
|
2185
|
+
htmlFor: formItemId,
|
|
2186
|
+
...props
|
|
2187
|
+
});
|
|
2188
|
+
}
|
|
2189
|
+
function FormControl({ ...props }) {
|
|
2190
|
+
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
|
|
2191
|
+
return /*#__PURE__*/ React.createElement(Slot, {
|
|
2192
|
+
"data-slot": "form-control",
|
|
2193
|
+
id: formItemId,
|
|
2194
|
+
"aria-describedby": !error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`,
|
|
2195
|
+
"aria-invalid": !!error,
|
|
2196
|
+
...props
|
|
2197
|
+
});
|
|
2198
|
+
}
|
|
2199
|
+
function FormDescription({ className, ...props }) {
|
|
2200
|
+
const { formDescriptionId } = useFormField();
|
|
2201
|
+
return /*#__PURE__*/ React.createElement("p", {
|
|
2202
|
+
"data-slot": "form-description",
|
|
2203
|
+
id: formDescriptionId,
|
|
2204
|
+
className: cn("text-muted-foreground text-sm", className),
|
|
2205
|
+
...props
|
|
2206
|
+
});
|
|
2207
|
+
}
|
|
2208
|
+
function FormMessage({ className, ...props }) {
|
|
2209
|
+
const { error, formMessageId } = useFormField();
|
|
2210
|
+
const body = error ? String(error?.message ?? "") : props.children;
|
|
2211
|
+
if (!body) {
|
|
2212
|
+
return null;
|
|
2213
|
+
}
|
|
2214
|
+
return /*#__PURE__*/ React.createElement("p", {
|
|
2215
|
+
"data-slot": "form-message",
|
|
2216
|
+
id: formMessageId,
|
|
2217
|
+
className: cn("text-destructive text-sm", className),
|
|
2218
|
+
...props
|
|
2219
|
+
}, body);
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
export { Form as F, FormItem as a, FormLabel as b, FormControl as c, FormDescription as d, FormMessage as e, FormField as f, get as g, appendErrors as h, useForm as i, set as s, useFormField as u };
|
|
2223
|
+
//# sourceMappingURL=form-CLuajH9A.mjs.map
|