-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathFormattedInput.jsx
175 lines (141 loc) · 4.35 KB
/
FormattedInput.jsx
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
/**
* @component
*/
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Input from '../../react-chayns-input/component/Input';
import Formatter from '../utils/Formatter';
/**
* A text input that automatically formats its input with a formatter. Since
* this component is based on the `Input`-component, it takes any of the
* `Input`-components props, which are not listed here.
*
* This component only works as an uncontrolled component, meaning that it does
* not take a `value`-prop.
*/
export default class FormattedInput extends Component {
lastSend = null;
constructor(props) {
super(props);
this.formatter = props.initialFormatter;
this.lastSend = props.defaultValue;
this.state = {
value: this.formatter.format(props.defaultValue) || '',
};
this.handleChange = this.handleChange.bind(this);
this.handleChangeEvent = this.handleChangeEvent.bind(this);
}
componentDidUpdate() {
if (this.selection && this.input) {
this.input.setSelectionRange(
this.selection.start,
this.selection.start
);
}
this.selection = null;
}
handleInputChange = (value, ...args) => {
const { formatter } = this;
const { value: oldValue } = this.state;
if (!(formatter instanceof Formatter)) {
return;
}
const { selectionStart, selectionEnd } = this.input;
const selection = {
start: selectionStart,
end: selectionEnd,
};
const validationInfo = formatter.validate(value, selection);
const newValue = validationInfo.valid ? value : oldValue;
if (!validationInfo.valid) {
this.selection = validationInfo.selection || null;
}
this.setState({
value: newValue,
});
if (validationInfo.valid) {
const parsedValue = formatter.parse(newValue);
this.handleChangeEvent(parsedValue, ...args);
}
};
handleChangeEvent = (value, ...args) => {
const { onChange } = this.props;
if (onChange && value !== this.lastSend) {
this.lastSend = value;
onChange(value, ...args);
}
};
handleChange = (value, ...args) => {
const { formatter } = this;
if (!(formatter instanceof Formatter)) {
return;
}
const parsedValue = formatter.parse(value);
this.setState({
value: formatter.format(parsedValue),
});
this.handleChangeEvent(parsedValue, ...args);
};
handleEnter = (value, ...args) => {
const { onEnter } = this.props;
if (onEnter) {
const { formatter } = this;
if (!(formatter instanceof Formatter)) {
return;
}
const parsedValue = formatter.parse(value);
onEnter(parsedValue, ...args);
}
};
render() {
const { value } = this.state;
const {
defaultValue,
initialFormatter,
inputRef,
...props
} = this.props;
if (!(initialFormatter instanceof Formatter)) {
return null;
}
return (
<Input
{...props}
inputRef={(ref) => {
this.input = ref;
if (inputRef) {
inputRef(ref);
}
}}
value={value}
onChange={this.handleInputChange}
onBlur={this.handleChange}
onEnter={this.handleEnter}
/>
);
}
}
FormattedInput.propTypes = {
/**
* An instance of a formatter that will be used to format the value of the
* input.
*/
initialFormatter: PropTypes.instanceOf(Formatter).isRequired,
/**
* The function that will be called on change.
*/
onChange: PropTypes.func,
onEnter: PropTypes.func,
inputRef: PropTypes.func,
/**
* The initial value of the input.
*/
defaultValue: PropTypes.any, // eslint-disable-line react/forbid-prop-types
};
FormattedInput.defaultProps = {
onChange: null,
onEnter: null,
defaultValue: null,
inputRef: null,
};
FormattedInput.displayName = 'FormattedInput';