|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | + |
| 4 | +import AntForm from '../common/form-wrapper'; |
| 5 | +import { validationError } from '../common/helpers'; |
| 6 | +import { meta, input } from '@data-driven-forms/common/src/prop-types-templates'; |
| 7 | +import { Select as AntSelect } from 'antd'; |
| 8 | +import { useFieldApi } from '@data-driven-forms/react-form-renderer'; |
| 9 | + |
| 10 | +const selectValue = (option) => option.sort((a, b) => a.label.localeCompare(b.label, 'en', { sensitivity: 'base' })).map((item) => item.value); |
| 11 | + |
| 12 | +const { Option } = AntSelect; |
| 13 | + |
| 14 | +const Select = (props) => { |
| 15 | + const { |
| 16 | + input, |
| 17 | + isReadOnly, |
| 18 | + isDisabled, |
| 19 | + isClearable, |
| 20 | + placeholder, |
| 21 | + isRequired, |
| 22 | + label, |
| 23 | + helperText, |
| 24 | + validateOnMount, |
| 25 | + meta, |
| 26 | + options, |
| 27 | + isSearchable, |
| 28 | + description, |
| 29 | + ...rest |
| 30 | + } = useFieldApi(props); |
| 31 | + const invalid = validationError(meta, validateOnMount); |
| 32 | + return ( |
| 33 | + <AntForm layout="vertical" label={label} isRequired={isRequired} invalid={invalid} help={invalid || helperText || description}> |
| 34 | + <AntSelect |
| 35 | + placeholder={placeholder || 'Please choose'} |
| 36 | + // value={options.filter(({ value }) => (rest.isMulti ? input.value.includes(value) : value === input.value))} |
| 37 | + {...input} |
| 38 | + mode={rest.isMulti ? 'tags' : ''} |
| 39 | + showSearch={!!isSearchable} |
| 40 | + filterOption={(input, option) => option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0} |
| 41 | + required={isRequired} |
| 42 | + allowClear={isClearable} |
| 43 | + notFoundContent="No option found" |
| 44 | + invalid={invalid} |
| 45 | + disabled={isDisabled || isReadOnly} |
| 46 | + onChange={(value, option) => input.onChange(rest.isMulti ? selectValue(option) : option ? option.value : undefined)} |
| 47 | + input={input} |
| 48 | + {...rest} |
| 49 | + > |
| 50 | + {options |
| 51 | + .filter((option) => Object.prototype.hasOwnProperty.call(option, 'value') && option.value !== null) |
| 52 | + .map(({ value, label }) => ( |
| 53 | + <Option key={value} value={value} label={label}> |
| 54 | + {label} |
| 55 | + </Option> |
| 56 | + ))} |
| 57 | + </AntSelect> |
| 58 | + </AntForm> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +Select.propTypes = { |
| 63 | + input, |
| 64 | + meta, |
| 65 | + isReadOnly: PropTypes.bool, |
| 66 | + isClearable: PropTypes.bool, |
| 67 | + isDisabled: PropTypes.bool, |
| 68 | + placeholder: PropTypes.node, |
| 69 | + isRequired: PropTypes.bool, |
| 70 | + label: PropTypes.node, |
| 71 | + helperText: PropTypes.node, |
| 72 | + validateOnMount: PropTypes.bool, |
| 73 | + isSearchable: PropTypes.bool, |
| 74 | + options: PropTypes.arrayOf(PropTypes.shape({ value: PropTypes.any.isRequired, label: PropTypes.node.isRequired })).isRequired, |
| 75 | + description: PropTypes.node |
| 76 | +}; |
| 77 | + |
| 78 | +export default Select; |
0 commit comments