|
1 | 1 | import * as React from 'react'; |
2 | | -import './index.css'; |
| 2 | +import { Global, css } from '@emotion/core'; |
| 3 | +import { Container, Input, Item } from './styles'; |
3 | 4 |
|
4 | 5 | const KEY_CODE = { |
5 | 6 | BACKSPACE: 8, |
@@ -104,47 +105,52 @@ export default ({ length = 4, onChange, placeholder = '·' }: Props) => { |
104 | 105 | }, [value]); |
105 | 106 |
|
106 | 107 | return ( |
107 | | - <div |
108 | | - className='ReactInputVerificationCode__container' |
109 | | - style={ |
110 | | - { |
111 | | - '--activeIndex': activeIndex, |
112 | | - '--itemsCount': length, |
113 | | - '--itemWidth': '4.5rem', |
114 | | - '--itemHeight': '5rem', |
115 | | - '--itemSpacing': '1rem', |
116 | | - } as React.CSSProperties |
117 | | - } |
118 | | - > |
119 | | - <input |
120 | | - ref={codeInputRef} |
121 | | - className='ReactInputVerificationCode__input' |
122 | | - autoComplete='one-time-code' |
123 | | - type='text' |
124 | | - inputMode='decimal' |
125 | | - id='one-time-code' |
126 | | - // use onKeyUp rather than onChange for a better control |
127 | | - // onChange is still needed to handle the autocompletion |
128 | | - // when receiving a code by SMS |
129 | | - onChange={onInputChange} |
130 | | - onKeyUp={onInputKeyUp} |
131 | | - onBlur={onInputBlur} |
| 108 | + <React.Fragment> |
| 109 | + <Global |
| 110 | + styles={css` |
| 111 | + :root { |
| 112 | + --ReactInputVerificationCode-itemWidth: 4.5rem; |
| 113 | + --ReactInputVerificationCode-itemHeight: 5rem; |
| 114 | + --ReactInputVerificationCode-itemSpacing: 1rem; |
| 115 | + } |
| 116 | + `} |
132 | 117 | /> |
133 | 118 |
|
134 | | - {itemsRef.map((ref, i) => ( |
135 | | - <div |
136 | | - key={i} |
137 | | - ref={ref} |
138 | | - role='button' |
139 | | - tabIndex={0} |
140 | | - className={`ReactInputVerificationCode__item ${ |
141 | | - activeIndex === i ? 'is-active' : '' |
142 | | - }`} |
143 | | - onFocus={onItemFocus(i)} |
144 | | - > |
145 | | - {value[i] || placeholder} |
146 | | - </div> |
147 | | - ))} |
148 | | - </div> |
| 119 | + <Container |
| 120 | + // needed for styling |
| 121 | + itemsCount={length} |
| 122 | + > |
| 123 | + <Input |
| 124 | + ref={codeInputRef} |
| 125 | + className='ReactInputVerificationCode__input' |
| 126 | + autoComplete='one-time-code' |
| 127 | + type='text' |
| 128 | + inputMode='decimal' |
| 129 | + id='one-time-code' |
| 130 | + // use onKeyUp rather than onChange for a better control |
| 131 | + // onChange is still needed to handle the autocompletion |
| 132 | + // when receiving a code by SMS |
| 133 | + onChange={onInputChange} |
| 134 | + onKeyUp={onInputKeyUp} |
| 135 | + onBlur={onInputBlur} |
| 136 | + // needed for styling |
| 137 | + activeIndex={activeIndex} |
| 138 | + /> |
| 139 | + |
| 140 | + {itemsRef.map((ref, i) => ( |
| 141 | + <Item |
| 142 | + key={i} |
| 143 | + ref={ref} |
| 144 | + role='button' |
| 145 | + tabIndex={0} |
| 146 | + onFocus={onItemFocus(i)} |
| 147 | + // needed for emotion-styled |
| 148 | + isActive={i === activeIndex} |
| 149 | + > |
| 150 | + {value[i] || placeholder} |
| 151 | + </Item> |
| 152 | + ))} |
| 153 | + </Container> |
| 154 | + </React.Fragment> |
149 | 155 | ); |
150 | 156 | }; |
0 commit comments