Skip to content

Commit

Permalink
feat: added selectAll method #172
Browse files Browse the repository at this point in the history
  • Loading branch information
adamberecz committed Dec 16, 2021
1 parent c43edd3 commit 2e2135f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
9 changes: 9 additions & 0 deletions src/composables/useOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -620,6 +628,7 @@ export default function useOptions (props, context, dep)
select,
deselect,
remove,
selectAll,
clear,
isSelected,
isDisabled,
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/composables/useOptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 2e2135f

Please sign in to comment.