@antify/ui 2.4.3 → 2.4.4

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.
@@ -121,7 +121,7 @@ watch(isOpen, () => {
121
121
  focusedDropDownItem.value =
122
122
  typeof _modelValue.value === 'string' || typeof _modelValue.value === 'number' ? _modelValue.value :
123
123
  Array.isArray(_modelValue.value) ? _modelValue.value[0] :
124
- props.options[0].value;
124
+ (props.options[0].value || null);
125
125
  } else {
126
126
  focusedDropDownItem.value = null;
127
127
  }
@@ -138,6 +138,56 @@ onUnmounted(() => {
138
138
  reference.value?.removeEventListener('keydown', onKeyDownDropDown);
139
139
  });
140
140
 
141
+ /**
142
+ * Get the next focusable select option.
143
+ * It skips group labels and goes to beginning of the list if it reaches the end.
144
+ *
145
+ * @param currentOptionIndex
146
+ */
147
+ function getNextFocusableSelectOption(currentOptionIndex: number): number | null {
148
+ for (let i = currentOptionIndex + 1; i < props.options.length; i++) {
149
+ if (!props.options[i].isGroupLabel) {
150
+ return i;
151
+ }
152
+ }
153
+
154
+ // If no next option is found, return the first option, but make sure
155
+ // to skip group labels again.
156
+ for (let i = 0; i < props.options.length; i++) {
157
+ if (!props.options[i].isGroupLabel) {
158
+ return i;
159
+ }
160
+ }
161
+
162
+ // Seems that the option list is empty or all options are group labels.
163
+ return null;
164
+ }
165
+
166
+ /**
167
+ * Get the prev focusable select option.
168
+ * It skips group labels and goes to end of the list if it reaches the end.
169
+ *
170
+ * @param currentOptionIndex
171
+ */
172
+ function getPrevFocusableSelectOption(currentOptionIndex: number): number | null {
173
+ for (let i = currentOptionIndex - 1; i >= 0; i--) {
174
+ if (!props.options[i].isGroupLabel) {
175
+ return i;
176
+ }
177
+ }
178
+
179
+ // If no previous option is found, return the last option, but make sure
180
+ // to skip group labels again.
181
+ for (let i = props.options.length - 1; i >= 0; i--) {
182
+ if (!props.options[i].isGroupLabel) {
183
+ return i;
184
+ }
185
+ }
186
+
187
+ // Seems that the option list is empty or all options are group labels.
188
+ return null;
189
+ }
190
+
141
191
  function onKeyDownDropDown(e: KeyboardEvent) {
142
192
  if (e.key === 'Enter') {
143
193
  if (props.closeOnEnter) {
@@ -160,12 +210,10 @@ function onKeyDownDropDown(e: KeyboardEvent) {
160
210
  isOpen.value = true;
161
211
 
162
212
  const index = props.options.findIndex(option => option.value === focusedDropDownItem.value);
163
- const option = props.options[index + 1];
213
+ const nextOptionIndex = getNextFocusableSelectOption(index);
164
214
 
165
- if (index === -1) {
166
- focusedDropDownItem.value = props.options[0].value;
167
- } else if (option !== undefined) {
168
- focusedDropDownItem.value = option.value;
215
+ if (nextOptionIndex !== null) {
216
+ focusedDropDownItem.value = props.options[nextOptionIndex].value || null;
169
217
  }
170
218
  }
171
219
 
@@ -174,10 +222,10 @@ function onKeyDownDropDown(e: KeyboardEvent) {
174
222
  isOpen.value = true;
175
223
 
176
224
  const index = props.options.findIndex(option => option.value === focusedDropDownItem.value);
177
- const option = props.options[index - 1];
225
+ const prevOptionIndex = getPrevFocusableSelectOption(index);
178
226
 
179
- if (option !== undefined) {
180
- focusedDropDownItem.value = option.value;
227
+ if (prevOptionIndex !== null) {
228
+ focusedDropDownItem.value = props.options[prevOptionIndex].value || null;
181
229
  }
182
230
  }
183
231
 
@@ -187,6 +235,10 @@ function onKeyDownDropDown(e: KeyboardEvent) {
187
235
  }
188
236
 
189
237
  function getActiveDropDownItemClasses(option: SelectOption) {
238
+ if (option.isGroupLabel) {
239
+ return {};
240
+ }
241
+
190
242
  const variants: Record<InputState, string> = {
191
243
  [InputState.base]: '!bg-base-100',
192
244
  [InputState.success]: 'bg-success-200',
@@ -201,16 +253,21 @@ function getActiveDropDownItemClasses(option: SelectOption) {
201
253
  } : {};
202
254
  }
203
255
 
204
- function onClickDropDownItem(e: MouseEvent, value: string | number | null) {
256
+ function onClickDropDownItem(e: MouseEvent, option: SelectOption) {
205
257
  e.preventDefault();
258
+
259
+ if (option.isGroupLabel) {
260
+ return;
261
+ }
262
+
206
263
  reference.value?.focus();
207
264
 
208
265
  if (props.closeOnSelectItem) {
209
266
  isOpen.value = false;
210
267
  }
211
268
 
212
- emit('selectElement', value);
213
- _modelValue.value = value;
269
+ emit('selectElement', option.value);
270
+ _modelValue.value = option.value || null;
214
271
  }
215
272
 
216
273
  watch(_modelValue, (val) => {
@@ -236,9 +293,13 @@ watch(_modelValue, (val) => {
236
293
  <div
237
294
  v-for="(option, index) in options"
238
295
  :key="`option-${index}`"
239
- :class="{...dropDownItemClasses, ...getActiveDropDownItemClasses(option)}"
240
- @mousedown="(e) => onClickDropDownItem(e, option.value)"
241
- @mouseover="() => focusedDropDownItem = option.value"
296
+ :class="{
297
+ ...dropDownItemClasses,
298
+ ...getActiveDropDownItemClasses(option),
299
+ 'font-bold': option.isGroupLabel,
300
+ }"
301
+ @mousedown="(e) => onClickDropDownItem(e, option)"
302
+ @mouseover="() => focusedDropDownItem = !option.isGroupLabel && option.value !== undefined ? option.value : null"
242
303
  >
243
304
  <slot
244
305
  name="contentLeft"
@@ -9,6 +9,7 @@ export declare const manyOptions: Story;
9
9
  export declare const nullable: Story;
10
10
  export declare const skeleton: Story;
11
11
  export declare const disabled: Story;
12
+ export declare const grouped: Story;
12
13
  export declare const withPlaceholder: Story;
13
14
  export declare const ellipsisText: Story;
14
15
  export declare const summary: Story;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.withPlaceholder = exports.summary = exports.skeleton = exports.nullable = exports.manyOptions = exports.ellipsisText = exports.disabled = exports.default = exports.WithSlots = exports.Docs = void 0;
6
+ exports.withPlaceholder = exports.summary = exports.skeleton = exports.nullable = exports.manyOptions = exports.grouped = exports.ellipsisText = exports.disabled = exports.default = exports.WithSlots = exports.Docs = void 0;
7
7
  var _Size = require("../../../enums/Size.enum");
8
8
  var _AntSelect = _interopRequireDefault(require("../AntSelect.vue"));
9
9
  var _AntIcon = _interopRequireDefault(require("../../AntIcon.vue"));
@@ -230,6 +230,45 @@ const disabled = exports.disabled = {
230
230
  disabled: true
231
231
  }
232
232
  };
233
+ const grouped = exports.grouped = {
234
+ render: (args, ctx) => ({
235
+ // @ts-ignore
236
+ components: Docs.render(args, ctx).components,
237
+ // @ts-ignore
238
+ setup: Docs.render(args, ctx).setup,
239
+ template: `
240
+ <AntSelect v-bind="args" v-model="modelValue"/>
241
+ `
242
+ }),
243
+ args: {
244
+ label: "Label",
245
+ description: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod",
246
+ options: [{
247
+ label: "Group 1",
248
+ isGroupLabel: true
249
+ }, {
250
+ label: "Option 1.1",
251
+ value: 1,
252
+ imgUrl: "https://fastly.picsum.photos/id/972/200/200.jpg?hmac=TeAvfwW2T9YMpPW-sWWTeAiseERb12wSeR3mYzuG6TE"
253
+ }, {
254
+ label: "Option 2.1",
255
+ value: 2,
256
+ imgUrl: "https://fastly.picsum.photos/id/314/200/200.jpg?hmac=bCAc2iO5ovLPrvwDQV31aBPS13QTyv33ut2H2wY4QXU"
257
+ }, {
258
+ label: "Group 2",
259
+ isGroupLabel: true
260
+ }, {
261
+ label: "Option 2.1",
262
+ value: 3,
263
+ imgUrl: "https://fastly.picsum.photos/id/972/200/200.jpg?hmac=TeAvfwW2T9YMpPW-sWWTeAiseERb12wSeR3mYzuG6TE"
264
+ }, {
265
+ label: "Option 2.2",
266
+ value: 4,
267
+ imgUrl: "https://fastly.picsum.photos/id/165/200/200.jpg?hmac=tQGrY9pm5ze9soSsZ5CNBt87zqnHfFwdPv_khau12Sw"
268
+ }],
269
+ modelValue: 1
270
+ }
271
+ };
233
272
  const withPlaceholder = exports.withPlaceholder = {
234
273
  render: Docs.render,
235
274
  args: {
@@ -245,6 +245,52 @@ export const disabled = {
245
245
  disabled: true
246
246
  }
247
247
  };
248
+ export const grouped = {
249
+ render: (args, ctx) => ({
250
+ // @ts-ignore
251
+ components: Docs.render(args, ctx).components,
252
+ // @ts-ignore
253
+ setup: Docs.render(args, ctx).setup,
254
+ template: `
255
+ <AntSelect v-bind="args" v-model="modelValue"/>
256
+ `
257
+ }),
258
+ args: {
259
+ label: "Label",
260
+ description: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod",
261
+ options: [
262
+ {
263
+ label: "Group 1",
264
+ isGroupLabel: true
265
+ },
266
+ {
267
+ label: "Option 1.1",
268
+ value: 1,
269
+ imgUrl: "https://fastly.picsum.photos/id/972/200/200.jpg?hmac=TeAvfwW2T9YMpPW-sWWTeAiseERb12wSeR3mYzuG6TE"
270
+ },
271
+ {
272
+ label: "Option 2.1",
273
+ value: 2,
274
+ imgUrl: "https://fastly.picsum.photos/id/314/200/200.jpg?hmac=bCAc2iO5ovLPrvwDQV31aBPS13QTyv33ut2H2wY4QXU"
275
+ },
276
+ {
277
+ label: "Group 2",
278
+ isGroupLabel: true
279
+ },
280
+ {
281
+ label: "Option 2.1",
282
+ value: 3,
283
+ imgUrl: "https://fastly.picsum.photos/id/972/200/200.jpg?hmac=TeAvfwW2T9YMpPW-sWWTeAiseERb12wSeR3mYzuG6TE"
284
+ },
285
+ {
286
+ label: "Option 2.2",
287
+ value: 4,
288
+ imgUrl: "https://fastly.picsum.photos/id/165/200/200.jpg?hmac=tQGrY9pm5ze9soSsZ5CNBt87zqnHfFwdPv_khau12Sw"
289
+ }
290
+ ],
291
+ modelValue: 1
292
+ }
293
+ };
248
294
  export const withPlaceholder = {
249
295
  render: Docs.render,
250
296
  args: {
@@ -1,4 +1,5 @@
1
1
  export type SelectOption = {
2
2
  label: string;
3
- value: string | number;
3
+ value?: string | number;
4
+ isGroupLabel?: boolean;
4
5
  } & Record<string, unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antify/ui",
3
- "version": "2.4.3",
3
+ "version": "2.4.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {