Skip to content

Commit f725d00

Browse files
committed
✨ Classnames support
1 parent e4d9f70 commit f725d00

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/docs/index.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ class Demo extends Component {
116116
numInputs={numInputs}
117117
isDisabled={isDisabled}
118118
hasErrored={hasErrored}
119-
errorStyle={{
120-
border: '1px solid red',
121-
}}
119+
errorStyle="error"
122120
onChange={this.handleOtpChange}
123121
separator={<span>{separator}</span>}
124122
shouldAutoFocus

src/docs/styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,7 @@ a {
107107
.margin-top--large {
108108
margin-top: 4rem;
109109
}
110+
111+
.error {
112+
border: 1px solid red !important;
113+
}

src/lib/index.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Props = {
1616
focusStyle?: Object,
1717
isDisabled?: boolean,
1818
disabledStyle?: Object,
19-
hasErrored?: boolean,
19+
hasErrored?: boolean,
2020
errorStyle?: Object,
2121
shouldAutoFocus?: boolean,
2222
};
@@ -26,6 +26,8 @@ type State = {
2626
otp: string[],
2727
};
2828

29+
const isObject = obj => typeof obj === 'object';
30+
2931
class SingleOtpInput extends PureComponent<*> {
3032
input: ?HTMLInputElement;
3133

@@ -56,6 +58,8 @@ class SingleOtpInput extends PureComponent<*> {
5658
}
5759
}
5860

61+
getClasses = (...classes) => classes.filter(c => !isObject(c) && c !== false).join(' ');
62+
5963
render() {
6064
const {
6165
separator,
@@ -76,6 +80,11 @@ class SingleOtpInput extends PureComponent<*> {
7680
style={Object.assign(
7781
{ width: '1em', textAlign: 'center' },
7882
inputStyle,
83+
focus && isObject(focusStyle) && focusStyle,
84+
isDisabled && isObject(disabledStyle) && disabledStyle,
85+
hasErrored && isObject(errorStyle) && errorStyle
86+
)}
87+
className={this.getClasses(
7988
focus && focusStyle,
8089
isDisabled && disabledStyle,
8190
hasErrored && errorStyle
@@ -251,7 +260,7 @@ class OtpInput extends Component<Props, State> {
251260
const { containerStyle } = this.props;
252261

253262
return (
254-
<div style={{ display: 'flex', ...containerStyle }}>
263+
<div style={Object.assign({ display: 'flex' }, isObject(containerStyle) && containerStyle )} className={!isObject(containerStyle) && containerStyle}>
255264
{this.renderInputs()}
256265
</div>
257266
);

0 commit comments

Comments
 (0)