@modular-forms/solid
Version:
The modular and type-safe form library for SolidJS
30 lines (29 loc) • 1.11 kB
JavaScript
import { createMemo } from 'solid-js';
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, } = {}) {
// eslint-disable-next-line solid/reactivity
return createMemo(() => {
// 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.get();
}
// Return whether field is present and matches filter options
return (!!field &&
(!shouldActive || field.active.get()) &&
(!shouldTouched || field.touched.get()) &&
(!shouldDirty || field.dirty.get()) &&
(!shouldValid || !field.error.get()));
})();
}