diff --git a/README.md b/README.md index cd601a9..81262d4 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,7 @@ Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](htt | **select** | `option` | Selects an option based on its value. | | **deselect** | `option` | Deselects an option based on its value. | | **remove** | `option` | Alias for `deselect`. | +| **selectAll** | | Selects all options if mode is `tags` or `multiple`. | | **clear** | | Deselects all selected options. | | **clearSearch** | | Clears current search query. | | **refreshOptions** | `callback` | Refreshes async options list. | diff --git a/src/composables/useOptions.js b/src/composables/useOptions.js index c2c59d9..18c009f 100644 --- a/src/composables/useOptions.js +++ b/src/composables/useOptions.js @@ -373,6 +373,14 @@ export default function useOptions (props, context, dep) } } + const selectAll = () => { + if (mode.value === 'single') { + return + } + + select(fo.value) + } + // no export const areAllEnabledSelected = (options) => { return options.find(o => !isSelected(o) && !o.disabled) === undefined @@ -620,6 +628,7 @@ export default function useOptions (props, context, dep) select, deselect, remove, + selectAll, clear, isSelected, isDisabled, diff --git a/tests/unit/composables/useOptions.spec.js b/tests/unit/composables/useOptions.spec.js index 8845de1..66805fe 100644 --- a/tests/unit/composables/useOptions.spec.js +++ b/tests/unit/composables/useOptions.spec.js @@ -1291,6 +1291,49 @@ describe('useOptions', () => { }) }) + describe('selectAll', () => { + it('should do nothing when single', async () => { + let select = createSelect({ + options: [1,2,3], + value: null, + }) + + select.vm.selectAll() + + await nextTick() + + expect(getValue(select)).toStrictEqual(null) + }) + + it('should select all when multiple', async () => { + let select = createSelect({ + mode: 'multiple', + options: [1,2,3], + value: [], + }) + + select.vm.selectAll() + + await nextTick() + + expect(getValue(select)).toStrictEqual([1,2,3]) + }) + + it('should select all when tags', async () => { + let select = createSelect({ + mode: 'tags', + options: [1,2,3], + value: [], + }) + + select.vm.selectAll() + + await nextTick() + + expect(getValue(select)).toStrictEqual([1,2,3]) + }) + }) + describe('isSelected', () => { it('should be true if value equals option value when object false and single', () => { let select = createSelect({