@modular-forms/solid
Version:
The modular and type-safe form library for SolidJS
30 lines (29 loc) • 1.22 kB
JavaScript
import { createMemo } from 'solid-js';
import { getFieldArrayStore } from '../utils';
/**
* Checks if the specified field array is included in the form.
*
* @param form The form of the field array.
* @param name The name of the field array.
* @param options The field array options.
*
* @returns Whether the field array is included.
*/
export function hasFieldArray(form, name, { shouldActive = true, shouldTouched = false, shouldDirty = false, shouldValid = false, } = {}) {
// eslint-disable-next-line solid/reactivity
return createMemo(() => {
// Get store of specified field array
const fieldArray = getFieldArrayStore(form, name);
// If field array is not present, set listener to be notified when a new
// field array is added
if (!fieldArray) {
form.internal.fieldArrayNames.get();
}
// Return whether field array is present and matches filter options
return (!!fieldArray &&
(!shouldActive || fieldArray.active.get()) &&
(!shouldTouched || fieldArray.touched.get()) &&
(!shouldDirty || fieldArray.dirty.get()) &&
(!shouldValid || !fieldArray.error.get()));
})();
}