@modular-forms/qwik
Version:
The modular and type-safe form library for Qwik
33 lines (32 loc) • 1.2 kB
JavaScript
import { getFieldArrayStore, getUniqueId, setFieldArrayValue } from '../utils';
/**
* Replaces a item of the field array.
*
* @param form The form of the field array.
* @param name The name of the field array.
* @param options The replace options.
*/
export function replace(form, name, options) {
// Get store of specified field array
const fieldArray = getFieldArrayStore(form, name);
// Continue if specified field array exists
if (fieldArray) {
// Destructure options
const { at: index } = options;
// Get last index of field array
const lastIndex = fieldArray.items.length - 1;
// Continue if specified index is valid
if (index >= 0 && index <= lastIndex) {
// Replace value of field array
setFieldArrayValue(form, name, options);
// Replace item at field array
fieldArray.items[index] = getUniqueId();
// Set touched at field array and form to true
fieldArray.touched = true;
form.touched = true;
// Set dirty at field array and form to true
fieldArray.dirty = true;
form.dirty = true;
}
}
}