@ariakit/react
Version:
Toolkit for building accessible web apps with React
1,044 lines (940 loc) • 37.7 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _ZGRVC6GZcjs = require('./ZGRVC6GZ.cjs');
var _V2KRYUNDcjs = require('./V2KRYUND.cjs');
var _FHQLDZSWcjs = require('./FHQLDZSW.cjs');
var _KRBLRFH6cjs = require('./KRBLRFH6.cjs');
var _SBAK37L7cjs = require('./SBAK37L7.cjs');
var _XM764FDRcjs = require('./XM764FDR.cjs');
var _I4KB2YNAcjs = require('./I4KB2YNA.cjs');
var _5FQZLBJ7cjs = require('./5FQZLBJ7.cjs');
var _MTBSKKM4cjs = require('./MTBSKKM4.cjs');
var _LZ6L3ECGcjs = require('./LZ6L3ECG.cjs');
// ../ariakit-react-components/dist/form/form-context.js
var _react = require('react');
var ctx = _LZ6L3ECGcjs.createStoreContext.call(void 0, [_XM764FDRcjs.CollectionContextProvider], [_XM764FDRcjs.CollectionScopedContextProvider]);
var useFormContext = ctx.useContext;
function useFormItemContext({ store, name: nameProp, component }) {
const context = useFormContext();
const form = store || context;
_LZ6L3ECGcjs.invariant.call(void 0, form, process.env.NODE_ENV !== "production" && `${component} must be wrapped in a Form component.`);
return {
store: form,
name: String(nameProp)
};
}
function useFormItem({ id: idProp, type, getItem: getItemProp, ...options }) {
const { store, name } = useFormItemContext(options);
const id = _LZ6L3ECGcjs.useId.call(void 0, idProp);
return {
store,
name,
id,
ref: _react.useRef.call(void 0, null),
getItem: _react.useCallback.call(void 0, (item) => {
const nextItem = {
...item,
id: id || item.id,
name,
type
};
if (getItemProp) return getItemProp(nextItem);
return nextItem;
}, [
id,
name,
type,
getItemProp
])
};
}
var useFormScopedContext = ctx.useScopedContext;
var useFormProviderContext = ctx.useProviderContext;
var FormContextProvider = ctx.ContextProvider;
var FormScopedContextProvider = ctx.ScopedContextProvider;
// ../ariakit-react-components/dist/form/form.js
var _jsxruntime = require('react/jsx-runtime');
var TagName = "form";
function isField(element, items) {
return items.some((item) => item.type === "field" && item.element === element);
}
function getFirstInvalidField(items) {
return _LZ6L3ECGcjs.sortBasedOnDOMPosition.call(void 0, items, (item) => item.element).find((item) => {
var _a;
return item.type === "field" && ((_a = item.element) == null ? void 0 : _a.getAttribute("aria-invalid")) === "true";
});
}
var useForm = _LZ6L3ECGcjs.createHook.call(void 0, function useForm2({ store, validateOnChange = true, validateOnBlur = true, resetOnUnmount = false, resetOnSubmit = true, autoFocusOnSubmit = true, ...props }) {
const context = useFormContext();
store = store || context;
_LZ6L3ECGcjs.invariant.call(void 0, store, process.env.NODE_ENV !== "production" && "Form must receive a `store` prop or be wrapped in a FormProvider component.");
const ref = _react.useRef.call(void 0, null);
const values = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "values");
const submitSucceed = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "submitSucceed");
const submitFailed = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "submitFailed");
const items = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "items");
const defaultValues = _LZ6L3ECGcjs.useInitialValue.call(void 0, values);
_react.useEffect.call(void 0, () => resetOnUnmount ? store == null ? void 0 : store.reset : void 0, [resetOnUnmount, store]);
_LZ6L3ECGcjs.useUpdateEffect.call(void 0, () => {
if (!validateOnChange) return;
if (values === defaultValues) return;
store == null ? void 0 : store.validate();
}, [
validateOnChange,
values,
defaultValues,
store
]);
_react.useEffect.call(void 0, () => {
if (!resetOnSubmit) return;
if (!submitSucceed) return;
store == null ? void 0 : store.reset();
}, [
resetOnSubmit,
submitSucceed,
store
]);
const [shouldFocusOnSubmit, setShouldFocusOnSubmit] = _react.useState.call(void 0, false);
_react.useEffect.call(void 0, () => {
if (!submitSucceed) return;
setShouldFocusOnSubmit(false);
}, [submitSucceed]);
_react.useEffect.call(void 0, () => {
var _a;
if (!shouldFocusOnSubmit) return;
if (!submitFailed) return;
const element = (_a = getFirstInvalidField(items)) == null ? void 0 : _a.element;
if (!element) return;
setShouldFocusOnSubmit(false);
element.focus();
if (_LZ6L3ECGcjs.isTextField.call(void 0, element)) element.select();
}, [
autoFocusOnSubmit,
submitFailed,
items
]);
const onSubmitProp = props.onSubmit;
const onSubmit = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onSubmitProp == null ? void 0 : onSubmitProp(event);
if (event.defaultPrevented) return;
event.preventDefault();
store == null ? void 0 : store.submit();
if (!autoFocusOnSubmit) return;
setShouldFocusOnSubmit(true);
});
const onBlurProp = props.onBlur;
const onBlur = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onBlurProp == null ? void 0 : onBlurProp(event);
if (event.defaultPrevented) return;
if (!validateOnBlur) return;
if (!store) return;
if (!isField(event.target, store.getState().items)) return;
store.validate();
});
const onResetProp = props.onReset;
const onReset = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onResetProp == null ? void 0 : onResetProp(event);
if (event.defaultPrevented) return;
event.preventDefault();
store == null ? void 0 : store.reset();
});
props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, FormScopedContextProvider, {
value: store,
children: element
}), [store]);
props = {
role: _LZ6L3ECGcjs.useTagName.call(void 0, ref, TagName) !== "form" ? "form" : void 0,
noValidate: true,
...props,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref),
onSubmit,
onBlur,
onReset
};
return props;
});
var Form = _LZ6L3ECGcjs.forwardRef.call(void 0, function Form2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName, useForm(props));
});
// ../ariakit-react-components/dist/form/form-control.js
var TagName2 = "input";
function getNamedElement(ref, name) {
const element = ref.current;
if (!element) return null;
if (element.name === name) return element;
if (element.form) return element.form.elements.namedItem(name);
return _LZ6L3ECGcjs.getDocument.call(void 0, element).getElementsByName(name)[0];
}
function getControlItemIds(items, name) {
const ids = {};
for (const item of items) {
if (item.name !== name) continue;
if (item.type === "label" && ids.labelId === void 0) ids.labelId = item.id;
else if (item.type === "error" && ids.errorId === void 0) ids.errorId = item.id;
else if (item.type === "description" && ids.descriptionId === void 0) ids.descriptionId = item.id;
if (ids.labelId !== void 0 && ids.errorId !== void 0 && ids.descriptionId !== void 0) break;
}
return ids;
}
function useControlState(form, name) {
const getItemIds = _react.useMemo.call(void 0, () => {
let prevItems;
let prevIds = {};
return (state) => {
if (state.items !== prevItems) {
prevItems = state.items;
prevIds = getControlItemIds(state.items, name);
}
return prevIds;
};
}, [name]);
return _5FQZLBJ7cjs.useStoreStateObject.call(void 0, form, {
labelId: (state) => getItemIds(state).labelId,
errorId: (state) => getItemIds(state).errorId,
descriptionId: (state) => getItemIds(state).descriptionId,
invalid: () => !!form.getError(name) && form.getFieldTouched(name)
});
}
var useFormControl = _LZ6L3ECGcjs.createHook.call(void 0, function useFormControl2({ store, name: nameProp, getItem: getItemProp, touchOnBlur = true, ...props }) {
const { store: form, name, id, ref, getItem } = useFormItem({
store,
name: nameProp,
id: props.id,
type: "field",
getItem: getItemProp,
component: "FormControl"
});
form.useValidate(async () => {
const element = getNamedElement(ref, name);
if (!element) return;
await Promise.resolve();
if ("validity" in element && !element.validity.valid) form.setError(name, element.validationMessage);
});
const onBlurProp = props.onBlur;
const touchOnBlurProp = _LZ6L3ECGcjs.useBooleanEvent.call(void 0, touchOnBlur);
const onBlur = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onBlurProp == null ? void 0 : onBlurProp(event);
if (event.defaultPrevented) return;
if (!touchOnBlurProp(event)) return;
form.setFieldTouched(name, true);
});
const { labelId, errorId, descriptionId, invalid } = useControlState(form, name);
const describedBy = _LZ6L3ECGcjs.cx.call(void 0, errorId, descriptionId, props["aria-describedby"]);
props = {
"aria-labelledby": props["aria-label"] != null ? void 0 : labelId,
"aria-invalid": invalid,
...props,
id,
"aria-describedby": describedBy || void 0,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref),
onBlur
};
props = _SBAK37L7cjs.useCollectionItem.call(void 0, {
store: form,
...props,
name,
getItem
});
return props;
});
var FormControl = _LZ6L3ECGcjs.memo.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function FormControl2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName2, useFormControl(props));
}));
// ../ariakit-react-components/dist/form/form-checkbox.js
var TagName3 = "input";
var useFormCheckbox = _LZ6L3ECGcjs.createHook.call(void 0, function useFormCheckbox2({ store, name: nameProp, value, checked, defaultChecked, ...props }) {
const { store: form, name } = useFormItemContext({
store,
name: nameProp,
component: "FormCheckbox"
});
props = _V2KRYUNDcjs.useCheckbox.call(void 0, {
store: _V2KRYUNDcjs.useCheckboxStore.call(void 0, {
value: form.useValue(name),
setValue: (value2) => form.setValue(name, value2)
}),
value,
checked,
...props
});
props = useFormControl({
store: form,
name,
"aria-labelledby": void 0,
...props
});
return props;
});
var FormCheckbox = _LZ6L3ECGcjs.memo.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function FormCheckbox2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName3, useFormCheckbox(props));
}));
// ../ariakit-react-components/dist/form/form-description.js
var TagName4 = "div";
var useFormDescription = _LZ6L3ECGcjs.createHook.call(void 0, function useFormDescription2({ store, name: nameProp, getItem: getItemProp, ...props }) {
const { store: form, id, ref, getItem } = useFormItem({
store,
name: nameProp,
id: props.id,
type: "description",
getItem: getItemProp,
component: "FormDescription"
});
props = {
...props,
id,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref)
};
props = _SBAK37L7cjs.useCollectionItem.call(void 0, {
store: form,
...props,
getItem
});
return props;
});
var FormDescription = _LZ6L3ECGcjs.memo.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function FormDescription2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName4, useFormDescription(props));
}));
// ../ariakit-react-components/dist/form/form-error.js
var TagName5 = "div";
var useFormError = _LZ6L3ECGcjs.createHook.call(void 0, function useFormError2({ store, name: nameProp, getItem: getItemProp, ...props }) {
const { store: form, name, id, ref, getItem } = useFormItem({
store,
name: nameProp,
id: props.id,
type: "error",
getItem: getItemProp,
component: "FormError"
});
props = {
role: "alert",
children: _5FQZLBJ7cjs.useStoreState.call(void 0, form, () => {
const error = form.getError(name);
if (error == null) return;
if (!form.getFieldTouched(name)) return;
return error;
}),
...props,
id,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref)
};
props = _SBAK37L7cjs.useCollectionItem.call(void 0, {
store: form,
...props,
getItem
});
return props;
});
var FormError = _LZ6L3ECGcjs.memo.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function FormError2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName5, useFormError(props));
}));
// ../ariakit-react-components/dist/form/form-field.js
var TagName6 = "input";
var useFormField = _LZ6L3ECGcjs.createHook.call(void 0, function useFormField2(props) {
return useFormControl(props);
});
var FormField = _LZ6L3ECGcjs.memo.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function FormField2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName6, useFormField(props));
}));
// ../ariakit-react-components/dist/form/form-group.js
var TagName7 = "div";
var useFormGroup = _LZ6L3ECGcjs.createHook.call(void 0, function useFormGroup2({ store, ...props }) {
props = _KRBLRFH6cjs.useGroup.call(void 0, props);
return props;
});
var FormGroup = _LZ6L3ECGcjs.forwardRef.call(void 0, function FormGroup2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName7, useFormGroup(props));
});
// ../ariakit-react-components/dist/form/form-group-label.js
var TagName8 = "div";
var useFormGroupLabel = _LZ6L3ECGcjs.createHook.call(void 0, function useFormGroupLabel2({ store, ...props }) {
props = _KRBLRFH6cjs.useGroupLabel.call(void 0, props);
return props;
});
var FormGroupLabel = _LZ6L3ECGcjs.forwardRef.call(void 0, function FormGroupLabel2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName8, useFormGroupLabel(props));
});
// ../ariakit-react-components/dist/form/form-input.js
var TagName9 = "input";
var useFormInput = _LZ6L3ECGcjs.createHook.call(void 0, function useFormInput2({ store, name: nameProp, ...props }) {
const { store: form, name } = useFormItemContext({
store,
name: nameProp,
component: "FormInput"
});
const onChangeProp = props.onChange;
const onChange = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onChangeProp == null ? void 0 : onChangeProp(event);
if (event.defaultPrevented) return;
form.setValue(name, event.target.value);
});
props = {
value: form.useValue(name),
...props,
onChange
};
props = _MTBSKKM4cjs.useFocusable.call(void 0, props);
props = useFormControl({
store: form,
name,
...props
});
return props;
});
var FormInput = _LZ6L3ECGcjs.memo.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function FormInput2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName9, useFormInput(props));
}));
// ../ariakit-react-components/dist/form/form-label.js
var TagName10 = "label";
function supportsNativeLabel(tagName) {
return tagName === "input" || tagName === "textarea" || tagName === "select" || tagName === "meter" || tagName === "progress";
}
var useFormLabel = _LZ6L3ECGcjs.createHook.call(void 0, function useFormLabel2({ store, name: nameProp, getItem: getItemProp, ...props }) {
const { store: form, name, id, ref, getItem } = useFormItem({
store,
name: nameProp,
id: props.id,
type: "label",
getItem: getItemProp,
component: "FormLabel"
});
const field = _5FQZLBJ7cjs.useStoreState.call(void 0, form, (state) => state.items.find((item) => item.type === "field" && item.name === name));
const isNativeLabel = supportsNativeLabel(_LZ6L3ECGcjs.useTagName.call(void 0, field == null ? void 0 : field.element, "input"));
const onClickProp = props.onClick;
const onClick = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onClickProp == null ? void 0 : onClickProp(event);
if (event.defaultPrevented) return;
if (isNativeLabel) return;
const fieldElement = field == null ? void 0 : field.element;
if (!fieldElement) return;
queueMicrotask(() => {
const focusableElement = _LZ6L3ECGcjs.getFirstTabbableIn.call(void 0, fieldElement, true, true);
focusableElement == null ? void 0 : focusableElement.focus();
if ((focusableElement == null ? void 0 : focusableElement.getAttribute("role")) === "combobox") return;
focusableElement == null ? void 0 : focusableElement.click();
});
});
props = {
render: isNativeLabel ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "label", {}) : /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", {}),
htmlFor: isNativeLabel ? field == null ? void 0 : field.id : void 0,
...props,
id,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref),
onClick
};
if (!isNativeLabel) props = {
...props,
style: {
cursor: "default",
...props.style
}
};
props = _SBAK37L7cjs.useCollectionItem.call(void 0, {
store: form,
...props,
getItem
});
return props;
});
var FormLabel = _LZ6L3ECGcjs.memo.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function FormLabel2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName10, useFormLabel(props));
}));
// ../ariakit-react-components/dist/form/form-store.js
// ../ariakit-components/dist/form/form-store.js
var maxArrayIndex = 2 ** 32 - 2;
function getPath(path) {
if (!Array.isArray(path)) return String(path).split(".");
return path.map((key) => {
if (typeof key === "number") return key;
return String(key);
});
}
function isPrototypePathSegment(key) {
if (key === "__proto__") return true;
if (key === "constructor") return true;
return false;
}
function nextFrame() {
return new Promise((resolve) => {
const timeoutId = setTimeout(resolve, 100);
requestAnimationFrame(() => {
clearTimeout(timeoutId);
resolve();
});
});
}
function hasMessages(object) {
const keys = Object.keys(object);
for (const key of keys) {
if (isPrototypePathSegment(key)) continue;
if (_LZ6L3ECGcjs.isObject.call(void 0, object[key])) {
if (hasMessages(object[key])) return true;
continue;
}
if (object[key]) return true;
}
return false;
}
function get(values, path, defaultValue2) {
return getPathValue(values, getPath(path), defaultValue2);
}
function getPathValue(values, path, defaultValue2) {
var _a;
const [key, ...rest] = path;
if (key == null || !values || isPrototypePathSegment(key)) return defaultValue2;
if (!rest.length) return (_a = values[key]) != null ? _a : defaultValue2;
return getPathValue(values[key], rest, defaultValue2);
}
function isArrayIndex(key) {
if (typeof key !== "string" && typeof key !== "number") return false;
const stringKey = String(key);
const index = Number(stringKey);
return Number.isInteger(index) && index >= 0 && index <= maxArrayIndex && String(index) === stringKey;
}
function getOrCreateNested(nestedValues, nextKey) {
if (Array.isArray(nestedValues) || _LZ6L3ECGcjs.isObject.call(void 0, nestedValues)) return nestedValues;
return isArrayIndex(nextKey) ? [] : {};
}
function setObjectValue(values, key, value) {
const result = {};
if (values) {
const keys = Object.keys(values);
for (const propertyKey of keys) {
if (isPrototypePathSegment(propertyKey)) continue;
result[propertyKey] = values[propertyKey];
}
}
result[key] = value;
return result;
}
function set(values, path, value) {
const pathKeys = getPath(path);
if (pathKeys.some(isPrototypePathSegment)) return values;
return setPath(values, pathKeys, value);
}
function setPath(values, path, value) {
const [k, ...rest] = path;
if (k == null) return values;
const key = k;
const isArrayIndexKey = isArrayIndex(k);
const nestedValues = (isArrayIndexKey ? values || [] : values || {})[key];
const nextKey = rest[0];
const result = rest.length && nextKey != null ? setPath(getOrCreateNested(nestedValues, nextKey), rest, value) : value;
if (isArrayIndexKey) {
const index = Number(key);
if (values && Array.isArray(values)) {
const copy = [...values];
copy[index] = result;
return copy;
}
const nextValues = [];
nextValues[index] = result;
return nextValues;
}
return setObjectValue(values, key, result);
}
function setAll(values, value) {
const result = {};
const keys = Object.keys(values);
for (const key of keys) {
if (isPrototypePathSegment(key)) continue;
const currentValue = values[key];
if (Array.isArray(currentValue)) result[key] = currentValue.map((v) => {
if (_LZ6L3ECGcjs.isObject.call(void 0, v)) return setAll(v, value);
return value;
});
else if (_LZ6L3ECGcjs.isObject.call(void 0, currentValue)) result[key] = setAll(currentValue, value);
else result[key] = value;
}
return result;
}
function getNameHandler(cache, prevKeys = []) {
return { get(target, key) {
if (typeof key === "symbol") {
if (key === Symbol.toPrimitive) return () => prevKeys.join(".");
return;
}
if (key === "toString") return () => prevKeys.join(".");
if (key === "valueOf") return () => prevKeys.join(".");
const nextKeys = [...prevKeys, key];
const nextKey = nextKeys.join(".");
if (cache[nextKey]) return cache[nextKey];
const nextProxy = new Proxy(target, getNameHandler(cache, nextKeys));
cache[nextKey] = nextProxy;
return nextProxy;
} };
}
function getStoreCallbacks(store) {
return store == null ? void 0 : store.__unstableCallbacks;
}
function createNames() {
return new Proxy(/* @__PURE__ */ Object.create(null), getNameHandler(/* @__PURE__ */ Object.create(null)));
}
function createFormStore(props = {}) {
var _a;
_5FQZLBJ7cjs.throwOnConflictingProps.call(void 0, props, props.store);
const syncState = (_a = props.store) == null ? void 0 : _a.getState();
const collection = _XM764FDRcjs.createCollectionStore.call(void 0, props);
const values = _LZ6L3ECGcjs.defaultValue.call(void 0, props.values, syncState == null ? void 0 : syncState.values, props.defaultValues, {});
const errors = _LZ6L3ECGcjs.defaultValue.call(void 0, props.errors, syncState == null ? void 0 : syncState.errors, props.defaultErrors, {});
const touched = _LZ6L3ECGcjs.defaultValue.call(void 0, props.touched, syncState == null ? void 0 : syncState.touched, props.defaultTouched, {});
const form = _5FQZLBJ7cjs.createStore.call(void 0, {
...collection.getState(),
values,
errors,
touched,
validating: _LZ6L3ECGcjs.defaultValue.call(void 0, syncState == null ? void 0 : syncState.validating, false),
submitting: _LZ6L3ECGcjs.defaultValue.call(void 0, syncState == null ? void 0 : syncState.submitting, false),
submitSucceed: _LZ6L3ECGcjs.defaultValue.call(void 0, syncState == null ? void 0 : syncState.submitSucceed, 0),
submitFailed: _LZ6L3ECGcjs.defaultValue.call(void 0, syncState == null ? void 0 : syncState.submitFailed, 0),
valid: !hasMessages(errors)
}, collection, props.store);
const syncCallbacks = getStoreCallbacks(props.store);
const syncCallbacksState = syncCallbacks == null ? void 0 : syncCallbacks.getState();
const callbacks = _5FQZLBJ7cjs.createStore.call(void 0, {
validate: (syncCallbacksState == null ? void 0 : syncCallbacksState.validate) || [],
submit: (syncCallbacksState == null ? void 0 : syncCallbacksState.submit) || []
}, syncCallbacks);
_5FQZLBJ7cjs.setup.call(void 0, form, () => _5FQZLBJ7cjs.init.call(void 0, callbacks));
_5FQZLBJ7cjs.setup.call(void 0, form, () => _5FQZLBJ7cjs.sync.call(void 0, form, ["validating", "errors"], (state) => {
if (state.validating) return;
form.setState("valid", !hasMessages(state.errors));
}));
const validate = async () => {
form.setState("validating", true);
form.setState("errors", {});
const validateCallbacks = callbacks.getState().validate;
try {
for (const callback of validateCallbacks) await callback(form.getState());
await nextFrame();
return !hasMessages(form.getState().errors);
} finally {
form.setState("validating", false);
}
};
return {
...collection,
...form,
names: createNames(),
setValues: (values2) => form.setState("values", values2),
getValue: (name) => get(form.getState().values, name),
setValue: (name, value) => form.setState("values", (values2) => {
const prevValue = get(values2, name);
const nextValue = _LZ6L3ECGcjs.applyState.call(void 0, value, prevValue);
if (nextValue === prevValue) return values2;
return set(values2, name, nextValue);
}),
pushValue: (name, value) => form.setState("values", (values2) => {
return set(values2, name, [...get(values2, name, []), value]);
}),
removeValue: (name, index) => form.setState("values", (values2) => {
const array = get(values2, name, []);
return set(values2, name, [
...array.slice(0, index),
null,
...array.slice(index + 1)
]);
}),
setErrors: (errors2) => form.setState("errors", errors2),
getError: (name) => get(form.getState().errors, name),
setError: (name, error) => form.setState("errors", (errors2) => {
const prevError = get(errors2, name);
const nextError = _LZ6L3ECGcjs.applyState.call(void 0, error, prevError);
if (nextError === prevError) return errors2;
return set(errors2, name, nextError);
}),
setTouched: (touched2) => form.setState("touched", touched2),
getFieldTouched: (name) => !!get(form.getState().touched, name),
setFieldTouched: (name, value) => form.setState("touched", (touched2) => {
const prevValue = get(touched2, name);
const nextValue = _LZ6L3ECGcjs.applyState.call(void 0, value, prevValue);
if (nextValue === prevValue) return touched2;
return set(touched2, name, nextValue);
}),
onValidate: (callback) => {
callbacks.setState("validate", (callbacks2) => [...callbacks2, callback]);
return () => {
callbacks.setState("validate", (callbacks2) => callbacks2.filter((c) => c !== callback));
};
},
validate,
onSubmit: (callback) => {
callbacks.setState("submit", (callbacks2) => [...callbacks2, callback]);
return () => {
callbacks.setState("submit", (callbacks2) => callbacks2.filter((c) => c !== callback));
};
},
submit: async () => {
form.setState("submitting", true);
form.setState("touched", setAll(form.getState().values, true));
try {
if (await validate()) {
const submitCallbacks = callbacks.getState().submit;
for (const callback of submitCallbacks) await callback(form.getState());
await nextFrame();
if (!hasMessages(form.getState().errors)) {
form.setState("submitSucceed", (count) => count + 1);
return true;
}
}
form.setState("submitFailed", (count) => count + 1);
return false;
} catch (error) {
form.setState("submitFailed", (count) => count + 1);
throw error;
} finally {
form.setState("submitting", false);
}
},
reset: () => {
form.setState("values", values);
form.setState("errors", errors);
form.setState("touched", touched);
form.setState("validating", false);
form.setState("submitting", false);
form.setState("submitSucceed", 0);
form.setState("submitFailed", 0);
form.setState("valid", !hasMessages(errors));
},
__unstableCallbacks: callbacks
};
}
// ../ariakit-react-components/dist/form/form-store.js
function useFormStoreProps(store, update, props) {
store = _XM764FDRcjs.useCollectionStoreProps.call(void 0, store, update, props);
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "values", "setValues");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "errors", "setErrors");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "touched", "setTouched");
const useValue = _react.useCallback.call(void 0, (name) => _5FQZLBJ7cjs.useStoreState.call(void 0, store, () => store.getValue(name)), [store]);
const useValidate = _react.useCallback.call(void 0, (callback) => {
callback = _LZ6L3ECGcjs.useEvent.call(void 0, callback);
_react.useEffect.call(void 0, () => store.onValidate(callback), [_5FQZLBJ7cjs.useStoreState.call(void 0, store, "items"), callback]);
}, [store]);
const useSubmit = _react.useCallback.call(void 0, (callback) => {
callback = _LZ6L3ECGcjs.useEvent.call(void 0, callback);
_react.useEffect.call(void 0, () => store.onSubmit(callback), [_5FQZLBJ7cjs.useStoreState.call(void 0, store, "items"), callback]);
}, [store]);
return _react.useMemo.call(void 0, () => ({
...store,
useValue,
useValidate,
useSubmit
}), [
store,
useValue,
useValidate,
useSubmit
]);
}
function useFormStore(props = {}) {
const [store, update] = _5FQZLBJ7cjs.useStore.call(void 0, createFormStore, props);
return useFormStoreProps(store, update, props);
}
// ../ariakit-react-components/dist/form/form-provider.js
function FormProvider(props = {}) {
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, FormContextProvider, {
value: useFormStore(props),
children: props.children
});
}
// ../ariakit-react-components/dist/form/utils.js
function isArrayFieldName(fieldName, name) {
return fieldName === name || fieldName.startsWith(`${name}.`);
}
function getArrayFieldIndex(fieldName, name) {
var _a;
const prefix = `${name}.`;
if (!fieldName.startsWith(prefix)) return NaN;
const index = (_a = fieldName.slice(prefix.length).match(/^\d+/)) == null ? void 0 : _a[0];
return index ? Number.parseInt(index, 10) : NaN;
}
// ../ariakit-react-components/dist/form/form-push.js
var TagName11 = "button";
function findFirstFieldByNameAndIndex(items, name, index) {
return items == null ? void 0 : items.find((item) => item.type === "field" && getArrayFieldIndex(item.name, name) === index);
}
var useFormPush = _LZ6L3ECGcjs.createHook.call(void 0, function useFormPush2({ store, value, name: nameProp, getItem: getItemProp, autoFocusOnClick = true, ...props }) {
const { store: form, name } = useFormItemContext({
store,
name: nameProp,
component: "FormPush"
});
const items = _5FQZLBJ7cjs.useStoreState.call(void 0, form, "items");
const [focusIndex, setFocusIndex] = _react.useState.call(void 0, null);
_react.useEffect.call(void 0, () => {
var _a;
if (focusIndex == null) return;
const element = (_a = findFirstFieldByNameAndIndex(items, name, focusIndex)) == null ? void 0 : _a.element;
if (!element) return;
element.focus();
setFocusIndex(null);
}, [
items,
focusIndex,
name
]);
const getItem = _react.useCallback.call(void 0, (item) => {
const nextItem = {
...item,
type: "button",
name
};
if (getItemProp) return getItemProp(nextItem);
return nextItem;
}, [name, getItemProp]);
const onClickProp = props.onClick;
const onClick = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
var _a, _b;
onClickProp == null ? void 0 : onClickProp(event);
if (event.defaultPrevented) return;
const length = (_b = (_a = form.getValue(name)) == null ? void 0 : _a.length) != null ? _b : 0;
form.pushValue(name, value);
if (!autoFocusOnClick) return;
setFocusIndex(length);
});
props = {
...props,
onClick
};
props = _I4KB2YNAcjs.useButton.call(void 0, props);
props = _SBAK37L7cjs.useCollectionItem.call(void 0, {
store: form,
...props,
getItem
});
return props;
});
var FormPush = _LZ6L3ECGcjs.forwardRef.call(void 0, function FormPush2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName11, useFormPush(props));
});
// ../ariakit-react-components/dist/form/form-radio.js
var TagName12 = "input";
var useFormRadio = _LZ6L3ECGcjs.createHook.call(void 0, function useFormRadio2({ store, name: nameProp, value, ...props }) {
const { store: form, name } = useFormItemContext({
store,
name: nameProp,
component: "FormRadio"
});
const onChangeProp = props.onChange;
const onChange = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onChangeProp == null ? void 0 : onChangeProp(event);
if (event.defaultPrevented) return;
form.setValue(name, value);
});
const checkedProp = props.checked;
const checked = _5FQZLBJ7cjs.useStoreState.call(void 0, form, () => checkedProp != null ? checkedProp : form.getValue(name) === value);
props = {
...props,
checked,
onChange
};
props = _ZGRVC6GZcjs.useRadio.call(void 0, {
name,
value,
...props
});
props = useFormControl({
store: form,
name,
"aria-labelledby": void 0,
...props
});
return props;
});
var FormRadio = _LZ6L3ECGcjs.memo.call(void 0, _LZ6L3ECGcjs.forwardRef.call(void 0, function FormRadio2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName12, useFormRadio(props));
}));
// ../ariakit-react-components/dist/form/form-radio-group.js
var TagName13 = "div";
var useFormRadioGroup = _LZ6L3ECGcjs.createHook.call(void 0, function useFormRadioGroup2({ store, ...props }) {
props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _FHQLDZSWcjs.CompositeScopedContextProvider, {
value: void 0,
children: element
}), []);
props = {
role: "radiogroup",
...props
};
props = useFormGroup(props);
return props;
});
var FormRadioGroup = _LZ6L3ECGcjs.forwardRef.call(void 0, function FormRadioGroup2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName13, useFormRadioGroup(props));
});
// ../ariakit-react-components/dist/form/form-remove.js
var TagName14 = "button";
function findNextOrPreviousField(items, name, index) {
const fields = items == null ? void 0 : items.filter((item) => item.type === "field" && isArrayFieldName(item.name, name));
const nextField = fields == null ? void 0 : fields.find((field) => getArrayFieldIndex(field.name, name) > index);
if (nextField) return nextField;
return fields == null ? void 0 : fields.reverse().find((field) => getArrayFieldIndex(field.name, name) < index);
}
function findPushButton(items, name) {
return items == null ? void 0 : items.find((item) => item.type === "button" && item.name === name);
}
var useFormRemove = _LZ6L3ECGcjs.createHook.call(void 0, function useFormRemove2({ store, name: nameProp, index, autoFocusOnClick = true, ...props }) {
const { store: form, name } = useFormItemContext({
store,
name: nameProp,
component: "FormRemove"
});
const onClickProp = props.onClick;
const onClick = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
var _a, _b, _c;
onClickProp == null ? void 0 : onClickProp(event);
if (event.defaultPrevented) return;
form.removeValue(name, index);
if (!autoFocusOnClick) return;
const { items } = form.getState();
const element = (_a = findNextOrPreviousField(items, name, index)) == null ? void 0 : _a.element;
if (element) {
element.focus();
if (_LZ6L3ECGcjs.isTextField.call(void 0, element)) element.select();
} else (_c = (_b = findPushButton(items, name)) == null ? void 0 : _b.element) == null ? void 0 : _c.focus();
});
props = {
...props,
onClick
};
props = _I4KB2YNAcjs.useButton.call(void 0, props);
return props;
});
var FormRemove = _LZ6L3ECGcjs.forwardRef.call(void 0, function FormRemove2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName14, useFormRemove(props));
});
// ../ariakit-react-components/dist/form/form-reset.js
var TagName15 = "button";
var useFormReset = _LZ6L3ECGcjs.createHook.call(void 0, function useFormReset2({ store, ...props }) {
const context = useFormContext();
store = store || context;
_LZ6L3ECGcjs.invariant.call(void 0, store, process.env.NODE_ENV !== "production" && "FormReset must be wrapped in a Form component.");
props = {
type: "reset",
disabled: _5FQZLBJ7cjs.useStoreState.call(void 0, store, "submitting"),
...props
};
props = _I4KB2YNAcjs.useButton.call(void 0, props);
return props;
});
var FormReset = _LZ6L3ECGcjs.forwardRef.call(void 0, function FormReset2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName15, useFormReset(props));
});
// ../ariakit-react-components/dist/form/form-submit.js
var TagName16 = "button";
var useFormSubmit = _LZ6L3ECGcjs.createHook.call(void 0, function useFormSubmit2({ store, accessibleWhenDisabled = true, ...props }) {
const context = useFormContext();
store = store || context;
_LZ6L3ECGcjs.invariant.call(void 0, store, process.env.NODE_ENV !== "production" && "FormSubmit must be wrapped in a Form component.");
props = {
type: "submit",
disabled: _5FQZLBJ7cjs.useStoreState.call(void 0, store, "submitting"),
...props
};
props = _I4KB2YNAcjs.useButton.call(void 0, {
...props,
accessibleWhenDisabled
});
return props;
});
var FormSubmit = _LZ6L3ECGcjs.forwardRef.call(void 0, function FormSubmit2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName16, useFormSubmit(props));
});
exports.useFormContext = useFormContext; exports.Form = Form; exports.FormControl = FormControl; exports.FormCheckbox = FormCheckbox; exports.FormDescription = FormDescription; exports.FormError = FormError; exports.FormField = FormField; exports.FormGroup = FormGroup; exports.FormGroupLabel = FormGroupLabel; exports.FormInput = FormInput; exports.FormLabel = FormLabel; exports.useFormStore = useFormStore; exports.FormProvider = FormProvider; exports.FormPush = FormPush; exports.FormRadio = FormRadio; exports.FormRadioGroup = FormRadioGroup; exports.FormRemove = FormRemove; exports.FormReset = FormReset; exports.FormSubmit = FormSubmit;