@underverse-ui/underverse 0.2.27 → 0.2.28

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/index.cjs CHANGED
@@ -1670,53 +1670,9 @@ var Switch_default = Switch;
1670
1670
 
1671
1671
  // ../../components/ui/label.tsx
1672
1672
  var React7 = __toESM(require("react"), 1);
1673
-
1674
- // ../../node_modules/class-variance-authority/dist/index.mjs
1675
- var import_clsx2 = require("clsx");
1676
- var falsyToString = (value) => typeof value === "boolean" ? `${value}` : value === 0 ? "0" : value;
1677
- var cx = import_clsx2.clsx;
1678
- var cva = (base, config) => (props) => {
1679
- var _config_compoundVariants;
1680
- if ((config === null || config === void 0 ? void 0 : config.variants) == null) return cx(base, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
1681
- const { variants, defaultVariants } = config;
1682
- const getVariantClassNames = Object.keys(variants).map((variant) => {
1683
- const variantProp = props === null || props === void 0 ? void 0 : props[variant];
1684
- const defaultVariantProp = defaultVariants === null || defaultVariants === void 0 ? void 0 : defaultVariants[variant];
1685
- if (variantProp === null) return null;
1686
- const variantKey = falsyToString(variantProp) || falsyToString(defaultVariantProp);
1687
- return variants[variant][variantKey];
1688
- });
1689
- const propsWithoutUndefined = props && Object.entries(props).reduce((acc, param) => {
1690
- let [key, value] = param;
1691
- if (value === void 0) {
1692
- return acc;
1693
- }
1694
- acc[key] = value;
1695
- return acc;
1696
- }, {});
1697
- const getCompoundVariantClassNames = config === null || config === void 0 ? void 0 : (_config_compoundVariants = config.compoundVariants) === null || _config_compoundVariants === void 0 ? void 0 : _config_compoundVariants.reduce((acc, param) => {
1698
- let { class: cvClass, className: cvClassName, ...compoundVariantOptions } = param;
1699
- return Object.entries(compoundVariantOptions).every((param2) => {
1700
- let [key, value] = param2;
1701
- return Array.isArray(value) ? value.includes({
1702
- ...defaultVariants,
1703
- ...propsWithoutUndefined
1704
- }[key]) : {
1705
- ...defaultVariants,
1706
- ...propsWithoutUndefined
1707
- }[key] === value;
1708
- }) ? [
1709
- ...acc,
1710
- cvClass,
1711
- cvClassName
1712
- ] : acc;
1713
- }, []);
1714
- return cx(base, getVariantClassNames, getCompoundVariantClassNames, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
1715
- };
1716
-
1717
- // ../../components/ui/label.tsx
1673
+ var import_class_variance_authority = require("class-variance-authority");
1718
1674
  var import_jsx_runtime9 = require("react/jsx-runtime");
1719
- var labelVariants = cva(
1675
+ var labelVariants = (0, import_class_variance_authority.cva)(
1720
1676
  "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
1721
1677
  );
1722
1678
  var Label = React7.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
@@ -10764,1835 +10720,11 @@ function DataTable({
10764
10720
  var DataTable_default = DataTable;
10765
10721
 
10766
10722
  // ../../components/ui/Form.tsx
10767
- var React40 = __toESM(require("react"), 1);
10768
-
10769
- // ../../node_modules/react-hook-form/dist/index.esm.mjs
10770
- var import_react25 = __toESM(require("react"), 1);
10771
- var isCheckBoxInput = (element) => element.type === "checkbox";
10772
- var isDateObject = (value) => value instanceof Date;
10773
- var isNullOrUndefined = (value) => value == null;
10774
- var isObjectType = (value) => typeof value === "object";
10775
- var isObject = (value) => !isNullOrUndefined(value) && !Array.isArray(value) && isObjectType(value) && !isDateObject(value);
10776
- var getEventValue = (event) => isObject(event) && event.target ? isCheckBoxInput(event.target) ? event.target.checked : event.target.value : event;
10777
- var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
10778
- var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
10779
- var isPlainObject = (tempObject) => {
10780
- const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
10781
- return isObject(prototypeCopy) && prototypeCopy.hasOwnProperty("isPrototypeOf");
10782
- };
10783
- var isWeb = typeof window !== "undefined" && typeof window.HTMLElement !== "undefined" && typeof document !== "undefined";
10784
- function cloneObject(data) {
10785
- if (data instanceof Date) {
10786
- return new Date(data);
10787
- }
10788
- const isFileListInstance = typeof FileList !== "undefined" && data instanceof FileList;
10789
- if (isWeb && (data instanceof Blob || isFileListInstance)) {
10790
- return data;
10791
- }
10792
- const isArray = Array.isArray(data);
10793
- if (!isArray && !(isObject(data) && isPlainObject(data))) {
10794
- return data;
10795
- }
10796
- const copy = isArray ? [] : Object.create(Object.getPrototypeOf(data));
10797
- for (const key in data) {
10798
- if (Object.prototype.hasOwnProperty.call(data, key)) {
10799
- copy[key] = cloneObject(data[key]);
10800
- }
10801
- }
10802
- return copy;
10803
- }
10804
- var isKey = (value) => /^\w*$/.test(value);
10805
- var isUndefined = (val) => val === void 0;
10806
- var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
10807
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, "").split(/\.|\[/));
10808
- var get = (object, path, defaultValue) => {
10809
- if (!path || !isObject(object)) {
10810
- return defaultValue;
10811
- }
10812
- const result = (isKey(path) ? [path] : stringToPath(path)).reduce((result2, key) => isNullOrUndefined(result2) ? result2 : result2[key], object);
10813
- return isUndefined(result) || result === object ? isUndefined(object[path]) ? defaultValue : object[path] : result;
10814
- };
10815
- var isBoolean = (value) => typeof value === "boolean";
10816
- var isFunction = (value) => typeof value === "function";
10817
- var set = (object, path, value) => {
10818
- let index = -1;
10819
- const tempPath = isKey(path) ? [path] : stringToPath(path);
10820
- const length = tempPath.length;
10821
- const lastIndex = length - 1;
10822
- while (++index < length) {
10823
- const key = tempPath[index];
10824
- let newValue = value;
10825
- if (index !== lastIndex) {
10826
- const objValue = object[key];
10827
- newValue = isObject(objValue) || Array.isArray(objValue) ? objValue : !isNaN(+tempPath[index + 1]) ? [] : {};
10828
- }
10829
- if (key === "__proto__" || key === "constructor" || key === "prototype") {
10830
- return;
10831
- }
10832
- object[key] = newValue;
10833
- object = object[key];
10834
- }
10835
- };
10836
- var EVENTS = {
10837
- BLUR: "blur",
10838
- FOCUS_OUT: "focusout",
10839
- CHANGE: "change"
10840
- };
10841
- var VALIDATION_MODE = {
10842
- onBlur: "onBlur",
10843
- onChange: "onChange",
10844
- onSubmit: "onSubmit",
10845
- onTouched: "onTouched",
10846
- all: "all"
10847
- };
10848
- var INPUT_VALIDATION_RULES = {
10849
- max: "max",
10850
- min: "min",
10851
- maxLength: "maxLength",
10852
- minLength: "minLength",
10853
- pattern: "pattern",
10854
- required: "required",
10855
- validate: "validate"
10856
- };
10857
- var HookFormContext = import_react25.default.createContext(null);
10858
- HookFormContext.displayName = "HookFormContext";
10859
- var useFormContext = () => import_react25.default.useContext(HookFormContext);
10860
- var FormProvider = (props) => {
10861
- const { children, ...data } = props;
10862
- return import_react25.default.createElement(HookFormContext.Provider, { value: data }, children);
10863
- };
10864
- var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
10865
- const result = {
10866
- defaultValues: control._defaultValues
10867
- };
10868
- for (const key in formState) {
10869
- Object.defineProperty(result, key, {
10870
- get: () => {
10871
- const _key = key;
10872
- if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
10873
- control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
10874
- }
10875
- localProxyFormState && (localProxyFormState[_key] = true);
10876
- return formState[_key];
10877
- }
10878
- });
10879
- }
10880
- return result;
10881
- };
10882
- var useIsomorphicLayoutEffect = typeof window !== "undefined" ? import_react25.default.useLayoutEffect : import_react25.default.useEffect;
10883
- function useFormState(props) {
10884
- const methods = useFormContext();
10885
- const { control = methods.control, disabled, name, exact } = props || {};
10886
- const [formState, updateFormState] = import_react25.default.useState(control._formState);
10887
- const _localProxyFormState = import_react25.default.useRef({
10888
- isDirty: false,
10889
- isLoading: false,
10890
- dirtyFields: false,
10891
- touchedFields: false,
10892
- validatingFields: false,
10893
- isValidating: false,
10894
- isValid: false,
10895
- errors: false
10896
- });
10897
- useIsomorphicLayoutEffect(() => control._subscribe({
10898
- name,
10899
- formState: _localProxyFormState.current,
10900
- exact,
10901
- callback: (formState2) => {
10902
- !disabled && updateFormState({
10903
- ...control._formState,
10904
- ...formState2
10905
- });
10906
- }
10907
- }), [name, disabled, exact]);
10908
- import_react25.default.useEffect(() => {
10909
- _localProxyFormState.current.isValid && control._setValid(true);
10910
- }, [control]);
10911
- return import_react25.default.useMemo(() => getProxyFormState(formState, control, _localProxyFormState.current, false), [formState, control]);
10912
- }
10913
- var isString = (value) => typeof value === "string";
10914
- var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
10915
- if (isString(names)) {
10916
- isGlobal && _names.watch.add(names);
10917
- return get(formValues, names, defaultValue);
10918
- }
10919
- if (Array.isArray(names)) {
10920
- return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
10921
- }
10922
- isGlobal && (_names.watchAll = true);
10923
- return formValues;
10924
- };
10925
- var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
10926
- function deepEqual(object1, object2, _internal_visited = /* @__PURE__ */ new WeakSet()) {
10927
- if (isPrimitive(object1) || isPrimitive(object2)) {
10928
- return Object.is(object1, object2);
10929
- }
10930
- if (isDateObject(object1) && isDateObject(object2)) {
10931
- return object1.getTime() === object2.getTime();
10932
- }
10933
- const keys1 = Object.keys(object1);
10934
- const keys2 = Object.keys(object2);
10935
- if (keys1.length !== keys2.length) {
10936
- return false;
10937
- }
10938
- if (_internal_visited.has(object1) || _internal_visited.has(object2)) {
10939
- return true;
10940
- }
10941
- _internal_visited.add(object1);
10942
- _internal_visited.add(object2);
10943
- for (const key of keys1) {
10944
- const val1 = object1[key];
10945
- if (!keys2.includes(key)) {
10946
- return false;
10947
- }
10948
- if (key !== "ref") {
10949
- const val2 = object2[key];
10950
- if (isDateObject(val1) && isDateObject(val2) || isObject(val1) && isObject(val2) || Array.isArray(val1) && Array.isArray(val2) ? !deepEqual(val1, val2, _internal_visited) : !Object.is(val1, val2)) {
10951
- return false;
10952
- }
10953
- }
10954
- }
10955
- return true;
10956
- }
10957
- function useWatch(props) {
10958
- const methods = useFormContext();
10959
- const { control = methods.control, name, defaultValue, disabled, exact, compute } = props || {};
10960
- const _defaultValue = import_react25.default.useRef(defaultValue);
10961
- const _compute = import_react25.default.useRef(compute);
10962
- const _computeFormValues = import_react25.default.useRef(void 0);
10963
- const _prevControl = import_react25.default.useRef(control);
10964
- const _prevName = import_react25.default.useRef(name);
10965
- _compute.current = compute;
10966
- const [value, updateValue] = import_react25.default.useState(() => {
10967
- const defaultValue2 = control._getWatch(name, _defaultValue.current);
10968
- return _compute.current ? _compute.current(defaultValue2) : defaultValue2;
10969
- });
10970
- const getCurrentOutput = import_react25.default.useCallback((values) => {
10971
- const formValues = generateWatchOutput(name, control._names, values || control._formValues, false, _defaultValue.current);
10972
- return _compute.current ? _compute.current(formValues) : formValues;
10973
- }, [control._formValues, control._names, name]);
10974
- const refreshValue = import_react25.default.useCallback((values) => {
10975
- if (!disabled) {
10976
- const formValues = generateWatchOutput(name, control._names, values || control._formValues, false, _defaultValue.current);
10977
- if (_compute.current) {
10978
- const computedFormValues = _compute.current(formValues);
10979
- if (!deepEqual(computedFormValues, _computeFormValues.current)) {
10980
- updateValue(computedFormValues);
10981
- _computeFormValues.current = computedFormValues;
10982
- }
10983
- } else {
10984
- updateValue(formValues);
10985
- }
10986
- }
10987
- }, [control._formValues, control._names, disabled, name]);
10988
- useIsomorphicLayoutEffect(() => {
10989
- if (_prevControl.current !== control || !deepEqual(_prevName.current, name)) {
10990
- _prevControl.current = control;
10991
- _prevName.current = name;
10992
- refreshValue();
10993
- }
10994
- return control._subscribe({
10995
- name,
10996
- formState: {
10997
- values: true
10998
- },
10999
- exact,
11000
- callback: (formState) => {
11001
- refreshValue(formState.values);
11002
- }
11003
- });
11004
- }, [control, exact, name, refreshValue]);
11005
- import_react25.default.useEffect(() => control._removeUnmounted());
11006
- const controlChanged = _prevControl.current !== control;
11007
- const prevName = _prevName.current;
11008
- const computedOutput = import_react25.default.useMemo(() => {
11009
- if (disabled) {
11010
- return null;
11011
- }
11012
- const nameChanged = !controlChanged && !deepEqual(prevName, name);
11013
- const shouldReturnImmediate = controlChanged || nameChanged;
11014
- return shouldReturnImmediate ? getCurrentOutput() : null;
11015
- }, [disabled, controlChanged, name, prevName, getCurrentOutput]);
11016
- return computedOutput !== null ? computedOutput : value;
11017
- }
11018
- function useController(props) {
11019
- const methods = useFormContext();
11020
- const { name, disabled, control = methods.control, shouldUnregister, defaultValue, exact = true } = props;
11021
- const isArrayField = isNameInFieldArray(control._names.array, name);
11022
- const defaultValueMemo = import_react25.default.useMemo(() => get(control._formValues, name, get(control._defaultValues, name, defaultValue)), [control, name, defaultValue]);
11023
- const value = useWatch({
11024
- control,
11025
- name,
11026
- defaultValue: defaultValueMemo,
11027
- exact
11028
- });
11029
- const formState = useFormState({
11030
- control,
11031
- name,
11032
- exact
11033
- });
11034
- const _props = import_react25.default.useRef(props);
11035
- const _previousNameRef = import_react25.default.useRef(void 0);
11036
- const _registerProps = import_react25.default.useRef(control.register(name, {
11037
- ...props.rules,
11038
- value,
11039
- ...isBoolean(props.disabled) ? { disabled: props.disabled } : {}
11040
- }));
11041
- _props.current = props;
11042
- const fieldState = import_react25.default.useMemo(() => Object.defineProperties({}, {
11043
- invalid: {
11044
- enumerable: true,
11045
- get: () => !!get(formState.errors, name)
11046
- },
11047
- isDirty: {
11048
- enumerable: true,
11049
- get: () => !!get(formState.dirtyFields, name)
11050
- },
11051
- isTouched: {
11052
- enumerable: true,
11053
- get: () => !!get(formState.touchedFields, name)
11054
- },
11055
- isValidating: {
11056
- enumerable: true,
11057
- get: () => !!get(formState.validatingFields, name)
11058
- },
11059
- error: {
11060
- enumerable: true,
11061
- get: () => get(formState.errors, name)
11062
- }
11063
- }), [formState, name]);
11064
- const onChange = import_react25.default.useCallback((event) => _registerProps.current.onChange({
11065
- target: {
11066
- value: getEventValue(event),
11067
- name
11068
- },
11069
- type: EVENTS.CHANGE
11070
- }), [name]);
11071
- const onBlur = import_react25.default.useCallback(() => _registerProps.current.onBlur({
11072
- target: {
11073
- value: get(control._formValues, name),
11074
- name
11075
- },
11076
- type: EVENTS.BLUR
11077
- }), [name, control._formValues]);
11078
- const ref = import_react25.default.useCallback((elm) => {
11079
- const field2 = get(control._fields, name);
11080
- if (field2 && field2._f && elm) {
11081
- field2._f.ref = {
11082
- focus: () => isFunction(elm.focus) && elm.focus(),
11083
- select: () => isFunction(elm.select) && elm.select(),
11084
- setCustomValidity: (message) => isFunction(elm.setCustomValidity) && elm.setCustomValidity(message),
11085
- reportValidity: () => isFunction(elm.reportValidity) && elm.reportValidity()
11086
- };
11087
- }
11088
- }, [control._fields, name]);
11089
- const field = import_react25.default.useMemo(() => ({
11090
- name,
11091
- value,
11092
- ...isBoolean(disabled) || formState.disabled ? { disabled: formState.disabled || disabled } : {},
11093
- onChange,
11094
- onBlur,
11095
- ref
11096
- }), [name, disabled, formState.disabled, onChange, onBlur, ref, value]);
11097
- import_react25.default.useEffect(() => {
11098
- const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
11099
- const previousName = _previousNameRef.current;
11100
- if (previousName && previousName !== name && !isArrayField) {
11101
- control.unregister(previousName);
11102
- }
11103
- control.register(name, {
11104
- ..._props.current.rules,
11105
- ...isBoolean(_props.current.disabled) ? { disabled: _props.current.disabled } : {}
11106
- });
11107
- const updateMounted = (name2, value2) => {
11108
- const field2 = get(control._fields, name2);
11109
- if (field2 && field2._f) {
11110
- field2._f.mount = value2;
11111
- }
11112
- };
11113
- updateMounted(name, true);
11114
- if (_shouldUnregisterField) {
11115
- const value2 = cloneObject(get(control._options.defaultValues, name, _props.current.defaultValue));
11116
- set(control._defaultValues, name, value2);
11117
- if (isUndefined(get(control._formValues, name))) {
11118
- set(control._formValues, name, value2);
11119
- }
11120
- }
11121
- !isArrayField && control.register(name);
11122
- _previousNameRef.current = name;
11123
- return () => {
11124
- (isArrayField ? _shouldUnregisterField && !control._state.action : _shouldUnregisterField) ? control.unregister(name) : updateMounted(name, false);
11125
- };
11126
- }, [name, control, isArrayField, shouldUnregister]);
11127
- import_react25.default.useEffect(() => {
11128
- control._setDisabledField({
11129
- disabled,
11130
- name
11131
- });
11132
- }, [disabled, name, control]);
11133
- return import_react25.default.useMemo(() => ({
11134
- field,
11135
- formState,
11136
- fieldState
11137
- }), [field, formState, fieldState]);
11138
- }
11139
- var Controller = (props) => props.render(useController(props));
11140
- var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria ? {
11141
- ...errors[name],
11142
- types: {
11143
- ...errors[name] && errors[name].types ? errors[name].types : {},
11144
- [type]: message || true
11145
- }
11146
- } : {};
11147
- var convertToArrayPayload = (value) => Array.isArray(value) ? value : [value];
11148
- var createSubject = () => {
11149
- let _observers = [];
11150
- const next = (value) => {
11151
- for (const observer of _observers) {
11152
- observer.next && observer.next(value);
11153
- }
11154
- };
11155
- const subscribe = (observer) => {
11156
- _observers.push(observer);
11157
- return {
11158
- unsubscribe: () => {
11159
- _observers = _observers.filter((o) => o !== observer);
11160
- }
11161
- };
11162
- };
11163
- const unsubscribe = () => {
11164
- _observers = [];
11165
- };
11166
- return {
11167
- get observers() {
11168
- return _observers;
11169
- },
11170
- next,
11171
- subscribe,
11172
- unsubscribe
11173
- };
11174
- };
11175
- function extractFormValues(fieldsState, formValues) {
11176
- const values = {};
11177
- for (const key in fieldsState) {
11178
- if (fieldsState.hasOwnProperty(key)) {
11179
- const fieldState = fieldsState[key];
11180
- const fieldValue = formValues[key];
11181
- if (fieldState && isObject(fieldState) && fieldValue) {
11182
- const nestedFieldsState = extractFormValues(fieldState, fieldValue);
11183
- if (isObject(nestedFieldsState)) {
11184
- values[key] = nestedFieldsState;
11185
- }
11186
- } else if (fieldsState[key]) {
11187
- values[key] = fieldValue;
11188
- }
11189
- }
11190
- }
11191
- return values;
11192
- }
11193
- var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
11194
- var isFileInput = (element) => element.type === "file";
11195
- var isHTMLElement = (value) => {
11196
- if (!isWeb) {
11197
- return false;
11198
- }
11199
- const owner = value ? value.ownerDocument : 0;
11200
- return value instanceof (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement);
11201
- };
11202
- var isMultipleSelect = (element) => element.type === `select-multiple`;
11203
- var isRadioInput = (element) => element.type === "radio";
11204
- var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
11205
- var live = (ref) => isHTMLElement(ref) && ref.isConnected;
11206
- function baseGet(object, updatePath) {
11207
- const length = updatePath.slice(0, -1).length;
11208
- let index = 0;
11209
- while (index < length) {
11210
- object = isUndefined(object) ? index++ : object[updatePath[index++]];
11211
- }
11212
- return object;
11213
- }
11214
- function isEmptyArray(obj) {
11215
- for (const key in obj) {
11216
- if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
11217
- return false;
11218
- }
11219
- }
11220
- return true;
11221
- }
11222
- function unset(object, path) {
11223
- const paths = Array.isArray(path) ? path : isKey(path) ? [path] : stringToPath(path);
11224
- const childObject = paths.length === 1 ? object : baseGet(object, paths);
11225
- const index = paths.length - 1;
11226
- const key = paths[index];
11227
- if (childObject) {
11228
- delete childObject[key];
11229
- }
11230
- if (index !== 0 && (isObject(childObject) && isEmptyObject(childObject) || Array.isArray(childObject) && isEmptyArray(childObject))) {
11231
- unset(object, paths.slice(0, -1));
11232
- }
11233
- return object;
11234
- }
11235
- var objectHasFunction = (data) => {
11236
- for (const key in data) {
11237
- if (isFunction(data[key])) {
11238
- return true;
11239
- }
11240
- }
11241
- return false;
11242
- };
11243
- function isTraversable(value) {
11244
- return Array.isArray(value) || isObject(value) && !objectHasFunction(value);
11245
- }
11246
- function markFieldsDirty(data, fields = {}) {
11247
- for (const key in data) {
11248
- const value = data[key];
11249
- if (isTraversable(value)) {
11250
- fields[key] = Array.isArray(value) ? [] : {};
11251
- markFieldsDirty(value, fields[key]);
11252
- } else if (!isUndefined(value)) {
11253
- fields[key] = true;
11254
- }
11255
- }
11256
- return fields;
11257
- }
11258
- function getDirtyFields(data, formValues, dirtyFieldsFromValues) {
11259
- if (!dirtyFieldsFromValues) {
11260
- dirtyFieldsFromValues = markFieldsDirty(formValues);
11261
- }
11262
- for (const key in data) {
11263
- const value = data[key];
11264
- if (isTraversable(value)) {
11265
- if (isUndefined(formValues) || isPrimitive(dirtyFieldsFromValues[key])) {
11266
- dirtyFieldsFromValues[key] = markFieldsDirty(value, Array.isArray(value) ? [] : {});
11267
- } else {
11268
- getDirtyFields(value, isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
11269
- }
11270
- } else {
11271
- const formValue = formValues[key];
11272
- dirtyFieldsFromValues[key] = !deepEqual(value, formValue);
11273
- }
11274
- }
11275
- return dirtyFieldsFromValues;
11276
- }
11277
- var defaultResult = {
11278
- value: false,
11279
- isValid: false
11280
- };
11281
- var validResult = { value: true, isValid: true };
11282
- var getCheckboxValue = (options) => {
11283
- if (Array.isArray(options)) {
11284
- if (options.length > 1) {
11285
- const values = options.filter((option) => option && option.checked && !option.disabled).map((option) => option.value);
11286
- return { value: values, isValid: !!values.length };
11287
- }
11288
- return options[0].checked && !options[0].disabled ? (
11289
- // @ts-expect-error expected to work in the browser
11290
- options[0].attributes && !isUndefined(options[0].attributes.value) ? isUndefined(options[0].value) || options[0].value === "" ? validResult : { value: options[0].value, isValid: true } : validResult
11291
- ) : defaultResult;
11292
- }
11293
- return defaultResult;
11294
- };
11295
- var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value) ? value : valueAsNumber ? value === "" ? NaN : value ? +value : value : valueAsDate && isString(value) ? new Date(value) : setValueAs ? setValueAs(value) : value;
11296
- var defaultReturn = {
11297
- isValid: false,
11298
- value: null
11299
- };
11300
- var getRadioValue = (options) => Array.isArray(options) ? options.reduce((previous, option) => option && option.checked && !option.disabled ? {
11301
- isValid: true,
11302
- value: option.value
11303
- } : previous, defaultReturn) : defaultReturn;
11304
- function getFieldValue(_f) {
11305
- const ref = _f.ref;
11306
- if (isFileInput(ref)) {
11307
- return ref.files;
11308
- }
11309
- if (isRadioInput(ref)) {
11310
- return getRadioValue(_f.refs).value;
11311
- }
11312
- if (isMultipleSelect(ref)) {
11313
- return [...ref.selectedOptions].map(({ value }) => value);
11314
- }
11315
- if (isCheckBoxInput(ref)) {
11316
- return getCheckboxValue(_f.refs).value;
11317
- }
11318
- return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
11319
- }
11320
- var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
11321
- const fields = {};
11322
- for (const name of fieldsNames) {
11323
- const field = get(_fields, name);
11324
- field && set(fields, name, field._f);
11325
- }
11326
- return {
11327
- criteriaMode,
11328
- names: [...fieldsNames],
11329
- fields,
11330
- shouldUseNativeValidation
11331
- };
11332
- };
11333
- var isRegex = (value) => value instanceof RegExp;
11334
- var getRuleValue = (rule) => isUndefined(rule) ? rule : isRegex(rule) ? rule.source : isObject(rule) ? isRegex(rule.value) ? rule.value.source : rule.value : rule;
11335
- var getValidationModes = (mode) => ({
11336
- isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
11337
- isOnBlur: mode === VALIDATION_MODE.onBlur,
11338
- isOnChange: mode === VALIDATION_MODE.onChange,
11339
- isOnAll: mode === VALIDATION_MODE.all,
11340
- isOnTouch: mode === VALIDATION_MODE.onTouched
11341
- });
11342
- var ASYNC_FUNCTION = "AsyncFunction";
11343
- 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));
11344
- var hasValidation = (options) => options.mount && (options.required || options.min || options.max || options.maxLength || options.minLength || options.pattern || options.validate);
11345
- var isWatched = (name, _names, isBlurEvent) => !isBlurEvent && (_names.watchAll || _names.watch.has(name) || [..._names.watch].some((watchName) => name.startsWith(watchName) && /^\.\w+/.test(name.slice(watchName.length))));
11346
- var iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
11347
- for (const key of fieldsNames || Object.keys(fields)) {
11348
- const field = get(fields, key);
11349
- if (field) {
11350
- const { _f, ...currentField } = field;
11351
- if (_f) {
11352
- if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
11353
- return true;
11354
- } else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
11355
- return true;
11356
- } else {
11357
- if (iterateFieldsByAction(currentField, action)) {
11358
- break;
11359
- }
11360
- }
11361
- } else if (isObject(currentField)) {
11362
- if (iterateFieldsByAction(currentField, action)) {
11363
- break;
11364
- }
11365
- }
11366
- }
11367
- }
11368
- return;
11369
- };
11370
- function schemaErrorLookup(errors, _fields, name) {
11371
- const error = get(errors, name);
11372
- if (error || isKey(name)) {
11373
- return {
11374
- error,
11375
- name
11376
- };
11377
- }
11378
- const names = name.split(".");
11379
- while (names.length) {
11380
- const fieldName = names.join(".");
11381
- const field = get(_fields, fieldName);
11382
- const foundError = get(errors, fieldName);
11383
- if (field && !Array.isArray(field) && name !== fieldName) {
11384
- return { name };
11385
- }
11386
- if (foundError && foundError.type) {
11387
- return {
11388
- name: fieldName,
11389
- error: foundError
11390
- };
11391
- }
11392
- if (foundError && foundError.root && foundError.root.type) {
11393
- return {
11394
- name: `${fieldName}.root`,
11395
- error: foundError.root
11396
- };
11397
- }
11398
- names.pop();
11399
- }
11400
- return {
11401
- name
11402
- };
11403
- }
11404
- var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
11405
- updateFormState(formStateData);
11406
- const { name, ...formState } = formStateData;
11407
- return isEmptyObject(formState) || Object.keys(formState).length >= Object.keys(_proxyFormState).length || Object.keys(formState).find((key) => _proxyFormState[key] === (!isRoot || VALIDATION_MODE.all));
11408
- };
11409
- var shouldSubscribeByName = (name, signalName, exact) => !name || !signalName || name === signalName || convertToArrayPayload(name).some((currentName) => currentName && (exact ? currentName === signalName : currentName.startsWith(signalName) || signalName.startsWith(currentName)));
11410
- var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
11411
- if (mode.isOnAll) {
11412
- return false;
11413
- } else if (!isSubmitted && mode.isOnTouch) {
11414
- return !(isTouched || isBlurEvent);
11415
- } else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
11416
- return !isBlurEvent;
11417
- } else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
11418
- return isBlurEvent;
11419
- }
11420
- return true;
11421
- };
11422
- var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
11423
- var updateFieldArrayRootError = (errors, error, name) => {
11424
- const fieldArrayErrors = convertToArrayPayload(get(errors, name));
11425
- set(fieldArrayErrors, "root", error[name]);
11426
- set(errors, name, fieldArrayErrors);
11427
- return errors;
11428
- };
11429
- function getValidateError(result, ref, type = "validate") {
11430
- if (isString(result) || Array.isArray(result) && result.every(isString) || isBoolean(result) && !result) {
11431
- return {
11432
- type,
11433
- message: isString(result) ? result : "",
11434
- ref
11435
- };
11436
- }
11437
- }
11438
- var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData) ? validationData : {
11439
- value: validationData,
11440
- message: ""
11441
- };
11442
- var validateField = async (field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
11443
- const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount } = field._f;
11444
- const inputValue = get(formValues, name);
11445
- if (!mount || disabledFieldNames.has(name)) {
11446
- return {};
11447
- }
11448
- const inputRef = refs ? refs[0] : ref;
11449
- const setCustomValidity = (message) => {
11450
- if (shouldUseNativeValidation && inputRef.reportValidity) {
11451
- inputRef.setCustomValidity(isBoolean(message) ? "" : message || "");
11452
- inputRef.reportValidity();
11453
- }
11454
- };
11455
- const error = {};
11456
- const isRadio = isRadioInput(ref);
11457
- const isCheckBox = isCheckBoxInput(ref);
11458
- const isRadioOrCheckbox2 = isRadio || isCheckBox;
11459
- const isEmpty = (valueAsNumber || isFileInput(ref)) && isUndefined(ref.value) && isUndefined(inputValue) || isHTMLElement(ref) && ref.value === "" || inputValue === "" || Array.isArray(inputValue) && !inputValue.length;
11460
- const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
11461
- const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
11462
- const message = exceedMax ? maxLengthMessage : minLengthMessage;
11463
- error[name] = {
11464
- type: exceedMax ? maxType : minType,
11465
- message,
11466
- ref,
11467
- ...appendErrorsCurry(exceedMax ? maxType : minType, message)
11468
- };
11469
- };
11470
- if (isFieldArray ? !Array.isArray(inputValue) || !inputValue.length : required && (!isRadioOrCheckbox2 && (isEmpty || isNullOrUndefined(inputValue)) || isBoolean(inputValue) && !inputValue || isCheckBox && !getCheckboxValue(refs).isValid || isRadio && !getRadioValue(refs).isValid)) {
11471
- const { value, message } = isString(required) ? { value: !!required, message: required } : getValueAndMessage(required);
11472
- if (value) {
11473
- error[name] = {
11474
- type: INPUT_VALIDATION_RULES.required,
11475
- message,
11476
- ref: inputRef,
11477
- ...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message)
11478
- };
11479
- if (!validateAllFieldCriteria) {
11480
- setCustomValidity(message);
11481
- return error;
11482
- }
11483
- }
11484
- }
11485
- if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
11486
- let exceedMax;
11487
- let exceedMin;
11488
- const maxOutput = getValueAndMessage(max);
11489
- const minOutput = getValueAndMessage(min);
11490
- if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
11491
- const valueNumber = ref.valueAsNumber || (inputValue ? +inputValue : inputValue);
11492
- if (!isNullOrUndefined(maxOutput.value)) {
11493
- exceedMax = valueNumber > maxOutput.value;
11494
- }
11495
- if (!isNullOrUndefined(minOutput.value)) {
11496
- exceedMin = valueNumber < minOutput.value;
11497
- }
11498
- } else {
11499
- const valueDate = ref.valueAsDate || new Date(inputValue);
11500
- const convertTimeToDate = (time) => /* @__PURE__ */ new Date((/* @__PURE__ */ new Date()).toDateString() + " " + time);
11501
- const isTime = ref.type == "time";
11502
- const isWeek = ref.type == "week";
11503
- if (isString(maxOutput.value) && inputValue) {
11504
- exceedMax = isTime ? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value) : isWeek ? inputValue > maxOutput.value : valueDate > new Date(maxOutput.value);
11505
- }
11506
- if (isString(minOutput.value) && inputValue) {
11507
- exceedMin = isTime ? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value) : isWeek ? inputValue < minOutput.value : valueDate < new Date(minOutput.value);
11508
- }
11509
- }
11510
- if (exceedMax || exceedMin) {
11511
- getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
11512
- if (!validateAllFieldCriteria) {
11513
- setCustomValidity(error[name].message);
11514
- return error;
11515
- }
11516
- }
11517
- }
11518
- if ((maxLength || minLength) && !isEmpty && (isString(inputValue) || isFieldArray && Array.isArray(inputValue))) {
11519
- const maxLengthOutput = getValueAndMessage(maxLength);
11520
- const minLengthOutput = getValueAndMessage(minLength);
11521
- const exceedMax = !isNullOrUndefined(maxLengthOutput.value) && inputValue.length > +maxLengthOutput.value;
11522
- const exceedMin = !isNullOrUndefined(minLengthOutput.value) && inputValue.length < +minLengthOutput.value;
11523
- if (exceedMax || exceedMin) {
11524
- getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
11525
- if (!validateAllFieldCriteria) {
11526
- setCustomValidity(error[name].message);
11527
- return error;
11528
- }
11529
- }
11530
- }
11531
- if (pattern && !isEmpty && isString(inputValue)) {
11532
- const { value: patternValue, message } = getValueAndMessage(pattern);
11533
- if (isRegex(patternValue) && !inputValue.match(patternValue)) {
11534
- error[name] = {
11535
- type: INPUT_VALIDATION_RULES.pattern,
11536
- message,
11537
- ref,
11538
- ...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message)
11539
- };
11540
- if (!validateAllFieldCriteria) {
11541
- setCustomValidity(message);
11542
- return error;
11543
- }
11544
- }
11545
- }
11546
- if (validate) {
11547
- if (isFunction(validate)) {
11548
- const result = await validate(inputValue, formValues);
11549
- const validateError = getValidateError(result, inputRef);
11550
- if (validateError) {
11551
- error[name] = {
11552
- ...validateError,
11553
- ...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message)
11554
- };
11555
- if (!validateAllFieldCriteria) {
11556
- setCustomValidity(validateError.message);
11557
- return error;
11558
- }
11559
- }
11560
- } else if (isObject(validate)) {
11561
- let validationResult = {};
11562
- for (const key in validate) {
11563
- if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
11564
- break;
11565
- }
11566
- const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
11567
- if (validateError) {
11568
- validationResult = {
11569
- ...validateError,
11570
- ...appendErrorsCurry(key, validateError.message)
11571
- };
11572
- setCustomValidity(validateError.message);
11573
- if (validateAllFieldCriteria) {
11574
- error[name] = validationResult;
11575
- }
11576
- }
11577
- }
11578
- if (!isEmptyObject(validationResult)) {
11579
- error[name] = {
11580
- ref: inputRef,
11581
- ...validationResult
11582
- };
11583
- if (!validateAllFieldCriteria) {
11584
- return error;
11585
- }
11586
- }
11587
- }
11588
- }
11589
- setCustomValidity(true);
11590
- return error;
11591
- };
11592
- var defaultOptions = {
11593
- mode: VALIDATION_MODE.onSubmit,
11594
- reValidateMode: VALIDATION_MODE.onChange,
11595
- shouldFocusError: true
11596
- };
11597
- function createFormControl(props = {}) {
11598
- let _options = {
11599
- ...defaultOptions,
11600
- ...props
11601
- };
11602
- let _formState = {
11603
- submitCount: 0,
11604
- isDirty: false,
11605
- isReady: false,
11606
- isLoading: isFunction(_options.defaultValues),
11607
- isValidating: false,
11608
- isSubmitted: false,
11609
- isSubmitting: false,
11610
- isSubmitSuccessful: false,
11611
- isValid: false,
11612
- touchedFields: {},
11613
- dirtyFields: {},
11614
- validatingFields: {},
11615
- errors: _options.errors || {},
11616
- disabled: _options.disabled || false
11617
- };
11618
- let _fields = {};
11619
- let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values) ? cloneObject(_options.defaultValues || _options.values) || {} : {};
11620
- let _formValues = _options.shouldUnregister ? {} : cloneObject(_defaultValues);
11621
- let _state = {
11622
- action: false,
11623
- mount: false,
11624
- watch: false,
11625
- keepIsValid: false
11626
- };
11627
- let _names = {
11628
- mount: /* @__PURE__ */ new Set(),
11629
- disabled: /* @__PURE__ */ new Set(),
11630
- unMount: /* @__PURE__ */ new Set(),
11631
- array: /* @__PURE__ */ new Set(),
11632
- watch: /* @__PURE__ */ new Set()
11633
- };
11634
- let delayErrorCallback;
11635
- let timer = 0;
11636
- const defaultProxyFormState = {
11637
- isDirty: false,
11638
- dirtyFields: false,
11639
- validatingFields: false,
11640
- touchedFields: false,
11641
- isValidating: false,
11642
- isValid: false,
11643
- errors: false
11644
- };
11645
- const _proxyFormState = {
11646
- ...defaultProxyFormState
11647
- };
11648
- let _proxySubscribeFormState = {
11649
- ..._proxyFormState
11650
- };
11651
- const _subjects = {
11652
- array: createSubject(),
11653
- state: createSubject()
11654
- };
11655
- const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
11656
- const debounce = (callback) => (wait) => {
11657
- clearTimeout(timer);
11658
- timer = setTimeout(callback, wait);
11659
- };
11660
- const _setValid = async (shouldUpdateValid) => {
11661
- if (_state.keepIsValid) {
11662
- return;
11663
- }
11664
- if (!_options.disabled && (_proxyFormState.isValid || _proxySubscribeFormState.isValid || shouldUpdateValid)) {
11665
- let isValid;
11666
- if (_options.resolver) {
11667
- isValid = isEmptyObject((await _runSchema()).errors);
11668
- _updateIsValidating();
11669
- } else {
11670
- isValid = await executeBuiltInValidation(_fields, true);
11671
- }
11672
- if (isValid !== _formState.isValid) {
11673
- _subjects.state.next({
11674
- isValid
11675
- });
11676
- }
11677
- }
11678
- };
11679
- const _updateIsValidating = (names, isValidating) => {
11680
- if (!_options.disabled && (_proxyFormState.isValidating || _proxyFormState.validatingFields || _proxySubscribeFormState.isValidating || _proxySubscribeFormState.validatingFields)) {
11681
- (names || Array.from(_names.mount)).forEach((name) => {
11682
- if (name) {
11683
- isValidating ? set(_formState.validatingFields, name, isValidating) : unset(_formState.validatingFields, name);
11684
- }
11685
- });
11686
- _subjects.state.next({
11687
- validatingFields: _formState.validatingFields,
11688
- isValidating: !isEmptyObject(_formState.validatingFields)
11689
- });
11690
- }
11691
- };
11692
- const _setFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
11693
- if (args && method && !_options.disabled) {
11694
- _state.action = true;
11695
- if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
11696
- const fieldValues = method(get(_fields, name), args.argA, args.argB);
11697
- shouldSetValues && set(_fields, name, fieldValues);
11698
- }
11699
- if (shouldUpdateFieldsAndState && Array.isArray(get(_formState.errors, name))) {
11700
- const errors = method(get(_formState.errors, name), args.argA, args.argB);
11701
- shouldSetValues && set(_formState.errors, name, errors);
11702
- unsetEmptyArray(_formState.errors, name);
11703
- }
11704
- if ((_proxyFormState.touchedFields || _proxySubscribeFormState.touchedFields) && shouldUpdateFieldsAndState && Array.isArray(get(_formState.touchedFields, name))) {
11705
- const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
11706
- shouldSetValues && set(_formState.touchedFields, name, touchedFields);
11707
- }
11708
- if (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) {
11709
- _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
11710
- }
11711
- _subjects.state.next({
11712
- name,
11713
- isDirty: _getDirty(name, values),
11714
- dirtyFields: _formState.dirtyFields,
11715
- errors: _formState.errors,
11716
- isValid: _formState.isValid
11717
- });
11718
- } else {
11719
- set(_formValues, name, values);
11720
- }
11721
- };
11722
- const updateErrors = (name, error) => {
11723
- set(_formState.errors, name, error);
11724
- _subjects.state.next({
11725
- errors: _formState.errors
11726
- });
11727
- };
11728
- const _setErrors = (errors) => {
11729
- _formState.errors = errors;
11730
- _subjects.state.next({
11731
- errors: _formState.errors,
11732
- isValid: false
11733
- });
11734
- };
11735
- const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
11736
- const field = get(_fields, name);
11737
- if (field) {
11738
- const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
11739
- isUndefined(defaultValue) || ref && ref.defaultChecked || shouldSkipSetValueAs ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f)) : setFieldValue(name, defaultValue);
11740
- _state.mount && !_state.action && _setValid();
11741
- }
11742
- };
11743
- const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
11744
- let shouldUpdateField = false;
11745
- let isPreviousDirty = false;
11746
- const output = {
11747
- name
11748
- };
11749
- if (!_options.disabled) {
11750
- if (!isBlurEvent || shouldDirty) {
11751
- if (_proxyFormState.isDirty || _proxySubscribeFormState.isDirty) {
11752
- isPreviousDirty = _formState.isDirty;
11753
- _formState.isDirty = output.isDirty = _getDirty();
11754
- shouldUpdateField = isPreviousDirty !== output.isDirty;
11755
- }
11756
- const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
11757
- isPreviousDirty = !!get(_formState.dirtyFields, name);
11758
- isCurrentFieldPristine ? unset(_formState.dirtyFields, name) : set(_formState.dirtyFields, name, true);
11759
- output.dirtyFields = _formState.dirtyFields;
11760
- shouldUpdateField = shouldUpdateField || (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) && isPreviousDirty !== !isCurrentFieldPristine;
11761
- }
11762
- if (isBlurEvent) {
11763
- const isPreviousFieldTouched = get(_formState.touchedFields, name);
11764
- if (!isPreviousFieldTouched) {
11765
- set(_formState.touchedFields, name, isBlurEvent);
11766
- output.touchedFields = _formState.touchedFields;
11767
- shouldUpdateField = shouldUpdateField || (_proxyFormState.touchedFields || _proxySubscribeFormState.touchedFields) && isPreviousFieldTouched !== isBlurEvent;
11768
- }
11769
- }
11770
- shouldUpdateField && shouldRender && _subjects.state.next(output);
11771
- }
11772
- return shouldUpdateField ? output : {};
11773
- };
11774
- const shouldRenderByError = (name, isValid, error, fieldState) => {
11775
- const previousFieldError = get(_formState.errors, name);
11776
- const shouldUpdateValid = (_proxyFormState.isValid || _proxySubscribeFormState.isValid) && isBoolean(isValid) && _formState.isValid !== isValid;
11777
- if (_options.delayError && error) {
11778
- delayErrorCallback = debounce(() => updateErrors(name, error));
11779
- delayErrorCallback(_options.delayError);
11780
- } else {
11781
- clearTimeout(timer);
11782
- delayErrorCallback = null;
11783
- error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
11784
- }
11785
- if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) || !isEmptyObject(fieldState) || shouldUpdateValid) {
11786
- const updatedFormState = {
11787
- ...fieldState,
11788
- ...shouldUpdateValid && isBoolean(isValid) ? { isValid } : {},
11789
- errors: _formState.errors,
11790
- name
11791
- };
11792
- _formState = {
11793
- ..._formState,
11794
- ...updatedFormState
11795
- };
11796
- _subjects.state.next(updatedFormState);
11797
- }
11798
- };
11799
- const _runSchema = async (name) => {
11800
- _updateIsValidating(name, true);
11801
- const result = await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
11802
- return result;
11803
- };
11804
- const executeSchemaAndUpdateState = async (names) => {
11805
- const { errors } = await _runSchema(names);
11806
- _updateIsValidating(names);
11807
- if (names) {
11808
- for (const name of names) {
11809
- const error = get(errors, name);
11810
- error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
11811
- }
11812
- } else {
11813
- _formState.errors = errors;
11814
- }
11815
- return errors;
11816
- };
11817
- const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
11818
- valid: true
11819
- }) => {
11820
- for (const name in fields) {
11821
- const field = fields[name];
11822
- if (field) {
11823
- const { _f, ...fieldValue } = field;
11824
- if (_f) {
11825
- const isFieldArrayRoot = _names.array.has(_f.name);
11826
- const isPromiseFunction = field._f && hasPromiseValidation(field._f);
11827
- if (isPromiseFunction && _proxyFormState.validatingFields) {
11828
- _updateIsValidating([_f.name], true);
11829
- }
11830
- const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
11831
- if (isPromiseFunction && _proxyFormState.validatingFields) {
11832
- _updateIsValidating([_f.name]);
11833
- }
11834
- if (fieldError[_f.name]) {
11835
- context.valid = false;
11836
- if (shouldOnlyCheckValid) {
11837
- break;
11838
- }
11839
- }
11840
- !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));
11841
- }
11842
- !isEmptyObject(fieldValue) && await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context);
11843
- }
11844
- }
11845
- return context.valid;
11846
- };
11847
- const _removeUnmounted = () => {
11848
- for (const name of _names.unMount) {
11849
- const field = get(_fields, name);
11850
- field && (field._f.refs ? field._f.refs.every((ref) => !live(ref)) : !live(field._f.ref)) && unregister(name);
11851
- }
11852
- _names.unMount = /* @__PURE__ */ new Set();
11853
- };
11854
- const _getDirty = (name, data) => !_options.disabled && (name && data && set(_formValues, name, data), !deepEqual(getValues(), _defaultValues));
11855
- const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
11856
- ..._state.mount ? _formValues : isUndefined(defaultValue) ? _defaultValues : isString(names) ? { [names]: defaultValue } : defaultValue
11857
- }, isGlobal, defaultValue);
11858
- const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
11859
- const setFieldValue = (name, value, options = {}) => {
11860
- const field = get(_fields, name);
11861
- let fieldValue = value;
11862
- if (field) {
11863
- const fieldReference = field._f;
11864
- if (fieldReference) {
11865
- !fieldReference.disabled && set(_formValues, name, getFieldValueAs(value, fieldReference));
11866
- fieldValue = isHTMLElement(fieldReference.ref) && isNullOrUndefined(value) ? "" : value;
11867
- if (isMultipleSelect(fieldReference.ref)) {
11868
- [...fieldReference.ref.options].forEach((optionRef) => optionRef.selected = fieldValue.includes(optionRef.value));
11869
- } else if (fieldReference.refs) {
11870
- if (isCheckBoxInput(fieldReference.ref)) {
11871
- fieldReference.refs.forEach((checkboxRef) => {
11872
- if (!checkboxRef.defaultChecked || !checkboxRef.disabled) {
11873
- if (Array.isArray(fieldValue)) {
11874
- checkboxRef.checked = !!fieldValue.find((data) => data === checkboxRef.value);
11875
- } else {
11876
- checkboxRef.checked = fieldValue === checkboxRef.value || !!fieldValue;
11877
- }
11878
- }
11879
- });
11880
- } else {
11881
- fieldReference.refs.forEach((radioRef) => radioRef.checked = radioRef.value === fieldValue);
11882
- }
11883
- } else if (isFileInput(fieldReference.ref)) {
11884
- fieldReference.ref.value = "";
11885
- } else {
11886
- fieldReference.ref.value = fieldValue;
11887
- if (!fieldReference.ref.type) {
11888
- _subjects.state.next({
11889
- name,
11890
- values: cloneObject(_formValues)
11891
- });
11892
- }
11893
- }
11894
- }
11895
- }
11896
- (options.shouldDirty || options.shouldTouch) && updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
11897
- options.shouldValidate && trigger(name);
11898
- };
11899
- const setValues = (name, value, options) => {
11900
- for (const fieldKey in value) {
11901
- if (!value.hasOwnProperty(fieldKey)) {
11902
- return;
11903
- }
11904
- const fieldValue = value[fieldKey];
11905
- const fieldName = name + "." + fieldKey;
11906
- const field = get(_fields, fieldName);
11907
- (_names.array.has(name) || isObject(fieldValue) || field && !field._f) && !isDateObject(fieldValue) ? setValues(fieldName, fieldValue, options) : setFieldValue(fieldName, fieldValue, options);
11908
- }
11909
- };
11910
- const setValue = (name, value, options = {}) => {
11911
- const field = get(_fields, name);
11912
- const isFieldArray = _names.array.has(name);
11913
- const cloneValue = cloneObject(value);
11914
- set(_formValues, name, cloneValue);
11915
- if (isFieldArray) {
11916
- _subjects.array.next({
11917
- name,
11918
- values: cloneObject(_formValues)
11919
- });
11920
- if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields || _proxySubscribeFormState.isDirty || _proxySubscribeFormState.dirtyFields) && options.shouldDirty) {
11921
- _subjects.state.next({
11922
- name,
11923
- dirtyFields: getDirtyFields(_defaultValues, _formValues),
11924
- isDirty: _getDirty(name, cloneValue)
11925
- });
11926
- }
11927
- } else {
11928
- field && !field._f && !isNullOrUndefined(cloneValue) ? setValues(name, cloneValue, options) : setFieldValue(name, cloneValue, options);
11929
- }
11930
- isWatched(name, _names) && _subjects.state.next({ ..._formState, name });
11931
- _subjects.state.next({
11932
- name: _state.mount ? name : void 0,
11933
- values: cloneObject(_formValues)
11934
- });
11935
- };
11936
- const onChange = async (event) => {
11937
- _state.mount = true;
11938
- const target = event.target;
11939
- let name = target.name;
11940
- let isFieldValueUpdated = true;
11941
- const field = get(_fields, name);
11942
- const _updateIsFieldValueUpdated = (fieldValue) => {
11943
- isFieldValueUpdated = Number.isNaN(fieldValue) || isDateObject(fieldValue) && isNaN(fieldValue.getTime()) || deepEqual(fieldValue, get(_formValues, name, fieldValue));
11944
- };
11945
- const validationModeBeforeSubmit = getValidationModes(_options.mode);
11946
- const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
11947
- if (field) {
11948
- let error;
11949
- let isValid;
11950
- const fieldValue = target.type ? getFieldValue(field._f) : getEventValue(event);
11951
- const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
11952
- const shouldSkipValidation = !hasValidation(field._f) && !_options.resolver && !get(_formState.errors, name) && !field._f.deps || skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
11953
- const watched = isWatched(name, _names, isBlurEvent);
11954
- set(_formValues, name, fieldValue);
11955
- if (isBlurEvent) {
11956
- if (!target || !target.readOnly) {
11957
- field._f.onBlur && field._f.onBlur(event);
11958
- delayErrorCallback && delayErrorCallback(0);
11959
- }
11960
- } else if (field._f.onChange) {
11961
- field._f.onChange(event);
11962
- }
11963
- const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent);
11964
- const shouldRender = !isEmptyObject(fieldState) || watched;
11965
- !isBlurEvent && _subjects.state.next({
11966
- name,
11967
- type: event.type,
11968
- values: cloneObject(_formValues)
11969
- });
11970
- if (shouldSkipValidation) {
11971
- if (_proxyFormState.isValid || _proxySubscribeFormState.isValid) {
11972
- if (_options.mode === "onBlur") {
11973
- if (isBlurEvent) {
11974
- _setValid();
11975
- }
11976
- } else if (!isBlurEvent) {
11977
- _setValid();
11978
- }
11979
- }
11980
- return shouldRender && _subjects.state.next({ name, ...watched ? {} : fieldState });
11981
- }
11982
- !isBlurEvent && watched && _subjects.state.next({ ..._formState });
11983
- if (_options.resolver) {
11984
- const { errors } = await _runSchema([name]);
11985
- _updateIsValidating([name]);
11986
- _updateIsFieldValueUpdated(fieldValue);
11987
- if (isFieldValueUpdated) {
11988
- const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
11989
- const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
11990
- error = errorLookupResult.error;
11991
- name = errorLookupResult.name;
11992
- isValid = isEmptyObject(errors);
11993
- }
11994
- } else {
11995
- _updateIsValidating([name], true);
11996
- error = (await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
11997
- _updateIsValidating([name]);
11998
- _updateIsFieldValueUpdated(fieldValue);
11999
- if (isFieldValueUpdated) {
12000
- if (error) {
12001
- isValid = false;
12002
- } else if (_proxyFormState.isValid || _proxySubscribeFormState.isValid) {
12003
- isValid = await executeBuiltInValidation(_fields, true);
12004
- }
12005
- }
12006
- }
12007
- if (isFieldValueUpdated) {
12008
- field._f.deps && (!Array.isArray(field._f.deps) || field._f.deps.length > 0) && trigger(field._f.deps);
12009
- shouldRenderByError(name, isValid, error, fieldState);
12010
- }
12011
- }
12012
- };
12013
- const _focusInput = (ref, key) => {
12014
- if (get(_formState.errors, key) && ref.focus) {
12015
- ref.focus();
12016
- return 1;
12017
- }
12018
- return;
12019
- };
12020
- const trigger = async (name, options = {}) => {
12021
- let isValid;
12022
- let validationResult;
12023
- const fieldNames = convertToArrayPayload(name);
12024
- if (_options.resolver) {
12025
- const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
12026
- isValid = isEmptyObject(errors);
12027
- validationResult = name ? !fieldNames.some((name2) => get(errors, name2)) : isValid;
12028
- } else if (name) {
12029
- validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
12030
- const field = get(_fields, fieldName);
12031
- return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
12032
- }))).every(Boolean);
12033
- !(!validationResult && !_formState.isValid) && _setValid();
12034
- } else {
12035
- validationResult = isValid = await executeBuiltInValidation(_fields);
12036
- }
12037
- _subjects.state.next({
12038
- ...!isString(name) || (_proxyFormState.isValid || _proxySubscribeFormState.isValid) && isValid !== _formState.isValid ? {} : { name },
12039
- ..._options.resolver || !name ? { isValid } : {},
12040
- errors: _formState.errors
12041
- });
12042
- options.shouldFocus && !validationResult && iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
12043
- return validationResult;
12044
- };
12045
- const getValues = (fieldNames, config) => {
12046
- let values = {
12047
- ..._state.mount ? _formValues : _defaultValues
12048
- };
12049
- if (config) {
12050
- values = extractFormValues(config.dirtyFields ? _formState.dirtyFields : _formState.touchedFields, values);
12051
- }
12052
- return isUndefined(fieldNames) ? values : isString(fieldNames) ? get(values, fieldNames) : fieldNames.map((name) => get(values, name));
12053
- };
12054
- const getFieldState = (name, formState) => ({
12055
- invalid: !!get((formState || _formState).errors, name),
12056
- isDirty: !!get((formState || _formState).dirtyFields, name),
12057
- error: get((formState || _formState).errors, name),
12058
- isValidating: !!get(_formState.validatingFields, name),
12059
- isTouched: !!get((formState || _formState).touchedFields, name)
12060
- });
12061
- const clearErrors = (name) => {
12062
- name && convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
12063
- _subjects.state.next({
12064
- errors: name ? _formState.errors : {}
12065
- });
12066
- };
12067
- const setError = (name, error, options) => {
12068
- const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
12069
- const currentError = get(_formState.errors, name) || {};
12070
- const { ref: currentRef, message, type, ...restOfErrorTree } = currentError;
12071
- set(_formState.errors, name, {
12072
- ...restOfErrorTree,
12073
- ...error,
12074
- ref
12075
- });
12076
- _subjects.state.next({
12077
- name,
12078
- errors: _formState.errors,
12079
- isValid: false
12080
- });
12081
- options && options.shouldFocus && ref && ref.focus && ref.focus();
12082
- };
12083
- const watch = (name, defaultValue) => isFunction(name) ? _subjects.state.subscribe({
12084
- next: (payload) => "values" in payload && name(_getWatch(void 0, defaultValue), payload)
12085
- }) : _getWatch(name, defaultValue, true);
12086
- const _subscribe = (props2) => _subjects.state.subscribe({
12087
- next: (formState) => {
12088
- if (shouldSubscribeByName(props2.name, formState.name, props2.exact) && shouldRenderFormState(formState, props2.formState || _proxyFormState, _setFormState, props2.reRenderRoot)) {
12089
- props2.callback({
12090
- values: { ..._formValues },
12091
- ..._formState,
12092
- ...formState,
12093
- defaultValues: _defaultValues
12094
- });
12095
- }
12096
- }
12097
- }).unsubscribe;
12098
- const subscribe = (props2) => {
12099
- _state.mount = true;
12100
- _proxySubscribeFormState = {
12101
- ..._proxySubscribeFormState,
12102
- ...props2.formState
12103
- };
12104
- return _subscribe({
12105
- ...props2,
12106
- formState: {
12107
- ...defaultProxyFormState,
12108
- ...props2.formState
12109
- }
12110
- });
12111
- };
12112
- const unregister = (name, options = {}) => {
12113
- for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
12114
- _names.mount.delete(fieldName);
12115
- _names.array.delete(fieldName);
12116
- if (!options.keepValue) {
12117
- unset(_fields, fieldName);
12118
- unset(_formValues, fieldName);
12119
- }
12120
- !options.keepError && unset(_formState.errors, fieldName);
12121
- !options.keepDirty && unset(_formState.dirtyFields, fieldName);
12122
- !options.keepTouched && unset(_formState.touchedFields, fieldName);
12123
- !options.keepIsValidating && unset(_formState.validatingFields, fieldName);
12124
- !_options.shouldUnregister && !options.keepDefaultValue && unset(_defaultValues, fieldName);
12125
- }
12126
- _subjects.state.next({
12127
- values: cloneObject(_formValues)
12128
- });
12129
- _subjects.state.next({
12130
- ..._formState,
12131
- ...!options.keepDirty ? {} : { isDirty: _getDirty() }
12132
- });
12133
- !options.keepIsValid && _setValid();
12134
- };
12135
- const _setDisabledField = ({ disabled, name }) => {
12136
- if (isBoolean(disabled) && _state.mount || !!disabled || _names.disabled.has(name)) {
12137
- disabled ? _names.disabled.add(name) : _names.disabled.delete(name);
12138
- }
12139
- };
12140
- const register = (name, options = {}) => {
12141
- let field = get(_fields, name);
12142
- const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
12143
- set(_fields, name, {
12144
- ...field || {},
12145
- _f: {
12146
- ...field && field._f ? field._f : { ref: { name } },
12147
- name,
12148
- mount: true,
12149
- ...options
12150
- }
12151
- });
12152
- _names.mount.add(name);
12153
- if (field) {
12154
- _setDisabledField({
12155
- disabled: isBoolean(options.disabled) ? options.disabled : _options.disabled,
12156
- name
12157
- });
12158
- } else {
12159
- updateValidAndValue(name, true, options.value);
12160
- }
12161
- return {
12162
- ...disabledIsDefined ? { disabled: options.disabled || _options.disabled } : {},
12163
- ..._options.progressive ? {
12164
- required: !!options.required,
12165
- min: getRuleValue(options.min),
12166
- max: getRuleValue(options.max),
12167
- minLength: getRuleValue(options.minLength),
12168
- maxLength: getRuleValue(options.maxLength),
12169
- pattern: getRuleValue(options.pattern)
12170
- } : {},
12171
- name,
12172
- onChange,
12173
- onBlur: onChange,
12174
- ref: (ref) => {
12175
- if (ref) {
12176
- register(name, options);
12177
- field = get(_fields, name);
12178
- const fieldRef = isUndefined(ref.value) ? ref.querySelectorAll ? ref.querySelectorAll("input,select,textarea")[0] || ref : ref : ref;
12179
- const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
12180
- const refs = field._f.refs || [];
12181
- if (radioOrCheckbox ? refs.find((option) => option === fieldRef) : fieldRef === field._f.ref) {
12182
- return;
12183
- }
12184
- set(_fields, name, {
12185
- _f: {
12186
- ...field._f,
12187
- ...radioOrCheckbox ? {
12188
- refs: [
12189
- ...refs.filter(live),
12190
- fieldRef,
12191
- ...Array.isArray(get(_defaultValues, name)) ? [{}] : []
12192
- ],
12193
- ref: { type: fieldRef.type, name }
12194
- } : { ref: fieldRef }
12195
- }
12196
- });
12197
- updateValidAndValue(name, false, void 0, fieldRef);
12198
- } else {
12199
- field = get(_fields, name, {});
12200
- if (field._f) {
12201
- field._f.mount = false;
12202
- }
12203
- (_options.shouldUnregister || options.shouldUnregister) && !(isNameInFieldArray(_names.array, name) && _state.action) && _names.unMount.add(name);
12204
- }
12205
- }
12206
- };
12207
- };
12208
- const _focusError = () => _options.shouldFocusError && iterateFieldsByAction(_fields, _focusInput, _names.mount);
12209
- const _disableForm = (disabled) => {
12210
- if (isBoolean(disabled)) {
12211
- _subjects.state.next({ disabled });
12212
- iterateFieldsByAction(_fields, (ref, name) => {
12213
- const currentField = get(_fields, name);
12214
- if (currentField) {
12215
- ref.disabled = currentField._f.disabled || disabled;
12216
- if (Array.isArray(currentField._f.refs)) {
12217
- currentField._f.refs.forEach((inputRef) => {
12218
- inputRef.disabled = currentField._f.disabled || disabled;
12219
- });
12220
- }
12221
- }
12222
- }, 0, false);
12223
- }
12224
- };
12225
- const handleSubmit = (onValid, onInvalid) => async (e) => {
12226
- let onValidError = void 0;
12227
- if (e) {
12228
- e.preventDefault && e.preventDefault();
12229
- e.persist && e.persist();
12230
- }
12231
- let fieldValues = cloneObject(_formValues);
12232
- _subjects.state.next({
12233
- isSubmitting: true
12234
- });
12235
- if (_options.resolver) {
12236
- const { errors, values } = await _runSchema();
12237
- _updateIsValidating();
12238
- _formState.errors = errors;
12239
- fieldValues = cloneObject(values);
12240
- } else {
12241
- await executeBuiltInValidation(_fields);
12242
- }
12243
- if (_names.disabled.size) {
12244
- for (const name of _names.disabled) {
12245
- unset(fieldValues, name);
12246
- }
12247
- }
12248
- unset(_formState.errors, "root");
12249
- if (isEmptyObject(_formState.errors)) {
12250
- _subjects.state.next({
12251
- errors: {}
12252
- });
12253
- try {
12254
- await onValid(fieldValues, e);
12255
- } catch (error) {
12256
- onValidError = error;
12257
- }
12258
- } else {
12259
- if (onInvalid) {
12260
- await onInvalid({ ..._formState.errors }, e);
12261
- }
12262
- _focusError();
12263
- setTimeout(_focusError);
12264
- }
12265
- _subjects.state.next({
12266
- isSubmitted: true,
12267
- isSubmitting: false,
12268
- isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
12269
- submitCount: _formState.submitCount + 1,
12270
- errors: _formState.errors
12271
- });
12272
- if (onValidError) {
12273
- throw onValidError;
12274
- }
12275
- };
12276
- const resetField = (name, options = {}) => {
12277
- if (get(_fields, name)) {
12278
- if (isUndefined(options.defaultValue)) {
12279
- setValue(name, cloneObject(get(_defaultValues, name)));
12280
- } else {
12281
- setValue(name, options.defaultValue);
12282
- set(_defaultValues, name, cloneObject(options.defaultValue));
12283
- }
12284
- if (!options.keepTouched) {
12285
- unset(_formState.touchedFields, name);
12286
- }
12287
- if (!options.keepDirty) {
12288
- unset(_formState.dirtyFields, name);
12289
- _formState.isDirty = options.defaultValue ? _getDirty(name, cloneObject(get(_defaultValues, name))) : _getDirty();
12290
- }
12291
- if (!options.keepError) {
12292
- unset(_formState.errors, name);
12293
- _proxyFormState.isValid && _setValid();
12294
- }
12295
- _subjects.state.next({ ..._formState });
12296
- }
12297
- };
12298
- const _reset = (formValues, keepStateOptions = {}) => {
12299
- const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
12300
- const cloneUpdatedValues = cloneObject(updatedValues);
12301
- const isEmptyResetValues = isEmptyObject(formValues);
12302
- const values = isEmptyResetValues ? _defaultValues : cloneUpdatedValues;
12303
- if (!keepStateOptions.keepDefaultValues) {
12304
- _defaultValues = updatedValues;
12305
- }
12306
- if (!keepStateOptions.keepValues) {
12307
- if (keepStateOptions.keepDirtyValues) {
12308
- const fieldsToCheck = /* @__PURE__ */ new Set([
12309
- ..._names.mount,
12310
- ...Object.keys(getDirtyFields(_defaultValues, _formValues))
12311
- ]);
12312
- for (const fieldName of Array.from(fieldsToCheck)) {
12313
- get(_formState.dirtyFields, fieldName) ? set(values, fieldName, get(_formValues, fieldName)) : setValue(fieldName, get(values, fieldName));
12314
- }
12315
- } else {
12316
- if (isWeb && isUndefined(formValues)) {
12317
- for (const name of _names.mount) {
12318
- const field = get(_fields, name);
12319
- if (field && field._f) {
12320
- const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
12321
- if (isHTMLElement(fieldReference)) {
12322
- const form = fieldReference.closest("form");
12323
- if (form) {
12324
- form.reset();
12325
- break;
12326
- }
12327
- }
12328
- }
12329
- }
12330
- }
12331
- if (keepStateOptions.keepFieldsRef) {
12332
- for (const fieldName of _names.mount) {
12333
- setValue(fieldName, get(values, fieldName));
12334
- }
12335
- } else {
12336
- _fields = {};
12337
- }
12338
- }
12339
- _formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? cloneObject(_defaultValues) : {} : cloneObject(values);
12340
- _subjects.array.next({
12341
- values: { ...values }
12342
- });
12343
- _subjects.state.next({
12344
- values: { ...values }
12345
- });
12346
- }
12347
- _names = {
12348
- mount: keepStateOptions.keepDirtyValues ? _names.mount : /* @__PURE__ */ new Set(),
12349
- unMount: /* @__PURE__ */ new Set(),
12350
- array: /* @__PURE__ */ new Set(),
12351
- disabled: /* @__PURE__ */ new Set(),
12352
- watch: /* @__PURE__ */ new Set(),
12353
- watchAll: false,
12354
- focus: ""
12355
- };
12356
- _state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid || !!keepStateOptions.keepDirtyValues || !_options.shouldUnregister && !isEmptyObject(values);
12357
- _state.watch = !!_options.shouldUnregister;
12358
- _state.keepIsValid = !!keepStateOptions.keepIsValid;
12359
- _state.action = false;
12360
- if (!keepStateOptions.keepErrors) {
12361
- _formState.errors = {};
12362
- }
12363
- _subjects.state.next({
12364
- submitCount: keepStateOptions.keepSubmitCount ? _formState.submitCount : 0,
12365
- isDirty: isEmptyResetValues ? false : keepStateOptions.keepDirty ? _formState.isDirty : !!(keepStateOptions.keepDefaultValues && !deepEqual(formValues, _defaultValues)),
12366
- isSubmitted: keepStateOptions.keepIsSubmitted ? _formState.isSubmitted : false,
12367
- dirtyFields: isEmptyResetValues ? {} : keepStateOptions.keepDirtyValues ? keepStateOptions.keepDefaultValues && _formValues ? getDirtyFields(_defaultValues, _formValues) : _formState.dirtyFields : keepStateOptions.keepDefaultValues && formValues ? getDirtyFields(_defaultValues, formValues) : keepStateOptions.keepDirty ? _formState.dirtyFields : {},
12368
- touchedFields: keepStateOptions.keepTouched ? _formState.touchedFields : {},
12369
- errors: keepStateOptions.keepErrors ? _formState.errors : {},
12370
- isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful ? _formState.isSubmitSuccessful : false,
12371
- isSubmitting: false,
12372
- defaultValues: _defaultValues
12373
- });
12374
- };
12375
- const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues) ? formValues(_formValues) : formValues, { ..._options.resetOptions, ...keepStateOptions });
12376
- const setFocus = (name, options = {}) => {
12377
- const field = get(_fields, name);
12378
- const fieldReference = field && field._f;
12379
- if (fieldReference) {
12380
- const fieldRef = fieldReference.refs ? fieldReference.refs[0] : fieldReference.ref;
12381
- if (fieldRef.focus) {
12382
- setTimeout(() => {
12383
- fieldRef.focus();
12384
- options.shouldSelect && isFunction(fieldRef.select) && fieldRef.select();
12385
- });
12386
- }
12387
- }
12388
- };
12389
- const _setFormState = (updatedFormState) => {
12390
- _formState = {
12391
- ..._formState,
12392
- ...updatedFormState
12393
- };
12394
- };
12395
- const _resetDefaultValues = () => isFunction(_options.defaultValues) && _options.defaultValues().then((values) => {
12396
- reset(values, _options.resetOptions);
12397
- _subjects.state.next({
12398
- isLoading: false
12399
- });
12400
- });
12401
- const methods = {
12402
- control: {
12403
- register,
12404
- unregister,
12405
- getFieldState,
12406
- handleSubmit,
12407
- setError,
12408
- _subscribe,
12409
- _runSchema,
12410
- _updateIsValidating,
12411
- _focusError,
12412
- _getWatch,
12413
- _getDirty,
12414
- _setValid,
12415
- _setFieldArray,
12416
- _setDisabledField,
12417
- _setErrors,
12418
- _getFieldArray,
12419
- _reset,
12420
- _resetDefaultValues,
12421
- _removeUnmounted,
12422
- _disableForm,
12423
- _subjects,
12424
- _proxyFormState,
12425
- get _fields() {
12426
- return _fields;
12427
- },
12428
- get _formValues() {
12429
- return _formValues;
12430
- },
12431
- get _state() {
12432
- return _state;
12433
- },
12434
- set _state(value) {
12435
- _state = value;
12436
- },
12437
- get _defaultValues() {
12438
- return _defaultValues;
12439
- },
12440
- get _names() {
12441
- return _names;
12442
- },
12443
- set _names(value) {
12444
- _names = value;
12445
- },
12446
- get _formState() {
12447
- return _formState;
12448
- },
12449
- get _options() {
12450
- return _options;
12451
- },
12452
- set _options(value) {
12453
- _options = {
12454
- ..._options,
12455
- ...value
12456
- };
12457
- }
12458
- },
12459
- subscribe,
12460
- trigger,
12461
- register,
12462
- handleSubmit,
12463
- watch,
12464
- setValue,
12465
- getValues,
12466
- reset,
12467
- resetField,
12468
- clearErrors,
12469
- unregister,
12470
- setError,
12471
- setFocus,
12472
- getFieldState
12473
- };
12474
- return {
12475
- ...methods,
12476
- formControl: methods
12477
- };
12478
- }
12479
- function useForm(props = {}) {
12480
- const _formControl = import_react25.default.useRef(void 0);
12481
- const _values = import_react25.default.useRef(void 0);
12482
- const [formState, updateFormState] = import_react25.default.useState({
12483
- isDirty: false,
12484
- isValidating: false,
12485
- isLoading: isFunction(props.defaultValues),
12486
- isSubmitted: false,
12487
- isSubmitting: false,
12488
- isSubmitSuccessful: false,
12489
- isValid: false,
12490
- submitCount: 0,
12491
- dirtyFields: {},
12492
- touchedFields: {},
12493
- validatingFields: {},
12494
- errors: props.errors || {},
12495
- disabled: props.disabled || false,
12496
- isReady: false,
12497
- defaultValues: isFunction(props.defaultValues) ? void 0 : props.defaultValues
12498
- });
12499
- if (!_formControl.current) {
12500
- if (props.formControl) {
12501
- _formControl.current = {
12502
- ...props.formControl,
12503
- formState
12504
- };
12505
- if (props.defaultValues && !isFunction(props.defaultValues)) {
12506
- props.formControl.reset(props.defaultValues, props.resetOptions);
12507
- }
12508
- } else {
12509
- const { formControl, ...rest } = createFormControl(props);
12510
- _formControl.current = {
12511
- ...rest,
12512
- formState
12513
- };
12514
- }
12515
- }
12516
- const control = _formControl.current.control;
12517
- control._options = props;
12518
- useIsomorphicLayoutEffect(() => {
12519
- const sub = control._subscribe({
12520
- formState: control._proxyFormState,
12521
- callback: () => updateFormState({ ...control._formState }),
12522
- reRenderRoot: true
12523
- });
12524
- updateFormState((data) => ({
12525
- ...data,
12526
- isReady: true
12527
- }));
12528
- control._formState.isReady = true;
12529
- return sub;
12530
- }, [control]);
12531
- import_react25.default.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
12532
- import_react25.default.useEffect(() => {
12533
- if (props.mode) {
12534
- control._options.mode = props.mode;
12535
- }
12536
- if (props.reValidateMode) {
12537
- control._options.reValidateMode = props.reValidateMode;
12538
- }
12539
- }, [control, props.mode, props.reValidateMode]);
12540
- import_react25.default.useEffect(() => {
12541
- if (props.errors) {
12542
- control._setErrors(props.errors);
12543
- control._focusError();
12544
- }
12545
- }, [control, props.errors]);
12546
- import_react25.default.useEffect(() => {
12547
- props.shouldUnregister && control._subjects.state.next({
12548
- values: control._getWatch()
12549
- });
12550
- }, [control, props.shouldUnregister]);
12551
- import_react25.default.useEffect(() => {
12552
- if (control._proxyFormState.isDirty) {
12553
- const isDirty = control._getDirty();
12554
- if (isDirty !== formState.isDirty) {
12555
- control._subjects.state.next({
12556
- isDirty
12557
- });
12558
- }
12559
- }
12560
- }, [control, formState.isDirty]);
12561
- import_react25.default.useEffect(() => {
12562
- var _a;
12563
- if (props.values && !deepEqual(props.values, _values.current)) {
12564
- control._reset(props.values, {
12565
- keepFieldsRef: true,
12566
- ...control._options.resetOptions
12567
- });
12568
- if (!((_a = control._options.resetOptions) === null || _a === void 0 ? void 0 : _a.keepIsValid)) {
12569
- control._setValid();
12570
- }
12571
- _values.current = props.values;
12572
- updateFormState((state) => ({ ...state }));
12573
- } else {
12574
- control._resetDefaultValues();
12575
- }
12576
- }, [control, props.values]);
12577
- import_react25.default.useEffect(() => {
12578
- if (!control._state.mount) {
12579
- control._setValid();
12580
- control._state.mount = true;
12581
- }
12582
- if (control._state.watch) {
12583
- control._state.watch = false;
12584
- control._subjects.state.next({ ...control._formState });
12585
- }
12586
- control._removeUnmounted();
12587
- });
12588
- _formControl.current.formState = getProxyFormState(formState, control);
12589
- return _formControl.current;
12590
- }
12591
-
12592
- // ../../components/ui/Form.tsx
10723
+ var React39 = __toESM(require("react"), 1);
10724
+ var import_react_hook_form = require("react-hook-form");
12593
10725
  var import_next_intl8 = require("next-intl");
12594
10726
  var import_jsx_runtime48 = require("react/jsx-runtime");
12595
- var FormConfigContext = React40.createContext({ size: "md" });
10727
+ var FormConfigContext = React39.createContext({ size: "md" });
12596
10728
  var FormWrapper = ({
12597
10729
  children,
12598
10730
  onSubmit,
@@ -12602,28 +10734,28 @@ var FormWrapper = ({
12602
10734
  size = "md",
12603
10735
  ...props
12604
10736
  }) => {
12605
- const methods = useForm({
10737
+ const methods = (0, import_react_hook_form.useForm)({
12606
10738
  defaultValues: initialValues
12607
10739
  });
12608
- React40.useEffect(() => {
10740
+ React39.useEffect(() => {
12609
10741
  if (initialValues) {
12610
10742
  methods.reset(initialValues);
12611
10743
  }
12612
10744
  }, [JSON.stringify(initialValues)]);
12613
10745
  const { validationSchema: _, ...formProps } = props;
12614
- return /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormProvider, { ...methods, children: /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormConfigContext.Provider, { value: { size }, children: /* @__PURE__ */ (0, import_jsx_runtime48.jsx)("form", { onSubmit: methods.handleSubmit(onSubmit), className, ...formProps, children }) }) });
10746
+ return /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(import_react_hook_form.FormProvider, { ...methods, children: /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormConfigContext.Provider, { value: { size }, children: /* @__PURE__ */ (0, import_jsx_runtime48.jsx)("form", { onSubmit: methods.handleSubmit(onSubmit), className, ...formProps, children }) }) });
12615
10747
  };
12616
10748
  var Form = FormWrapper;
12617
- var FormFieldContext = React40.createContext({});
10749
+ var FormFieldContext = React39.createContext({});
12618
10750
  var FormField = ({
12619
10751
  ...props
12620
10752
  }) => {
12621
- return /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(Controller, { ...props }) });
10753
+ return /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(import_react_hook_form.Controller, { ...props }) });
12622
10754
  };
12623
10755
  var useFormField = () => {
12624
- const fieldContext = React40.useContext(FormFieldContext);
12625
- const itemContext = React40.useContext(FormItemContext);
12626
- const { getFieldState, formState } = useFormContext();
10756
+ const fieldContext = React39.useContext(FormFieldContext);
10757
+ const itemContext = React39.useContext(FormItemContext);
10758
+ const { getFieldState, formState } = (0, import_react_hook_form.useFormContext)();
12627
10759
  if (!fieldContext) {
12628
10760
  try {
12629
10761
  const t = (0, import_next_intl8.useTranslations)("Form");
@@ -12643,15 +10775,15 @@ var useFormField = () => {
12643
10775
  ...fieldState
12644
10776
  };
12645
10777
  };
12646
- var FormItemContext = React40.createContext({});
12647
- var FormItem = React40.forwardRef(({ className, ...props }, ref) => {
12648
- const id = React40.useId();
10778
+ var FormItemContext = React39.createContext({});
10779
+ var FormItem = React39.forwardRef(({ className, ...props }, ref) => {
10780
+ const id = React39.useId();
12649
10781
  return /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormItemContext.Provider, { value: { id }, children: /* @__PURE__ */ (0, import_jsx_runtime48.jsx)("div", { ref, className: cn("space-y-2", className), ...props }) });
12650
10782
  });
12651
10783
  FormItem.displayName = "FormItem";
12652
- var FormLabel = React40.forwardRef(({ className, children, required, ...props }, ref) => {
10784
+ var FormLabel = React39.forwardRef(({ className, children, required, ...props }, ref) => {
12653
10785
  const { error, formItemId } = useFormField();
12654
- const config = React40.useContext(FormConfigContext);
10786
+ const config = React39.useContext(FormConfigContext);
12655
10787
  const sizeClass = config.size === "sm" ? "text-xs" : config.size === "lg" ? "text-base" : "text-sm";
12656
10788
  return /* @__PURE__ */ (0, import_jsx_runtime48.jsxs)(
12657
10789
  Label,
@@ -12668,7 +10800,7 @@ var FormLabel = React40.forwardRef(({ className, children, required, ...props },
12668
10800
  );
12669
10801
  });
12670
10802
  FormLabel.displayName = "FormLabel";
12671
- var FormControl = React40.forwardRef(({ ...props }, ref) => {
10803
+ var FormControl = React39.forwardRef(({ ...props }, ref) => {
12672
10804
  const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
12673
10805
  return /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(
12674
10806
  "div",
@@ -12682,12 +10814,12 @@ var FormControl = React40.forwardRef(({ ...props }, ref) => {
12682
10814
  );
12683
10815
  });
12684
10816
  FormControl.displayName = "FormControl";
12685
- var FormDescription = React40.forwardRef(({ className, ...props }, ref) => {
10817
+ var FormDescription = React39.forwardRef(({ className, ...props }, ref) => {
12686
10818
  const { formDescriptionId } = useFormField();
12687
10819
  return /* @__PURE__ */ (0, import_jsx_runtime48.jsx)("p", { ref, id: formDescriptionId, className: cn("text-sm text-muted-foreground", className), ...props });
12688
10820
  });
12689
10821
  FormDescription.displayName = "FormDescription";
12690
- var FormMessage = React40.forwardRef(({ className, children, ...props }, ref) => {
10822
+ var FormMessage = React39.forwardRef(({ className, children, ...props }, ref) => {
12691
10823
  const { error, formMessageId } = useFormField();
12692
10824
  const body = error ? String(error?.message) : children;
12693
10825
  if (!body) {
@@ -12696,7 +10828,7 @@ var FormMessage = React40.forwardRef(({ className, children, ...props }, ref) =>
12696
10828
  return /* @__PURE__ */ (0, import_jsx_runtime48.jsx)("p", { ref, id: formMessageId, className: cn("text-sm font-medium text-destructive", className), ...props, children: body });
12697
10829
  });
12698
10830
  FormMessage.displayName = "FormMessage";
12699
- var FormInput = React40.forwardRef(({ name, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormConfigContext.Consumer, { children: ({ size }) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(
10831
+ var FormInput = React39.forwardRef(({ name, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormConfigContext.Consumer, { children: ({ size }) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(
12700
10832
  FormField,
12701
10833
  {
12702
10834
  name,
@@ -12707,7 +10839,7 @@ var FormInput = React40.forwardRef(({ name, ...props }, ref) => /* @__PURE__ */
12707
10839
  }
12708
10840
  ) }));
12709
10841
  FormInput.displayName = "FormInput";
12710
- var FormCheckbox = React40.forwardRef(({ name, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormConfigContext.Consumer, { children: ({ size }) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(
10842
+ var FormCheckbox = React39.forwardRef(({ name, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormConfigContext.Consumer, { children: ({ size }) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(
12711
10843
  FormField,
12712
10844
  {
12713
10845
  name,
@@ -12731,9 +10863,9 @@ var FormCheckbox = React40.forwardRef(({ name, ...props }, ref) => /* @__PURE__
12731
10863
  }
12732
10864
  ) }));
12733
10865
  FormCheckbox.displayName = "FormCheckbox";
12734
- var FormActions = React40.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)("div", { ref, className: cn("flex gap-2 justify-end", className), ...props }));
10866
+ var FormActions = React39.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)("div", { ref, className: cn("flex gap-2 justify-end", className), ...props }));
12735
10867
  FormActions.displayName = "FormActions";
12736
- var FormSubmitButton = React40.forwardRef(({ children, loading: loading2, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormConfigContext.Consumer, { children: ({ size }) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(Button_default, { ref, type: "submit", size: props.size ?? size, disabled: loading2, ...props, children }) }));
10868
+ var FormSubmitButton = React39.forwardRef(({ children, loading: loading2, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(FormConfigContext.Consumer, { children: ({ size }) => /* @__PURE__ */ (0, import_jsx_runtime48.jsx)(Button_default, { ref, type: "submit", size: props.size ?? size, disabled: loading2, ...props, children }) }));
12737
10869
  FormSubmitButton.displayName = "FormSubmitButton";
12738
10870
 
12739
10871
  // ../../components/ui/NotificationModal.tsx
@@ -12936,7 +11068,7 @@ function AccessDenied({
12936
11068
 
12937
11069
  // ../../components/ui/ThemeToggleHeadless.tsx
12938
11070
  var import_lucide_react30 = require("lucide-react");
12939
- var import_react26 = require("react");
11071
+ var import_react25 = require("react");
12940
11072
  var import_react_dom10 = require("react-dom");
12941
11073
  var import_jsx_runtime52 = require("react/jsx-runtime");
12942
11074
  function ThemeToggleHeadless({
@@ -12945,11 +11077,11 @@ function ThemeToggleHeadless({
12945
11077
  labels,
12946
11078
  className
12947
11079
  }) {
12948
- const [isOpen, setIsOpen] = (0, import_react26.useState)(false);
12949
- const [mounted, setMounted] = (0, import_react26.useState)(false);
12950
- const triggerRef = (0, import_react26.useRef)(null);
12951
- const [dropdownPosition, setDropdownPosition] = (0, import_react26.useState)(null);
12952
- (0, import_react26.useEffect)(() => setMounted(true), []);
11080
+ const [isOpen, setIsOpen] = (0, import_react25.useState)(false);
11081
+ const [mounted, setMounted] = (0, import_react25.useState)(false);
11082
+ const triggerRef = (0, import_react25.useRef)(null);
11083
+ const [dropdownPosition, setDropdownPosition] = (0, import_react25.useState)(null);
11084
+ (0, import_react25.useEffect)(() => setMounted(true), []);
12953
11085
  const themes = [
12954
11086
  { value: "light", label: labels?.light ?? "Light", icon: import_lucide_react30.Sun },
12955
11087
  { value: "dark", label: labels?.dark ?? "Dark", icon: import_lucide_react30.Moon },
@@ -13038,7 +11170,7 @@ function ThemeToggleHeadless({
13038
11170
  }
13039
11171
 
13040
11172
  // ../../components/ui/LanguageSwitcherHeadless.tsx
13041
- var import_react27 = require("react");
11173
+ var import_react26 = require("react");
13042
11174
  var import_react_dom11 = require("react-dom");
13043
11175
  var import_lucide_react31 = require("lucide-react");
13044
11176
  var import_jsx_runtime53 = require("react/jsx-runtime");
@@ -13049,9 +11181,9 @@ function LanguageSwitcherHeadless({
13049
11181
  labels,
13050
11182
  className
13051
11183
  }) {
13052
- const [isOpen, setIsOpen] = (0, import_react27.useState)(false);
13053
- const [dropdownPosition, setDropdownPosition] = (0, import_react27.useState)(null);
13054
- const triggerButtonRef = (0, import_react27.useRef)(null);
11184
+ const [isOpen, setIsOpen] = (0, import_react26.useState)(false);
11185
+ const [dropdownPosition, setDropdownPosition] = (0, import_react26.useState)(null);
11186
+ const triggerButtonRef = (0, import_react26.useRef)(null);
13055
11187
  const currentLanguage = locales.find((l) => l.code === currentLocale) || locales[0];
13056
11188
  const calculatePosition = () => {
13057
11189
  const rect = triggerButtonRef.current?.getBoundingClientRect();