Skip to content

Commit 8daa397

Browse files
committed
Added select.js
1 parent a8bbac3 commit 8daa397

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

packages/ant-component-mapper/src/files/component-mapper.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@ import Checkbox from './checkbox';
88
import DatePicker from './date-picker';
99
import TimePicker from './time-picker';
1010
import Radio from './radio';
11-
11+
import Select from './select';
1212
export const components = {};
1313

1414
const componentMapper = {
1515
[componentTypes.TEXT_FIELD]: TextField,
1616
[componentTypes.TEXTAREA]: TextArea,
17-
[componentTypes.SELECT]: () => <div>Select field</div>,
17+
[componentTypes.SELECT]: Select,
1818
[componentTypes.CHECKBOX]: Checkbox,
1919
[componentTypes.SUB_FORM]: () => <div>sub form</div>,
2020
[componentTypes.RADIO]: Radio,
2121
[componentTypes.TABS]: Tabs,
2222
[componentTypes.DATE_PICKER]: DatePicker,
2323
[componentTypes.TIME_PICKER]: TimePicker,
24-
[componentTypes.SELECT]: () => <div>Select field</div>,
2524
[componentTypes.PLAIN_TEXT]: PlainText,
26-
[componentTypes.SWITCH]: <div>Switch</div>
25+
[componentTypes.SWITCH]: () => <div>switch</div>
2726
};
2827

2928
export default componentMapper;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)