@94ai/nf-virtual-select 8.1.1 → 8.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,14 @@ declare var _default: import("vue").DefineComponent<{
19
19
  type: BooleanConstructor;
20
20
  default: boolean;
21
21
  };
22
+ isKeywordHighlight: {
23
+ type: BooleanConstructor;
24
+ default: boolean;
25
+ };
26
+ keywordLabelAlias: {
27
+ type: StringConstructor;
28
+ default: string;
29
+ };
22
30
  }, {}, {}, {}, {}, import("vue/types/v3-component-options").ComponentOptionsMixin, import("vue/types/v3-component-options").ComponentOptionsMixin, {}, string, Readonly<import("vue").ExtractPropTypes<{
23
31
  index: {
24
32
  type: NumberConstructor;
@@ -40,8 +48,18 @@ declare var _default: import("vue").DefineComponent<{
40
48
  type: BooleanConstructor;
41
49
  default: boolean;
42
50
  };
51
+ isKeywordHighlight: {
52
+ type: BooleanConstructor;
53
+ default: boolean;
54
+ };
55
+ keywordLabelAlias: {
56
+ type: StringConstructor;
57
+ default: string;
58
+ };
43
59
  }>>, {
44
60
  source: Record<string, any>;
45
61
  showTip: boolean;
62
+ isKeywordHighlight: boolean;
63
+ keywordLabelAlias: string;
46
64
  }>;
47
65
  export default _default;
@@ -33,6 +33,16 @@ const _sfc_main$2 = {
33
33
  // 提示
34
34
  type: Boolean,
35
35
  default: false
36
+ },
37
+ /** 关键词高亮 */
38
+ isKeywordHighlight: {
39
+ type: Boolean,
40
+ default: false
41
+ },
42
+ /** 关键词高亮label别名 */
43
+ keywordLabelAlias: {
44
+ type: String,
45
+ default: "keywordHighlightLabel"
36
46
  }
37
47
  }
38
48
  };
@@ -53,7 +63,15 @@ var _sfc_render$2 = function render() {
53
63
  "effect": "dark",
54
64
  "placement": "left"
55
65
  }
56
- }, [_c("span", [_vm._v(_vm._s(_vm.source[_vm.label]))])]) : _c("span", [_vm._v(_vm._s(_vm.source[_vm.label]))])];
66
+ }, [_vm.isKeywordHighlight ? _c("span", {
67
+ domProps: {
68
+ "innerHTML": _vm._s(_vm.source[_vm.keywordLabelAlias])
69
+ }
70
+ }) : _c("span", [_vm._v(_vm._s(_vm.source[_vm.label]))])]) : [_vm.isKeywordHighlight ? _c("span", {
71
+ domProps: {
72
+ "innerHTML": _vm._s(_vm.source[_vm.keywordLabelAlias])
73
+ }
74
+ }) : _c("span", [_vm._v(_vm._s(_vm.source[_vm.label]))])]];
57
75
  }, {
58
76
  "slot": "default",
59
77
  "row": _vm.source
@@ -2532,6 +2550,20 @@ const _sfc_main = {
2532
2550
  height: {
2533
2551
  type: String,
2534
2552
  default: "245px"
2553
+ },
2554
+ filterable: {
2555
+ type: Boolean,
2556
+ default: false
2557
+ },
2558
+ /** 关键词高亮 */
2559
+ keywordHighlight: {
2560
+ type: Boolean,
2561
+ default: true
2562
+ },
2563
+ /** 关键词高亮label别名 */
2564
+ keywordLabelAlias: {
2565
+ type: String,
2566
+ default: "keywordHighlightLabel"
2535
2567
  }
2536
2568
  },
2537
2569
  data() {
@@ -2558,7 +2590,9 @@ const _sfc_main = {
2558
2590
  value: that.keyValue[1],
2559
2591
  showTip: that.showTip,
2560
2592
  source: this.source,
2561
- index: this.index
2593
+ index: this.index,
2594
+ isKeywordHighlight: that.isKeywordHighlight,
2595
+ keywordLabelAlias: that.keywordLabelAlias
2562
2596
  },
2563
2597
  scopedSlots: this.$scopedSlots
2564
2598
  });
@@ -2566,23 +2600,44 @@ const _sfc_main = {
2566
2600
  };
2567
2601
  return {
2568
2602
  itemComponent: this.dataComponent ?? ElOption,
2569
- selectArr: []
2603
+ selectArr: [],
2604
+ keyword: ""
2605
+ // 当前搜索关键词
2570
2606
  };
2571
2607
  },
2572
2608
  computed: {
2609
+ /** 当前条件是否符合关键词高亮 */
2610
+ isKeywordHighlight() {
2611
+ return this.bindProps.keywordHighlight && !!(this.bindProps.filterable || this.isVirtualSelect);
2612
+ },
2573
2613
  config() {
2574
2614
  const configData = {
2575
2615
  ...this.bindProps,
2576
2616
  "popper-class": (this.bindProps.popperClass || "") + ` ${this.isVirtualSelect ? "nf-virtual-select-virtual-list-popper" : "nf-virtual-select-el-select-popper"}${this.bindProps.multiple && this.selectAll ? " nf-virtual-select-all" : ""}`
2577
2617
  };
2618
+ let filterMethod = void 0;
2578
2619
  if (this.isVirtualSelect) {
2579
- return {
2580
- ...configData,
2581
- "filter-method": this.filterMethod,
2582
- filterable: true
2620
+ filterMethod = this.filterMethod;
2621
+ } else if (this.isKeywordHighlight) {
2622
+ filterMethod = async (...args) => {
2623
+ var _a, _b;
2624
+ try {
2625
+ await ((_b = (_a = this.bindProps)["filter-method"]) == null ? void 0 : _b.call(_a, ...args));
2626
+ } catch (err) {
2627
+ console.error("<nf-virtual-select> filterMethod error: ", err);
2628
+ }
2629
+ this.highLightFilterMethod(...args);
2583
2630
  };
2584
2631
  }
2585
- return configData;
2632
+ return {
2633
+ ...configData,
2634
+ ...this.isVirtualSelect ? {
2635
+ filterable: true
2636
+ } : {},
2637
+ ...filterMethod ? {
2638
+ "filter-method": filterMethod
2639
+ } : {}
2640
+ };
2586
2641
  },
2587
2642
  currentVal: {
2588
2643
  get() {
@@ -2604,6 +2659,8 @@ const _sfc_main = {
2604
2659
  clearable: true,
2605
2660
  ...this.itemOptions,
2606
2661
  ...this.$attrs,
2662
+ keywordHighlight: !!(this.keywordHighlight || this.itemOptions.keywordHighlight),
2663
+ filterable: !!(this.filterable || this.itemOptions.filterable),
2607
2664
  virtualList: this.isVirtualSelect,
2608
2665
  placeholder: this.placeholderDesc,
2609
2666
  multiple: this.isMultipleSelect,
@@ -2747,7 +2804,7 @@ const _sfc_main = {
2747
2804
  init() {
2748
2805
  var _a;
2749
2806
  if (this.isVirtualSelect) {
2750
- this.selectArr = ((_a = this.optionsList) == null ? void 0 : _a.map((it) => ({
2807
+ let selectArr = ((_a = this.optionsList) == null ? void 0 : _a.map((it) => ({
2751
2808
  ...it,
2752
2809
  [this.keyValue[0]]: it[this.keyValue[0]],
2753
2810
  [this.keyValue[1]]: it[this.keyValue[1]],
@@ -2756,27 +2813,43 @@ const _sfc_main = {
2756
2813
  }))) ?? [];
2757
2814
  if (this.currentVal && this.echoValueTop) {
2758
2815
  if ((this.itemOptions.multiple || this.multiple) && Array.isArray(this.currentVal)) {
2759
- const multiple_elements = this.selectArr.filter((item) => this.currentVal.includes(item[this.keyValue[1]]));
2760
- this.selectArr = [...multiple_elements, ...this.selectArr.filter((item) => !multiple_elements.includes(item))];
2816
+ const multiple_elements = selectArr.filter((item) => this.currentVal.includes(item[this.keyValue[1]]));
2817
+ selectArr = [...multiple_elements, ...selectArr.filter((item) => !multiple_elements.includes(item))];
2761
2818
  } else {
2762
- const index2 = this.selectArr.findIndex((item) => item.value === this.currentVal);
2819
+ const index2 = selectArr.findIndex((item) => item.value === this.currentVal);
2763
2820
  if (index2 !== -1) {
2764
- const obj = this.selectArr.splice(index2, 1)[0];
2765
- this.selectArr.unshift(obj);
2821
+ const obj = selectArr.splice(index2, 1)[0];
2822
+ selectArr.unshift(obj);
2766
2823
  }
2767
2824
  }
2768
2825
  }
2826
+ if (this.isKeywordHighlight) {
2827
+ this.keywordHighlightFn(selectArr);
2828
+ }
2829
+ this.selectArr = selectArr;
2769
2830
  } else {
2770
- this.selectArr = (this.optionsList || []).map((it) => ({
2831
+ let selectArr = [];
2832
+ selectArr = (this.optionsList || []).map((it) => ({
2771
2833
  ...it,
2772
2834
  [this.keyValue[0]]: it[this.keyValue[0]],
2773
2835
  [this.keyValue[1]]: it[this.keyValue[1]],
2774
2836
  disabled: it["disabled"] ?? false
2775
2837
  }));
2838
+ if (this.isKeywordHighlight) {
2839
+ if (this.keyword) {
2840
+ selectArr = selectArr.filter((item) => {
2841
+ var _a2;
2842
+ return (_a2 = item[this.keyValue[0]]) == null ? void 0 : _a2.toLowerCase().includes(this.keyword.toLowerCase());
2843
+ });
2844
+ }
2845
+ this.keywordHighlightFn(selectArr);
2846
+ }
2847
+ this.selectArr = selectArr;
2776
2848
  }
2777
2849
  },
2778
2850
  // 搜索
2779
2851
  async filterMethod(query) {
2852
+ this.keyword = query;
2780
2853
  const filterHandler = this.bindProps["filter-method"];
2781
2854
  if (query !== "") {
2782
2855
  if (filterHandler) {
@@ -2797,8 +2870,14 @@ const _sfc_main = {
2797
2870
  };
2798
2871
  });
2799
2872
  }
2873
+ if (this.isKeywordHighlight) {
2874
+ this.keywordHighlightFn(this.selectArr);
2875
+ }
2800
2876
  } else if (filterHandler) {
2801
2877
  await filterHandler(query);
2878
+ if (this.isKeywordHighlight) {
2879
+ this.keywordHighlightFn(this.selectArr);
2880
+ }
2802
2881
  } else {
2803
2882
  this.init();
2804
2883
  }
@@ -2818,10 +2897,32 @@ const _sfc_main = {
2818
2897
  return go;
2819
2898
  }
2820
2899
  }
2821
- if (!bool && this.isVirtualSelect) {
2822
- this.reset();
2900
+ if (!bool) {
2901
+ this.keyword = "";
2902
+ if (this.isVirtualSelect) {
2903
+ this.reset();
2904
+ }
2823
2905
  this.init();
2824
2906
  }
2907
+ },
2908
+ highLightFilterMethod(query = "") {
2909
+ this.keyword = query;
2910
+ this.init();
2911
+ },
2912
+ keywordHighlightFn(list) {
2913
+ const getHtmlLabel = (keyword, label, className = "keyword-highlight") => {
2914
+ if (!keyword || !label) {
2915
+ return label;
2916
+ }
2917
+ const escapedKeyword = keyword.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
2918
+ const regex = new RegExp(escapedKeyword, "gi");
2919
+ return label.replace(regex, (match) => {
2920
+ return `<span class="${className}">${match}</span>`;
2921
+ });
2922
+ };
2923
+ list.forEach((item) => {
2924
+ item[this.keywordLabelAlias] = getHtmlLabel(this.keyword, item[this.keyValue[0]]);
2925
+ });
2825
2926
  }
2826
2927
  },
2827
2928
  beforeDestroy() {
@@ -2939,7 +3040,15 @@ var _sfc_render = function render2() {
2939
3040
  "effect": "dark",
2940
3041
  "placement": "left"
2941
3042
  }
2942
- }, [_c("span", [_vm._v(_vm._s(item[_vm.keyValue[0]]))])]) : _c("span", [_vm._v(" " + _vm._s(item[_vm.keyValue[0]]) + " ")])];
3043
+ }, [_vm.isKeywordHighlight ? _c("span", {
3044
+ domProps: {
3045
+ "innerHTML": _vm._s(item[_vm.keywordLabelAlias])
3046
+ }
3047
+ }) : _c("span", [_vm._v(_vm._s(item[_vm.keyValue[0]]))])]) : [_vm.isKeywordHighlight ? _c("span", {
3048
+ domProps: {
3049
+ "innerHTML": _vm._s(item[_vm.keywordLabelAlias])
3050
+ }
3051
+ }) : _c("span", [_vm._v(_vm._s(item[_vm.keyValue[0]]))])]];
2943
3052
  }, {
2944
3053
  "slot": "default",
2945
3054
  "row": item
@@ -31,6 +31,16 @@ const _sfc_main$2 = {
31
31
  // 提示
32
32
  type: Boolean,
33
33
  default: false
34
+ },
35
+ /** 关键词高亮 */
36
+ isKeywordHighlight: {
37
+ type: Boolean,
38
+ default: false
39
+ },
40
+ /** 关键词高亮label别名 */
41
+ keywordLabelAlias: {
42
+ type: String,
43
+ default: "keywordHighlightLabel"
34
44
  }
35
45
  }
36
46
  };
@@ -51,7 +61,15 @@ var _sfc_render$2 = function render() {
51
61
  "effect": "dark",
52
62
  "placement": "left"
53
63
  }
54
- }, [_c("span", [_vm._v(_vm._s(_vm.source[_vm.label]))])]) : _c("span", [_vm._v(_vm._s(_vm.source[_vm.label]))])];
64
+ }, [_vm.isKeywordHighlight ? _c("span", {
65
+ domProps: {
66
+ "innerHTML": _vm._s(_vm.source[_vm.keywordLabelAlias])
67
+ }
68
+ }) : _c("span", [_vm._v(_vm._s(_vm.source[_vm.label]))])]) : [_vm.isKeywordHighlight ? _c("span", {
69
+ domProps: {
70
+ "innerHTML": _vm._s(_vm.source[_vm.keywordLabelAlias])
71
+ }
72
+ }) : _c("span", [_vm._v(_vm._s(_vm.source[_vm.label]))])]];
55
73
  }, {
56
74
  "slot": "default",
57
75
  "row": _vm.source
@@ -2530,6 +2548,20 @@ const _sfc_main = {
2530
2548
  height: {
2531
2549
  type: String,
2532
2550
  default: "245px"
2551
+ },
2552
+ filterable: {
2553
+ type: Boolean,
2554
+ default: false
2555
+ },
2556
+ /** 关键词高亮 */
2557
+ keywordHighlight: {
2558
+ type: Boolean,
2559
+ default: true
2560
+ },
2561
+ /** 关键词高亮label别名 */
2562
+ keywordLabelAlias: {
2563
+ type: String,
2564
+ default: "keywordHighlightLabel"
2533
2565
  }
2534
2566
  },
2535
2567
  data() {
@@ -2556,7 +2588,9 @@ const _sfc_main = {
2556
2588
  value: that.keyValue[1],
2557
2589
  showTip: that.showTip,
2558
2590
  source: this.source,
2559
- index: this.index
2591
+ index: this.index,
2592
+ isKeywordHighlight: that.isKeywordHighlight,
2593
+ keywordLabelAlias: that.keywordLabelAlias
2560
2594
  },
2561
2595
  scopedSlots: this.$scopedSlots
2562
2596
  });
@@ -2564,23 +2598,44 @@ const _sfc_main = {
2564
2598
  };
2565
2599
  return {
2566
2600
  itemComponent: this.dataComponent ?? ElOption,
2567
- selectArr: []
2601
+ selectArr: [],
2602
+ keyword: ""
2603
+ // 当前搜索关键词
2568
2604
  };
2569
2605
  },
2570
2606
  computed: {
2607
+ /** 当前条件是否符合关键词高亮 */
2608
+ isKeywordHighlight() {
2609
+ return this.bindProps.keywordHighlight && !!(this.bindProps.filterable || this.isVirtualSelect);
2610
+ },
2571
2611
  config() {
2572
2612
  const configData = {
2573
2613
  ...this.bindProps,
2574
2614
  "popper-class": (this.bindProps.popperClass || "") + ` ${this.isVirtualSelect ? "nf-virtual-select-virtual-list-popper" : "nf-virtual-select-el-select-popper"}${this.bindProps.multiple && this.selectAll ? " nf-virtual-select-all" : ""}`
2575
2615
  };
2616
+ let filterMethod = void 0;
2576
2617
  if (this.isVirtualSelect) {
2577
- return {
2578
- ...configData,
2579
- "filter-method": this.filterMethod,
2580
- filterable: true
2618
+ filterMethod = this.filterMethod;
2619
+ } else if (this.isKeywordHighlight) {
2620
+ filterMethod = async (...args) => {
2621
+ var _a, _b;
2622
+ try {
2623
+ await ((_b = (_a = this.bindProps)["filter-method"]) == null ? void 0 : _b.call(_a, ...args));
2624
+ } catch (err) {
2625
+ console.error("<nf-virtual-select> filterMethod error: ", err);
2626
+ }
2627
+ this.highLightFilterMethod(...args);
2581
2628
  };
2582
2629
  }
2583
- return configData;
2630
+ return {
2631
+ ...configData,
2632
+ ...this.isVirtualSelect ? {
2633
+ filterable: true
2634
+ } : {},
2635
+ ...filterMethod ? {
2636
+ "filter-method": filterMethod
2637
+ } : {}
2638
+ };
2584
2639
  },
2585
2640
  currentVal: {
2586
2641
  get() {
@@ -2602,6 +2657,8 @@ const _sfc_main = {
2602
2657
  clearable: true,
2603
2658
  ...this.itemOptions,
2604
2659
  ...this.$attrs,
2660
+ keywordHighlight: !!(this.keywordHighlight || this.itemOptions.keywordHighlight),
2661
+ filterable: !!(this.filterable || this.itemOptions.filterable),
2605
2662
  virtualList: this.isVirtualSelect,
2606
2663
  placeholder: this.placeholderDesc,
2607
2664
  multiple: this.isMultipleSelect,
@@ -2745,7 +2802,7 @@ const _sfc_main = {
2745
2802
  init() {
2746
2803
  var _a;
2747
2804
  if (this.isVirtualSelect) {
2748
- this.selectArr = ((_a = this.optionsList) == null ? void 0 : _a.map((it) => ({
2805
+ let selectArr = ((_a = this.optionsList) == null ? void 0 : _a.map((it) => ({
2749
2806
  ...it,
2750
2807
  [this.keyValue[0]]: it[this.keyValue[0]],
2751
2808
  [this.keyValue[1]]: it[this.keyValue[1]],
@@ -2754,27 +2811,43 @@ const _sfc_main = {
2754
2811
  }))) ?? [];
2755
2812
  if (this.currentVal && this.echoValueTop) {
2756
2813
  if ((this.itemOptions.multiple || this.multiple) && Array.isArray(this.currentVal)) {
2757
- const multiple_elements = this.selectArr.filter((item) => this.currentVal.includes(item[this.keyValue[1]]));
2758
- this.selectArr = [...multiple_elements, ...this.selectArr.filter((item) => !multiple_elements.includes(item))];
2814
+ const multiple_elements = selectArr.filter((item) => this.currentVal.includes(item[this.keyValue[1]]));
2815
+ selectArr = [...multiple_elements, ...selectArr.filter((item) => !multiple_elements.includes(item))];
2759
2816
  } else {
2760
- const index2 = this.selectArr.findIndex((item) => item.value === this.currentVal);
2817
+ const index2 = selectArr.findIndex((item) => item.value === this.currentVal);
2761
2818
  if (index2 !== -1) {
2762
- const obj = this.selectArr.splice(index2, 1)[0];
2763
- this.selectArr.unshift(obj);
2819
+ const obj = selectArr.splice(index2, 1)[0];
2820
+ selectArr.unshift(obj);
2764
2821
  }
2765
2822
  }
2766
2823
  }
2824
+ if (this.isKeywordHighlight) {
2825
+ this.keywordHighlightFn(selectArr);
2826
+ }
2827
+ this.selectArr = selectArr;
2767
2828
  } else {
2768
- this.selectArr = (this.optionsList || []).map((it) => ({
2829
+ let selectArr = [];
2830
+ selectArr = (this.optionsList || []).map((it) => ({
2769
2831
  ...it,
2770
2832
  [this.keyValue[0]]: it[this.keyValue[0]],
2771
2833
  [this.keyValue[1]]: it[this.keyValue[1]],
2772
2834
  disabled: it["disabled"] ?? false
2773
2835
  }));
2836
+ if (this.isKeywordHighlight) {
2837
+ if (this.keyword) {
2838
+ selectArr = selectArr.filter((item) => {
2839
+ var _a2;
2840
+ return (_a2 = item[this.keyValue[0]]) == null ? void 0 : _a2.toLowerCase().includes(this.keyword.toLowerCase());
2841
+ });
2842
+ }
2843
+ this.keywordHighlightFn(selectArr);
2844
+ }
2845
+ this.selectArr = selectArr;
2774
2846
  }
2775
2847
  },
2776
2848
  // 搜索
2777
2849
  async filterMethod(query) {
2850
+ this.keyword = query;
2778
2851
  const filterHandler = this.bindProps["filter-method"];
2779
2852
  if (query !== "") {
2780
2853
  if (filterHandler) {
@@ -2795,8 +2868,14 @@ const _sfc_main = {
2795
2868
  };
2796
2869
  });
2797
2870
  }
2871
+ if (this.isKeywordHighlight) {
2872
+ this.keywordHighlightFn(this.selectArr);
2873
+ }
2798
2874
  } else if (filterHandler) {
2799
2875
  await filterHandler(query);
2876
+ if (this.isKeywordHighlight) {
2877
+ this.keywordHighlightFn(this.selectArr);
2878
+ }
2800
2879
  } else {
2801
2880
  this.init();
2802
2881
  }
@@ -2816,10 +2895,32 @@ const _sfc_main = {
2816
2895
  return go;
2817
2896
  }
2818
2897
  }
2819
- if (!bool && this.isVirtualSelect) {
2820
- this.reset();
2898
+ if (!bool) {
2899
+ this.keyword = "";
2900
+ if (this.isVirtualSelect) {
2901
+ this.reset();
2902
+ }
2821
2903
  this.init();
2822
2904
  }
2905
+ },
2906
+ highLightFilterMethod(query = "") {
2907
+ this.keyword = query;
2908
+ this.init();
2909
+ },
2910
+ keywordHighlightFn(list) {
2911
+ const getHtmlLabel = (keyword, label, className = "keyword-highlight") => {
2912
+ if (!keyword || !label) {
2913
+ return label;
2914
+ }
2915
+ const escapedKeyword = keyword.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
2916
+ const regex = new RegExp(escapedKeyword, "gi");
2917
+ return label.replace(regex, (match) => {
2918
+ return `<span class="${className}">${match}</span>`;
2919
+ });
2920
+ };
2921
+ list.forEach((item) => {
2922
+ item[this.keywordLabelAlias] = getHtmlLabel(this.keyword, item[this.keyValue[0]]);
2923
+ });
2823
2924
  }
2824
2925
  },
2825
2926
  beforeDestroy() {
@@ -2937,7 +3038,15 @@ var _sfc_render = function render2() {
2937
3038
  "effect": "dark",
2938
3039
  "placement": "left"
2939
3040
  }
2940
- }, [_c("span", [_vm._v(_vm._s(item[_vm.keyValue[0]]))])]) : _c("span", [_vm._v(" " + _vm._s(item[_vm.keyValue[0]]) + " ")])];
3041
+ }, [_vm.isKeywordHighlight ? _c("span", {
3042
+ domProps: {
3043
+ "innerHTML": _vm._s(item[_vm.keywordLabelAlias])
3044
+ }
3045
+ }) : _c("span", [_vm._v(_vm._s(item[_vm.keyValue[0]]))])]) : [_vm.isKeywordHighlight ? _c("span", {
3046
+ domProps: {
3047
+ "innerHTML": _vm._s(item[_vm.keywordLabelAlias])
3048
+ }
3049
+ }) : _c("span", [_vm._v(_vm._s(item[_vm.keyValue[0]]))])]];
2941
3050
  }, {
2942
3051
  "slot": "default",
2943
3052
  "row": item
@@ -12,9 +12,13 @@
12
12
  effect="dark"
13
13
  placement="left"
14
14
  >
15
- <span>{{ source[label] }}</span>
15
+ <span v-if="isKeywordHighlight" v-html="source[keywordLabelAlias]" />
16
+ <span v-else>{{ source[label] }}</span>
16
17
  </el-tooltip>
17
- <span v-else>{{ source[label] }}</span>
18
+ <template v-else>
19
+ <span v-if="isKeywordHighlight" v-html="source[keywordLabelAlias]" />
20
+ <span v-else>{{ source[label] }}</span>
21
+ </template>
18
22
  </slot>
19
23
  </el-option>
20
24
  </template>
@@ -44,6 +48,16 @@ export default {
44
48
  showTip: { // 提示
45
49
  type: Boolean,
46
50
  default: false
51
+ },
52
+ /** 关键词高亮 */
53
+ isKeywordHighlight: {
54
+ type: Boolean,
55
+ default: false
56
+ },
57
+ /** 关键词高亮label别名 */
58
+ keywordLabelAlias: {
59
+ type: String,
60
+ default: 'keywordHighlightLabel'
47
61
  }
48
62
  }
49
63
  }
@@ -92,7 +92,7 @@ export default defineComponent({
92
92
  } catch (error) {
93
93
  // console.log(error)
94
94
  }
95
- },
95
+ }
96
96
  });
97
97
  </script>
98
98
 
@@ -84,11 +84,13 @@
84
84
  effect="dark"
85
85
  placement="left"
86
86
  v-if="showTip">
87
- <span>{{ item[keyValue[0]] }}</span>
87
+ <span v-if="isKeywordHighlight" v-html="item[keywordLabelAlias]" />
88
+ <span v-else>{{ item[keyValue[0]] }}</span>
88
89
  </el-tooltip>
89
- <span v-else>
90
- {{ item[keyValue[0]] }}
91
- </span>
90
+ <template v-else>
91
+ <span v-if="isKeywordHighlight" v-html="item[keywordLabelAlias]" />
92
+ <span v-else>{{ item[keyValue[0]] }}</span>
93
+ </template>
92
94
  </slot>
93
95
  </el-option>
94
96
  <slot />
@@ -298,6 +300,20 @@ export default {
298
300
  height: {
299
301
  type: String,
300
302
  default: '245px'
303
+ },
304
+ filterable: {
305
+ type: Boolean,
306
+ default: false
307
+ },
308
+ /** 关键词高亮 */
309
+ keywordHighlight: {
310
+ type: Boolean,
311
+ default: true
312
+ },
313
+ /** 关键词高亮label别名 */
314
+ keywordLabelAlias: {
315
+ type: String,
316
+ default: 'keywordHighlightLabel'
301
317
  }
302
318
  },
303
319
  data() {
@@ -322,7 +338,9 @@ export default {
322
338
  value: that.keyValue[1],
323
339
  showTip: that.showTip,
324
340
  source: this.source,
325
- index: this.index
341
+ index: this.index,
342
+ isKeywordHighlight: that.isKeywordHighlight,
343
+ keywordLabelAlias: that.keywordLabelAlias,
326
344
  },
327
345
  scopedSlots: this.$scopedSlots
328
346
  })
@@ -330,23 +348,38 @@ export default {
330
348
  }
331
349
  return {
332
350
  itemComponent: this.dataComponent ?? ElOption,
333
- selectArr: []
351
+ selectArr: [],
352
+ keyword: '', // 当前搜索关键词
334
353
  }
335
354
  },
336
355
  computed: {
356
+ /** 当前条件是否符合关键词高亮 */
357
+ isKeywordHighlight() {
358
+ return this.bindProps.keywordHighlight && !!(this.bindProps.filterable || this.isVirtualSelect)
359
+ },
337
360
  config() {
338
361
  const configData = {
339
362
  ...this.bindProps,
340
363
  'popper-class': (this.bindProps.popperClass || '') + ` ${this.isVirtualSelect ? 'nf-virtual-select-virtual-list-popper' : 'nf-virtual-select-el-select-popper'}` + `${(this.bindProps.multiple && this.selectAll) ? ' nf-virtual-select-all' : ''}`
341
364
  }
365
+ let filterMethod = undefined
342
366
  if (this.isVirtualSelect) {
343
- return {
344
- ...configData,
345
- 'filter-method': this.filterMethod,
346
- filterable: true,
367
+ filterMethod = this.filterMethod
368
+ } else if (this.isKeywordHighlight) {
369
+ filterMethod = async (...args) => {
370
+ try{
371
+ await this.bindProps['filter-method']?.(...args)
372
+ } catch (err) {
373
+ console.error('<nf-virtual-select> filterMethod error: ', err);
374
+ }
375
+ this.highLightFilterMethod(...args)
347
376
  }
348
377
  }
349
- return configData
378
+ return {
379
+ ...configData,
380
+ ...(this.isVirtualSelect ? { filterable: true } : {}),
381
+ ...(filterMethod ? { 'filter-method': filterMethod } : {})
382
+ }
350
383
  },
351
384
  currentVal: {
352
385
  get() {
@@ -367,6 +400,8 @@ export default {
367
400
  clearable: true,
368
401
  ...this.itemOptions,
369
402
  ...this.$attrs,
403
+ keywordHighlight: !!(this.keywordHighlight || this.itemOptions.keywordHighlight),
404
+ filterable: !!(this.filterable || this.itemOptions.filterable),
370
405
  virtualList: this.isVirtualSelect,
371
406
  placeholder: this.placeholderDesc,
372
407
  multiple: this.isMultipleSelect,
@@ -503,7 +538,7 @@ export default {
503
538
  if ((this.isVirtualSelect)) {
504
539
  // 虚拟
505
540
  // 无回显值
506
- this.selectArr = this.optionsList?.map(it => ({
541
+ let selectArr = this.optionsList?.map(it => ({
507
542
  ...it,
508
543
  [this.keyValue[0]]: it[this.keyValue[0]],
509
544
  [this.keyValue[1]]: it[this.keyValue[1]],
@@ -514,29 +549,42 @@ export default {
514
549
  // 有回显值
515
550
  if ((this.itemOptions.multiple || this.multiple) && Array.isArray(this.currentVal)) {
516
551
  // 多选
517
- const multiple_elements = this.selectArr.filter(item => this.currentVal.includes(item[this.keyValue[1]]))
518
- this.selectArr = [...multiple_elements, ...this.selectArr.filter(item => !multiple_elements.includes(item))]
552
+ const multiple_elements = selectArr.filter(item => this.currentVal.includes(item[this.keyValue[1]]))
553
+ selectArr = [...multiple_elements, ...selectArr.filter(item => !multiple_elements.includes(item))]
519
554
  } else {
520
555
  // 单选
521
- const index = this.selectArr.findIndex(item => item.value === this.currentVal)
556
+ const index = selectArr.findIndex(item => item.value === this.currentVal)
522
557
  if (index !== -1) {
523
- const obj = this.selectArr.splice(index, 1)[0]
524
- this.selectArr.unshift(obj)
558
+ const obj = selectArr.splice(index, 1)[0]
559
+ selectArr.unshift(obj)
525
560
  }
526
561
  }
527
562
  }
563
+ if (this.isKeywordHighlight) {
564
+ this.keywordHighlightFn(selectArr)
565
+ }
566
+ this.selectArr = selectArr
528
567
  } else {
529
568
  // 非虚拟选择器
530
- this.selectArr = (this.optionsList || []).map(it => ({
569
+ let selectArr = []
570
+ selectArr = (this.optionsList || []).map(it => ({
531
571
  ...it,
532
572
  [this.keyValue[0]]: it[this.keyValue[0]],
533
573
  [this.keyValue[1]]: it[this.keyValue[1]],
534
574
  disabled: it['disabled'] ?? false
535
575
  }))
576
+ if (this.isKeywordHighlight) {
577
+ if (this.keyword) {
578
+ selectArr = selectArr.filter(item => item[this.keyValue[0]]?.toLowerCase().includes(this.keyword.toLowerCase()))
579
+ }
580
+ this.keywordHighlightFn(selectArr)
581
+ }
582
+ this.selectArr = selectArr
536
583
  }
537
584
  },
538
585
  // 搜索
539
586
  async filterMethod(query) {
587
+ this.keyword = query
540
588
  const filterHandler = this.bindProps['filter-method']
541
589
  if (query !== '') {
542
590
  if (filterHandler) {
@@ -557,8 +605,14 @@ export default {
557
605
  }
558
606
  })
559
607
  }
608
+ if (this.isKeywordHighlight) {
609
+ this.keywordHighlightFn(this.selectArr)
610
+ }
560
611
  } else if (filterHandler) {
561
612
  await filterHandler(query)
613
+ if (this.isKeywordHighlight) {
614
+ this.keywordHighlightFn(this.selectArr)
615
+ }
562
616
  } else {
563
617
  this.init()
564
618
  }
@@ -575,11 +629,34 @@ export default {
575
629
  return go
576
630
  }
577
631
  }
578
- if (!bool && this.isVirtualSelect) {
579
- this.reset()
632
+ if (!bool) {
633
+ this.keyword = ''
634
+ if(this.isVirtualSelect){
635
+ this.reset()
636
+ }
580
637
  this.init()
581
638
  }
582
- }
639
+ },
640
+ highLightFilterMethod(query = '') {
641
+ this.keyword = query
642
+ this.init()
643
+ },
644
+ keywordHighlightFn(list) {
645
+ const getHtmlLabel = (keyword, label, className = 'keyword-highlight') => {
646
+ if (!keyword || !label) {
647
+ return label;
648
+ }
649
+ const escapedKeyword = keyword.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
650
+ const regex = new RegExp(escapedKeyword, 'gi');
651
+ return label.replace(regex, (match) => {
652
+ return `<span class="${className}">${match}</span>`;
653
+ });
654
+ }
655
+ list.forEach(item => {
656
+ item[this.keywordLabelAlias] = getHtmlLabel(this.keyword, item[this.keyValue[0]])
657
+ })
658
+ },
659
+
583
660
  },
584
661
  beforeDestroy() {
585
662
  this.reset()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@94ai/nf-virtual-select",
3
- "version": "8.1.1",
3
+ "version": "8.1.5",
4
4
  "description": "> TODO: description",
5
5
  "keywords": [],
6
6
  "author": "zoujiahe <zoujiahe@94ai.com>,DESKTOP-1EDDQ79\\dev026 <liuxiangxiang@94ai.com>",
@@ -14,8 +14,8 @@
14
14
  "url": "http://94ai.gitlab.com/zoujiahe/common-ui.git"
15
15
  },
16
16
  "dependencies": {
17
- "@94ai/common-utils": "^8.1.1",
18
- "@94ai/nf-theme-chalk": "^8.1.1",
17
+ "@94ai/common-utils": "^8.1.5",
18
+ "@94ai/nf-theme-chalk": "^8.1.5",
19
19
  "lodash-es": "^4.17.21",
20
20
  "sortablejs": "^1.15.3",
21
21
  "vue-demi": "^0.14.5",
@@ -34,5 +34,5 @@
34
34
  "types": "lib/index.d.ts",
35
35
  "main": "lib/nf-virtual-select.cjs.js",
36
36
  "module": "lib/nf-virtual-select.esm-bundler.js",
37
- "gitHead": "bac41650ac7fa93eb6b4b88f44f4b430a424b11f"
37
+ "gitHead": "0962e59960407b6757e168c9461819aef075bab7"
38
38
  }