Skip to content

Commit b88b64d

Browse files
Used refs instead of id to get wrapper, added showSuggestions prop.
1 parent 7ecd83f commit b88b64d

File tree

11 files changed

+219
-12
lines changed

11 files changed

+219
-12
lines changed

.storybook/config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { configure } from '@kadira/storybook';
2+
3+
function loadStories() {
4+
require('../stories');
5+
}
6+
7+
configure(loadStories, module);

.storybook/webpack.config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// you can use this file to add your custom webpack plugins, loaders and anything you like.
2+
// This is just the basic way to add addional webpack configurations.
3+
// For more information refer the docs: https://goo.gl/qPbSyX
4+
5+
// IMPORTANT
6+
// When you add this file, we won't add the default configurations which is similar
7+
// to "React Create App". This only has babel loader to load JavaScript.
8+
9+
module.exports = {
10+
plugins: [
11+
// your custom plugins
12+
],
13+
module: {
14+
loaders: [
15+
// add your custom loaders.
16+
],
17+
},
18+
};

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"vsicons.presets.angular": false
3+
}

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"start": "node tools/devServer.js",
1212
"build": "NODE_ENV=production ./tools/build.sh",
1313
"buildSite": "NODE_ENV=production webpack --config webpack.config.prod.js --progress",
14-
"prepublish": "npm run build"
14+
"prepublish": "npm run build",
15+
"storybook": "start-storybook -p 6006",
16+
"build-storybook": "build-storybook"
1517
},
1618
"repository": {
1719
"type": "git",
@@ -38,7 +40,8 @@
3840
"react-hot-loader": "^3.0.0-beta.6",
3941
"webpack": "^1.14.0",
4042
"webpack-dev-middleware": "^1.8.4",
41-
"webpack-hot-middleware": "^2.13.2"
43+
"webpack-hot-middleware": "^2.13.2",
44+
"@kadira/storybook": "^2.21.0"
4245
},
4346
"peerDependencies": {
4447
"react": "^0.14.0 || ^15.3.0"

site/src/basic.js

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class BasicExample extends Component {
3434
onChange={this.onChange}
3535
placeholder="Type a Programming Language"
3636
suggestions={data}
37+
shouldRenderSuggestions={value => value.length >= 0}
3738
fromSuggestionsOnly={false} />
3839
);
3940
}

src/Chips.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,18 @@ class Chips extends Component {
1717
};
1818
}
1919

20-
componentDidMount = () => {
21-
this.chips = document.getElementById('chips-wrapper');
22-
}
23-
2420
onBlur = e => {
25-
this.chips.focus();
21+
this.refs.wrapper.focus();
2622
}
2723

2824
onFocus = e => {
29-
this.chips.blur();
25+
this.refs.wrapper.blur();
3026
}
3127

3228
handleKeyDown = e => {
3329
if (!this.props.fromSuggestionsOnly && e.keyCode === 9) {
3430
e.preventDefault();
35-
if (this.state.value) this.addChip(this.state.value);
31+
if (this.state.value.trim()) this.addChip(this.state.value);
3632
}
3733
if (e.keyCode === 8) {
3834
this.onBackspace();
@@ -119,7 +115,7 @@ class Chips extends Component {
119115
render() {
120116

121117
const { value, suggestions } = this.state;
122-
const { placeholder, renderSuggestion, getSuggestionValue } = this.props;
118+
const { placeholder, renderSuggestion, getSuggestionValue, shouldRenderSuggestions } = this.props;
123119
const themr = themeable(this.props.theme);
124120

125121
const inputProps = {
@@ -132,13 +128,14 @@ class Chips extends Component {
132128
};
133129

134130
return (
135-
<div {...themr(200, 'chipsContainer')} id="chips-wrapper" >
131+
<div {...themr(200, 'chipsContainer')} ref="wrapper" >
136132
{this.renderChips()}
137133
<Autosuggest
138134
theme={this.props.theme}
139135
suggestions={this.state.suggestions}
140136
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
141137
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
138+
shouldRenderSuggestions={shouldRenderSuggestions}
142139
renderSuggestion={renderSuggestion}
143140
inputProps={inputProps}
144141
getSuggestionValue={getSuggestionValue}
@@ -156,6 +153,7 @@ Chips.propTypes = {
156153
fromSuggestionsOnly: PropTypes.bool,
157154
uniqueChips: PropTypes.bool,
158155
chips: PropTypes.array,
156+
shouldRenderSuggestions: PropTypes.func,
159157
getSuggestionValue: PropTypes.func,
160158
onChange: PropTypes.func,
161159
renderChip: PropTypes.func,

src/theme.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const theme = {
33
display: "flex",
44
position: "relative",
55
border: "1px solid #ccc",
6+
backgroundColor: '#fff',
67
font: "13.33333px Arial",
7-
zIndex: 1,
88
minHeight: 24,
99
alignItems: "center",
1010
flexWrap: "wrap",
@@ -34,6 +34,7 @@ const theme = {
3434
suggestionsList: {
3535
position: 'absolute',
3636
border: '1px solid #ccc',
37+
zIndex: 10,
3738
left: 0,
3839
top: '100%',
3940
width: '100%',

stories/Button.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
3+
const buttonStyles = {
4+
border: '1px solid #eee',
5+
borderRadius: 3,
6+
backgroundColor: '#FFFFFF',
7+
cursor: 'pointer',
8+
fontSize: 15,
9+
padding: '3px 10px',
10+
margin: 10,
11+
};
12+
13+
const Button = ({ children, onClick }) => (
14+
<button
15+
style={buttonStyles}
16+
onClick={onClick}
17+
>
18+
{children}
19+
</button>
20+
);
21+
22+
Button.propTypes = {
23+
children: React.PropTypes.string.isRequired,
24+
onClick: React.PropTypes.func,
25+
};
26+
27+
export default Button;

stories/Chips.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { Component } from 'react';
2+
import Chips from '../src';
3+
4+
class ChipsExample extends Component {
5+
6+
constructor(props) {
7+
super(props);
8+
this.state = {
9+
chips: []
10+
};
11+
}
12+
13+
updateChips = chips => {
14+
if (this.props.onChange) {
15+
this.props.onChange();
16+
}
17+
this.setState({ chips });
18+
}
19+
20+
render() {
21+
const { chips } = this.state;
22+
return (
23+
<Chips {...this.props} onChange={this.updateChips} value={chips}/>
24+
);
25+
}
26+
}
27+
28+
export default ChipsExample;

stories/Welcome.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from 'react';
2+
3+
const styles = {
4+
main: {
5+
margin: 15,
6+
maxWidth: 600,
7+
lineHeight: 1.4,
8+
fontFamily: '"Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif',
9+
},
10+
11+
logo: {
12+
width: 200,
13+
},
14+
15+
link: {
16+
color: '#1474f3',
17+
textDecoration: 'none',
18+
borderBottom: '1px solid #1474f3',
19+
paddingBottom: 2,
20+
},
21+
22+
code: {
23+
fontSize: 15,
24+
fontWeight: 600,
25+
padding: "2px 5px",
26+
border: "1px solid #eae9e9",
27+
borderRadius: 4,
28+
backgroundColor: '#f3f2f2',
29+
color: '#3a3a3a',
30+
},
31+
32+
note: {
33+
opacity: 0.5,
34+
}
35+
};
36+
37+
export default class Welcome extends React.Component {
38+
showApp(e) {
39+
e.preventDefault();
40+
if(this.props.showApp) this.props.showApp();
41+
}
42+
43+
render() {
44+
return (
45+
<div style={styles.main}>
46+
<h1>Welcome to STORYBOOK</h1>
47+
<p>
48+
This is a UI component dev environment for your app.
49+
</p>
50+
<p>
51+
We've added some basic stories inside the <code style={styles.code}>src/stories</code> directory.
52+
<br/>
53+
A story is a single state of one or more UI components. You can have as many stories as you want.
54+
<br/>
55+
(Basically a story is like a visual test case.)
56+
</p>
57+
<p>
58+
See these sample <a style={styles.link} href='#' onClick={this.showApp.bind(this)}>stories</a> for a component called <code style={styles.code}>Button</code>.
59+
</p>
60+
<p>
61+
Just like that, you can add your own components as stories.
62+
<br />
63+
You can also edit those components and see changes right away.
64+
<br />
65+
(Try editing the <code style={styles.code}>Button</code> component
66+
located at <code style={styles.code}>src/stories/Button.js</code>.)
67+
</p>
68+
<p>
69+
This is just one thing you can do with Storybook.
70+
<br/>
71+
Have a look at the <a style={styles.link} href="https://github.com/kadirahq/react-storybook" target="_blank">React Storybook</a> repo for more information.
72+
</p>
73+
<p style={styles.note}>
74+
<b>NOTE:</b>
75+
<br/>
76+
Have a look at the <code style={styles.code}>.storybook/webpack.config.js</code> to add webpack
77+
loaders and plugins you are using in this project.
78+
</p>
79+
</div>
80+
);
81+
}
82+
}

stories/index.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import { storiesOf, action, linkTo } from '@kadira/storybook';
3+
import Button from './Button';
4+
import Welcome from './Welcome';
5+
import Chips from './Chips';
6+
7+
storiesOf('Welcome', module)
8+
.add('to Storybook', () => (
9+
<Welcome showApp={linkTo('Button')}/>
10+
));
11+
12+
storiesOf('Button', module)
13+
.add('with text', () => (
14+
<Button onClick={action('clicked')}>Hello Button</Button>
15+
))
16+
.add('with some emoji', () => (
17+
<Button onClick={action('clicked')}>😀 😎 👍 💯</Button>
18+
));
19+
20+
const suggestions = [
21+
'JavaScript',
22+
'Ruby',
23+
'Python',
24+
'Java',
25+
'Swift',
26+
'C++',
27+
'C',
28+
'Objective C',
29+
'Go'
30+
];
31+
32+
storiesOf('Chips', module)
33+
.add('basic', () => (
34+
<Chips
35+
placeholder="Type a Programming Language"
36+
suggestions={suggestions}
37+
fromSuggestionsOnly={false}
38+
/>
39+
));

0 commit comments

Comments
 (0)