forked from input-output-hk/react-polymorph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.js
More file actions
176 lines (147 loc) · 4.03 KB
/
Input.js
File metadata and controls
176 lines (147 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// @flow
import React, { Component } from 'react';
// $FlowFixMe
import type { ComponentType, Element, SyntheticInputEvent } from 'react';
// external libraries
import createRef from 'create-react-ref/lib/createRef';
import { isString, flow } from 'lodash';
// internal components
import { withTheme } from './HOC/withTheme';
// internal utility functions
import { composeTheme, addThemeId, didThemePropsChange } from '../utils/themes';
// import constants
import { IDENTIFIERS } from '../themes/API';
type Props = {
autoFocus: boolean,
className: string,
context: {
theme: Object,
ROOT_THEME_API: Object
},
disabled: boolean,
error: string,
label: string | Element<any>,
onBlur: Function,
onChange: Function,
onFocus: Function,
maxLength: number,
minLength: number,
onKeyPress: Function,
placeholder: string,
readOnly: boolean,
setError: Function,
skin: ComponentType<any>,
theme: Object, // will take precedence over theme in context if passed
themeId: string,
themeOverrides: Object,
value: string
};
type State = {
error: string,
composedTheme: Object
};
class InputBase extends Component<Props, State> {
inputElement: Element<'input'>;
static defaultProps = {
autoFocus: false,
error: '',
readOnly: false,
theme: null,
themeId: IDENTIFIERS.INPUT,
themeOverrides: {},
value: ''
};
constructor(props: Props) {
super(props);
// define ref
this.inputElement = createRef();
const { context, themeId, theme, themeOverrides } = props;
this.state = {
composedTheme: composeTheme(
addThemeId(theme || context.theme, themeId),
addThemeId(themeOverrides, themeId),
context.ROOT_THEME_API
),
error: ''
};
}
componentDidMount() {
if (this.props.autoFocus) this.focus();
}
componentWillReceiveProps(nextProps: Props) {
didThemePropsChange(this.props, nextProps, this.setState.bind(this));
}
onChange = (event: SyntheticInputEvent<Element<'input'>>) => {
const { onChange, disabled } = this.props;
if (disabled) return;
if (onChange) onChange(this._processValue(event.target.value), event);
};
focus = () => {
const { inputElement } = this;
if (!inputElement.current) return;
inputElement.current.focus();
};
_setError = (error: string) => {
const { setError } = this.props;
// checks for setError func from FormField component
// if this Input instance is being used within the render function
// of a FormField instance, the error field within FormField's local state
// will be set
if (setError) setError(error);
this.setState({ error });
};
_processValue(value: string) {
return flow([
this._enforceStringValue,
this._enforceMaxLength,
this._enforceMinLength
]).call(this, value);
}
_enforceStringValue(value) {
if (!isString(value)) {
throw new Error('Values passed to Input::onChange must be strings');
}
return value;
}
_enforceMaxLength(value: string) {
const { maxLength } = this.props;
const isTooLong = maxLength != null && value.length > maxLength;
return isTooLong ? value.substring(0, maxLength) : value;
}
_enforceMinLength(value: string) {
const { minLength } = this.props;
const isTooShort = minLength != null && value.length < minLength;
if (isTooShort) {
this._setError('Please enter a valid input');
} else if (this.state.error !== '') {
this._setError('');
}
return value;
}
render() {
// destructuring props ensures only the "...rest" get passed down
const {
skin: InputSkin,
context,
theme,
themeOverrides,
onChange,
error,
maxLength,
minLength,
setError,
autoFocus,
...rest
} = this.props;
return (
<InputSkin
error={error || this.state.error}
onChange={this.onChange}
inputRef={this.inputElement}
theme={this.state.composedTheme}
{...rest}
/>
);
}
}
export const Input = withTheme(InputBase);