@skyscanner/backpack-web 4.0.1 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ # bpk-component-split-input
2
+
3
+ > Backpack split-input component.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install bpk-component-split-input --save-dev
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import React from 'react';
15
+ import BpkSplitInput, { INPUT_TYPES } from 'bpk-component-split-input';
16
+
17
+ export default () => (
18
+ <BpkSplitInput
19
+ type={INPUT_TYPES.number}
20
+ name="otpInput"
21
+ id="otpInput"
22
+ label="otp input"
23
+ onChange={(e) => console.log('On Input Change')}
24
+ onSubmit={(e) => console.log('On Submit')}
25
+ />
26
+ );
27
+ ```
28
+
29
+ ## Props
30
+
31
+
32
+ | Property | PropType | Required | Default Value |
33
+ | ---------------- | -------------------------- | ------------------- | ------------------------ |
34
+ | id | string | true | - |
35
+ | name | string | true | - |
36
+ | type | INPUT_TYPES (one of) | false | INPUT_TYPES.number |
37
+ | inputLength | number | false | 4 |
38
+ | placeholder | string | false | - |
39
+ | label | string | true | - |
40
+ | large | bool | false | true |
41
+ | onChange | bool | true | - |
42
+ | onSubmit | bool | true | - |
@@ -0,0 +1,27 @@
1
+ /*
2
+ * Backpack - Skyscanner's Design System
3
+ *
4
+ * Copyright 2016 Skyscanner Ltd
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ /* @flow strict */
19
+
20
+ import BpkSplitInput, {
21
+ type Props as BpkSplitInputProps,
22
+ INPUT_TYPES,
23
+ } from './src/BpkSplitInput';
24
+
25
+ export type { BpkSplitInputProps };
26
+ export { INPUT_TYPES };
27
+ export default BpkSplitInput;
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "bpk-component-split-input",
3
+ "version": "1.0.0",
4
+ "description": "Backpack split-input component.",
5
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git@github.com:Skyscanner/backpack.git"
9
+ },
10
+ "author": "Backpack Design System <backpack@skyscanner.net>",
11
+ "main": "index.js",
12
+ "publishConfig": {
13
+ "registry": "https://registry.npmjs.org/"
14
+ },
15
+ "dependencies": {
16
+ "bpk-component-input": "^6.1.16",
17
+ "bpk-mixins": "^31.1.1",
18
+ "bpk-react-utils": "^5.0.0",
19
+ "prop-types": "^15.5.8"
20
+ },
21
+ "peerDependencies": {
22
+ "react": "^16.3.0 || ^17.0.0"
23
+ }
24
+ }
@@ -0,0 +1,71 @@
1
+ /*
2
+ * Backpack - Skyscanner's Design System
3
+ *
4
+ * Copyright 2016 Skyscanner Ltd
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ /* @flow strict */
19
+
20
+ import React, { Component } from 'react';
21
+ import PropTypes from 'prop-types';
22
+
23
+ import BpkInput from '../../bpk-component-input';
24
+ import { cssModules } from '../../bpk-react-utils';
25
+
26
+ import STYLES from './BpkInputField.module.scss';
27
+
28
+ const getClassName = cssModules(STYLES);
29
+
30
+ class BpkInputField extends Component {
31
+ componentDidUpdate(prevProps) {
32
+ const { focus } = this.props;
33
+ if (prevProps.focus !== focus && this.input && focus) {
34
+ this.input.focus();
35
+ this.input.select();
36
+ }
37
+ }
38
+
39
+ render() {
40
+ const { focus, id, index, label, value, ...rest } = this.props;
41
+ return (
42
+ <div key={index} className={getClassName('BpkInputField')}>
43
+ <BpkInput
44
+ id={id}
45
+ autoComplete="off"
46
+ maxLength="1"
47
+ aria-label={`${label} ${index}`}
48
+ inputRef={(input) => {
49
+ this.input = input;
50
+ }}
51
+ value={value || ''}
52
+ {...rest}
53
+ />
54
+ </div>
55
+ );
56
+ }
57
+ }
58
+
59
+ BpkInputField.propTypes = {
60
+ id: PropTypes.string.isRequired,
61
+ label: PropTypes.string.isRequired,
62
+ value: PropTypes.string,
63
+ focus: PropTypes.bool.isRequired,
64
+ index: PropTypes.number.isRequired,
65
+ };
66
+
67
+ BpkInputField.defaultProps = {
68
+ value: '',
69
+ };
70
+
71
+ export default BpkInputField;
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Backpack - Skyscanner's Design System
3
+ *
4
+ * Copyright 2016 Skyscanner Ltd
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ /* stylelint-disable */
19
+ @import '~bpk-mixins';
20
+
21
+ .BpkInputField {
22
+ display: flex;
23
+ max-width: 3*bpk-spacing-xxl();
24
+ margin-right: bpk-spacing-xs();
25
+ align-items: center;
26
+
27
+ input {
28
+ text-align: center;
29
+ }
30
+ }
31
+
32
+ .BpkInputField:last-child {
33
+ margin-right: $bpk-spacing-none;
34
+ }
@@ -0,0 +1,243 @@
1
+ /*
2
+ * Backpack - Skyscanner's Design System
3
+ *
4
+ * Copyright 2016 Skyscanner Ltd
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ /* @flow strict */
19
+
20
+ import React, { Component } from 'react';
21
+ import PropTypes from 'prop-types';
22
+
23
+ import { cssModules } from '../../bpk-react-utils';
24
+ import { INPUT_TYPES } from '../../bpk-component-input';
25
+
26
+ import InputField from './BpkInputField';
27
+ import STYLES from './BpkSplitInput.module.scss';
28
+
29
+ const getClassName = cssModules(STYLES);
30
+
31
+ // keyCode constants
32
+ const BACKSPACE = 8;
33
+ const LEFT_ARROW = 37;
34
+ const RIGHT_ARROW = 39;
35
+ const DELETE = 46;
36
+ const SPACEBAR = 32;
37
+ const ENTER = 13;
38
+
39
+ class BpkSplitInput extends Component {
40
+ constructor(props) {
41
+ super(props);
42
+
43
+ this.state = {
44
+ focusedInput: 0,
45
+ inputValue: [],
46
+ };
47
+ }
48
+
49
+ onInputChange = (input) => {
50
+ this.setState({ inputValue: input });
51
+ const { onChange } = this.props;
52
+ const value = input.join('');
53
+ onChange(value);
54
+ };
55
+
56
+ updateInputValue = (value) => {
57
+ const { focusedInput, inputValue } = this.state;
58
+ inputValue[focusedInput] = value;
59
+
60
+ this.onInputChange(inputValue);
61
+ };
62
+
63
+ handleSubmit = () => {
64
+ const { onSubmit } = this.props;
65
+ const { inputValue } = this.state;
66
+ if (this.validateInput(inputValue)) {
67
+ const value = inputValue.join('');
68
+ onSubmit(value);
69
+ }
70
+ };
71
+
72
+ validateInput = (inputValue) => {
73
+ let index = 0;
74
+ while (index < inputValue.length) {
75
+ if (!this.isInputValid(inputValue[index])) {
76
+ this.setState({ focusedInput: index });
77
+ return false;
78
+ }
79
+ index += 1;
80
+ }
81
+ return true;
82
+ };
83
+
84
+ isNumeric = () => this.props.type === INPUT_TYPES.number;
85
+
86
+ isInputValid = (value) => {
87
+ const isTypeValid = this.isNumeric()
88
+ ? /^\d$/.test(value)
89
+ : typeof value === 'string';
90
+ return isTypeValid && value.trim().length === 1;
91
+ };
92
+
93
+ focusInput = (inputIndex) => {
94
+ const { inputLength } = this.props;
95
+ const focusedInput = Math.max(Math.min(inputLength - 1, inputIndex), 0);
96
+ this.setState({ focusedInput });
97
+ };
98
+
99
+ focusNextInput = () => {
100
+ const { focusedInput } = this.state;
101
+ this.focusInput(focusedInput + 1);
102
+ };
103
+
104
+ focusPreviousInput = () => {
105
+ const { focusedInput } = this.state;
106
+ this.focusInput(focusedInput - 1);
107
+ };
108
+
109
+ handleOnPaste = (e) => {
110
+ e.preventDefault();
111
+ const { inputLength } = this.props;
112
+ const { focusedInput, inputValue } = this.state;
113
+
114
+ const pastedData = e.clipboardData
115
+ .getData('text/plain')
116
+ .slice(0, inputLength - focusedInput)
117
+ .split('');
118
+ const charsReceived = pastedData.length;
119
+
120
+ let firstInvalidInputPosition;
121
+ let position = focusedInput;
122
+ for (position; position < charsReceived; position += 1) {
123
+ const firstElement = pastedData.shift();
124
+ if (this.isInputValid(firstElement)) {
125
+ inputValue[position] = firstElement;
126
+ } else if (typeof firstInvalidInputPosition === 'undefined') {
127
+ firstInvalidInputPosition = position;
128
+ }
129
+ }
130
+ this.focusInput(
131
+ typeof firstInvalidInputPosition !== 'undefined'
132
+ ? firstInvalidInputPosition
133
+ : position,
134
+ );
135
+ this.onInputChange(inputValue);
136
+ };
137
+
138
+ handleOnKeyDown = (e) => {
139
+ if (e.keyCode === BACKSPACE || e.key === 'Backspace') {
140
+ e.preventDefault();
141
+ this.updateInputValue('');
142
+ this.focusPreviousInput();
143
+ } else if (e.keyCode === DELETE || e.key === 'Delete') {
144
+ e.preventDefault();
145
+ this.updateInputValue('');
146
+ } else if (
147
+ e.keyCode === LEFT_ARROW ||
148
+ e.key === 'Left' ||
149
+ e.key === 'ArrowLeft'
150
+ ) {
151
+ e.preventDefault();
152
+ this.focusPreviousInput();
153
+ } else if (
154
+ e.keyCode === RIGHT_ARROW ||
155
+ e.key === 'Right' ||
156
+ e.key === 'ArrowRight'
157
+ ) {
158
+ e.preventDefault();
159
+ this.focusNextInput();
160
+ } else if (e.keyCode === ENTER || e.key === 'Enter') {
161
+ e.preventDefault();
162
+ this.handleSubmit();
163
+ } else if (
164
+ e.keyCode === SPACEBAR ||
165
+ e.key === ' ' ||
166
+ e.key === 'Spacebar' ||
167
+ e.key === 'Space'
168
+ ) {
169
+ e.preventDefault();
170
+ }
171
+ };
172
+
173
+ handleOnChange = (e) => {
174
+ const { value } = e.target;
175
+ if (this.isInputValid(value)) {
176
+ this.updateInputValue(value);
177
+ this.focusNextInput();
178
+ }
179
+ };
180
+
181
+ handleOnFocus = (e, index) => {
182
+ this.setState({ focusedInput: index });
183
+ e.target.select();
184
+ };
185
+
186
+ renderInputs = () => {
187
+ const { focusedInput, inputValue } = this.state;
188
+ const { id, inputLength, label, large, name, placeholder, type } =
189
+ this.props;
190
+ const inputs = [];
191
+ for (let index = 0; index < inputLength; index += 1) {
192
+ inputs.push(
193
+ <InputField
194
+ key={index}
195
+ index={index}
196
+ focus={focusedInput === index}
197
+ id={`${id}-${index}`}
198
+ label={label}
199
+ name={name}
200
+ type={type}
201
+ large={large}
202
+ value={inputValue && inputValue[index]}
203
+ placeholder={placeholder}
204
+ onChange={this.handleOnChange}
205
+ onInput={this.handleOnChange}
206
+ onKeyDown={this.handleOnKeyDown}
207
+ onPaste={this.handleOnPaste}
208
+ onFocus={(e) => this.handleOnFocus(e, index)}
209
+ />,
210
+ );
211
+ }
212
+
213
+ return inputs;
214
+ };
215
+
216
+ render() {
217
+ return (
218
+ <div className={getClassName('BpkSplitInput')}>{this.renderInputs()}</div>
219
+ );
220
+ }
221
+ }
222
+
223
+ BpkSplitInput.propTypes = {
224
+ type: PropTypes.oneOf([INPUT_TYPES.text, INPUT_TYPES.number]),
225
+ id: PropTypes.string.isRequired,
226
+ label: PropTypes.string.isRequired,
227
+ name: PropTypes.string.isRequired,
228
+ inputLength: PropTypes.number,
229
+ placeholder: PropTypes.string,
230
+ onChange: PropTypes.func.isRequired,
231
+ onSubmit: PropTypes.func.isRequired,
232
+ large: PropTypes.bool,
233
+ };
234
+
235
+ BpkSplitInput.defaultProps = {
236
+ type: INPUT_TYPES.number,
237
+ inputLength: 4,
238
+ large: true,
239
+ placeholder: '',
240
+ };
241
+
242
+ export default BpkSplitInput;
243
+ export { INPUT_TYPES };
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Backpack - Skyscanner's Design System
3
+ *
4
+ * Copyright 2016 Skyscanner Ltd
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ @keyframes bpk-keyframe-spin{100%{transform:rotate(1turn)}}.bpk-split-input{display:flex}
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Backpack - Skyscanner's Design System
3
+ *
4
+ * Copyright 2016 Skyscanner Ltd
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ /* stylelint-disable */
20
+
21
+ @import '~bpk-mixins/index';
22
+
23
+ .BpkSplitInput {
24
+ @include bpk-body-default;
25
+ display: flex;
26
+ width: 100%;
27
+ padding-top: bpk-spacing-xs();
28
+ flex-direction: row;
29
+ align-items: center;
30
+ direction: ltr;
31
+
32
+ &__textbox {
33
+ display: flex;
34
+ align-items: center;
35
+ }
36
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyscanner/backpack-web",
3
- "version": "4.0.1",
3
+ "version": "4.1.0",
4
4
  "description": "Backpack Design System web library",
5
5
  "repository": {
6
6
  "type": "git",