@@ -9,6 +9,7 @@ const DELETE = 46;
9
9
const SPACEBAR = 32 ;
10
10
11
11
type Props = {
12
+ placeholder : string ,
12
13
numInputs : number ,
13
14
onChange : Function ,
14
15
separator ?: Object ,
@@ -71,6 +72,7 @@ class SingleOtpInput extends PureComponent<*> {
71
72
72
73
render ( ) {
73
74
const {
75
+ placeholder,
74
76
separator,
75
77
isLastChild,
76
78
inputStyle,
@@ -98,6 +100,7 @@ class SingleOtpInput extends PureComponent<*> {
98
100
isDisabled && isStyleObject ( disabledStyle ) && disabledStyle ,
99
101
hasErrored && isStyleObject ( errorStyle ) && errorStyle
100
102
) }
103
+ placeholder = { placeholder }
101
104
className = { this . getClasses (
102
105
inputStyle ,
103
106
focus && focusStyle ,
@@ -135,6 +138,22 @@ class OtpInput extends Component<Props, State> {
135
138
getOtpValue = ( ) =>
136
139
this . props . value ? this . props . value . toString ( ) . split ( '' ) : [ ] ;
137
140
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
+
138
157
// Helper to return OTP from input
139
158
handleOtpChange = ( otp : string [ ] ) = > {
140
159
const { onChange } = this . props ;
@@ -275,10 +294,11 @@ class OtpInput extends Component<Props, State> {
275
294
} = this . props ;
276
295
const otp = this . getOtpValue ( ) ;
277
296
const inputs = [ ] ;
278
-
297
+ const placeholder = this . getPlaceholderValue ( ) ;
279
298
for ( let i = 0 ; i < numInputs ; i ++ ) {
280
299
inputs . push (
281
300
< SingleOtpInput
301
+ placeholder = { placeholder && placeholder [ i ] }
282
302
key = { i }
283
303
focus = { activeInput === i }
284
304
value = { otp && otp [ i ] }
0 commit comments