Skip to content

Commit f78d388

Browse files
authored
Feature/support decimal (#3)
* feat(support-decimal): support decimal point in the numpad * feat(readme): add readme with screenshot
1 parent 0b69b97 commit f78d388

File tree

7 files changed

+99
-9
lines changed

7 files changed

+99
-9
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
11
# react-random-numpad
22

3-
A random genarated number pad for react
3+
[![dependencies Status](https://david-dm.org/nutboltu/react-random-numpad/status.svg)](https://david-dm.org/nutboltu/react-random-numpad) [![Build Status](https://travis-ci.org/nutboltu/react-random-numpad.svg?branch=master)](https://travis-ci.org/nutboltu/react-random-numpad) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
4+
5+
A random genarated number pad for react
6+
7+
![Screenshot](/docs/react-random-numpad.png)
8+
9+
See [Demo and Documentation](https://nutboltu.github.io/react-random-numpad).
10+
11+
## Props
12+
13+
The component takes the following props.
14+
15+
| Prop | Type | Default | Description |
16+
|-------------------|------------|-------------|-------------|
17+
| `className` | string | - | Additional classnames for the component |
18+
| `supportDecimal` | boolean | false| Include decimal point support in the numpad|
19+
| `onChange` | _function_ | - |Callback function to invoke when a number is pressed |
20+
21+
## Installation
22+
23+
```bash
24+
npm install react-random-numpad --save
25+
```
26+
27+
## Usage
28+
29+
```javascript
30+
import RandomNumpad from "react-random-numpad";
31+
import "react-random-numpad/dist/style.min.css";
32+
33+
<RandomNumpad
34+
supportDecimal={false}
35+
onChange={() => {}}
36+
classNames="custom-class"
37+
/>
38+
```
39+
40+
## License
41+
42+
MIT Licensed. Copyright (c) Farhad Yasir 2019.

docs/react-random-numpad.png

5.94 KB
Loading

src/@types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export interface IRandomNumpadState {
44

55
export interface IRandomNumpadProps {
66
className?: string;
7+
supportDecimal?: boolean;
78
onChange: (value: string) => void;
89
}

src/components/RandomNumpad.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ class RandomNumpad
1919
this.randomArray = shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
2020
}
2121

22-
public addNumber = (value: number) => {
22+
public addNumber = (value: number | string) => {
2323
this.setState((prevState: IRandomNumpadState) => ({
2424
input: prevState.input.concat(value.toString()),
2525
}), () => {
2626
this.props.onChange(this.state.input);
2727
});
2828
}
2929

30+
public addDecimal = () => {
31+
if (this.state.input.indexOf('.') < 0) {
32+
this.addNumber('.');
33+
}
34+
}
35+
3036
public removeNumber = () => {
3137
this.setState((prevState: IRandomNumpadState) => ({
3238
input: prevState.input.slice(0, prevState.input.length - 1),
@@ -44,7 +50,7 @@ class RandomNumpad
4450
}
4551

4652
public render(): React.ReactNode {
47-
const { className } = this.props;
53+
const { className, supportDecimal } = this.props;
4854
return (
4955
<div className={`${className} ${styles.numpad}`}>
5056
{
@@ -58,8 +64,11 @@ class RandomNumpad
5864
</button>
5965
))
6066
}
67+
{
68+
supportDecimal &&
69+
<button className={styles.buttonKey} onClick={this.addDecimal}> . </button>
70+
}
6171
<button className={styles.buttonKey} onClick={this.removeNumber}> X </button>
62-
<button className={styles.buttonKey} onClick={this.clearNumber}> Cancel </button>
6372
</div>
6473
);
6574
}

src/components/__tests__/RandomNumpad.spec.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import { shallow } from 'enzyme';
55
import RandomNumpad from '../RandomNumpad';
66

77
describe('RandomNumpad Component Suite', () => {
8-
const getComponent = () => shallow(<RandomNumpad onChange={spy()} />);
9-
108
describe('FocusOnScroll Component', () => {
9+
test('should render 11 buttons', () => {
10+
const component = shallow(<RandomNumpad onChange={spy()} />);
11+
const buttonComponents = component.find('button');
12+
expect(buttonComponents).toHaveLength(11);
13+
});
1114
test('should render 12 buttons', () => {
12-
const buttonComponents = getComponent().find('button');
15+
const component = shallow(<RandomNumpad onChange={spy()} supportDecimal />);
16+
const buttonComponents = component.find('button');
1317
expect(buttonComponents).toHaveLength(12);
1418
});
1519
});

src/components/styles.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
width: 40px;
44
background-color: #ddd;
55
display: flex;
6-
align-items: center;
76
justify-content: center;
87
cursor: pointer;
9-
flex: 1 0 30%;
8+
flex: 0 0 30%;
109
margin: 5px;
1110
}
1211

1312
.numpad {
1413
display: flex;
1514
flex-wrap: wrap;
15+
justify-content: flex-end;
1616
}

src/stories/WithDecimalSupport.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as React from 'react';
2+
import { storiesOf } from '@storybook/react';
3+
import { withKnobs } from '@storybook/addon-knobs/react';
4+
import * as styles from './styles.css';
5+
6+
import RandomNumpad from '../components/RandomNumpad';
7+
8+
const stories = storiesOf('RandomNumpad', module);
9+
10+
stories.addDecorator(withKnobs);
11+
12+
class StoryComponent extends React.PureComponent<{}, { value: string }> {
13+
public state = {
14+
value: '',
15+
};
16+
17+
public onChange = (value: string) => {
18+
this.setState({
19+
value,
20+
});
21+
}
22+
23+
public render(): React.ReactNode {
24+
return (
25+
<div className={styles.storyComponent}>
26+
<input value={this.state.value} className={styles.input}/>
27+
<RandomNumpad onChange={this.onChange} className='custom-class' supportDecimal />
28+
</div>
29+
);
30+
}
31+
}
32+
33+
stories.add('With Decimal Support', (() => {
34+
return (
35+
<StoryComponent />
36+
);
37+
}));

0 commit comments

Comments
 (0)