Skip to content

Commit d2bccd1

Browse files
committed
✨ Error props and styling
1 parent 7d4c013 commit d2bccd1

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

src/docs/index.jsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Demo extends Component {
1111
numInputs: 4,
1212
separator: '-',
1313
isDisabled: false,
14+
hasErrored: false,
1415
};
1516
}
1617

@@ -22,8 +23,9 @@ class Demo extends Component {
2223
this.setState({ [e.target.name]: e.target.value });
2324
};
2425

25-
handleCheck = () => {
26-
this.setState(prevState => ({ isDisabled: !prevState.isDisabled }));
26+
handleCheck = e => {
27+
const { name } = e.target;
28+
this.setState(prevState => ({ [name]: !prevState[name] }));
2729
};
2830

2931
handleSubmit = e => {
@@ -32,7 +34,7 @@ class Demo extends Component {
3234
};
3335

3436
render() {
35-
const { otp, numInputs, separator, isDisabled } = this.state;
37+
const { otp, numInputs, separator, isDisabled, hasErrored } = this.state;
3638

3739
return (
3840
<div className="container">
@@ -79,6 +81,18 @@ class Demo extends Component {
7981
Disabled
8082
</label>
8183
</div>
84+
<div className="side-bar__segment">
85+
<label htmlFor="hasErrored">
86+
<input
87+
id="hasErrored"
88+
name="hasErrored"
89+
type="checkbox"
90+
checked={hasErrored}
91+
onChange={this.handleCheck}
92+
/>
93+
Error
94+
</label>
95+
</div>
8296
<div className="side-bar__segment side-bar__segment--bottom">
8397
<a href="https://github.com/ajayns/react-otp-input">
8498
Documentation and Source
@@ -100,7 +114,11 @@ class Demo extends Component {
100114
border: '1px solid rgba(0,0,0,0.3)',
101115
}}
102116
numInputs={numInputs}
103-
disabled={isDisabled}
117+
isDisabled={isDisabled}
118+
hasErrored={hasErrored}
119+
errorStyle={{
120+
border: '1px solid red',
121+
}}
104122
onChange={this.handleOtpChange}
105123
separator={<span>{separator}</span>}
106124
shouldAutoFocus

src/lib/index.jsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ type Props = {
1414
containerStyle?: Object,
1515
inputStyle?: Object,
1616
focusStyle?: Object,
17-
disabled?: boolean,
17+
isDisabled?: boolean,
1818
disabledStyle?: Object,
19+
hasErrored?: boolean,
20+
errorStyle?: Object,
1921
shouldAutoFocus?: boolean,
2022
};
2123

@@ -60,7 +62,9 @@ class SingleOtpInput extends PureComponent<*> {
6062
isLastChild,
6163
inputStyle,
6264
focus,
63-
disabled,
65+
isDisabled,
66+
hasErrored,
67+
errorStyle,
6468
focusStyle,
6569
disabledStyle,
6670
...rest
@@ -73,14 +77,15 @@ class SingleOtpInput extends PureComponent<*> {
7377
{ width: '1em', textAlign: 'center' },
7478
inputStyle,
7579
focus && focusStyle,
76-
disabled && disabledStyle
80+
isDisabled && disabledStyle,
81+
hasErrored && errorStyle
7782
)}
7883
type="tel"
7984
maxLength="1"
8085
ref={input => {
8186
this.input = input;
8287
}}
83-
disabled={disabled}
88+
disabled={isDisabled}
8489
{...rest}
8590
/>
8691
{!isLastChild && separator}
@@ -93,7 +98,7 @@ class OtpInput extends Component<Props, State> {
9398
static defaultProps = {
9499
numInputs: 4,
95100
onChange: (otp: number): void => console.log(otp),
96-
disabled: false,
101+
isDisabled: false,
97102
shouldAutoFocus: false,
98103
};
99104

@@ -203,8 +208,10 @@ class OtpInput extends Component<Props, State> {
203208
inputStyle,
204209
focusStyle,
205210
separator,
206-
disabled,
211+
isDisabled,
207212
disabledStyle,
213+
hasErrored,
214+
errorStyle,
208215
shouldAutoFocus,
209216
} = this.props;
210217
const inputs = [];
@@ -228,8 +235,10 @@ class OtpInput extends Component<Props, State> {
228235
inputStyle={inputStyle}
229236
focusStyle={focusStyle}
230237
isLastChild={i === numInputs - 1}
231-
disabled={disabled}
238+
isDisabled={isDisabled}
232239
disabledStyle={disabledStyle}
240+
hasErrored={hasErrored}
241+
errorStyle={errorStyle}
233242
shouldAutoFocus={shouldAutoFocus}
234243
/>
235244
);

0 commit comments

Comments
 (0)