index.jsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { ElSelect, ElOption } from 'element-plus'
  2. import { computed, ref } from 'vue'
  3. import { useFormInput, useOptions } from '@cip/components/hooks/form-input'
  4. import { formInputProps, fromInputEmits } from '../../form-input-props'
  5. export default {
  6. components: { ElSelect, ElOption },
  7. props: formInputProps,
  8. emits: [...fromInputEmits],
  9. setup (props, context) {
  10. const remoteSearchLoading = ref(false)
  11. const { width, securityConfig, proxyOtherValue, updateStream, ...formInput } = useFormInput(props, context, { maxOtherKey: 2 })
  12. // 是否多选
  13. const multiple = computed(() => {
  14. return securityConfig.value.multiple ?? false
  15. })
  16. const { optionProps, options, proxyOptionsValue } = useOptions(props, multiple, updateStream, context)
  17. // 是否可搜索
  18. const filterable = computed(() => {
  19. return securityConfig.value.filterable ?? false
  20. })
  21. // 是否允许创建
  22. const allowCreate = computed(() => {
  23. return securityConfig.value.allowCreate ?? false
  24. })
  25. // placeholder
  26. const placeholder = computed(() => {
  27. return formInput.placeholder.value ?? '请选择'
  28. })
  29. // 关闭远程搜索loading
  30. const closeRemoteLoading = () => {
  31. remoteSearchLoading.value = false
  32. }
  33. const remoteMethod = async (query) => {
  34. remoteSearchLoading.value = true
  35. try {
  36. options.value = await securityConfig.value.remoteMethod(query, props.dependOnValues)
  37. } finally {
  38. closeRemoteLoading()
  39. }
  40. }
  41. if (securityConfig.value.remoteMethod) {
  42. const query = proxyOtherValue[0] ? proxyOtherValue[0].value : ''
  43. remoteSearchLoading.value = true
  44. remoteMethod(query).finally(closeRemoteLoading)
  45. }
  46. const ElOptions = (optionsData) => optionsData.map((opt, index) => <ElOption key={index} disabled={opt[optionProps.value.disabled]} label={opt[optionProps.value.label] ?? opt} value={opt[optionProps.value.value] ?? opt}></ElOption>)
  47. // const ElGroup = (optionGroup) => optionGroup.map((group, index) => <ElOptionGroup key={index} label={group.label}>
  48. // {
  49. // ElOptions(group.options ?? [])
  50. // }
  51. // </ElOptionGroup>)
  52. // const renderOption = () => securityConfig.value.isGroupOption ? ElGroup(options.value) : ElOptions(options.value)
  53. return () => <ElSelect v-model={proxyOptionsValue.value}
  54. clearable={formInput.clearable.value}
  55. placeholder={placeholder.value}
  56. filterable={filterable.value || allowCreate.value || securityConfig.value.remote}
  57. allow-create={allowCreate.value}
  58. disabled={securityConfig.value.disabled}
  59. multiple={multiple.value}
  60. remote={securityConfig.value.remote}
  61. remoteMethod={securityConfig.value.remoteMethod && remoteMethod}
  62. collapse-tags={securityConfig.value.collapseTags}
  63. loading={remoteSearchLoading.value}
  64. style={{ width: width.value }}
  65. popper-class="cip-popper-class"
  66. no-match-text={formInput.noMatchText.value}>
  67. {ElOptions(options.value)}
  68. </ElSelect>
  69. }
  70. }