forked from linuxfoundation/crowd.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-array-field.js
More file actions
136 lines (114 loc) · 2.75 KB
/
Copy pathstring-array-field.js
File metadata and controls
136 lines (114 loc) · 2.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as yup from 'yup';
import GenericField from '@/shared/fields/generic-field';
import { i18n } from '@/i18n';
export default class StringArrayField extends GenericField {
constructor(name, label, config = {}) {
super(name, label);
this.placeholder = config.placeholder;
this.hint = config.hint;
this.required = config.required;
this.min = config.min;
this.max = config.max;
this.filterable = config.filterable || false;
}
forPresenter(value) {
return value || [];
}
forFilterPreview(value) {
return (value || []).join(' ');
}
forFormInitialValue(value) {
return value || [];
}
forFilterInitialValue(value) {
if (value && !Array.isArray(value)) {
return [value];
}
return value || [];
}
forFormRules() {
const output = [];
if (this.required) {
output.push({
type: 'array',
required: Boolean(this.required),
message: i18n('validation.mixed.required').replace(
'{path}',
this.label,
),
});
}
if (this.min || this.min === 0) {
output.push({
type: 'array',
min: this.min,
message: i18n('validation.array.min')
.replace('{path}', this.label)
.replace('{min}', this.min),
});
}
if (this.max || this.max === 0) {
output.push({
type: 'array',
max: this.max,
message: i18n('validation.array.max')
.replace('{path}', this.label)
.replace('{max}', this.max),
});
}
return output;
}
forFormCast() {
return yup
.array()
.compact()
.ensure()
.of(yup.string().trim())
.label(this.label)
.transform((value, originalValue) => {
if (!originalValue) {
return originalValue;
}
if (Array.isArray(originalValue)) {
return originalValue;
}
return [originalValue];
});
}
forFilterCast() {
return yup
.array()
.compact()
.ensure()
.of(yup.string().trim())
.label(this.label)
.transform((value, originalValue) => {
if (!originalValue) {
return originalValue;
}
if (Array.isArray(originalValue)) {
return originalValue;
}
return [originalValue];
});
}
forImport() {
let yupChain = yup
.mixed()
.label(this.label)
.transform((value) => (Array.isArray(value)
? value
: (value || '')
.trim()
.split(' ')
.filter((item) => Boolean(item))
.map((item) => item.trim())));
if (this.required) {
yupChain = yupChain.required();
}
return yupChain;
}
forExport() {
return yup.mixed().label(this.label);
}
}