@gitlab/ui 78.7.0 → 78.8.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 +7 -0
- package/dist/components/base/form/form_fields/form_fields.js +26 -11
- package/dist/tokens/css/tokens.css +1 -1
- package/dist/tokens/css/tokens.dark.css +1 -1
- package/dist/tokens/js/tokens.dark.js +1 -1
- package/dist/tokens/js/tokens.js +1 -1
- package/dist/tokens/scss/_tokens.dark.scss +1 -1
- package/dist/tokens/scss/_tokens.scss +1 -1
- package/package.json +1 -1
- package/src/components/base/form/form_fields/form_fields.md +10 -0
- package/src/components/base/form/form_fields/form_fields.spec.js +86 -5
- package/src/components/base/form/form_fields/form_fields.stories.js +26 -2
- package/src/components/base/form/form_fields/form_fields.vue +63 -38
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [78.8.0](https://gitlab.com/gitlab-org/gitlab-ui/compare/v78.7.0...v78.8.0) (2024-04-04)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **GlFormFields:** add support for additional slots ([6ff8a22](https://gitlab.com/gitlab-org/gitlab-ui/commit/6ff8a22adbbfa0df280482cf9681f7b751f11e43))
|
|
7
|
+
|
|
1
8
|
# [78.7.0](https://gitlab.com/gitlab-org/gitlab-ui/compare/v78.6.1...v78.7.0) (2024-04-03)
|
|
2
9
|
|
|
3
10
|
|
|
@@ -95,21 +95,33 @@ var script = {
|
|
|
95
95
|
fieldsToRender() {
|
|
96
96
|
return mapValues(this.fields, (field, fieldName) => {
|
|
97
97
|
const id = uniqueId('gl-form-field-');
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
98
|
+
const inputSlotName = `input(${fieldName})`;
|
|
99
|
+
const groupPassthroughSlotName = `group(${fieldName})-`;
|
|
100
|
+
const afterSlotName = `after(${fieldName})`;
|
|
101
|
+
const inputSlot = {
|
|
102
|
+
slotName: inputSlotName,
|
|
103
|
+
attrs: {
|
|
104
|
+
value: this.fieldValues[fieldName],
|
|
105
|
+
input: val => this.onFieldInput(fieldName, val),
|
|
106
|
+
blur: () => this.onFieldBlur(fieldName),
|
|
107
|
+
validation: this.fieldValidationProps[fieldName],
|
|
108
|
+
id
|
|
109
|
+
}
|
|
106
110
|
};
|
|
111
|
+
const groupPassthroughSlots = Object.keys(this.$scopedSlots).filter(slotName => slotName.startsWith(groupPassthroughSlotName)).map(slotName => {
|
|
112
|
+
const childSlotName = slotName.replace(groupPassthroughSlotName, '');
|
|
113
|
+
return {
|
|
114
|
+
slotName,
|
|
115
|
+
childSlotName
|
|
116
|
+
};
|
|
117
|
+
});
|
|
107
118
|
return {
|
|
108
119
|
...field,
|
|
109
120
|
id,
|
|
110
121
|
label: field.label || fieldName,
|
|
111
|
-
|
|
112
|
-
|
|
122
|
+
inputSlot,
|
|
123
|
+
groupPassthroughSlots,
|
|
124
|
+
afterSlotName
|
|
113
125
|
};
|
|
114
126
|
});
|
|
115
127
|
}
|
|
@@ -189,7 +201,10 @@ var script = {
|
|
|
189
201
|
const __vue_script__ = script;
|
|
190
202
|
|
|
191
203
|
/* template */
|
|
192
|
-
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',_vm._b({
|
|
204
|
+
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',_vm._b({attrs:{"label":field.label,"label-for":field.id,"invalid-feedback":_vm.fieldValidationProps[fieldName].invalidFeedback,"state":_vm.fieldValidationProps[fieldName].state},scopedSlots:_vm._u([_vm._l((field.groupPassthroughSlots),function(ref){
|
|
205
|
+
var slotName = ref.slotName;
|
|
206
|
+
var childSlotName = ref.childSlotName;
|
|
207
|
+
return {key:childSlotName,fn:function(){return [_vm._t(slotName)]},proxy:true}})],null,true)},'gl-form-group',field.groupAttrs,false),[_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(" "),_vm._v(" "),_vm._t(field.inputSlot.slotName,function(){return [_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))]},null,field.inputSlot.attrs)],2),_vm._v(" "),_vm._t(field.afterSlotName)]})],2)};
|
|
193
208
|
var __vue_staticRenderFns__ = [];
|
|
194
209
|
|
|
195
210
|
/* style */
|
package/dist/tokens/js/tokens.js
CHANGED
package/package.json
CHANGED
|
@@ -29,3 +29,13 @@ interface FieldDefinition<TValue> {
|
|
|
29
29
|
groupAttrs?: {};
|
|
30
30
|
}
|
|
31
31
|
```
|
|
32
|
+
|
|
33
|
+
## Slots
|
|
34
|
+
|
|
35
|
+
| Name | Description |
|
|
36
|
+
| ------ | ------ |
|
|
37
|
+
| `input(<fieldName>)` | Used to render components other than `GlFormInput`. |
|
|
38
|
+
| `group(<fieldName>)-label` | Used for `label` slot on `GlFormGroup` of a specific field. |
|
|
39
|
+
| `group(<fieldName>)-description` | Used for `description` slot on `GlFormGroup` of a specific field. |
|
|
40
|
+
| `group(<fieldName>)-label-description` | Used for `label-description` slot on `GlFormGroup` of a specific field. |
|
|
41
|
+
| `after(<fieldName>)` | Used to render content after `GlFormGroup` of a specific field. |
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import cloneDeep from 'lodash/cloneDeep';
|
|
2
2
|
import mapValues from 'lodash/mapValues';
|
|
3
3
|
import { nextTick } from 'vue';
|
|
4
|
-
import { shallowMount } from '@vue/test-utils';
|
|
4
|
+
import { shallowMount, mount } from '@vue/test-utils';
|
|
5
5
|
import GlFormGroup from '../form_group/form_group.vue';
|
|
6
6
|
import GlInput from '../form_input/form_input.vue';
|
|
7
7
|
import GlFormFields from './form_fields.vue';
|
|
@@ -43,11 +43,11 @@ describe('GlFormFields', () => {
|
|
|
43
43
|
let wrapper;
|
|
44
44
|
|
|
45
45
|
// region: factory --------------------------------------------------
|
|
46
|
-
const createComponent = (props = {}, options = {}) => {
|
|
46
|
+
const createComponent = (props = {}, options = {}, mountFn = shallowMount) => {
|
|
47
47
|
// why: Clone the constant so Vue doesn't turn it reactive
|
|
48
48
|
const fields = cloneDeep(TEST_FIELDS);
|
|
49
49
|
|
|
50
|
-
wrapper =
|
|
50
|
+
wrapper = mountFn(GlFormFields, {
|
|
51
51
|
propsData: {
|
|
52
52
|
fields,
|
|
53
53
|
values: {},
|
|
@@ -56,6 +56,7 @@ describe('GlFormFields', () => {
|
|
|
56
56
|
},
|
|
57
57
|
stubs: {
|
|
58
58
|
GlFormFieldValidator,
|
|
59
|
+
...options.stubs,
|
|
59
60
|
},
|
|
60
61
|
...options,
|
|
61
62
|
});
|
|
@@ -81,7 +82,7 @@ describe('GlFormFields', () => {
|
|
|
81
82
|
// eslint-disable-next-line unicorn/no-array-callback-reference
|
|
82
83
|
const findFormGroupsAsData = () => findFormGroups().map(mapFormGroupToData);
|
|
83
84
|
const findFormGroupFromLabel = (label) =>
|
|
84
|
-
wrapper.findAllComponents(GlFormGroup).wrappers.find((x) => x.
|
|
85
|
+
wrapper.findAllComponents(GlFormGroup).wrappers.find((x) => x.vm.$attrs.label === label);
|
|
85
86
|
const findInputFromLabel = (label) => findFormGroupFromLabel(label).findComponent(GlInput);
|
|
86
87
|
const findCustomInputFromLabel = (label) =>
|
|
87
88
|
findFormGroupFromLabel(label).find('[data-testid="test-custom-input"]');
|
|
@@ -363,7 +364,7 @@ describe('GlFormFields', () => {
|
|
|
363
364
|
});
|
|
364
365
|
});
|
|
365
366
|
|
|
366
|
-
describe('with scoped slot', () => {
|
|
367
|
+
describe('with input scoped slot', () => {
|
|
367
368
|
beforeEach(() => {
|
|
368
369
|
createComponent(
|
|
369
370
|
{
|
|
@@ -415,6 +416,86 @@ describe('GlFormFields', () => {
|
|
|
415
416
|
});
|
|
416
417
|
});
|
|
417
418
|
|
|
419
|
+
describe('with form group scoped slots', () => {
|
|
420
|
+
it('renders description slot', () => {
|
|
421
|
+
createComponent(
|
|
422
|
+
{},
|
|
423
|
+
{
|
|
424
|
+
scopedSlots: {
|
|
425
|
+
'group(username)-description': '<div data-testid="group-description-slot"></div>',
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
mount
|
|
429
|
+
);
|
|
430
|
+
|
|
431
|
+
expect(
|
|
432
|
+
findFormGroupFromLabel(TEST_FIELDS.username.label)
|
|
433
|
+
.find('[data-testid="group-description-slot"]')
|
|
434
|
+
.exists()
|
|
435
|
+
).toBe(true);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('renders label slot', () => {
|
|
439
|
+
createComponent(
|
|
440
|
+
{},
|
|
441
|
+
{
|
|
442
|
+
scopedSlots: {
|
|
443
|
+
'group(username)-label': `<div data-testid="group-label-slot">${TEST_FIELDS.username.label}</div>`,
|
|
444
|
+
},
|
|
445
|
+
},
|
|
446
|
+
mount
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
expect(
|
|
450
|
+
wrapper
|
|
451
|
+
.findAllComponents(GlFormGroup)
|
|
452
|
+
.wrappers.find((x) => x.text().includes(TEST_FIELDS.username.label))
|
|
453
|
+
.find('[data-testid="group-label-slot"]')
|
|
454
|
+
.exists()
|
|
455
|
+
).toBe(true);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it('renders label description slot', () => {
|
|
459
|
+
createComponent(
|
|
460
|
+
{},
|
|
461
|
+
{
|
|
462
|
+
scopedSlots: {
|
|
463
|
+
'group(username)-label-description':
|
|
464
|
+
'<div data-testid="group-label-description-slot"></div>',
|
|
465
|
+
},
|
|
466
|
+
},
|
|
467
|
+
mount
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
expect(
|
|
471
|
+
findFormGroupFromLabel(TEST_FIELDS.username.label)
|
|
472
|
+
.find('[data-testid="group-label-description-slot"]')
|
|
473
|
+
.exists()
|
|
474
|
+
).toBe(true);
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
describe('with after slot', () => {
|
|
479
|
+
beforeEach(() => {
|
|
480
|
+
createComponent(
|
|
481
|
+
{},
|
|
482
|
+
{
|
|
483
|
+
scopedSlots: {
|
|
484
|
+
'after(username)': '<div data-testid="after-slot"></div>',
|
|
485
|
+
},
|
|
486
|
+
}
|
|
487
|
+
);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
it('renders after slot', () => {
|
|
491
|
+
expect(
|
|
492
|
+
findFormGroupFromLabel(TEST_FIELDS.username.label).element.nextElementSibling.getAttribute(
|
|
493
|
+
'data-testid'
|
|
494
|
+
)
|
|
495
|
+
).toBe('after-slot');
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
|
|
418
499
|
// why: We have to do some manual reactivity to optimize how often we call
|
|
419
500
|
// field validators. Let's test that here.
|
|
420
501
|
describe('validation performance', () => {
|
|
@@ -2,7 +2,9 @@ import uniqueId from 'lodash/uniqueId';
|
|
|
2
2
|
import omit from 'lodash/omit';
|
|
3
3
|
import GlModal from '../../modal/modal.vue';
|
|
4
4
|
import GlButton from '../../button/button.vue';
|
|
5
|
+
import GlAlert from '../../alert/alert.vue';
|
|
5
6
|
import GlListbox from '../../new_dropdowns/listbox/listbox.vue';
|
|
7
|
+
import GlIcon from '../../icon/icon.vue';
|
|
6
8
|
import { setStoryTimeout } from '../../../../utils/test_utils';
|
|
7
9
|
import GlFormFields from './form_fields.vue';
|
|
8
10
|
import readme from './form_fields.md';
|
|
@@ -11,7 +13,7 @@ import { mapToNumber } from './mappers';
|
|
|
11
13
|
|
|
12
14
|
const Template = () => ({
|
|
13
15
|
ITEMS: ['Pizza', 'Keyboards', 'Guitars', 'Rocket ships'].map((text) => ({ text, value: text })),
|
|
14
|
-
components: { GlFormFields, GlButton, GlModal, GlListbox },
|
|
16
|
+
components: { GlFormFields, GlButton, GlModal, GlListbox, GlAlert, GlIcon },
|
|
15
17
|
data() {
|
|
16
18
|
return {
|
|
17
19
|
// why: We declare fields here so that we can test what binding the
|
|
@@ -62,6 +64,13 @@ const Template = () => ({
|
|
|
62
64
|
// JSON doesn't allow undefined values
|
|
63
65
|
return JSON.stringify(this.values, (key, value) => (value === undefined ? null : value), 2);
|
|
64
66
|
},
|
|
67
|
+
favoriteItemToggleText() {
|
|
68
|
+
if (!this.formValues.favoriteItem) {
|
|
69
|
+
return 'Select an item';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return null;
|
|
73
|
+
},
|
|
65
74
|
},
|
|
66
75
|
methods: {
|
|
67
76
|
onInputField({ name }) {
|
|
@@ -93,11 +102,26 @@ const Template = () => ({
|
|
|
93
102
|
<h3>Fields</h3>
|
|
94
103
|
<form :id="testFormId" @submit.prevent>
|
|
95
104
|
<gl-form-fields :fields="fields" v-model="formValues" :form-id="testFormId" :server-validations="serverValidations" @input-field="onInputField" @submit="onSubmit">
|
|
105
|
+
<template #group(confirmPassword)-label>
|
|
106
|
+
<div class="gl-display-flex gl-align-items-center gl-column-gap-3">
|
|
107
|
+
<span>Confirm Password</span>
|
|
108
|
+
<gl-icon name="information-o" />
|
|
109
|
+
</div>
|
|
110
|
+
</template>
|
|
111
|
+
<template #group(confirmPassword)-description>
|
|
112
|
+
Description using <code>group(confirmPassword)-description</code> slot
|
|
113
|
+
</template>
|
|
114
|
+
<template #after(confirmPassword)>
|
|
115
|
+
<gl-alert class="gl-mb-5" :dismissible="false">Custom content using <code>after(confirmPassword)</code> slot</gl-alert>
|
|
116
|
+
</template>
|
|
96
117
|
<template #input(custom)="{ id, value, input, blur }">
|
|
97
118
|
<button :id="id" @click="input(value + 1)" @blur="blur" type="button">{{value}}</button>
|
|
98
119
|
</template>
|
|
99
120
|
<template #input(favoriteItem)="{ id, value, input, blur }">
|
|
100
|
-
<gl-listbox :id="id" :items="$options.ITEMS" :selected="value" @select="input" @hidden="blur" />
|
|
121
|
+
<gl-listbox :id="id" :items="$options.ITEMS" :selected="value" :toggle-text="favoriteItemToggleText" @select="input" @hidden="blur" />
|
|
122
|
+
</template>
|
|
123
|
+
<template #group(favoriteItem)-label-description>
|
|
124
|
+
Label description using <code>group(favoriteItem)-label-description</code> slot
|
|
101
125
|
</template>
|
|
102
126
|
</gl-form-fields>
|
|
103
127
|
<gl-button type="submit" category="primary" :loading="loading">Submit</gl-button>
|
|
@@ -99,22 +99,39 @@ export default {
|
|
|
99
99
|
return mapValues(this.fields, (field, fieldName) => {
|
|
100
100
|
const id = uniqueId('gl-form-field-');
|
|
101
101
|
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
102
|
+
const inputSlotName = `input(${fieldName})`;
|
|
103
|
+
const groupPassthroughSlotName = `group(${fieldName})-`;
|
|
104
|
+
const afterSlotName = `after(${fieldName})`;
|
|
105
|
+
|
|
106
|
+
const inputSlot = {
|
|
107
|
+
slotName: inputSlotName,
|
|
108
|
+
attrs: {
|
|
109
|
+
value: this.fieldValues[fieldName],
|
|
110
|
+
input: (val) => this.onFieldInput(fieldName, val),
|
|
111
|
+
blur: () => this.onFieldBlur(fieldName),
|
|
112
|
+
validation: this.fieldValidationProps[fieldName],
|
|
113
|
+
id,
|
|
114
|
+
},
|
|
110
115
|
};
|
|
111
116
|
|
|
117
|
+
const groupPassthroughSlots = Object.keys(this.$scopedSlots)
|
|
118
|
+
.filter((slotName) => slotName.startsWith(groupPassthroughSlotName))
|
|
119
|
+
.map((slotName) => {
|
|
120
|
+
const childSlotName = slotName.replace(groupPassthroughSlotName, '');
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
slotName,
|
|
124
|
+
childSlotName,
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
|
|
112
128
|
return {
|
|
113
129
|
...field,
|
|
114
130
|
id,
|
|
115
131
|
label: field.label || fieldName,
|
|
116
|
-
|
|
117
|
-
|
|
132
|
+
inputSlot,
|
|
133
|
+
groupPassthroughSlots,
|
|
134
|
+
afterSlotName,
|
|
118
135
|
};
|
|
119
136
|
});
|
|
120
137
|
},
|
|
@@ -198,35 +215,43 @@ export default {
|
|
|
198
215
|
|
|
199
216
|
<template>
|
|
200
217
|
<div>
|
|
201
|
-
<
|
|
202
|
-
v-for
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
:value="fieldValues[fieldName]"
|
|
212
|
-
:validators="field.validators"
|
|
213
|
-
:should-validate="fieldDirtyStatuses[fieldName]"
|
|
214
|
-
@update="onFieldValidationUpdate(fieldName, $event)"
|
|
215
|
-
/>
|
|
216
|
-
<template v-if="field.scopedSlotAttrs">
|
|
217
|
-
<!-- @slot scoped slot that can be used for components other than `GlFormInput`. The name of the slot is `input(<fieldName>)`. -->
|
|
218
|
-
<slot :name="field.scopedSlotName" v-bind="field.scopedSlotAttrs"></slot>
|
|
219
|
-
</template>
|
|
220
|
-
<template v-else>
|
|
221
|
-
<gl-form-input
|
|
222
|
-
:id="field.id"
|
|
218
|
+
<template v-for="(field, fieldName) in fieldsToRender">
|
|
219
|
+
<!-- eslint-disable-next-line vue/valid-v-for -->
|
|
220
|
+
<gl-form-group
|
|
221
|
+
v-bind="field.groupAttrs"
|
|
222
|
+
:label="field.label"
|
|
223
|
+
:label-for="field.id"
|
|
224
|
+
:invalid-feedback="fieldValidationProps[fieldName].invalidFeedback"
|
|
225
|
+
:state="fieldValidationProps[fieldName].state"
|
|
226
|
+
>
|
|
227
|
+
<gl-form-field-validator
|
|
223
228
|
:value="fieldValues[fieldName]"
|
|
224
|
-
:
|
|
225
|
-
|
|
226
|
-
@
|
|
227
|
-
@blur="onFieldBlur(fieldName)"
|
|
229
|
+
:validators="field.validators"
|
|
230
|
+
:should-validate="fieldDirtyStatuses[fieldName]"
|
|
231
|
+
@update="onFieldValidationUpdate(fieldName, $event)"
|
|
228
232
|
/>
|
|
229
|
-
|
|
230
|
-
|
|
233
|
+
|
|
234
|
+
<template
|
|
235
|
+
v-for="{ slotName, childSlotName } in field.groupPassthroughSlots"
|
|
236
|
+
#[childSlotName]
|
|
237
|
+
>
|
|
238
|
+
<!-- @slot Can be used to pass slots to `GlFormGroup`. -->
|
|
239
|
+
<slot :name="slotName"></slot>
|
|
240
|
+
</template>
|
|
241
|
+
<!-- @slot Scoped slot that can be used for components other than `GlFormInput`. The name of the slot is `input(<fieldName>)`. -->
|
|
242
|
+
<slot :name="field.inputSlot.slotName" v-bind="field.inputSlot.attrs">
|
|
243
|
+
<gl-form-input
|
|
244
|
+
:id="field.id"
|
|
245
|
+
:value="fieldValues[fieldName]"
|
|
246
|
+
:state="fieldValidationProps[fieldName].state"
|
|
247
|
+
v-bind="field.inputAttrs"
|
|
248
|
+
@input="onFieldInput(fieldName, $event)"
|
|
249
|
+
@blur="onFieldBlur(fieldName)"
|
|
250
|
+
/>
|
|
251
|
+
</slot>
|
|
252
|
+
</gl-form-group>
|
|
253
|
+
<!-- @slot Can be used to add content the form group of a field. The name of the slot is `after(<fieldName>)`.-->
|
|
254
|
+
<slot :name="field.afterSlotName"></slot>
|
|
255
|
+
</template>
|
|
231
256
|
</div>
|
|
232
257
|
</template>
|