import Field, { FieldProps } from './Field'; /** * A container field for an array of values * * You must pass `childField` which is the underlying field for each value in the * list. `ListField` will call `format`, `parse`, and `normalize` on this field for * each value in the list. * * By default, `defaultValue` will be set to an empty array unless `blankAsNull` is * `true`, in which case it will be set to `null`. `normalize` and `parse` also behave * in the same way (a falsy value passed to these will either return an empty array * when `blankAsNull` is false or null when it is true). * * ## Usage * * ```js * import { CharField, IntegerField, ListField, viewModelFactory } from '@prestojs/viewmodel'; * * class User extends viewModelFactory({ * id: new IntegerField(), * name: new CharField(), * groupIds: new ListField({ childField: new IntegerField() }), * }, { pkFieldName: 'id' }) { * * } * // The groupIds go through normal IntegerField parsing so are converted to numbers * const user = new User({ id: 1, name: 'John', groupIds: ["1", "2", "3"] }); * console.log(user.groupIds) * // Output: [1, 2, 3] * ``` * * @extractdocs * @menugroup Fields * @typeParam T The value of each element in the list * @typeParam ParsableType This the type the field knows how to parse into `ValueType` when constructing a `ViewModel`. */ export default class ListField extends Field { static fieldClassName: string; childField: Field; constructor({ childField, blankAsNull, defaultValue, ...rest }: { /** * The underlying field used for each value in the list */ childField: Field; } & FieldProps); /** * Calls `childField.format` on each entry in the passed array */ format(value: T[]): any; /** * Calls `childField.parse` on each entry in the passed array * * If `value` is falsy or an empty array and `blankAsNull` is true * it will return `null` or if `blankAsNull` is false then it * will return an empty array. */ parse(value: ParsableType[] | null): T[] | null; /** * Calls `childField.parse` on each entry in the passed array * * If `value` is falsy or an empty array and `blankAsNull` is true * it will return `null` or if `blankAsNull` is false then it * will return an empty array. */ normalize(value: any): T[] | null; isEqual(value1: T[], value2: T[]): boolean; }