|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { Form, InputNumber, Input, DatePicker, Select, Checkbox } from 'antd'; |
| 4 | +import moment from 'moment'; |
| 5 | + |
| 6 | + |
| 7 | +const GenericInput = ({ category, collection = [], collectionType, |
| 8 | + type, name, label, onChange, placeholder, |
| 9 | + value, error }) => { |
| 10 | + |
| 11 | + function onChangeDate(name) { |
| 12 | + return function(date, dateString) { |
| 13 | + onChange(name, dateString); |
| 14 | + }; |
| 15 | + } |
| 16 | + |
| 17 | + function onChangeTimestamp(name) { |
| 18 | + return function(date) { |
| 19 | + let dateString = ''; |
| 20 | + if (date) { dateString = date.format(); } |
| 21 | + onChange(name, dateString); |
| 22 | + }; |
| 23 | + } |
| 24 | + |
| 25 | + function onChangeString(name) { |
| 26 | + return function(event) { |
| 27 | + onChange(name, event.target.value); |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + function onChangeNumber(name) { |
| 32 | + return function(value) { |
| 33 | + onChange(name, value); |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + function onChangeSelect(name) { |
| 38 | + return function(value) { |
| 39 | + onChange(name, { _id: value, _class: type }); |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + function onChangeBoolean(name) { |
| 44 | + return function(event) { |
| 45 | + onChange(name, event.target.checked); |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + function getMomentValue(value, dateFormat) { |
| 50 | + if (value) { |
| 51 | + return moment(value, dateFormat); |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + function filterSelect(input, option) { |
| 58 | + return (option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0); |
| 59 | + } |
| 60 | + |
| 61 | + let input = null; |
| 62 | + const dateFormat = 'YYYY-MM-DD'; |
| 63 | + const datetimeFormat = 'YYYY-MM-DD HH:mm:ss'; |
| 64 | + |
| 65 | + switch (type) { |
| 66 | + case '%Library.String': |
| 67 | + input = <Input value={value} onChange={onChangeString(name)} name={name}/>; |
| 68 | + break; |
| 69 | + case '%Library.Boolean': |
| 70 | + input = <Checkbox checked={value} onChange={onChangeBoolean(name)} name={name}>{label}</Checkbox>; |
| 71 | + break; |
| 72 | + case '%Library.Date': |
| 73 | + input = (<DatePicker format={dateFormat} value={getMomentValue(value, dateFormat)} |
| 74 | + name={name} onChange={onChangeDate(name)}/>); |
| 75 | + break; |
| 76 | + case '%Library.TimeStamp': |
| 77 | + input = (<DatePicker format={datetimeFormat} value={getMomentValue(value, moment.ISO_8601)} |
| 78 | + showTime name={name} onChange={onChangeTimestamp(name)}/>); |
| 79 | + break; |
| 80 | + case '%Library.Integer': |
| 81 | + case '%Library.Numeric': |
| 82 | + input = <InputNumber name={name} value={value} onChange={onChangeNumber(name)}/>; |
| 83 | + break; |
| 84 | + default: |
| 85 | + if (category === 'form') { |
| 86 | + |
| 87 | + if (!collectionType) { |
| 88 | + let options = collection.map(item => { |
| 89 | + return <Select.Option key={item._id} value={item._id}>{item.displayName}</Select.Option>; |
| 90 | + }); |
| 91 | + |
| 92 | + if (value) { |
| 93 | + // extract the id and cast it to string (<Select> accepts only string type for 'value' due PropType) |
| 94 | + value = value._id.toString(); |
| 95 | + } |
| 96 | + |
| 97 | + input = ( |
| 98 | + <Select showSearch style={{ width: 200 }} |
| 99 | + value={value} |
| 100 | + name={name} |
| 101 | + optionFilterProp="children" |
| 102 | + filterOption={filterSelect} |
| 103 | + onChange={onChangeSelect(name)}> |
| 104 | + {options} |
| 105 | + </Select> |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + if (input) { |
| 114 | + if (type !== '%Library.Boolean') { |
| 115 | + return ( |
| 116 | + <Form.Item label={label}> |
| 117 | + {input} |
| 118 | + </Form.Item> |
| 119 | + ); |
| 120 | + } else { |
| 121 | + return input; |
| 122 | + } |
| 123 | + |
| 124 | + } else { |
| 125 | + return <div/>; |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +GenericInput.propTypes = { |
| 130 | + type: PropTypes.string.isRequired, |
| 131 | + category: PropTypes.string.isRequired, |
| 132 | + collectionType: PropTypes.string.isRequired, |
| 133 | + collection: PropTypes.array, |
| 134 | + name: PropTypes.string.isRequired, |
| 135 | + label: PropTypes.string.isRequired, |
| 136 | + onChange: PropTypes.func.isRequired, |
| 137 | + placeholder: PropTypes.string, |
| 138 | + value: PropTypes.any, |
| 139 | + error: PropTypes.string |
| 140 | +}; |
| 141 | + |
| 142 | +export default GenericInput; |
0 commit comments