-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.ts
More file actions
54 lines (45 loc) · 1.75 KB
/
list.ts
File metadata and controls
54 lines (45 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { FieldDefinition } from './base'
import type { SchemaProvide } from '../types'
import type { SchemaDefinition } from '../schema'
export class ListFieldDefinition extends FieldDefinition<Record<string, unknown>[]> {
constructor(attrs: Record<string, unknown> = {}) {
super('list', 'array', attrs)
}
itemSchema(schema: SchemaProvide): this {
this._config.attrs = { ...this._config.attrs, itemSchema: schema }
return this
}
reorderable(r = true): this {
this._config.attrs = { ...this._config.attrs, reorderable: r }
return this
}
minItems(n: number): this {
this._config.validations = [...this._config.validations, { rule: 'minItems', params: { value: n } }]
return this
}
maxItems(n: number): this {
this._config.validations = [...this._config.validations, { rule: 'maxItems', params: { value: n } }]
return this
}
events(events: Record<string, unknown>): this {
this._config.attrs = { ...this._config.attrs, events }
return this
}
hooks(hooks: Record<string, unknown>): this {
this._config.attrs = { ...this._config.attrs, hooks }
return this
}
handlers(handlers: Record<string, unknown>): this {
this._config.attrs = { ...this._config.attrs, handlers }
return this
}
}
export function list(schema?: SchemaDefinition<any> | Record<string, unknown>): ListFieldDefinition {
if (schema && 'provide' in schema && typeof (schema as SchemaDefinition<any>).provide === 'function') {
const field = new ListFieldDefinition()
// call provide() so the stored value is plain data that survives deep cloning in toConfig()
field.itemSchema((schema as SchemaDefinition<any>).provide())
return field
}
return new ListFieldDefinition(schema as Record<string, unknown> | undefined)
}