|
1 | 1 | import { TextField } from '@material-ui/core'; |
2 | | -import React, { StatelessComponent } from 'react'; |
| 2 | +import React, { ChangeEvent } from 'react'; |
3 | 3 | import { |
4 | 4 | ComposedComponent, |
5 | 5 | ReactSchemaFormInputProps, |
6 | 6 | } from 'react-schema-form'; |
7 | 7 |
|
8 | | -const TextAreaField: StatelessComponent<ReactSchemaFormInputProps> = ({ |
9 | | - form, |
10 | | - error, |
11 | | - onChangeValidate, |
12 | | - value, |
13 | | -}) => ( |
14 | | - <div className={form.htmlClass}> |
15 | | - <TextField |
16 | | - type={form.type} |
17 | | - label={form.title} |
18 | | - placeholder={form.placeholder} |
19 | | - helperText={error || form.description} |
20 | | - error={!!error} |
21 | | - onChange={onChangeValidate} |
22 | | - value={value} |
23 | | - disabled={form.readonly} |
24 | | - fullWidth |
25 | | - multiline |
26 | | - rows={15} |
27 | | - rowsMax={15} |
28 | | - /> |
29 | | - </div> |
30 | | -); |
| 8 | +const getDisplayValue = (value: object | string): string => |
| 9 | + typeof value === 'string' ? value : JSON.stringify(value); |
| 10 | + |
| 11 | +class TextAreaField extends React.PureComponent< |
| 12 | + ReactSchemaFormInputProps<HTMLTextAreaElement> |
| 13 | +> { |
| 14 | + constructor(props: ReactSchemaFormInputProps<HTMLTextAreaElement>) { |
| 15 | + super(props); |
| 16 | + } |
| 17 | + |
| 18 | + _onChangeValidate = ( |
| 19 | + e: ChangeEvent<HTMLTextAreaElement>, |
| 20 | + v?: any, |
| 21 | + ): void => { |
| 22 | + const { form, onChangeValidate } = this.props; |
| 23 | + |
| 24 | + const tryParseJson = (value: string): string | object => { |
| 25 | + try { |
| 26 | + return JSON.parse(value); |
| 27 | + } catch (e) { |
| 28 | + return value; |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + onChangeValidate( |
| 33 | + { |
| 34 | + ...e, |
| 35 | + currentTarget: { |
| 36 | + ...e.currentTarget, |
| 37 | + // @ts-ignore |
| 38 | + value: |
| 39 | + form.schema.type === 'object' |
| 40 | + ? tryParseJson(e.target.value) |
| 41 | + : e.target.value, |
| 42 | + }, |
| 43 | + target: { |
| 44 | + // @ts-ignore |
| 45 | + value: |
| 46 | + form.schema.type === 'object' |
| 47 | + ? tryParseJson(e.target.value) |
| 48 | + : e.target.value, |
| 49 | + }, |
| 50 | + }, |
| 51 | + v, |
| 52 | + ); |
| 53 | + }; |
| 54 | + |
| 55 | + render() { |
| 56 | + const { form, error, value } = this.props; |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className={form.htmlClass}> |
| 60 | + <TextField |
| 61 | + type={form.type} |
| 62 | + label={form.title} |
| 63 | + placeholder={form.placeholder} |
| 64 | + helperText={error || form.description} |
| 65 | + error={!!error} |
| 66 | + onChange={this._onChangeValidate} |
| 67 | + value={getDisplayValue(value)} |
| 68 | + disabled={form.readonly} |
| 69 | + fullWidth |
| 70 | + multiline |
| 71 | + rows={15} |
| 72 | + rowsMax={15} |
| 73 | + /> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
31 | 78 |
|
32 | 79 | export default ComposedComponent(TextAreaField); |
0 commit comments