1- import { ref , toRefs , computed , watch , nextTick } from 'composition-api'
1+ import { ref , toRefs , computed , watch , nextTick , getCurrentInstance } from 'composition-api'
22import normalize from './../utils/normalize'
33import isObject from './../utils/isObject'
44import isNullish from './../utils/isNullish'
@@ -14,6 +14,8 @@ export default function useOptions (props, context, dep)
1414 groupOptions, groupHideEmpty, groupSelect,
1515 } = toRefs ( props )
1616
17+ const $this = getCurrentInstance ( ) . proxy
18+
1719 // ============ DEPENDENCIES ============
1820
1921 const iv = dep . iv
@@ -39,6 +41,9 @@ export default function useOptions (props, context, dep)
3941
4042 const resolving = ref ( false )
4143
44+ // no export
45+ const searchWatcher = ref ( null )
46+
4247 // ============== COMPUTED ==============
4348
4449 // no export
@@ -73,7 +78,7 @@ export default function useOptions (props, context, dep)
7378
7479 return eo
7580 } else {
76- let eo = optionsToArray ( ro . value || [ ] )
81+ let eo = optionsToArray ( ro . value || /* istanbul ignore next */ [ ] )
7782
7883 if ( ap . value . length ) {
7984 eo = eo . concat ( ap . value )
@@ -131,7 +136,7 @@ export default function useOptions (props, context, dep)
131136
132137 const multipleLabelText = computed ( ( ) => {
133138 return multipleLabel !== undefined && multipleLabel . value !== undefined
134- ? multipleLabel . value ( iv . value )
139+ ? multipleLabel . value ( iv . value , $this )
135140 : ( iv . value && iv . value . length > 1 ? `${ iv . value . length } options selected` : `1 option selected` )
136141 } )
137142
@@ -497,14 +502,24 @@ export default function useOptions (props, context, dep)
497502 const resolveOptions = ( callback ) => {
498503 resolving . value = true
499504
500- options . value ( search . value ) . then ( ( response ) => {
501- ro . value = response
505+ return new Promise ( ( resolve , reject ) => {
506+ options . value ( search . value , $this ) . then ( ( response ) => {
507+ ro . value = response || [ ]
502508
503- if ( typeof callback == 'function' ) {
504- callback ( response )
505- }
509+ if ( typeof callback == 'function' ) {
510+ callback ( response )
511+ }
506512
507- resolving . value = false
513+ resolving . value = false
514+ } ) . catch ( ( e ) => {
515+ console . error ( e )
516+
517+ ro . value = [ ]
518+
519+ resolving . value = false
520+ } ) . finally ( ( ) => {
521+ resolve ( )
522+ } )
508523 } )
509524 }
510525
@@ -515,21 +530,31 @@ export default function useOptions (props, context, dep)
515530 }
516531
517532 if ( mode . value === 'single' ) {
518- let newLabel = getOption ( iv . value [ valueProp . value ] ) [ label . value ]
533+ let option = getOption ( iv . value [ valueProp . value ] )
534+
535+ /* istanbul ignore else */
536+ if ( option !== undefined ) {
537+ let newLabel = option [ label . value ]
519538
520- iv . value [ label . value ] = newLabel
539+ iv . value [ label . value ] = newLabel
521540
522- if ( object . value ) {
523- ev . value [ label . value ] = newLabel
541+ if ( object . value ) {
542+ ev . value [ label . value ] = newLabel
543+ }
524544 }
525545 } else {
526546 iv . value . forEach ( ( val , i ) => {
527- let newLabel = getOption ( iv . value [ i ] [ valueProp . value ] ) [ label . value ]
547+ let option = getOption ( iv . value [ i ] [ valueProp . value ] )
528548
529- iv . value [ i ] [ label . value ] = newLabel
549+ /* istanbul ignore else */
550+ if ( option !== undefined ) {
551+ let newLabel = option [ label . value ]
530552
531- if ( object . value ) {
532- ev . value [ i ] [ label . value ] = newLabel
553+ iv . value [ i ] [ label . value ] = newLabel
554+
555+ if ( object . value ) {
556+ ev . value [ i ] [ label . value ] = newLabel
557+ }
533558 }
534559 } )
535560 }
@@ -554,6 +579,37 @@ export default function useOptions (props, context, dep)
554579 return mode . value === 'single' ? getOption ( val ) || { } : val . filter ( v => ! ! getOption ( v ) ) . map ( v => getOption ( v ) )
555580 }
556581
582+ // no export
583+ const initSearchWatcher = ( ) => {
584+ searchWatcher . value = watch ( search , ( query ) => {
585+ if ( query . length < minChars . value || ! query ) {
586+ return
587+ }
588+
589+ resolving . value = true
590+
591+ if ( clearOnSearch . value ) {
592+ ro . value = [ ]
593+ }
594+ setTimeout ( ( ) => {
595+ if ( query != search . value ) {
596+ return
597+ }
598+
599+ options . value ( search . value , $this ) . then ( ( response ) => {
600+ if ( query == search . value || ! search . value ) {
601+ ro . value = response
602+ pointer . value = fo . value . filter ( o => o . disabled !== true ) [ 0 ] || null
603+ resolving . value = false
604+ }
605+ } ) . catch ( /* istanbul ignore next */ ( e ) => {
606+ console . error ( e )
607+ } )
608+ } , delay . value )
609+
610+ } , { flush : 'sync' } )
611+ }
612+
557613 // ================ HOOKS ===============
558614
559615 if ( mode . value !== 'single' && ! isNullish ( ev . value ) && ! Array . isArray ( ev . value ) ) {
@@ -576,32 +632,19 @@ export default function useOptions (props, context, dep)
576632 // ============== WATCHERS ==============
577633
578634 if ( delay . value > - 1 ) {
579- watch ( search , ( query ) => {
580- if ( query . length < minChars . value ) {
581- return
582- }
583-
584- resolving . value = true
585-
586- if ( clearOnSearch . value ) {
587- ro . value = [ ]
588- }
589- setTimeout ( ( ) => {
590- if ( query != search . value ) {
591- return
592- }
635+ initSearchWatcher ( )
636+ }
593637
594- options . value ( search . value ) . then ( ( response ) => {
595- if ( query == search . value || ! search . value ) {
596- ro . value = response
597- pointer . value = fo . value . filter ( o => o . disabled !== true ) [ 0 ] || null
598- resolving . value = false
599- }
600- } )
601- } , delay . value )
638+ watch ( delay , ( value , old ) => {
639+ /* istanbul ignore else */
640+ if ( searchWatcher . value ) {
641+ searchWatcher . value ( )
642+ }
602643
603- } , { flush : 'sync' } )
604- }
644+ if ( value >= 0 ) {
645+ initSearchWatcher ( )
646+ }
647+ } )
605648
606649 watch ( ev , ( newValue ) => {
607650 if ( isNullish ( newValue ) ) {
@@ -625,17 +668,25 @@ export default function useOptions (props, context, dep)
625668 }
626669 } , { deep : true } )
627670
628- if ( typeof props . options !== 'function' ) {
629- watch ( options , ( n , o ) => {
671+ watch ( options , ( n , o ) => {
672+ if ( typeof props . options === 'function' ) {
673+ if ( resolveOnLoad . value ) {
674+ resolveOptions ( ( ) => {
675+ if ( Object . keys ( iv . value ) . length ) {
676+ initInternalValue ( )
677+ }
678+ } )
679+ }
680+ } else {
630681 ro . value = props . options
631682
632683 if ( ! Object . keys ( iv . value ) . length ) {
633684 initInternalValue ( )
634685 }
635686
636687 refreshLabels ( )
637- } )
638- }
688+ }
689+ } )
639690
640691 return {
641692 fo,
@@ -664,5 +715,6 @@ export default function useOptions (props, context, dep)
664715 handleTagRemove,
665716 refreshOptions,
666717 resolveOptions,
718+ refreshLabels,
667719 }
668720}
0 commit comments