Select.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { FC, forwardRef } from 'react'
  21. import { Select as AntSelect } from 'antd'
  22. import { IControl, IControlOption } from '../types'
  23. import { SelectValue } from 'antd/lib/select'
  24. import {
  25. CONTROL_MAX_TAG_COUNT,
  26. CONTROL_MAX_TAG_TEXT_LENGTH
  27. } from '../constants'
  28. import { filterSelectOption } from 'app/utils/util'
  29. const Option = AntSelect.Option
  30. declare const SelectSizes: ['default', 'large', 'small']
  31. interface ISelectProps {
  32. control: Omit<IControl, 'relatedItems' | 'relatedViews'>
  33. value?: SelectValue
  34. size?: typeof SelectSizes[number]
  35. onChange?: (
  36. value: SelectValue,
  37. option: React.ReactElement<any> | Array<React.ReactElement<any>>
  38. ) => void
  39. options: IControlOption[]
  40. }
  41. const Select: FC<ISelectProps> = (
  42. { control, value, size, onChange, options },
  43. ref
  44. ) => {
  45. const { multiple } = control
  46. return (
  47. <AntSelect
  48. showSearch
  49. allowClear
  50. placeholder="请选择"
  51. value={value}
  52. onChange={onChange}
  53. dropdownMatchSelectWidth={false}
  54. maxTagCount={CONTROL_MAX_TAG_COUNT}
  55. maxTagTextLength={CONTROL_MAX_TAG_TEXT_LENGTH}
  56. filterOption={filterSelectOption}
  57. {...(size && { size })}
  58. {...(multiple && { mode: 'multiple' })}
  59. ref={ref}
  60. >
  61. {options.map((o) => (
  62. <Option key={o.value} value={o.value}>
  63. {o.text}
  64. </Option>
  65. ))}
  66. </AntSelect>
  67. )
  68. }
  69. export default forwardRef(Select)