liusuyi
2023-04-24 4737f1e038743ced243c9e52423404d9034d6107
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<script>
  import { debounce, deepExtend, includes } from '../utils'
  import { MIN_INPUT_WIDTH, KEY_CODES, INPUT_DEBOUNCE_DELAY } from '../constants'
 
  const keysThatRequireMenuBeingOpen = [
    KEY_CODES.ENTER,
    KEY_CODES.END,
    KEY_CODES.HOME,
    KEY_CODES.ARROW_LEFT,
    KEY_CODES.ARROW_UP,
    KEY_CODES.ARROW_RIGHT,
    KEY_CODES.ARROW_DOWN,
  ]
 
  export default {
    name: 'vue-treeselect--input',
    inject: [ 'instance' ],
 
    data: () => ({
      inputWidth: MIN_INPUT_WIDTH,
      value: '',
    }),
 
    computed: {
      needAutoSize() {
        const { instance } = this
 
        return (
          instance.searchable &&
          !instance.disabled &&
          instance.multiple
        )
      },
 
      inputStyle() {
        return {
          width: this.needAutoSize ? `${this.inputWidth}px` : null,
        }
      },
    },
 
    watch: {
      'instance.trigger.searchQuery'(newValue) {
        this.value = newValue
      },
 
      value() {
        // istanbul ignore else
        if (this.needAutoSize) this.$nextTick(this.updateInputWidth)
      },
    },
 
    created() {
      this.debouncedCallback = debounce(
        this.updateSearchQuery,
        INPUT_DEBOUNCE_DELAY,
        { leading: true, trailing: true },
      )
    },
 
    methods: {
      clear() {
        this.onInput({
          target: { value: '' },
        })
      },
 
      focus() {
        const { instance } = this
 
        if (!instance.disabled) {
          this.$refs.input && this.$refs.input.focus()
        }
      },
 
      blur() {
        this.$refs.input && this.$refs.input.blur()
      },
 
      onFocus() {
        const { instance } = this
 
        instance.trigger.isFocused = true
        // istanbul ignore else
        if (instance.openOnFocus) instance.openMenu()
      },
 
      onBlur() {
        const { instance } = this
        const menu = instance.getMenu()
 
        // #15
        // istanbul ignore next
        if (menu && document.activeElement === menu) {
          return this.focus()
        }
 
        instance.trigger.isFocused = false
        instance.closeMenu()
      },
 
      onInput(evt) {
        const { value } = evt.target
 
        this.value = value
 
        if (value) {
          this.debouncedCallback()
        } else {
          this.debouncedCallback.cancel()
          this.updateSearchQuery()
        }
      },
 
      // 用 keyUp 事件存在一个问题,删除输入框最后一个字符也会导致取消选中最后一项
      onKeyDown(evt) {
        const { instance } = this
        // https://css-tricks.com/snippets/javascript/javascript-keycodes/
        // https://stackoverflow.com/questions/4471582/javascript-keycode-vs-which
        const key = 'which' in evt ? evt.which : /* istanbul ignore next */ evt.keyCode
 
        if (evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey)
          return
 
        if (!instance.menu.isOpen && includes(keysThatRequireMenuBeingOpen, key)) {
          evt.preventDefault()
          return instance.openMenu()
        }
 
        switch (key) {
        case KEY_CODES.BACKSPACE: {
          if (instance.backspaceRemoves && !this.value.length) {
            instance.removeLastValue()
          }
          break
        }
        case KEY_CODES.ENTER: {
          evt.preventDefault()
          if (instance.menu.current === null) return
          const current = instance.getNode(instance.menu.current)
          if (current.isBranch && instance.disableBranchNodes) return
          instance.select(current)
          break
        }
        case KEY_CODES.ESCAPE: {
          if (this.value.length) {
            this.clear()
          } else if (instance.menu.isOpen) {
            instance.closeMenu()
          }
          break
        }
        case KEY_CODES.END: {
          evt.preventDefault()
          instance.highlightLastOption()
          break
        }
        case KEY_CODES.HOME: {
          evt.preventDefault()
          instance.highlightFirstOption()
          break
        }
        case KEY_CODES.ARROW_LEFT: {
          const current = instance.getNode(instance.menu.current)
          if (current.isBranch && instance.shouldExpand(current)) {
            evt.preventDefault()
            instance.toggleExpanded(current)
          } else if (!current.isRootNode && (current.isLeaf || (current.isBranch && !(instance.shouldExpand(current))))) {
            evt.preventDefault()
            instance.setCurrentHighlightedOption(current.parentNode)
          }
          break
        }
        case KEY_CODES.ARROW_UP: {
          evt.preventDefault()
          instance.highlightPrevOption()
          break
        }
        case KEY_CODES.ARROW_RIGHT: {
          const current = instance.getNode(instance.menu.current)
          if (current.isBranch && !instance.shouldExpand(current)) {
            evt.preventDefault()
            instance.toggleExpanded(current)
          }
          break
        }
        case KEY_CODES.ARROW_DOWN: {
          evt.preventDefault()
          instance.highlightNextOption()
          break
        }
        case KEY_CODES.DELETE: {
          if (instance.deleteRemoves && !this.value.length) {
            instance.removeLastValue()
          }
          break
        }
        default: {
          // istanbul ignore else
          instance.openMenu()
        }
        }
      },
 
      onMouseDown(evt) {
        // istanbul ignore next
        if (this.value.length) {
          // Prevent it from bubbling to the top level and triggering `preventDefault()`
          // to make the textbox unselectable
          evt.stopPropagation()
        }
      },
 
      renderInputContainer() {
        const { instance } = this
        const props = {}
        const children = []
 
        if (instance.searchable && !instance.disabled) {
          children.push(this.renderInput())
          if (this.needAutoSize) children.push(this.renderSizer())
        }
 
        if (!instance.searchable) {
          deepExtend(props, {
            on: {
              focus: this.onFocus,
              blur: this.onBlur,
              keydown: this.onKeyDown,
            },
            ref: 'input',
          })
        }
 
        if (!instance.searchable && !instance.disabled) {
          deepExtend(props, {
            attrs: {
              tabIndex: instance.tabIndex,
            },
          })
        }
 
        return (
          <div class="vue-treeselect__input-container" {...props}>
            {children}
          </div>
        )
      },
 
      renderInput() {
        const { instance } = this
 
        return (
          <input ref="input"
            class="vue-treeselect__input"
            type="text"
            autocomplete="off"
            tabIndex={instance.tabIndex}
            required={instance.required && !instance.hasValue}
            value={this.value}
            style={this.inputStyle}
            onFocus={this.onFocus}
            onInput={this.onInput}
            onBlur={this.onBlur}
            onKeydown={this.onKeyDown}
            onMousedown={this.onMouseDown}
          />
        )
      },
 
      renderSizer() {
        return (
          <div ref="sizer" class="vue-treeselect__sizer">{this.value}</div>
        )
      },
 
      updateInputWidth() {
        this.inputWidth = Math.max(
          MIN_INPUT_WIDTH,
          this.$refs.sizer.scrollWidth + 15,
        )
      },
 
      updateSearchQuery() {
        const { instance } = this
 
        instance.trigger.searchQuery = this.value
      },
    },
 
    render() {
      return this.renderInputContainer()
    },
  }
</script>