@gitlab/ui 62.5.1 → 62.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/components/base/form/form_fields/form_field_validator.js +93 -0
- package/dist/components/base/form/form_fields/form_fields.js +213 -0
- package/dist/components/base/form/form_fields/mappers.js +13 -0
- package/dist/components/base/form/form_fields/validators.js +16 -0
- package/dist/components/base/toggle/toggle.js +1 -1
- package/dist/index.js +1 -0
- package/dist/utils.js +4 -0
- package/package.json +5 -5
- package/src/components/base/form/form_fields/form_field_validator.spec.js +51 -0
- package/src/components/base/form/form_fields/form_field_validator.vue +59 -0
- package/src/components/base/form/form_fields/form_fields.md +28 -0
- package/src/components/base/form/form_fields/form_fields.spec.js +424 -0
- package/src/components/base/form/form_fields/form_fields.stories.js +102 -0
- package/src/components/base/form/form_fields/form_fields.vue +219 -0
- package/src/components/base/form/form_fields/mappers.js +11 -0
- package/src/components/base/form/form_fields/mappers.spec.js +17 -0
- package/src/components/base/form/form_fields/validators.js +16 -0
- package/src/components/base/form/form_fields/validators.spec.js +29 -0
- package/src/components/base/toggle/toggle.spec.js +8 -4
- package/src/components/base/toggle/toggle.vue +1 -0
- package/src/index.js +1 -0
- package/src/utils.js +3 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [62.6.0](https://gitlab.com/gitlab-org/gitlab-ui/compare/v62.5.2...v62.6.0) (2023-05-02)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* Add gl-form-fields component ([a17bcb6](https://gitlab.com/gitlab-org/gitlab-ui/commit/a17bcb6c8db0444dcd7f5bd799fe681891dbdcc9))
|
|
7
|
+
|
|
8
|
+
## [62.5.2](https://gitlab.com/gitlab-org/gitlab-ui/compare/v62.5.1...v62.5.2) (2023-05-01)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **GlToggle:** set aria-disabled attribute ([a899eb7](https://gitlab.com/gitlab-org/gitlab-ui/commit/a899eb7acd23d089ddb43c3bcd66a0aae7fe3c6f))
|
|
14
|
+
|
|
1
15
|
## [62.5.1](https://gitlab.com/gitlab-org/gitlab-ui/compare/v62.5.0...v62.5.1) (2023-04-28)
|
|
2
16
|
|
|
3
17
|
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import __vue_normalize__ from 'vue-runtime-helpers/dist/normalize-component.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* FormFieldValidator
|
|
5
|
+
*
|
|
6
|
+
* This is an internal component which is used to watch on specific field/value
|
|
7
|
+
* pairs and emits changes to `invalidFeedback`.
|
|
8
|
+
*
|
|
9
|
+
* **why:** Without this separate component, *any* change to *any* value
|
|
10
|
+
* was causing *all* validators to run. A separate renderless component
|
|
11
|
+
* helps us isolate this logic *and* react only to what we need to.
|
|
12
|
+
*/
|
|
13
|
+
var script = {
|
|
14
|
+
name: 'GlFormFieldValidator',
|
|
15
|
+
props: {
|
|
16
|
+
value: {
|
|
17
|
+
required: true,
|
|
18
|
+
// ESLint requires "validator" or "type". Any kind of value is valid.
|
|
19
|
+
validator: () => true
|
|
20
|
+
},
|
|
21
|
+
validators: {
|
|
22
|
+
type: Array,
|
|
23
|
+
required: false,
|
|
24
|
+
default: () => []
|
|
25
|
+
},
|
|
26
|
+
shouldValidate: {
|
|
27
|
+
type: Boolean,
|
|
28
|
+
required: false,
|
|
29
|
+
default: false
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
computed: {
|
|
33
|
+
invalidFeedback() {
|
|
34
|
+
if (!this.shouldValidate) {
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
const result = this.validators.reduce((acc, validateFn) => {
|
|
38
|
+
// If we already have an invalid message, let's just use that one.
|
|
39
|
+
if (acc) {
|
|
40
|
+
return acc;
|
|
41
|
+
}
|
|
42
|
+
return validateFn(this.value);
|
|
43
|
+
}, '');
|
|
44
|
+
|
|
45
|
+
// Force falsey string for type consistency.
|
|
46
|
+
return result || '';
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
watch: {
|
|
50
|
+
invalidFeedback(newVal) {
|
|
51
|
+
this.$emit('update', newVal);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
render() {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/* script */
|
|
60
|
+
const __vue_script__ = script;
|
|
61
|
+
|
|
62
|
+
/* template */
|
|
63
|
+
|
|
64
|
+
/* style */
|
|
65
|
+
const __vue_inject_styles__ = undefined;
|
|
66
|
+
/* scoped */
|
|
67
|
+
const __vue_scope_id__ = undefined;
|
|
68
|
+
/* module identifier */
|
|
69
|
+
const __vue_module_identifier__ = undefined;
|
|
70
|
+
/* functional template */
|
|
71
|
+
const __vue_is_functional_template__ = undefined;
|
|
72
|
+
/* style inject */
|
|
73
|
+
|
|
74
|
+
/* style inject SSR */
|
|
75
|
+
|
|
76
|
+
/* style inject shadow dom */
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
const __vue_component__ = __vue_normalize__(
|
|
81
|
+
{},
|
|
82
|
+
__vue_inject_styles__,
|
|
83
|
+
__vue_script__,
|
|
84
|
+
__vue_scope_id__,
|
|
85
|
+
__vue_is_functional_template__,
|
|
86
|
+
__vue_module_identifier__,
|
|
87
|
+
false,
|
|
88
|
+
undefined,
|
|
89
|
+
undefined,
|
|
90
|
+
undefined
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
export default __vue_component__;
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import isFunction from 'lodash/isFunction';
|
|
2
|
+
import mapValues from 'lodash/mapValues';
|
|
3
|
+
import uniqueId from 'lodash/uniqueId';
|
|
4
|
+
import GlFormGroup from '../form_group/form_group';
|
|
5
|
+
import GlFormInput from '../form_input/form_input';
|
|
6
|
+
import GlFormFieldValidator from './form_field_validator';
|
|
7
|
+
import __vue_normalize__ from 'vue-runtime-helpers/dist/normalize-component.js';
|
|
8
|
+
|
|
9
|
+
var script = {
|
|
10
|
+
name: 'GlFormFields',
|
|
11
|
+
components: {
|
|
12
|
+
GlFormGroup,
|
|
13
|
+
GlFormInput,
|
|
14
|
+
GlFormFieldValidator
|
|
15
|
+
},
|
|
16
|
+
model: {
|
|
17
|
+
prop: 'values',
|
|
18
|
+
event: 'input'
|
|
19
|
+
},
|
|
20
|
+
props: {
|
|
21
|
+
/**
|
|
22
|
+
* Object of keys to FieldDefinitions.
|
|
23
|
+
* The shape of the keys will be the same for `values` and what's emitted by the `input` event.
|
|
24
|
+
*
|
|
25
|
+
* @typedef {object} FieldDefinition
|
|
26
|
+
* @template TValue=string
|
|
27
|
+
* @property {string} label - Label text to show for this field.
|
|
28
|
+
* @property {undefined | Object} inputAttrs - Properties that are passed to the actual input for this field.
|
|
29
|
+
* @property {undefined | function(string): TValue} mapValue - Function that maps the inputted string value to the field's actual value (e.g. a Number).
|
|
30
|
+
* @property {undefined | Array<function(TValue): string | undefined>=} validators - Collection of validator functions.
|
|
31
|
+
*
|
|
32
|
+
* @type {{ [key: string]: FieldDefinition }}
|
|
33
|
+
*/
|
|
34
|
+
fields: {
|
|
35
|
+
type: Object,
|
|
36
|
+
required: true
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* The current value for each field, by key.
|
|
40
|
+
* Keys should match between `values` and `fields`.
|
|
41
|
+
*/
|
|
42
|
+
values: {
|
|
43
|
+
type: Object,
|
|
44
|
+
required: true
|
|
45
|
+
},
|
|
46
|
+
/**
|
|
47
|
+
* The id of the form element to handle "submit" listening.
|
|
48
|
+
*/
|
|
49
|
+
formId: {
|
|
50
|
+
type: String,
|
|
51
|
+
required: true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
data() {
|
|
55
|
+
return {
|
|
56
|
+
fieldDirtyStatuses: {},
|
|
57
|
+
fieldValidations: {}
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
computed: {
|
|
61
|
+
formElement() {
|
|
62
|
+
return document.getElementById(this.formId);
|
|
63
|
+
},
|
|
64
|
+
fieldValidationProps() {
|
|
65
|
+
return mapValues(this.fields, (_, fieldName) => {
|
|
66
|
+
const invalidFeedback = this.fieldValidations[fieldName] || '';
|
|
67
|
+
return {
|
|
68
|
+
invalidFeedback,
|
|
69
|
+
state: invalidFeedback ? false : null
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
fieldValues() {
|
|
74
|
+
return mapValues(this.fields, (_, fieldName) => {
|
|
75
|
+
if (fieldName in this.values) {
|
|
76
|
+
return this.values[fieldName];
|
|
77
|
+
}
|
|
78
|
+
return this.getMappedValue(fieldName, undefined);
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
fieldNames() {
|
|
82
|
+
return Object.keys(this.fields);
|
|
83
|
+
},
|
|
84
|
+
fieldsToRender() {
|
|
85
|
+
return mapValues(this.fields, (field, fieldName) => {
|
|
86
|
+
const id = uniqueId('gl-form-field-');
|
|
87
|
+
const scopedSlotName = `input(${fieldName})`;
|
|
88
|
+
const hasScopedSlot = this.$scopedSlots[scopedSlotName];
|
|
89
|
+
const scopedSlotAttrs = hasScopedSlot && {
|
|
90
|
+
value: this.fieldValues[fieldName],
|
|
91
|
+
input: val => this.onFieldInput(fieldName, val),
|
|
92
|
+
blur: () => this.onFieldBlur(fieldName),
|
|
93
|
+
validation: this.fieldValidationProps[fieldName],
|
|
94
|
+
id
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
...field,
|
|
98
|
+
id,
|
|
99
|
+
label: field.label || fieldName,
|
|
100
|
+
scopedSlotName,
|
|
101
|
+
scopedSlotAttrs
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
mounted() {
|
|
107
|
+
var _this$formElement;
|
|
108
|
+
// why: We emit initial values as a convenience so that `v-model="values"` can be easily initialized.
|
|
109
|
+
this.$emit('input', this.fieldValues);
|
|
110
|
+
(_this$formElement = this.formElement) === null || _this$formElement === void 0 ? void 0 : _this$formElement.addEventListener('submit', this.onFormSubmission);
|
|
111
|
+
},
|
|
112
|
+
destroyed() {
|
|
113
|
+
var _this$formElement2;
|
|
114
|
+
(_this$formElement2 = this.formElement) === null || _this$formElement2 === void 0 ? void 0 : _this$formElement2.removeEventListener('submit', this.onFormSubmission);
|
|
115
|
+
},
|
|
116
|
+
methods: {
|
|
117
|
+
setFieldDirty(fieldName) {
|
|
118
|
+
this.$set(this.fieldDirtyStatuses, fieldName, true);
|
|
119
|
+
},
|
|
120
|
+
setAllFieldsDirty() {
|
|
121
|
+
this.fieldNames.forEach(fieldName => this.setFieldDirty(fieldName));
|
|
122
|
+
},
|
|
123
|
+
hasAllFieldsValid() {
|
|
124
|
+
// note: Only check "fieldNames" since "fields" could have changed since the life of "fieldValidations"
|
|
125
|
+
return this.fieldNames.every(fieldName => !this.fieldValidations[fieldName]);
|
|
126
|
+
},
|
|
127
|
+
async checkBeforeSubmission() {
|
|
128
|
+
this.setAllFieldsDirty();
|
|
129
|
+
await this.$nextTick();
|
|
130
|
+
return this.hasAllFieldsValid();
|
|
131
|
+
},
|
|
132
|
+
getMappedValue(fieldName, val) {
|
|
133
|
+
const field = this.fields[fieldName];
|
|
134
|
+
if (isFunction(field === null || field === void 0 ? void 0 : field.mapValue)) {
|
|
135
|
+
return field.mapValue(val);
|
|
136
|
+
}
|
|
137
|
+
return val;
|
|
138
|
+
},
|
|
139
|
+
onFieldValidationUpdate(fieldName, invalidFeedback) {
|
|
140
|
+
this.$set(this.fieldValidations, fieldName, invalidFeedback);
|
|
141
|
+
},
|
|
142
|
+
onFieldBlur(fieldName) {
|
|
143
|
+
this.setFieldDirty(fieldName);
|
|
144
|
+
},
|
|
145
|
+
onFieldInput(fieldName, inputValue) {
|
|
146
|
+
const val = this.getMappedValue(fieldName, inputValue);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Emitted when any of the form values change. Used by `v-model`.
|
|
150
|
+
*/
|
|
151
|
+
this.$emit('input', {
|
|
152
|
+
...this.values,
|
|
153
|
+
[fieldName]: val
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Emitted when a form input emits the `input` event.
|
|
158
|
+
*/
|
|
159
|
+
this.$emit('input-field', {
|
|
160
|
+
name: fieldName,
|
|
161
|
+
value: val
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
async onFormSubmission(e) {
|
|
165
|
+
e.preventDefault();
|
|
166
|
+
const isValid = await this.checkBeforeSubmission();
|
|
167
|
+
if (isValid) {
|
|
168
|
+
/**
|
|
169
|
+
* Emitted when the form is submitted and all of the form fields are valid.
|
|
170
|
+
*/
|
|
171
|
+
this.$emit('submit', e);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/* script */
|
|
178
|
+
const __vue_script__ = script;
|
|
179
|
+
|
|
180
|
+
/* template */
|
|
181
|
+
var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',_vm._l((_vm.fieldsToRender),function(field,fieldName){return _c('gl-form-group',{key:fieldName,attrs:{"label":field.label,"label-for":field.id,"invalid-feedback":_vm.fieldValidationProps[fieldName].invalidFeedback,"state":_vm.fieldValidationProps[fieldName].state}},[_c('gl-form-field-validator',{attrs:{"value":_vm.fieldValues[fieldName],"validators":field.validators,"should-validate":_vm.fieldDirtyStatuses[fieldName]},on:{"update":function($event){return _vm.onFieldValidationUpdate(fieldName, $event)}}}),_vm._v(" "),(field.scopedSlotAttrs)?[_vm._t(field.scopedSlotName,null,null,field.scopedSlotAttrs)]:[_c('gl-form-input',_vm._b({attrs:{"id":field.id,"value":_vm.fieldValues[fieldName],"state":_vm.fieldValidationProps[fieldName].state},on:{"input":function($event){return _vm.onFieldInput(fieldName, $event)},"blur":function($event){return _vm.onFieldBlur(fieldName)}}},'gl-form-input',field.inputAttrs,false))]],2)}),1)};
|
|
182
|
+
var __vue_staticRenderFns__ = [];
|
|
183
|
+
|
|
184
|
+
/* style */
|
|
185
|
+
const __vue_inject_styles__ = undefined;
|
|
186
|
+
/* scoped */
|
|
187
|
+
const __vue_scope_id__ = undefined;
|
|
188
|
+
/* module identifier */
|
|
189
|
+
const __vue_module_identifier__ = undefined;
|
|
190
|
+
/* functional template */
|
|
191
|
+
const __vue_is_functional_template__ = false;
|
|
192
|
+
/* style inject */
|
|
193
|
+
|
|
194
|
+
/* style inject SSR */
|
|
195
|
+
|
|
196
|
+
/* style inject shadow dom */
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
const __vue_component__ = __vue_normalize__(
|
|
201
|
+
{ render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
|
|
202
|
+
__vue_inject_styles__,
|
|
203
|
+
__vue_script__,
|
|
204
|
+
__vue_scope_id__,
|
|
205
|
+
__vue_is_functional_template__,
|
|
206
|
+
__vue_module_identifier__,
|
|
207
|
+
false,
|
|
208
|
+
undefined,
|
|
209
|
+
undefined,
|
|
210
|
+
undefined
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
export default __vue_component__;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// This contains core mapping behavior (like mapping to native JavaScript types)
|
|
2
|
+
// and**should not** contain domain-specific mappers.
|
|
3
|
+
//
|
|
4
|
+
// ```
|
|
5
|
+
// // Good
|
|
6
|
+
// export const mapToBoolean = ...
|
|
7
|
+
//
|
|
8
|
+
// // Bad
|
|
9
|
+
// export const mapToApolloCacheWidget = ...
|
|
10
|
+
// ```
|
|
11
|
+
const mapToNumber = x => Number(x || 0);
|
|
12
|
+
|
|
13
|
+
export { mapToNumber };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// This contains core validating behavior and **should not** contain
|
|
2
|
+
// domain-specific validations.
|
|
3
|
+
//
|
|
4
|
+
// Look to what's allowed in HTML attributes as a good basis for what belongs here
|
|
5
|
+
//
|
|
6
|
+
// ```
|
|
7
|
+
// // Good
|
|
8
|
+
// export const required = ...
|
|
9
|
+
//
|
|
10
|
+
// // Bad
|
|
11
|
+
// export const projectPathIsUnique = ...
|
|
12
|
+
// ```
|
|
13
|
+
const factory = (failMessage, isValid) => val => !isValid(val) ? failMessage : '';
|
|
14
|
+
const required = failMessage => factory(failMessage, val => val !== '' && val !== null && val !== undefined);
|
|
15
|
+
|
|
16
|
+
export { factory, required };
|
|
@@ -145,7 +145,7 @@ var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=
|
|
|
145
145
|
'gl-toggle': true,
|
|
146
146
|
'is-checked': _vm.value,
|
|
147
147
|
'is-disabled': _vm.disabled,
|
|
148
|
-
},attrs:{"role":"switch","aria-checked":_vm.isChecked,"aria-labelledby":_vm.labelId,"aria-describedby":_vm.helpId,"type":"button"},on:{"click":function($event){$event.preventDefault();return _vm.toggleFeature.apply(null, arguments)}}},[(_vm.isLoading)?_c('gl-loading-icon',{staticClass:"toggle-loading",attrs:{"color":"light"}}):_c('span',{class:{ 'toggle-icon': true, disabled: _vm.disabled }},[_c('gl-icon',{attrs:{"name":_vm.icon,"size":16}})],1)],1),_vm._v(" "),(_vm.shouldRenderHelp)?_c('span',{staticClass:"gl-help-label",attrs:{"id":_vm.helpId,"data-testid":"toggle-help"}},[_vm._t("help",function(){return [_vm._v(_vm._s(_vm.help))]})],2):_vm._e()])};
|
|
148
|
+
},attrs:{"role":"switch","aria-checked":_vm.isChecked,"aria-labelledby":_vm.labelId,"aria-describedby":_vm.helpId,"aria-disabled":_vm.disabled,"type":"button"},on:{"click":function($event){$event.preventDefault();return _vm.toggleFeature.apply(null, arguments)}}},[(_vm.isLoading)?_c('gl-loading-icon',{staticClass:"toggle-loading",attrs:{"color":"light"}}):_c('span',{class:{ 'toggle-icon': true, disabled: _vm.disabled }},[_c('gl-icon',{attrs:{"name":_vm.icon,"size":16}})],1)],1),_vm._v(" "),(_vm.shouldRenderHelp)?_c('span',{staticClass:"gl-help-label",attrs:{"id":_vm.helpId,"data-testid":"toggle-help"}},[_vm._t("help",function(){return [_vm._v(_vm._s(_vm.help))]})],2):_vm._e()])};
|
|
149
149
|
var __vue_staticRenderFns__ = [];
|
|
150
150
|
|
|
151
151
|
/* style */
|
package/dist/index.js
CHANGED
|
@@ -37,6 +37,7 @@ export { default as GlFormRadioGroup } from './components/base/form/form_radio_g
|
|
|
37
37
|
export { default as GlFormSelect } from './components/base/form/form_select/form_select';
|
|
38
38
|
export { default as GlFormTextarea } from './components/base/form/form_textarea/form_textarea';
|
|
39
39
|
export { default as GlFormGroup } from './components/base/form/form_group/form_group';
|
|
40
|
+
export { default as GlFormFields } from './components/base/form/form_fields/form_fields';
|
|
40
41
|
export { default as GlSearchBoxByType } from './components/base/search_box_by_type/search_box_by_type';
|
|
41
42
|
export { default as GlSearchBoxByClick } from './components/base/search_box_by_click/search_box_by_click';
|
|
42
43
|
export { default as GlDropdownItem } from './components/base/dropdown/dropdown_item';
|
package/dist/utils.js
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
1
|
export { GlBreakpointInstance, breakpoints } from './utils/breakpoints';
|
|
2
|
+
import * as validators from './components/base/form/form_fields/validators';
|
|
3
|
+
export { validators as formValidators };
|
|
4
|
+
import * as mappers from './components/base/form/form_fields/mappers';
|
|
5
|
+
export { mappers as formMappers };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gitlab/ui",
|
|
3
|
-
"version": "62.
|
|
3
|
+
"version": "62.6.0",
|
|
4
4
|
"description": "GitLab UI Components",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -83,8 +83,8 @@
|
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
85
|
"@arkweid/lefthook": "0.7.7",
|
|
86
|
-
"@babel/core": "^7.21.
|
|
87
|
-
"@babel/preset-env": "^7.21.
|
|
86
|
+
"@babel/core": "^7.21.5",
|
|
87
|
+
"@babel/preset-env": "^7.21.5",
|
|
88
88
|
"@babel/preset-react": "^7.18.6",
|
|
89
89
|
"@gitlab/eslint-plugin": "19.0.0",
|
|
90
90
|
"@gitlab/fonts": "^1.2.0",
|
|
@@ -116,9 +116,9 @@
|
|
|
116
116
|
"babel-loader": "^8.0.5",
|
|
117
117
|
"babel-plugin-require-context-hook": "^1.0.0",
|
|
118
118
|
"bootstrap": "4.6.2",
|
|
119
|
-
"cypress": "12.
|
|
119
|
+
"cypress": "12.11.0",
|
|
120
120
|
"emoji-regex": "^10.0.0",
|
|
121
|
-
"eslint": "8.
|
|
121
|
+
"eslint": "8.39.0",
|
|
122
122
|
"eslint-import-resolver-jest": "3.0.2",
|
|
123
123
|
"eslint-plugin-cypress": "2.13.2",
|
|
124
124
|
"eslint-plugin-storybook": "0.6.11",
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { mount } from '@vue/test-utils';
|
|
2
|
+
import GlFormFieldValidator from './form_field_validator.vue';
|
|
3
|
+
import { required } from './validators';
|
|
4
|
+
|
|
5
|
+
const TEST_VALUE = 'root';
|
|
6
|
+
const TEST_MESSAGE = 'this is a required field';
|
|
7
|
+
|
|
8
|
+
describe('GlFormFieldValidators', () => {
|
|
9
|
+
let wrapper;
|
|
10
|
+
|
|
11
|
+
const createWrapper = (props = {}) => {
|
|
12
|
+
wrapper = mount(GlFormFieldValidator, {
|
|
13
|
+
propsData: {
|
|
14
|
+
value: TEST_VALUE,
|
|
15
|
+
validators: [required(TEST_MESSAGE)],
|
|
16
|
+
shouldValidate: true,
|
|
17
|
+
...props,
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
it('should call validators with value prop', () => {
|
|
22
|
+
const validators = [jest.fn(), jest.fn(), jest.fn()];
|
|
23
|
+
|
|
24
|
+
createWrapper({ validators });
|
|
25
|
+
|
|
26
|
+
validators.forEach((validator) => expect(validator).toHaveBeenCalledWith(TEST_VALUE));
|
|
27
|
+
});
|
|
28
|
+
it('should not call validators when told not to', () => {
|
|
29
|
+
const validators = [jest.fn(), jest.fn(), jest.fn()];
|
|
30
|
+
|
|
31
|
+
createWrapper({ validators, shouldValidate: false });
|
|
32
|
+
|
|
33
|
+
validators.forEach((validator) => expect(validator).not.toHaveBeenCalledWith(TEST_VALUE));
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should not render anything ever forever', () => {
|
|
37
|
+
createWrapper();
|
|
38
|
+
|
|
39
|
+
expect(wrapper.html()).toBe('');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should emit update with result of validator', async () => {
|
|
43
|
+
createWrapper();
|
|
44
|
+
|
|
45
|
+
expect(wrapper.emitted('update')).toBeUndefined();
|
|
46
|
+
|
|
47
|
+
await wrapper.setProps({ value: '' });
|
|
48
|
+
|
|
49
|
+
expect(wrapper.emitted('update')).toEqual([[TEST_MESSAGE]]);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/**
|
|
3
|
+
* FormFieldValidator
|
|
4
|
+
*
|
|
5
|
+
* This is an internal component which is used to watch on specific field/value
|
|
6
|
+
* pairs and emits changes to `invalidFeedback`.
|
|
7
|
+
*
|
|
8
|
+
* **why:** Without this separate component, *any* change to *any* value
|
|
9
|
+
* was causing *all* validators to run. A separate renderless component
|
|
10
|
+
* helps us isolate this logic *and* react only to what we need to.
|
|
11
|
+
*/
|
|
12
|
+
export default {
|
|
13
|
+
name: 'GlFormFieldValidator',
|
|
14
|
+
props: {
|
|
15
|
+
value: {
|
|
16
|
+
required: true,
|
|
17
|
+
// ESLint requires "validator" or "type". Any kind of value is valid.
|
|
18
|
+
validator: () => true,
|
|
19
|
+
},
|
|
20
|
+
validators: {
|
|
21
|
+
type: Array,
|
|
22
|
+
required: false,
|
|
23
|
+
default: () => [],
|
|
24
|
+
},
|
|
25
|
+
shouldValidate: {
|
|
26
|
+
type: Boolean,
|
|
27
|
+
required: false,
|
|
28
|
+
default: false,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
computed: {
|
|
32
|
+
invalidFeedback() {
|
|
33
|
+
if (!this.shouldValidate) {
|
|
34
|
+
return '';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const result = this.validators.reduce((acc, validateFn) => {
|
|
38
|
+
// If we already have an invalid message, let's just use that one.
|
|
39
|
+
if (acc) {
|
|
40
|
+
return acc;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return validateFn(this.value);
|
|
44
|
+
}, '');
|
|
45
|
+
|
|
46
|
+
// Force falsey string for type consistency.
|
|
47
|
+
return result || '';
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
watch: {
|
|
51
|
+
invalidFeedback(newVal) {
|
|
52
|
+
this.$emit('update', newVal);
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
render() {
|
|
56
|
+
return null;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
</script>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
## Usage
|
|
2
|
+
|
|
3
|
+
`GlFormFields` provides form builder functionality for ease of building simple
|
|
4
|
+
forms out of other GitLab UI form components.
|
|
5
|
+
|
|
6
|
+
For a code example, look at the story. It covers usage of `mapValue`, `validators`,
|
|
7
|
+
custom form elements, and `inputAttrs`.
|
|
8
|
+
|
|
9
|
+
## Fields type
|
|
10
|
+
|
|
11
|
+
Each value of `fields` prop is expected to be a `FieldDefinition`. See below for the shape of this type:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
interface FieldDefinition<TValue> {
|
|
15
|
+
// Label text to show for this field.
|
|
16
|
+
label: string;
|
|
17
|
+
|
|
18
|
+
// Collection of validator functions
|
|
19
|
+
validators?: Array<(value: TValue) => string | undefined>;
|
|
20
|
+
|
|
21
|
+
// Function that maps the inputted string value to the field's actual value
|
|
22
|
+
// (e.g. a Number).
|
|
23
|
+
mapValue?: (input: string) => TValue;
|
|
24
|
+
|
|
25
|
+
// Properties that are passed to the actual input for this field.
|
|
26
|
+
inputAttrs?: {};
|
|
27
|
+
}
|
|
28
|
+
```
|