Skip to content

Commit 6d9fdd3

Browse files
committed
feat: add examples
1 parent dcd632e commit 6d9fdd3

File tree

7 files changed

+117
-34
lines changed

7 files changed

+117
-34
lines changed

examples/.storybook/config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { configure } from '@storybook/react'
2-
import { withOptions } from '@storybook/addon-options'
2+
import { setOptions } from '@storybook/addon-options'
33

4-
withOptions({
4+
setOptions({
55
name: 'One React',
66
url: 'https://github.com/one-react/input',
77
goFullScreen: false,
8-
showAddonPanel: true,
9-
addonPanelInRight: true,
8+
showAddonPanel: false,
9+
addonPanelInRight: false,
1010
})
1111

1212
// automatically import all files ending with *.story.tsx

examples/stories/example.tsx

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,69 @@
1-
import { Input } from 'or-input'
21
import React, { PureComponent } from 'react'
32

3+
import Input from 'or-input'
4+
45
export default class Example extends PureComponent<{}, {}> {
56
state = {
7+
mail: '',
68
name: '',
7-
age: ''
9+
keywords: '',
10+
age: '',
11+
zipcode: ''
812
}
913

1014
render() {
1115
return (
12-
<div>
13-
<Input
14-
placeholder="name"
15-
value={this.state.name}
16-
maxlength={12}
17-
onChange={this.handleChange('name')}
18-
/>
19-
<Input
20-
numericInput={true}
21-
placeholder="age"
22-
value={this.state.age}
23-
maxlength={12}
24-
onChange={this.handleChange('age')}
25-
/>
16+
<div className="example-wrapper">
17+
<div className="default-input">
18+
<h1>default input:</h1>
19+
<Input
20+
placeholder="mail"
21+
value={this.state.mail}
22+
onChange={this.handleChange('mail')}
23+
/>
24+
</div>
25+
<div className="">
26+
<h1>input with maxlength:</h1>
27+
<div className="input-with-label">
28+
<div className="input-label">Name:</div>
29+
<Input
30+
placeholder="name maxlength 10"
31+
value={this.state.name}
32+
maxlength={10}
33+
onChange={this.handleChange('name')}
34+
/>
35+
</div>
36+
<div className="input-with-label">
37+
<div className="input-label">Keywords:</div>
38+
<Input
39+
placeholder="keywords maxlength 12"
40+
value={this.state.keywords}
41+
maxlength={12}
42+
onChange={this.handleChange('keywords')}
43+
/>
44+
</div>
45+
</div>
46+
<div className="">
47+
<h1>numeric input:</h1>
48+
<Input
49+
numericInput={true}
50+
placeholder="age"
51+
value={this.state.age}
52+
maxlength={3}
53+
onChange={this.handleChange('age')}
54+
/>
55+
</div>
56+
<div className="">
57+
<h1>input value can be cleared:</h1>
58+
<Input
59+
canClear={false}
60+
numericInput={true}
61+
placeholder="zipcode"
62+
value={this.state.zipcode}
63+
maxlength={8}
64+
onChange={this.handleChange('zipcode')}
65+
/>
66+
</div>
2667
</div>
2768
)
2869
}

examples/stories/index.story.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import React from 'react'
44

55
import { previewCode } from './util'
66

7+
import Input from 'or-input'
78
import Example from './example'
89

910
import './styles.scss'
1011

11-
storiesOf('or-sample', module)
12+
storiesOf('or-input', module)
1213
.addDecorator(
1314
withInfo({
1415
inline: true,
16+
propTables: [Input],
1517
propTablesExclude: [Example],
1618
styles: {
1719
jsxInfoContent: {
@@ -21,7 +23,7 @@ storiesOf('or-sample', module)
2123
}
2224
})
2325
)
24-
.add('sample', () => <Example />, {
26+
.add('example', () => <Example />, {
2527
info: {
2628
source: false,
2729
text: previewCode(require('!!raw-loader!./example.tsx'))

examples/stories/styles.scss

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1-
.or-btn {
2-
margin-left: 20px;
3-
margin-bottom: 20px;
1+
.example-wrapper {
2+
h1 {
3+
margin-top: 50px;
4+
}
5+
6+
.default-input {
7+
.or-input {
8+
width: 160px;
9+
}
10+
}
11+
12+
.input-with-label {
13+
display: flex;
14+
align-items: center;
15+
width: 280px;
16+
margin-bottom: 20px;
17+
18+
.input-label {
19+
flex: 1;
20+
}
21+
22+
.or-input {
23+
width: 200px;
24+
}
25+
}
426
}
27+

src/Input.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,41 @@ import React, { PureComponent } from 'react'
33

44
interface Props {
55
/**
6-
* additional classname for input
6+
* additional classname
77
*/
88
classname?: string
99

10-
type?: 'text' | 'number'
11-
10+
/**
11+
* whether the input box can only enter numbers
12+
* @default false
13+
*/
1214
numericInput?: boolean
1315

16+
/**
17+
* placeholder for input box
18+
*/
1419
placeholder?: string
1520

21+
/**
22+
* value of the input box
23+
*/
1624
value: string
1725

26+
/**
27+
* maxlength of the input box
28+
*/
1829
maxlength?: number
1930

20-
withClearIcon?: boolean
31+
/**
32+
* whether the value of the input box can be cleared by clicking the clear icon
33+
* @default true
34+
*/
35+
canClear?: boolean
2136

2237
/**
2338
* callback triggered input's onchange event
2439
**/
25-
onChange?: (value) => void
40+
onChange: (value) => void
2641
}
2742

2843
export class Input extends PureComponent<Props, {}> {
@@ -32,7 +47,7 @@ export class Input extends PureComponent<Props, {}> {
3247
placeholder,
3348
value,
3449
maxlength,
35-
withClearIcon = true
50+
canClear = true
3651
} = this.props
3752
const inputClass = clx(classname, 'or-input')
3853

@@ -44,7 +59,7 @@ export class Input extends PureComponent<Props, {}> {
4459
maxLength={maxlength}
4560
onChange={this.handleChange}
4661
/>
47-
{withClearIcon && (
62+
{canClear && (
4863
<div className="or-clear-icon" onClick={this.handleClear} />
4964
)}
5065
</div>

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import './styles.scss'
2-
export { Input } from './Input'
2+
export { Input as default } from './Input'

src/styles.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
display: inline-block;
66

77
input{
8+
width: 100%;
89
outline: none;
910
line-height: 38px;
1011
height: 38px;
11-
padding: 0 5px;
12+
padding: 0 30px 0 5px;
1213
font-size: 14px;
14+
box-sizing: border-box;
1315
}
1416

1517
input::-webkit-outer-spin-button,

0 commit comments

Comments
 (0)