Skip to content

Commit 13dbe83

Browse files
authored
[feature]: placeholder for each input (#122)
1 parent 2a5c7c2 commit 13dbe83

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const ReactCodeInput = dynamic(import('react-code-input'));
8888
| type | string | Only types like: `text`, `number`, `password` and `tel` are accepted. |
8989
| fields | number | Allowed amount of characters to enter. |
9090
| value | string | Setting the value of code input field. |
91+
| placeholder | string | Setting the placeholder of code input field. |
9192
| name | string | Setting the name of component. |
9293
| onChange | func | Function, which is called whenever there is a change of value in the input box. |
9394
| touch | func | Marks the given fields as "touched" to show errors. |

src/ReactCodeInput.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export interface ReactCodeInputProps {
1717
// Allowed amount of characters to enter.
1818
fields?: number
1919

20+
// Placeholder of the inputs.
21+
placeholder?: string
22+
2023
// Value of the input
2124
value?: string
2225

src/ReactCodeInput.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ class ReactCodeInput extends Component {
237237
autoFocus,
238238
autoComplete,
239239
pattern,
240-
inputMode
240+
inputMode,
241+
placeholder
241242
} = this.props,
242243
{ disabled, input, isValid, defaultInputStyle } = this.state,
243244
styles = {
@@ -299,6 +300,7 @@ class ReactCodeInput extends Component {
299300
data-valid={isValid}
300301
pattern={pattern}
301302
inputMode={inputMode}
303+
placeholder={placeholder}
302304
/>
303305
);
304306
})}
@@ -324,6 +326,7 @@ ReactCodeInput.defaultProps = {
324326
ReactCodeInput.propTypes = {
325327
type: PropTypes.oneOf(['text', 'number', 'password', 'tel']),
326328
fields: PropTypes.number,
329+
placeholder: PropTypes.string,
327330
value: PropTypes.string,
328331
onChange: PropTypes.func,
329332
name: PropTypes.string,

src/ReactCodeInput.spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React from 'react';
22
import CodeInputField from './ReactCodeInput.js';
33

44
const numbers = '123456',
5-
chars = '123FE3';
5+
chars = '123FE3',
6+
placeholder = '0';
67

78
describe('CodeInputField', () => {
89
test('renders without error', () => {
@@ -13,9 +14,10 @@ describe('CodeInputField', () => {
1314

1415
test('renders field without any values', () => {
1516
const wrapper = shallow(<CodeInputField/>);
16-
17+
1718
expect(wrapper.find('input')).toHaveLength(4);
1819
expect(wrapper.state().fields).toEqual(4);
20+
expect(CodeInputField.defaultProps.placeholder).toBeUndefined();
1921
expect(wrapper.state().value).toEqual('');
2022
expect(wrapper.state().type).toEqual('text');
2123
expect(wrapper.state().input).toBeInstanceOf(Array);
@@ -40,12 +42,22 @@ describe('CodeInputField', () => {
4042
}
4143
expect(val.join('')).toEqual(chars);
4244
});
45+
46+
test(`renders component with placeholder: ${placeholder}`, () => {
47+
const wrapper = render(<CodeInputField value={numbers} fields={6} type="number" placeholder={placeholder}/>),
48+
val = [];
49+
for (let i = 0; i < numbers.length; i += 1) {
50+
val.push(wrapper.find('input')[i].attribs.value);
51+
}
52+
expect(val.join('')).toEqual(numbers);
53+
});
4354

4455
test('mount component with props: "fields={6}"', () => {
4556
const wrapper = mount(<CodeInputField fields={6}/>);
4657
expect(wrapper.props().fields).toEqual(6);
4758
});
4859

60+
4961
test('should have 4 input felds', () => {
5062
const wrapper = shallow(<CodeInputField/>);
5163
expect(wrapper.find('input')).toHaveLength(4);

src/ReactCodeInput.stories.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import React from 'react';
2-
3-
import { storiesOf } from '@storybook/react';
4-
import { action } from '@storybook/addon-actions';
5-
import { withKnobs, boolean, object, text, select, number } from '@storybook/addon-knobs';
6-
import { withInfo } from '@storybook/addon-info';
1+
import React from 'react';
2+
import { storiesOf } from '@storybook/react';
3+
import { action } from '@storybook/addon-actions';
4+
import { array, withKnobs, boolean, object, text, select, number } from '@storybook/addon-knobs';
5+
import { withInfo } from '@storybook/addon-info';
76

87
import ReactCodeInput from './ReactCodeInput.js';
98
import CodeInputField from "./ReactCodeInput";
@@ -86,6 +85,9 @@ propVariantStories
8685
.add('fields', () =>
8786
<ReactCodeInput fields={number('fields', 6)} />
8887
)
88+
.add('placeholder', () =>
89+
<ReactCodeInput placeholder={text('placeholder', '0')} />
90+
)
8991
.add('inputStyle', () =>
9092
<ReactCodeInput inputStyle={object('inputStyle', inputStyle)} />
9193
)

0 commit comments

Comments
 (0)