index.vue 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <el-select v-model="proxyValue"
  3. :clearable="clearable"
  4. :placeholder="placeholder"
  5. :filterable="filterable || allowCreate || config?.remote"
  6. :allow-create="allowCreate"
  7. :disabled="disabled"
  8. :multiple="multiple"
  9. :remote="config?.remote"
  10. :remoteMethod="config?.remoteMethod && remoteMethod"
  11. :style="{width}">
  12. <el-option v-for="(option, index) in options"
  13. :key="index"
  14. :disabled="option[optionProps.disabled]"
  15. :label="option[optionProps.label] ?? option"
  16. :value="option[optionProps.value] ?? option">
  17. </el-option>
  18. </el-select>
  19. </template>
  20. <script>
  21. import { ElSelect, ElOption } from 'element-plus'
  22. import { computed } from 'vue'
  23. import { useFormInput, useOptions } from '@cip/components/hooks/form-input'
  24. import { formInputProps } from '../../form-input-props'
  25. export default {
  26. components: { ElSelect, ElOption },
  27. props: formInputProps,
  28. emits: ['update:modelValue', 'update:otherValue'],
  29. setup (props, context) {
  30. const { width, securityConfig, proxyOtherValue, ...formInput } = useFormInput(props, context)
  31. // 是否多选
  32. const multiple = computed(() => {
  33. return securityConfig.value.multiple ?? false
  34. })
  35. const { optionProps, options, getOptions, getValue, getModelValue, getOtherValue } = useOptions(props, multiple, formInput.emitInput, formInput.emitOtherValue)
  36. // 是否可搜索
  37. const filterable = computed(() => {
  38. return securityConfig.value.filterable ?? false
  39. })
  40. // 是否允许创建
  41. const allowCreate = computed(() => {
  42. return securityConfig.value.allowCreate ?? false
  43. })
  44. // 另一个键
  45. const otherKey = computed(() => {
  46. return securityConfig.value.otherKey ?? ''
  47. })
  48. // 内部代理一个Value
  49. const proxyValue = computed({
  50. get () {
  51. return getValue(props.modelValue)
  52. },
  53. set (value) {
  54. const modelValue = getModelValue(value)
  55. context.emit('update:modelValue', modelValue)
  56. if (otherKey.value) {
  57. const otherValue = getOtherValue(modelValue, value)
  58. context.emit('update:otherValue', otherValue)
  59. }
  60. }
  61. })
  62. const remoteMethod = async (query) => { options.value = await securityConfig.value.remoteMethod(query, props.dependOnValues) }
  63. if (securityConfig.value.remoteMethod) {
  64. const query = proxyOtherValue[0] ? proxyOtherValue[0].value : ''
  65. remoteMethod(query)
  66. }
  67. return {
  68. ...formInput,
  69. securityConfig,
  70. multiple,
  71. options,
  72. getOptions,
  73. filterable,
  74. allowCreate,
  75. proxyValue,
  76. width,
  77. optionProps,
  78. remoteMethod
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="less" scoped>
  84. .el-select{
  85. width: 100%;
  86. }
  87. </style>