a5f8be01431256b444a934c6de115b5553e5d9ff9c4de2a4a7d15f7358180d9490cf217d69e293880803fd984c74f8119f8bccf4f26bc57dc8b178d2dfb12c 7.5 KB

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