|
370 | 370 | */
|
371 | 371 | options: {
|
372 | 372 | type: Array,
|
373 |
| - default() { |
| 373 | + default () { |
374 | 374 | return []
|
375 |
| - }, |
| 375 | + } |
376 | 376 | },
|
377 | 377 |
|
378 | 378 | /**
|
|
469 | 469 | */
|
470 | 470 | getOptionLabel: {
|
471 | 471 | type: Function,
|
472 |
| - default(option) { |
| 472 | + default (option) { |
473 | 473 | if (typeof option === 'object') {
|
474 | 474 | if (this.label && option[this.label]) {
|
475 | 475 | return option[this.label]
|
476 | 476 | }
|
477 | 477 | }
|
478 |
| - return option; |
| 478 | + return option |
479 | 479 | }
|
480 | 480 | },
|
481 | 481 |
|
|
538 | 538 | */
|
539 | 539 | createOption: {
|
540 | 540 | type: Function,
|
541 |
| - default(newOption) { |
| 541 | + default (newOption) { |
542 | 542 | if (typeof this.mutableOptions[0] === 'object') {
|
543 | 543 | newOption = {[this.label]: newOption}
|
544 | 544 | }
|
|
583 | 583 | dir: {
|
584 | 584 | type: String,
|
585 | 585 | default: 'auto'
|
586 |
| - }, |
| 586 | + } |
587 | 587 | },
|
588 | 588 |
|
589 |
| - data() { |
| 589 | + data () { |
590 | 590 | return {
|
591 | 591 | search: '',
|
592 | 592 | open: false,
|
|
598 | 598 | watch: {
|
599 | 599 | /**
|
600 | 600 | * When the value prop changes, update
|
601 |
| - * the internal mutableValue. |
| 601 | + * the internal mutableValue. |
602 | 602 | * @param {mixed} val
|
603 | 603 | * @return {void}
|
604 | 604 | */
|
605 |
| - value(val) { |
606 |
| - this.mutableValue = val |
| 605 | + value (val) { |
| 606 | + this.mutableValue = val |
607 | 607 | },
|
608 | 608 |
|
609 | 609 | /**
|
|
612 | 612 | * @param {string|object} old
|
613 | 613 | * @return {void}
|
614 | 614 | */
|
615 |
| - mutableValue(val, old) { |
| 615 | + mutableValue (val, old) { |
616 | 616 | if (this.multiple) {
|
617 |
| - this.onChange ? this.onChange(val) : null |
| 617 | + if (this.onChange) this.onChange(val) |
618 | 618 | } else {
|
619 |
| - this.onChange && val !== old ? this.onChange(val) : null |
| 619 | + if (this.onChange && val !== old) this.onChange(val) |
620 | 620 | }
|
621 | 621 | },
|
622 | 622 |
|
|
626 | 626 | * @param {array} val
|
627 | 627 | * @return {void}
|
628 | 628 | */
|
629 |
| - options(val) { |
| 629 | + options (val) { |
630 | 630 | this.mutableOptions = val
|
631 | 631 | },
|
632 | 632 |
|
633 | 633 | /**
|
634 |
| - * Maybe reset the mutableValue |
| 634 | + * Maybe reset the mutableValue |
635 | 635 | * when mutableOptions change.
|
636 | 636 | * @return {[type]} [description]
|
637 | 637 | */
|
638 |
| - mutableOptions() { |
| 638 | + mutableOptions () { |
639 | 639 | if (!this.taggable && this.resetOnOptionsChange) {
|
640 |
| - this.mutableValue = this.multiple ? [] : null |
| 640 | + this.mutableValue = this.multiple ? [] : null |
641 | 641 | }
|
642 | 642 | },
|
643 | 643 |
|
644 | 644 | /**
|
645 |
| - * Always reset the mutableValue when |
| 645 | + * Always reset the mutableValue when |
646 | 646 | * the multiple prop changes.
|
647 | 647 | * @param {Boolean} val
|
648 | 648 | * @return {void}
|
649 | 649 | */
|
650 |
| - multiple(val) { |
651 |
| - this.mutableValue = val ? [] : null |
| 650 | + multiple (val) { |
| 651 | + this.mutableValue = val ? [] : null |
652 | 652 | }
|
653 | 653 | },
|
654 | 654 |
|
655 | 655 | /**
|
656 | 656 | * Clone props into mutable values,
|
657 | 657 | * attach any event listeners.
|
658 | 658 | */
|
659 |
| - created() { |
660 |
| - this.mutableValue = this.value |
| 659 | + created () { |
| 660 | + this.mutableValue = this.value |
661 | 661 | this.mutableOptions = this.options.slice(0)
|
662 |
| - this.mutableLoading = this.loading |
| 662 | + this.mutableLoading = this.loading |
663 | 663 |
|
664 | 664 | this.$on('option:created', this.maybePushTag)
|
665 | 665 | },
|
|
671 | 671 | * @param {Object|String} option
|
672 | 672 | * @return {void}
|
673 | 673 | */
|
674 |
| - select(option) { |
| 674 | + select (option) { |
675 | 675 | if (this.isOptionSelected(option)) {
|
676 | 676 | this.deselect(option)
|
677 | 677 | } else {
|
|
696 | 696 | * @param {Object|String} option
|
697 | 697 | * @return {void}
|
698 | 698 | */
|
699 |
| - deselect(option) { |
| 699 | + deselect (option) { |
700 | 700 | if (this.multiple) {
|
701 | 701 | let ref = -1
|
702 | 702 | this.mutableValue.forEach((val) => {
|
703 |
| - if (val === option || typeof val === 'object' && val[this.label] === option[this.label]) { |
| 703 | + if (val === option || (typeof val === 'object' && val[this.label] === option[this.label])) { |
704 | 704 | ref = val
|
705 | 705 | }
|
706 | 706 | })
|
707 |
| - var index = this.mutableValue.indexOf(ref) |
| 707 | + const index = this.mutableValue.indexOf(ref) |
708 | 708 | this.mutableValue.splice(index, 1)
|
709 | 709 | } else {
|
710 | 710 | this.mutableValue = null
|
|
716 | 716 | * @param {Object|String} option
|
717 | 717 | * @return {void}
|
718 | 718 | */
|
719 |
| - onAfterSelect(option) { |
| 719 | + onAfterSelect (option) { |
720 | 720 | if (this.closeOnSelect) {
|
721 | 721 | this.open = !this.open
|
722 | 722 | this.$refs.search.blur()
|
|
732 | 732 | * @param {Event} e
|
733 | 733 | * @return {void}
|
734 | 734 | */
|
735 |
| - toggleDropdown(e) { |
736 |
| - if (e.target === this.$refs.openIndicator || e.target === this.$refs.search || e.target === this.$refs.toggle || e.target === this.$el) { |
| 735 | + toggleDropdown (e) { |
| 736 | + if (e.target === this.$refs.openIndicator || e.target === this.$refs.search || |
| 737 | + e.target === this.$refs.toggle || e.target === this.$el) { |
737 | 738 | if (this.open) {
|
738 | 739 | this.$refs.search.blur() // dropdown will close on blur
|
739 | 740 | } else {
|
|
750 | 751 | * @param {Object|String} option
|
751 | 752 | * @return {Boolean} True when selected | False otherwise
|
752 | 753 | */
|
753 |
| - isOptionSelected(option) { |
| 754 | + isOptionSelected (option) { |
754 | 755 | if (this.multiple && this.mutableValue) {
|
755 | 756 | let selected = false
|
756 | 757 | this.mutableValue.forEach(opt => {
|
757 | 758 | if (typeof opt === 'object' && opt[this.label] === option[this.label]) {
|
758 | 759 | selected = true
|
759 | 760 | } else if (typeof opt === 'object' && opt[this.label] === option) {
|
760 | 761 | selected = true
|
761 |
| - } |
762 |
| - else if (opt === option) { |
| 762 | + } else if (opt === option) { |
763 | 763 | selected = true
|
764 | 764 | }
|
765 | 765 | })
|
|
774 | 774 | * Otherwise, blur the search input to close the dropdown.
|
775 | 775 | * @return {void}
|
776 | 776 | */
|
777 |
| - onEscape() { |
| 777 | + onEscape () { |
778 | 778 | if (!this.search.length) {
|
779 | 779 | this.$refs.search.blur()
|
780 | 780 | } else {
|
|
787 | 787 | * @emits {search:blur}
|
788 | 788 | * @return {void}
|
789 | 789 | */
|
790 |
| - onSearchBlur() { |
| 790 | + onSearchBlur () { |
791 | 791 | if (this.clearSearchOnBlur) {
|
792 | 792 | this.search = ''
|
793 | 793 | }
|
|
800 | 800 | * @emits {search:focus}
|
801 | 801 | * @return {void}
|
802 | 802 | */
|
803 |
| - onSearchFocus() { |
| 803 | + onSearchFocus () { |
804 | 804 | this.open = true
|
805 | 805 | this.$emit('search:focus')
|
806 | 806 | },
|
|
810 | 810 | * text in the search input, & there's tags to delete
|
811 | 811 | * @return {this.value}
|
812 | 812 | */
|
813 |
| - maybeDeleteValue() { |
| 813 | + maybeDeleteValue () { |
| 814 | + let value = null |
| 815 | +
|
814 | 816 | if (!this.$refs.search.value.length && this.mutableValue) {
|
815 |
| - return this.multiple ? this.mutableValue.pop() : this.mutableValue = null |
| 817 | + if (this.multiple) { |
| 818 | + value = this.mutableValue.pop() |
| 819 | + } else { |
| 820 | + value = this.mutableValue = null |
| 821 | + } |
816 | 822 | }
|
| 823 | +
|
| 824 | + return value |
817 | 825 | },
|
818 | 826 |
|
819 | 827 | /**
|
|
823 | 831 | * @param {Object || String} option
|
824 | 832 | * @return {boolean}
|
825 | 833 | */
|
826 |
| - optionExists(option) { |
| 834 | + optionExists (option) { |
827 | 835 | let exists = false
|
828 | 836 |
|
829 | 837 | this.mutableOptions.forEach(opt => {
|
|
844 | 852 | * @param {Object || String} option
|
845 | 853 | * @return {void}
|
846 | 854 | */
|
847 |
| - maybePushTag(option) { |
| 855 | + maybePushTag (option) { |
848 | 856 | if (this.pushTags) {
|
849 | 857 | this.mutableOptions.push(option)
|
850 | 858 | }
|
|
857 | 865 | * Classes to be output on .dropdown
|
858 | 866 | * @return {Object}
|
859 | 867 | */
|
860 |
| - dropdownClasses() { |
| 868 | + dropdownClasses () { |
861 | 869 | return {
|
862 | 870 | open: this.dropdownOpen,
|
863 | 871 | single: !this.multiple,
|
|
874 | 882 | * If search text should clear on blur
|
875 | 883 | * @return {Boolean} True when single and clearSearchOnSelect
|
876 | 884 | */
|
877 |
| - clearSearchOnBlur() { |
| 885 | + clearSearchOnBlur () { |
878 | 886 | return this.clearSearchOnSelect && !this.multiple
|
879 | 887 | },
|
880 | 888 |
|
|
883 | 891 | * search input
|
884 | 892 | * @return {Boolean} True if non empty value
|
885 | 893 | */
|
886 |
| - searching() { |
| 894 | + searching () { |
887 | 895 | return !!this.search
|
888 | 896 | },
|
889 | 897 |
|
|
892 | 900 | * dropdown menu.
|
893 | 901 | * @return {Boolean} True if open
|
894 | 902 | */
|
895 |
| - dropdownOpen() { |
| 903 | + dropdownOpen () { |
896 | 904 | return this.noDrop ? false : this.open && !this.mutableLoading
|
897 | 905 | },
|
898 | 906 |
|
|
901 | 909 | * & there is no value selected.
|
902 | 910 | * @return {String} Placeholder text
|
903 | 911 | */
|
904 |
| - searchPlaceholder() { |
| 912 | + searchPlaceholder () { |
905 | 913 | if (this.isValueEmpty && this.placeholder) {
|
906 |
| - return this.placeholder; |
| 914 | + return this.placeholder |
907 | 915 | }
|
908 | 916 | },
|
909 | 917 |
|
|
915 | 923 | *
|
916 | 924 | * @return {array}
|
917 | 925 | */
|
918 |
| - filteredOptions() { |
| 926 | + filteredOptions () { |
919 | 927 | if (!this.filterable && !this.taggable) {
|
920 | 928 | return this.mutableOptions.slice()
|
921 | 929 | }
|
|
937 | 945 | * Check if there aren't any options selected.
|
938 | 946 | * @return {Boolean}
|
939 | 947 | */
|
940 |
| - isValueEmpty() { |
| 948 | + isValueEmpty () { |
941 | 949 | if (this.mutableValue) {
|
942 | 950 | if (typeof this.mutableValue === 'object') {
|
943 | 951 | return !Object.keys(this.mutableValue).length
|
944 | 952 | }
|
945 | 953 | return !this.mutableValue.length
|
946 | 954 | }
|
947 | 955 |
|
948 |
| - return true; |
| 956 | + return true |
949 | 957 | },
|
950 | 958 |
|
951 | 959 | /**
|
952 | 960 | * Return the current value in array format.
|
953 | 961 | * @return {Array}
|
954 | 962 | */
|
955 |
| - valueAsArray() { |
| 963 | + valueAsArray () { |
956 | 964 | if (this.multiple) {
|
957 | 965 | return this.mutableValue
|
958 | 966 | } else if (this.mutableValue) {
|
|
961 | 969 |
|
962 | 970 | return []
|
963 | 971 | }
|
964 |
| - }, |
| 972 | + } |
965 | 973 |
|
966 | 974 | }
|
967 | 975 | </script>
|
0 commit comments