UNPKG

5.49 kBJavaScriptView Raw
1var __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};
7import React from 'react';
8import compose from 'recompose/compose';
9import clsx from 'clsx';
10import { observer } from 'mobx-react';
11import { toJS } from 'mobx';
12import withStyles from '@material-ui/core/styles/withStyles';
13import TextField from '@material-ui/core/TextField';
14import FormControl from '@material-ui/core/FormControl';
15import createStyles from '@material-ui/core/styles/createStyles';
16import isEmpty from 'lodash/isEmpty';
17import isEqual from 'lodash/isEqual';
18import assign from 'lodash/assign';
19import { Animation } from '../FieldTranslate';
20import { TransformLabel } from '../utils/TransformLabel';
21import { RenderErrorsDefault } from '../RenderErrorsDefault';
22const 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});
35let 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};
102InputComponent = __decorate([
103 observer
104], InputComponent);
105export const Input = compose(withStyles(styles, { name: 'Input' }))(InputComponent);
106export default Input;
107//# sourceMappingURL=Input.js.map
\No newline at end of file