Skip to content

Commit 19680fa

Browse files
aromalanilapollonian
authored andcommitted
➕ Added placeholder support
1 parent 95ca83d commit 19680fa

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/lib/index.jsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const DELETE = 46;
99
const SPACEBAR = 32;
1010

1111
type Props = {
12+
placeholder: string,
1213
numInputs: number,
1314
onChange: Function,
1415
separator?: Object,
@@ -71,6 +72,7 @@ class SingleOtpInput extends PureComponent<*> {
7172

7273
render() {
7374
const {
75+
placeholder,
7476
separator,
7577
isLastChild,
7678
inputStyle,
@@ -98,6 +100,7 @@ class SingleOtpInput extends PureComponent<*> {
98100
isDisabled && isStyleObject(disabledStyle) && disabledStyle,
99101
hasErrored && isStyleObject(errorStyle) && errorStyle
100102
)}
103+
placeholder={placeholder}
101104
className={this.getClasses(
102105
inputStyle,
103106
focus && focusStyle,
@@ -135,6 +138,22 @@ class OtpInput extends Component<Props, State> {
135138
getOtpValue = () =>
136139
this.props.value ? this.props.value.toString().split('') : [];
137140

141+
getPlaceholderValue=()=>{
142+
const {placeholder,numInputs}= this.props;
143+
144+
if(!placeholder) return;
145+
146+
if(placeholder.length===1){
147+
return placeholder.repeat(numInputs);
148+
}
149+
else if(placeholder.length===numInputs){
150+
return placeholder
151+
}
152+
else{
153+
console.error("Invalid placeholder value");
154+
}
155+
}
156+
138157
// Helper to return OTP from input
139158
handleOtpChange = (otp: string[]) => {
140159
const { onChange } = this.props;
@@ -275,10 +294,11 @@ class OtpInput extends Component<Props, State> {
275294
} = this.props;
276295
const otp = this.getOtpValue();
277296
const inputs = [];
278-
297+
const placeholder = this.getPlaceholderValue();
279298
for (let i = 0; i < numInputs; i++) {
280299
inputs.push(
281300
<SingleOtpInput
301+
placeholder={placeholder && placeholder[i]}
282302
key={i}
283303
focus={activeInput === i}
284304
value={otp && otp[i]}

0 commit comments

Comments
 (0)