Skip to content

Commit 2614f40

Browse files
committed
initial commit
0 parents  commit 2614f40

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Selector
2+
3+
Like a radio form-control, but with custom HTML elements
4+
5+
![screenshot.png](screenshot.png)
6+
7+
```jsx
8+
const opts = [
9+
{ img: '/img/pic1.jpg', label: 'First Item', value: 1 },
10+
{ img: '/img/pic2.jpg', label: 'Second Item', value: 2 },
11+
{ img: '/img/pic3.jpg', label: 'Third Item', value: 3 }
12+
]
13+
const onSelect = opt => this.setState({ current: opt.value })
14+
<Selector options={opts} value={this.state.current} onSelect={onSelect} />
15+
```
16+
17+
```jsx
18+
const opts = [
19+
{ el: <div><strong>First</strong> El</div>, value: 1 },
20+
{ el: <div><strong>Second</strong> El</div>, value: 2 },
21+
{ el: <div><strong>Third</strong> El</div>, value: 3 }
22+
]
23+
const onSelect = opt => this.setState({ current: opt.value })
24+
<Selector options={opts} value={this.state.current} onSelect={onSelect} />
25+
</div>
26+
```

demo.jsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react'
2+
import Selector from './index'
3+
4+
export default class SelectorDemo extends React.Component {
5+
constructor(props) {
6+
super(props)
7+
this.state = {
8+
current1: 1,
9+
current2: 1
10+
}
11+
}
12+
render() {
13+
const opts1 = [
14+
{ img: '/img/pic1.jpg', label: 'First Item', value: 1 },
15+
{ img: '/img/pic2.jpg', label: 'Second Item', value: 2 },
16+
{ img: '/img/pic3.jpg', label: 'Third Item', value: 3 }
17+
]
18+
const onSelect1 = opt => { this.setState({ current1: opt.value }); console.log('selected', opt) }
19+
const opts2 = [
20+
{ el: <div><strong>First</strong> El</div>, value: 1 },
21+
{ el: <div><strong>Second</strong> El</div>, value: 2 },
22+
{ el: <div><strong>Third</strong> El</div>, value: 3 }
23+
]
24+
const onSelect2 = opt => { this.setState({ current2: opt.value }); console.log('selected', opt) }
25+
return <div>
26+
<h1>patchkit-selector</h1>
27+
<section className="selector-label-img">
28+
<header>&lt;Selector options=[&#123; img:, label:, value: &#125;, ...]&gt;</header>
29+
<div className="content">
30+
<Selector options={opts1} value={this.state.current1} onSelect={onSelect1} />
31+
</div>
32+
</section>
33+
<section className="selector-el">
34+
<header>&lt;Selector options=[&#123; el:, value: &#125;, ...]&gt;</header>
35+
<div className="content">
36+
<Selector options={opts2} value={this.state.current2} onSelect={onSelect2} />
37+
</div>
38+
</section>
39+
</div>
40+
}
41+
}

index.jsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
import cls from 'classnames'
3+
4+
export default class Selector extends React.Component {
5+
render() {
6+
return <div className="selector">
7+
{ this.props.options.map((o, i) => {
8+
const onClick = () => this.props.onSelect(o)
9+
return <div key={i} className={ cls({ option: true, selected: o.value === this.props.value }) } onClick={onClick}>
10+
{ o.el }
11+
{ o.label ? <div>{o.label}</div> : '' }
12+
{ o.img ? <img src={o.img} /> : '' }
13+
</div>
14+
}) }
15+
</div>
16+
}
17+
}

package.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "patchkit-selector",
3+
"version": "1.0.0",
4+
"description": "Like a radio form-control, but with custom HTML elements",
5+
"main": "index.jsx",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/patchkit/patchkit-selector.git"
12+
},
13+
"keywords": [
14+
"react"
15+
],
16+
"author": "Paul Frazee <[email protected]>",
17+
"license": "GPL-3.0",
18+
"bugs": {
19+
"url": "https://github.com/patchkit/patchkit-selector/issues"
20+
},
21+
"homepage": "https://github.com/patchkit/patchkit-selector#readme",
22+
"dependencies": {
23+
"classnames": "^2.2.3",
24+
"react": "^0.14.6"
25+
},
26+
"devDependencies": {
27+
"babel": "^6.3.26",
28+
"babel-preset-es2015": "^6.3.13",
29+
"babel-preset-react": "^6.3.13",
30+
"babel-preset-stage-0": "^6.3.13",
31+
"babelify": "^7.2.0",
32+
"browserify": "^13.0.0",
33+
"less": "^2.5.3"
34+
},
35+
"browserify": {
36+
"transform": [
37+
[
38+
"envify",
39+
{
40+
"global": true
41+
}
42+
],
43+
[
44+
"babelify",
45+
{
46+
"presets": [
47+
"es2015",
48+
"stage-0",
49+
"react"
50+
]
51+
}
52+
]
53+
]
54+
}
55+
}

screenshot.png

65.4 KB
Loading

styles.less

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.selector {
2+
display: flex;
3+
.option {
4+
flex: 1;
5+
text-align: center;
6+
color: gray;
7+
cursor: pointer;
8+
padding: 10px;
9+
10+
&:hover {
11+
background: #eee;
12+
}
13+
14+
div {
15+
margin-bottom: 5px;
16+
}
17+
img {
18+
border-radius: 10px;
19+
border: 5px solid transparent;
20+
}
21+
&.selected {
22+
color: #4da1ff;
23+
img {
24+
border-color: #4da1ff;
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)