@gitlab/ui 62.5.2 → 62.7.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 CHANGED
@@ -1,3 +1,17 @@
1
+ # [62.7.0](https://gitlab.com/gitlab-org/gitlab-ui/compare/v62.6.0...v62.7.0) (2023-05-03)
2
+
3
+
4
+ ### Features
5
+
6
+ * Add row-gap utility class ([9b913ff](https://gitlab.com/gitlab-org/gitlab-ui/commit/9b913ff56590d0c077fdb886c4696309fa165236))
7
+
8
+ # [62.6.0](https://gitlab.com/gitlab-org/gitlab-ui/compare/v62.5.2...v62.6.0) (2023-05-02)
9
+
10
+
11
+ ### Features
12
+
13
+ * Add gl-form-fields component ([a17bcb6](https://gitlab.com/gitlab-org/gitlab-ui/commit/a17bcb6c8db0444dcd7f5bd799fe681891dbdcc9))
14
+
1
15
  ## [62.5.2](https://gitlab.com/gitlab-org/gitlab-ui/compare/v62.5.1...v62.5.2) (2023-05-01)
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 };