@modular-forms/react
Version:
The modular and type-safe form library for React
29 lines (28 loc) • 1.07 kB
JavaScript
import { computed } from '@preact/signals-react';
import { getFieldStore } from '../utils';
/**
* Checks if the specified field is included in the form.
*
* @param form The form of the field.
* @param name The name of the field.
* @param options The field options.
*
* @returns Whether the field is included.
*/
export function hasField(form, name, { shouldActive = true, shouldTouched = false, shouldDirty = false, shouldValid = false, } = {}) {
return computed(() => {
// Get store of specified field
const field = getFieldStore(form, name);
// If field is not present, set listener to be notified when a new field is
// added
if (!field) {
form.internal.fieldNames.value;
}
// Return whether field is present and matches filter options
return (!!field &&
(!shouldActive || field.active.value) &&
(!shouldTouched || field.touched.value) &&
(!shouldDirty || field.dirty.value) &&
(!shouldValid || !field.error.value));
}).value;
}