-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.jsx
222 lines (194 loc) · 5.53 KB
/
index.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { isNumeric } from '@shopgate/engage/core';
import style from './style';
/**
* The Quantity Picker component.
*/
class QuantityPicker extends Component {
static propTypes = {
editMode: PropTypes.bool,
onChange: PropTypes.func,
onToggleEditMode: PropTypes.func,
quantity: PropTypes.number,
};
static defaultProps = {
editMode: false,
onChange: () => {},
quantity: 1,
onToggleEditMode: () => {},
};
/**
* Constructor.
* @param {Object} props The component props.
*/
constructor(props) {
super(props);
this.regex = /^([0-9]{0,4})$/;
this.state = {
quantity: this.initialQuantity,
};
}
/**
* Called after mount. Focuses the input if the edit mode is active.
*/
componentDidMount() {
if (this.props.editMode) {
this.input.focus();
}
/**
* Prevent the opening of the context menu when this
* input is focused and the value is selected.
*/
this.input.addEventListener('contextmenu', (event) => {
event.preventDefault();
event.stopPropagation();
return false;
});
}
/**
* The componentWillReceiveProps lifecycle hook. I will bring the input into the correct state.
* @param {Object} nextProps The next set of props.
*/
componentWillReceiveProps(nextProps) {
if (nextProps.editMode) {
this.input.focus();
} else {
this.input.blur();
}
if (nextProps.quantity && this.props.quantity !== nextProps.quantity) {
this.updateQuantityInState(nextProps.quantity);
}
}
/**
* Only update when certain state changes are made.
* @param {Object} nextProps The next set of props.
* @param {Object} nextState The next component state.
* @returns {boolean}
*/
shouldComponentUpdate(nextProps, nextState) {
return this.state.quantity !== nextState.quality;
}
/**
* Returns the initial quantity based on the props.
* If the props are set to 0 or lower, it will fall back to 1.
* @return {number}
*/
get initialQuantity() {
if (this.props.quantity === 0 || this.props.quantity < this.defaultQuantity) {
return this.defaultQuantity;
}
return this.props.quantity;
}
/**
* Sets the comoponent ref.
* @param {HTMLElement} input The input field ref.
*/
setRef = (input) => {
this.input = input;
};
/**
* The default quantity.
* @type {number}
*/
defaultQuantity = 1;
/**
* Event handler for the the onChange event of the input.
* @param {Object} event The event object.
*/
handleInputChange = (event) => {
const prevQuantity = this.state.quantity;
let nextQuantity = event.target.value;
if (this.regex.test(nextQuantity) === false) {
// Reset the invalid value to the previous
nextQuantity = prevQuantity;
}
// Update the quantity state with the new input.
this.updateQuantityInState(nextQuantity);
};
/**
* Event handler for the the onClick event of the input.
* @param {Object} event The event object.
*/
handleInputClick = (event) => {
// Prevent the native focus event ...
event.stopPropagation();
event.preventDefault();
// ... and trigger it manually.
// This way we avoid the select actions (copy, paste, cut) to become visible.
this.input.blur();
this.props.onToggleEditMode(true);
};
/**
* Event handler for the the onFocus event of the input.
*/
handleInputFocus = () => {
this.input.select();
this.props.onToggleEditMode(true);
};
/**
* Event handler for the onSubmit event of the form which wraps the input.
* @param {Object} event The event object.
*/
handleSubmitForm = (event) => {
event.preventDefault();
this.input.blur();
};
/**
* Event handler for the the onBlur event of the input.
* @param {Object} event The event object.
*/
handleInputBlur = (event) => {
/**
* Performs the actual logic to handle the blur event of the input.
*/
const handleBlur = () => {
// Deactivate the edit mode
this.props.onToggleEditMode(false);
if (this.props.quantity !== this.state.quantity) {
this.props.onChange(parseInt(this.state.quantity, 10));
}
};
if (!event.target.value || event.target.value < this.defaultQuantity) {
// Set the quantity state back to default, if the value of the input is invalid.
this.updateQuantityInState(this.defaultQuantity, handleBlur);
} else {
handleBlur();
}
};
/**
* Updates the quantity within the component state. It takes care,
* that the state quantity is always a numeric value.
* @param {string|number} quantity The new quantity
* @param {Function} [callback] Callback for the setState call.
*/
updateQuantityInState(quantity, callback = () => {}) {
const sanitizedQuantity = isNumeric(quantity) ? parseInt(quantity, 10) : '';
this.setState({
quantity: sanitizedQuantity,
}, callback);
}
/**
* Renders the component.
* @return {JSX}
*/
render() {
return (
<form onSubmit={this.handleSubmitForm}>
<input
ref={this.setRef}
type="number"
className={style}
value={this.state.quantity}
onChange={this.handleInputChange}
onClick={this.handleInputClick}
onFocus={this.handleInputFocus}
onBlur={this.handleInputBlur}
min={this.defaultQuantity}
data-test-id="quantityPicker"
/>
</form>
);
}
}
export default QuantityPicker;