index.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <el-select v-model="proxyOptionsValue"
  3. :clearable="clearable"
  4. :placeholder="placeholder"
  5. :filterable="filterable || allowCreate || securityConfig.remote"
  6. :allow-create="allowCreate"
  7. :disabled="disabled"
  8. :multiple="multiple"
  9. :remote="securityConfig.remote"
  10. :remoteMethod="securityConfig.remoteMethod && remoteMethod"
  11. :collapse-tags="securityConfig.collapseTags"
  12. :loading="remoteSearchLoading"
  13. :style="{width}">
  14. <el-option v-for="(option, index) in options"
  15. :key="index"
  16. :disabled="option[optionProps.disabled]"
  17. :label="option[optionProps.label] ?? option"
  18. :value="option[optionProps.value] ?? option">
  19. </el-option>
  20. </el-select>
  21. </template>
  22. <script>
  23. import { ElSelect, ElOption } from 'element-plus'
  24. import { computed, ref } from 'vue'
  25. import { useFormInput, useOptions } from '@cip/components/hooks/form-input'
  26. import { formInputProps, fromInputEmits } from '../../form-input-props'
  27. export default {
  28. components: { ElSelect, ElOption },
  29. props: formInputProps,
  30. emits: [...fromInputEmits],
  31. setup (props, context) {
  32. const remoteSearchLoading = ref(false)
  33. const { width, securityConfig, proxyOtherValue, updateStream, ...formInput } = useFormInput(props, context, { maxOtherKey: 2 })
  34. // 是否多选
  35. const multiple = computed(() => {
  36. return securityConfig.value.multiple ?? false
  37. })
  38. const { optionProps, options, proxyOptionsValue } = useOptions(props, multiple, updateStream, context)
  39. // 是否可搜索
  40. const filterable = computed(() => {
  41. return securityConfig.value.filterable ?? false
  42. })
  43. // 是否允许创建
  44. const allowCreate = computed(() => {
  45. return securityConfig.value.allowCreate ?? false
  46. })
  47. // 关闭远程搜索loading
  48. const closeRemoteLoading = () => {
  49. remoteSearchLoading.value = false
  50. }
  51. const remoteMethod = async (query) => {
  52. remoteSearchLoading.value = true
  53. try {
  54. options.value = await securityConfig.value.remoteMethod(query, props.dependOnValues)
  55. } finally {
  56. closeRemoteLoading()
  57. }
  58. }
  59. if (securityConfig.value.remoteMethod) {
  60. const query = proxyOtherValue[0] ? proxyOtherValue[0].value : ''
  61. remoteSearchLoading.value = true
  62. remoteMethod(query).finally(closeRemoteLoading)
  63. }
  64. return {
  65. ...formInput,
  66. remoteSearchLoading,
  67. securityConfig,
  68. width,
  69. multiple,
  70. filterable,
  71. allowCreate,
  72. remoteMethod,
  73. optionProps,
  74. options,
  75. proxyOptionsValue
  76. }
  77. }
  78. }
  79. </script>
  80. <style lang="less" scoped>
  81. .el-select{
  82. width: 100%;
  83. }
  84. </style>