1 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
2 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
3 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
4 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
5 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
6 | };
|
7 | import React from 'react';
|
8 | import compose from 'recompose/compose';
|
9 | import clsx from 'clsx';
|
10 | import { observer } from 'mobx-react';
|
11 | import { toJS } from 'mobx';
|
12 | import withStyles from '@material-ui/core/styles/withStyles';
|
13 | import TextField from '@material-ui/core/TextField';
|
14 | import FormControl from '@material-ui/core/FormControl';
|
15 | import createStyles from '@material-ui/core/styles/createStyles';
|
16 | import isEmpty from 'lodash/isEmpty';
|
17 | import isEqual from 'lodash/isEqual';
|
18 | import assign from 'lodash/assign';
|
19 | import { Animation } from '../FieldTranslate';
|
20 | import { TransformLabel } from '../utils/TransformLabel';
|
21 | import { RenderErrorsDefault } from '../RenderErrorsDefault';
|
22 | const styles = (theme) => createStyles({
|
23 | root: Object.assign(Object.assign({}, theme.mixins.gutters()), { paddingTop: theme.spacing(2), paddingBottom: theme.spacing(2) }),
|
24 | formControl: {},
|
25 | widthNormal: { width: '100%' },
|
26 | InputLabelProps: {
|
27 | overflow: 'hidden',
|
28 | whiteSpace: 'nowrap',
|
29 | textOverflow: 'ellipsis',
|
30 | },
|
31 | formHelperTextPropsRoot: {
|
32 | wordBreak: 'break-word',
|
33 | },
|
34 | });
|
35 | let InputComponent = class InputComponent extends React.Component {
|
36 | constructor(props) {
|
37 | super(props);
|
38 | this.callbacks = [];
|
39 | this.errors = [];
|
40 | this.animation = true;
|
41 | this.getProps = (props) => (Object.assign({}, props.fieldProxy));
|
42 | this.getLastProps = () => this.getProps(this.props);
|
43 | this.state = { type: props.type };
|
44 | }
|
45 | UNSAFE_componentWillUpdate(newProps) {
|
46 | const { errors } = this.getProps(newProps);
|
47 | if (!isEqual(toJS(errors), this.errors)) {
|
48 | this.animation = true;
|
49 | }
|
50 | }
|
51 | componentDidUpdate() {
|
52 | this.callbacks.forEach(callback => callback());
|
53 | this.callbacks = [];
|
54 | this.errors = toJS(this.props.fieldProxy.errors || []);
|
55 | }
|
56 | render() {
|
57 | const { classes, changeField, fieldProxy } = this.props;
|
58 | const { ns, label, name, disabled, fullWidth = true, errors, autoComplete, InputProps, textFieldProps, value, renderErrors: RenderErrors, } = fieldProxy;
|
59 | const { type } = this.state;
|
60 | return (React.createElement(FormControl, Object.assign({}, { fullWidth }, { className: classes.formControl, variant: "outlined" }),
|
61 | React.createElement(TextField, Object.assign({ label: React.createElement(TransformLabel, Object.assign({}, { label, ns, name })), inputRef: element => (fieldProxy.inputRef = element), error: !isEmpty(errors), FormHelperTextProps: {
|
62 | component: ({ children, className }) => {
|
63 | const child = (React.createElement("div", { className: className }, children));
|
64 | if (this.animation && !isEmpty(errors)) {
|
65 | this.animation = false;
|
66 | return React.createElement(Animation, null, child);
|
67 | }
|
68 | return child;
|
69 | },
|
70 | style: {},
|
71 | classes: { root: classes.formHelperTextPropsRoot },
|
72 | }, helperText: errors &&
|
73 | ((RenderErrors && (React.createElement(RenderErrors, Object.assign({}, { errors, fieldProxy })))) || React.createElement(RenderErrorsDefault, Object.assign({}, { errors, fieldProxy }))), InputProps: typeof InputProps === 'function'
|
74 | ? InputProps({
|
75 | type,
|
76 | fieldProxy,
|
77 | changeType: (type, callback) => {
|
78 | if (type !== this.state.type)
|
79 | this.setState({ type }, callback);
|
80 | },
|
81 | })
|
82 | : InputProps, InputLabelProps: {
|
83 | shrink: type === 'date' ? true : undefined,
|
84 | classes: {
|
85 | root: clsx(classes.InputLabelProps, classes.widthNormal),
|
86 | },
|
87 | }, onChange: e => {
|
88 | const { onChange } = fieldProxy;
|
89 | const callback = (onChange || changeField)(assign(e, { fieldProxy }));
|
90 | typeof callback === 'function' &&
|
91 | this.callbacks.push(callback);
|
92 | }, onBlur: () => {
|
93 | const { fieldProxy } = this.props;
|
94 | fieldProxy && fieldProxy.markAsTouched();
|
95 | } }, Object.assign(Object.assign({}, textFieldProps), { name,
|
96 | type,
|
97 | value,
|
98 | disabled,
|
99 | autoComplete })))));
|
100 | }
|
101 | };
|
102 | InputComponent = __decorate([
|
103 | observer
|
104 | ], InputComponent);
|
105 | export const Input = compose(withStyles(styles, { name: 'Input' }))(InputComponent);
|
106 | export default Input;
|
107 |
|
\ | No newline at end of file |