Skip to content

Commit 21a912e

Browse files
authored
Merge pull request #411 from negezor/perf
perf: Improved performance and reduced memory consumption
2 parents bd603ce + 9b7ff14 commit 21a912e

20 files changed

+252
-392
lines changed

dist/multiselect.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/multiselect.js

Lines changed: 79 additions & 129 deletions
Large diffs are not rendered by default.

dist/multiselect.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/multiselect.mjs

Lines changed: 79 additions & 129 deletions
Large diffs are not rendered by default.

src/composables/useA11y.js

Lines changed: 31 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { toRefs, onMounted, ref, computed } from 'vue'
2+
import toRef from './../utils/toRef'
23

34
export default function useA11y (props, context, dep)
45
{
@@ -19,72 +20,50 @@ export default function useA11y (props, context, dep)
1920

2021
// ============== COMPUTED ==============
2122

22-
const ariaAssist = computed(() => {
23-
let texts = []
23+
const ariaAssist = toRef(() => (
24+
`${id.value ? id.value + '-' : ''}assist`
25+
))
2426

25-
if (id && id.value) {
26-
texts.push(id.value)
27-
}
28-
29-
texts.push('assist')
30-
31-
return texts.join('-')
32-
})
33-
34-
const ariaControls = computed(() => {
35-
let texts = []
36-
37-
if (id && id.value) {
38-
texts.push(id.value)
39-
}
40-
41-
texts.push('multiselect-options')
42-
43-
return texts.join('-')
44-
})
45-
46-
const ariaActiveDescendant = computed(() => {
47-
let texts = []
48-
49-
if (id && id.value) {
50-
texts.push(id.value)
51-
}
27+
const ariaControls = toRef(() => (
28+
`${id.value ? id.value + '-' : ''}multiselect-options`
29+
))
5230

31+
const ariaActiveDescendant = toRef(() => {
5332
if (pointer.value) {
54-
texts.push(pointer.value.group ? 'multiselect-group' : 'multiselect-option')
33+
let texts = id.value
34+
? `${id.value}-`
35+
: '';
36+
37+
texts += `${pointer.value.group ? 'multiselect-group' : 'multiselect-option'}-`
5538

56-
texts.push(pointer.value.group ? pointer.value.index : pointer.value[valueProp.value])
39+
texts += pointer.value.group ? pointer.value.index : pointer.value[valueProp.value]
5740

58-
return texts.join('-')
41+
return texts
5942
}
6043
})
6144

62-
63-
64-
const ariaPlaceholder = computed(() => {
45+
const ariaPlaceholder = toRef(() => {
6546
return placeholder.value
6647
})
6748

68-
const ariaMultiselectable = computed(() => {
49+
const ariaMultiselectable = toRef(() => {
6950
return mode.value !== 'single'
7051
})
7152

7253
const ariaLabel = computed(() => {
73-
let ariaLabel = ''
74-
7554
if (mode.value === 'single' && hasSelected.value) {
76-
ariaLabel += iv.value[labelProp.value]
55+
return iv.value[labelProp.value]
7756
}
7857

7958
if (mode.value === 'multiple' && hasSelected.value) {
80-
ariaLabel += multipleLabelText.value
59+
return multipleLabelText.value
8160
}
8261

8362
if (mode.value === 'tags' && hasSelected.value) {
84-
ariaLabel += iv.value.map(v => v[labelProp.value]).join(', ')
63+
return iv.value.map(v => v[labelProp.value]).join(', ')
8564
}
8665

87-
return ariaLabel
66+
return ''
8867
})
8968

9069
const arias = computed(() => {
@@ -107,59 +86,25 @@ export default function useA11y (props, context, dep)
10786

10887
// =============== METHODS ==============
10988

110-
const ariaOptionId = (option) => {
111-
let texts = []
112-
113-
if (id && id.value) {
114-
texts.push(id.value)
115-
}
116-
117-
texts.push('multiselect-option')
118-
119-
texts.push(option[valueProp.value])
120-
121-
return texts.join('-')
122-
}
123-
124-
const ariaGroupId = (option) => {
125-
let texts = []
126-
127-
if (id && id.value) {
128-
texts.push(id.value)
129-
}
130-
131-
texts.push('multiselect-group')
89+
const ariaOptionId = (option) => (
90+
`${id.value ? id.value + '-' : ''}multiselect-option-${option[valueProp.value]}`
91+
)
13292

133-
texts.push(option.index)
93+
const ariaGroupId = (option) => (
94+
`${id.value ? id.value + '-' : ''}multiselect-group-${option.index}`
95+
)
13496

135-
return texts.join('-')
136-
}
137-
138-
const ariaOptionLabel = (label) => {
139-
let texts = []
140-
141-
texts.push(label)
142-
143-
return texts.join(' ')
144-
}
97+
const ariaOptionLabel = (label) => `${label}`
14598

146-
const ariaGroupLabel = (label) => {
147-
let texts = []
99+
const ariaGroupLabel = (label) => `${label}`
148100

149-
texts.push(label)
150-
151-
return texts.join(' ')
152-
}
153-
154-
const ariaTagLabel = (label) => {
155-
return `${label} ❎`
156-
}
101+
const ariaTagLabel = (label) => `${label} ❎`
157102

158103
// =============== HOOKS ================
159104

160105
onMounted(() => {
161106
/* istanbul ignore next */
162-
if (id && id.value && document && document.querySelector) {
107+
if (id.value && document && document.querySelector) {
163108
let forTag = document.querySelector(`[for="${id.value}"]`)
164109
label.value = forTag ? forTag.innerText : null
165110
}

src/composables/useClasses.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { computed, toRefs } from 'vue'
2+
import toRef from './../utils/toRef'
23

34
export default function useClasses (props, context, dependencies)
45
{const {
@@ -19,7 +20,7 @@ export default function useClasses (props, context, dependencies)
1920

2021
// ============== COMPUTED ==============
2122

22-
const classes = computed(() => ({
23+
const classes = toRef(() => ({
2324
container: 'multiselect',
2425
containerDisabled: 'is-disabled',
2526
containerOpen: 'is-open',
@@ -76,7 +77,7 @@ export default function useClasses (props, context, dependencies)
7677
...classes_.value,
7778
}))
7879

79-
const showDropdown = computed(() => {
80+
const showDropdown = toRef(() => {
8081
return !!(isOpen.value && showOptions.value && (!resolving.value || (resolving.value && fo.value.length)))
8182
})
8283

src/composables/useDropdown.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { ref, toRefs, getCurrentInstance, computed, onMounted, onBeforeUnmount, nextTick } from 'vue'
1+
import { ref, toRefs, getCurrentInstance, onMounted, onBeforeUnmount, nextTick } from 'vue'
22
import { createPopper } from '@popperjs/core/lib/popper-lite'
33
import preventOverflow from '@popperjs/core/lib/modifiers/preventOverflow'
44
import flip from '@popperjs/core/lib/modifiers/flip'
5+
import toRef from './../utils/toRef'
56

67
export default function useDropdown (props, context, dep)
78
{
@@ -22,11 +23,11 @@ export default function useDropdown (props, context, dep)
2223

2324
// ============== COMPUTED ==============
2425

25-
const appended = computed(() => {
26+
const appended = toRef(() => {
2627
return appendTo.value || appendToBody.value
2728
})
2829

29-
const placement = computed(() => {
30+
const placement = toRef(() => {
3031
return (openDirection.value === 'top' && forcedPlacement.value === 'bottom') ||
3132
(openDirection.value === 'bottom' && forcedPlacement.value !== 'top')
3233
? 'bottom'

src/composables/useKeyboard.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { toRefs, computed, getCurrentInstance } from 'vue'
1+
import { toRefs, getCurrentInstance } from 'vue'
2+
import toRef from './../utils/toRef'
23

34
export default function useKeyboard (props, context, dep)
45
{
@@ -32,12 +33,12 @@ export default function useKeyboard (props, context, dep)
3233
// ============== COMPUTED ==============
3334

3435
// no export
35-
const createOption = computed(() => {
36+
const createOption = toRef(() => {
3637
return createTag.value || createOption_.value || false
3738
})
3839

3940
// no export
40-
const addOptionOn = computed(() => {
41+
const addOptionOn = toRef(() => {
4142
if (addTagOn.value !== undefined) {
4243
return addTagOn.value
4344
}

src/composables/useMultiselect.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ref, toRefs, computed, nextTick } from 'vue'
1+
import { ref, toRefs } from 'vue'
2+
import toRef from './../utils/toRef'
23

34
export default function useMultiselect (props, context, dep)
45
{
@@ -22,7 +23,7 @@ export default function useMultiselect (props, context, dep)
2223

2324
// ============== COMPUTED ==============
2425

25-
const tabindex = computed(() => {
26+
const tabindex = toRef(() => {
2627
return searchable.value || disabled.value ? -1 : 0
2728
})
2829

src/composables/useOptions.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import normalize from './../utils/normalize'
33
import isObject from './../utils/isObject'
44
import isNullish from './../utils/isNullish'
55
import arraysEqual from './../utils/arraysEqual'
6+
import toRef from './../utils/toRef'
67

78
export default function useOptions (props, context, dep)
89
{
@@ -56,12 +57,12 @@ export default function useOptions (props, context, dep)
5657
})
5758

5859
// no export
59-
const createOption = computed(() => {
60+
const createOption = toRef(() => {
6061
return createTag.value || createOption_.value || false
6162
})
6263

6364
// no export
64-
const appendNewOption = computed(() => {
65+
const appendNewOption = toRef(() => {
6566
if (appendNewTag.value !== undefined) {
6667
return appendNewTag.value
6768
} else if (appendNewOption_.value !== undefined) {
@@ -197,17 +198,17 @@ export default function useOptions (props, context, dep)
197198
})
198199

199200
const multipleLabelText = computed(() => {
200-
return multipleLabel !== undefined && multipleLabel.value !== undefined
201+
return multipleLabel.value !== undefined
201202
? multipleLabel.value(iv.value, $this)
202203
: (iv.value && iv.value.length > 1 ? `${iv.value.length} options selected` : `1 option selected`)
203204
})
204205

205-
const noOptions = computed(() => {
206+
const noOptions = toRef(() => {
206207
return !eo.value.length && !resolving.value && !createdOption.value.length
207208
})
208209

209210

210-
const noResults = computed(() => {
211+
const noResults = toRef(() => {
211212
return eo.value.length > 0 && fo.value.length == 0 && ((search.value && groupped.value) || !groupped.value)
212213
})
213214

@@ -234,7 +235,7 @@ export default function useOptions (props, context, dep)
234235
})
235236

236237
// no export
237-
const nullValue = computed(() => {
238+
const nullValue = toRef(() => {
238239
switch (mode.value) {
239240
case 'single':
240241
return null
@@ -245,7 +246,7 @@ export default function useOptions (props, context, dep)
245246
}
246247
})
247248

248-
const busy = computed(() => {
249+
const busy = toRef(() => {
249250
return loading.value || resolving.value
250251
})
251252

@@ -349,7 +350,7 @@ export default function useOptions (props, context, dep)
349350
return
350351
}
351352

352-
if (onCreate && onCreate.value && !isSelected(option) && option.__CREATE__) {
353+
if (onCreate.value && !isSelected(option) && option.__CREATE__) {
353354
option = { ...option }
354355
delete option.__CREATE__
355356

src/composables/usePointerAction.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { toRefs, watch, nextTick, computed } from 'vue'
2+
import toRef from './../utils/toRef'
23

34
export default function usePointer (props, context, dep)
45
{
@@ -32,11 +33,11 @@ export default function usePointer (props, context, dep)
3233
return fg.value.filter(g => !g[disabledProp.value])
3334
})
3435

35-
const canPointGroups = computed(() => {
36+
const canPointGroups = toRef(() => {
3637
return mode.value !== 'single' && groupSelect.value
3738
})
3839

39-
const isPointerGroup = computed(() => {
40+
const isPointerGroup = toRef(() => {
4041
return pointer.value && pointer.value.group
4142
})
4243

src/composables/useRefs.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { ref } from 'vue'
1+
import { shallowRef } from 'vue'
22

33
export default function useRefs (props, context, dep)
44
{
55
// ================ DATA ================
66

7-
const multiselect = ref(null)
7+
const multiselect = shallowRef(null)
88

9-
const wrapper = ref(null)
9+
const wrapper = shallowRef(null)
1010

11-
const tags = ref(null)
11+
const tags = shallowRef(null)
1212

13-
const input = ref(null)
13+
const input = shallowRef(null)
1414

15-
const dropdown = ref(null)
15+
const dropdown = shallowRef(null)
1616

1717
return {
1818
multiselect,

src/composables/useScroll.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { toRefs, watch, nextTick, onMounted, ref, computed } from 'vue'
1+
import { toRefs, watch, nextTick, onMounted, ref, shallowRef, computed } from 'vue'
2+
import toRef from '../utils/toRef'
23

34
export default function useScroll (props, context, dep)
45
{
@@ -19,11 +20,11 @@ export default function useScroll (props, context, dep)
1920
// no export
2021
const observer = ref(null)
2122

22-
const infiniteLoader = ref(null)
23+
const infiniteLoader = shallowRef(null)
2324

2425
// ============== COMPUTED ==============
2526

26-
const hasMore = computed(() => {
27+
const hasMore = toRef(() => {
2728
return offset.value < pfo.value.length
2829
})
2930

0 commit comments

Comments
 (0)