Skip to content

Commit 6d2c288

Browse files
authored
Merge pull request #11 from devfolioco/input-restrict-num
Prop to restrict input to numbers
2 parents 3fd8581 + 21b3786 commit 6d2c288

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ export default class App extends Component {
122122
<td>false</td>
123123
<td>Auto focuses input on inital page load.</td>
124124
</tr>
125+
<tr>
126+
<td>isInputNum</td>
127+
<td>boolean</td>
128+
<td>false</td>
129+
<td>false</td>
130+
<td>Restrict input to only numbers.</td>
131+
</tr>
125132
</table>
126133

127134
## Development

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-otp-input",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "A fully customizable, one-time password input component for the web built with React",
55
"main": "lib/index.js",
66
"scripts": {

src/demo/index.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Demo extends Component {
1212
separator: '-',
1313
isDisabled: false,
1414
hasErrored: false,
15+
isInputNum: false,
1516
};
1617
}
1718

@@ -34,7 +35,7 @@ class Demo extends Component {
3435
};
3536

3637
render() {
37-
const { otp, numInputs, separator, isDisabled, hasErrored } = this.state;
38+
const { otp, numInputs, separator, isDisabled, hasErrored, isInputNum } = this.state;
3839

3940
return (
4041
<div className="container">
@@ -93,6 +94,18 @@ class Demo extends Component {
9394
Error
9495
</label>
9596
</div>
97+
<div className="side-bar__segment">
98+
<label htmlFor="isInputNum">
99+
<input
100+
id="isInputNum"
101+
name="isInputNum"
102+
type="checkbox"
103+
checked={isInputNum}
104+
onChange={this.handleCheck}
105+
/>
106+
Input numbers only
107+
</label>
108+
</div>
96109
<div className="side-bar__segment side-bar__segment--bottom">
97110
<a href="https://github.com/ajayns/react-otp-input">
98111
Documentation and Source
@@ -119,6 +132,7 @@ class Demo extends Component {
119132
errorStyle="error"
120133
onChange={this.handleOtpChange}
121134
separator={<span>{separator}</span>}
135+
isInputNum={isInputNum}
122136
shouldAutoFocus
123137
/>
124138
</div>

src/lib/index.jsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Props = {
1919
hasErrored?: boolean,
2020
errorStyle?: Object,
2121
shouldAutoFocus?: boolean,
22+
isInputNum?: boolean,
2223
};
2324

2425
type State = {
@@ -76,9 +77,12 @@ class SingleOtpInput extends PureComponent<*> {
7677
focusStyle,
7778
disabledStyle,
7879
shouldAutoFocus,
80+
isInputNum,
7981
...rest
8082
} = this.props;
8183

84+
const numValueLimits = isInputNum ? { min: 0, max: 9 } : {};
85+
8286
return (
8387
<div style={{ display: 'flex', alignItems: 'center' }}>
8488
<input
@@ -94,7 +98,8 @@ class SingleOtpInput extends PureComponent<*> {
9498
isDisabled && disabledStyle,
9599
hasErrored && errorStyle
96100
)}
97-
type="tel"
101+
type={isInputNum ? 'number' : 'tel'}
102+
{...numValueLimits}
98103
maxLength="1"
99104
ref={input => {
100105
this.input = input;
@@ -227,6 +232,7 @@ class OtpInput extends Component<Props, State> {
227232
hasErrored,
228233
errorStyle,
229234
shouldAutoFocus,
235+
isInputNum,
230236
} = this.props;
231237
const inputs = [];
232238

@@ -255,6 +261,7 @@ class OtpInput extends Component<Props, State> {
255261
hasErrored={hasErrored}
256262
errorStyle={errorStyle}
257263
shouldAutoFocus={shouldAutoFocus}
264+
isInputNum={isInputNum}
258265
/>
259266
);
260267
}

0 commit comments

Comments
 (0)