Skip to content

Commit

Permalink
storybook works
Browse files Browse the repository at this point in the history
  • Loading branch information
lorensr committed Aug 9, 2016
0 parents commit 0273bd3
Show file tree
Hide file tree
Showing 11 changed files with 616 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

# production
build

# misc
.DS_Store
npm-debug.log
10 changes: 10 additions & 0 deletions .storybook/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { setAddon, configure } from '@kadira/storybook'
import infoAddon from '@kadira/react-storybook-addon-info'

setAddon(infoAddon);

function loadStories() {
require('../src/stories')
}

configure(loadStories, module)
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 by Loren Sands-Ramshaw

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



For src/SegmentedControl.css:

Copyright (c) 2016 by François St-Germain (http://codepen.io/fstgerm/pen/Jafyj)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
A good-lookin' segmented control React component: `<SegmentedControl>`. Includes a version for [Formsy](https://github.com/christianalfoni/formsy-react): `<FormsySegmentedControl>`.

## Component background

In iOS, a segmented control is usually used to [display different views](https://developer.apple.com/ios/human-interface-guidelines/ui-controls/segmented-controls/) (the equivalent of [tabs](https://material.google.com/components/tabs.html) in material design). However, segmented controls are increasingly being used in lieu of [plain radio buttons](https://material.google.com/components/selection-controls.html#selection-controls-radio-button) or select inputs (dropdowns, or material [menus](https://material.google.com/components/menus.html)). See for instance the designer [lukew](http://www.twitter.com/lukew)'s recommendations:

- [Dropdowns should be the UI of Last Resort](http://www.lukew.com/ff/entry.asp?1950)
- [Obvious Always Wins](http://www.lukew.com/ff/entry.asp?1945)
- [Video: Using Radio Groups for Input](http://www.lukew.com/ff/entry.asp?1890)

Other good choices are radio groups, like [this Ionic component](http://ionicframework.com/docs/v2/components/#radio), or a button list, as used in the Yo or Thumbtack apps, that looks the same, except on click, instead of seeing a checkmark, you're taken to the next screen.

A similar component in material design is the [toggle button](https://material.google.com/components/buttons.html#buttons-toggle-buttons).

## Component

*[Component demo]*

pics

## Usage

```sh
npm install --save segmented-control
```

`<SegmentedControl>` props:

- `name: PropTypes.string.isRequired`: name of the radio `<input>`s. Also the attribute included in the first argument of Formsy's `onSubmit`.
- `options: PropTypes.array.isRequired`: each an object with:
- `label`: display text
- `value`: value passed to `setValue` and Formsy's `onSubmit`
- `default: true`: one object must have this
- `disabled: true`: optional
- `style: PropTypes.object`: common styles are width and color
- `setValue: PropTypes.func`: callback on input change, passed the value string. Called once initially with the default value on mount.

```jsx
import { SegmentedControl } from 'segmented-control'
...
<SegmentedControl
name="oneDisabled"
options={[
{ label: "One", value: "one", disabled: true },
{ label: "Two", value: "two" },
{ label: "Three", value: "three", default: true },
{ label: "Four", value: "four" }
]}
setValue={newValue => this.doSomething(newValue)}
style={{ width: 400, color: '#ab47bc' }} // purple400
/>
```

`<FormsySegmentedControl>` has the same props, but includes `Formsy.Mixin` and calls Formsy's `setValue`, so that the value is contained in `onSubmit` (see the event triggered by submitting the [demo form]()). In the example below, the first arg of `onSubmit` would be `{exampleInput: 'two'}`:

```jsx
import Formsy from 'formsy-react'
import RaisedButton from 'material-ui/RaisedButton'
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'
import { FormsySegmentedControl } from 'segmented-control'

...

<MuiThemeProvider>
<Formsy.Form
onValidSubmit={this.onSubmit}
>
<FormsySegmentedControl
name="exampleInput"
options={[
{ label: "One", value: "one" },
{ label: "Two", value: "two", default: true },
{ label: "Three", value: "three" }
]}
style={{ width: 300, color: 'rgb(0, 188, 212)' }} // match default material-ui primary teal
/>
<RaisedButton
type="submit"
label="submit formsy form"
style={{
display: 'block',
width: 200,
margin: "20px auto"
}}
primary
/>
</Formsy.Form>
</MuiThemeProvider>
```

## Credits

- CSS from @fstgerm: http://code.fstgerm.com/pure-css-segmented-controls/
- Contributions by [these fine folks]
21 changes: 21 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>React App</title>
</head>
<body>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` in this folder.
To create a production bundle, use `npm run build`.
-->
</body>
</html>
55 changes: 55 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "segmented-control",
"version": "0.0.1",
"description": "💅 A good-lookin' segmented control React component",
"keywords": [
"react",
"reactjs",
"react-component",
"segmented-control",
"forms",
"input",
"select",
"radio",
"formsy",
"formsy-react",
"formsy-component"
],
"license": "MIT",
"files": [
"src"
],
"main": [
"src/index.js"
],
"repository": {
"type" : "git",
"url" : "https://github.com/lorensr/segmented-control.git"
},
"devDependencies": {
"react-scripts": "0.2.1",
"@kadira/storybook": "^2.0.0",
"@kadira/react-storybook-addon-info": "^3.1.2",
"@kadira/react-storybook-decorator-centered": "^1.0.0",
"@kadira/storybook-deployer": "^1.0.0",
"react-tap-event-plugin": "^1.0.0",
"material-ui": "^0.15.3"
},
"dependencies": {
"react": "^15.2.1",
"react-dom": "^15.2.1",
"formsy-react": "^0.18.1",
"lodash": "^4.14.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject",
"storybook": "start-storybook -p 9009",
"build-storybook": "build-storybook",
"deploy-storybook": "storybook-to-ghpages"
},
"eslintConfig": {
"extends": "./node_modules/react-scripts/config/eslint.js"
}
}
19 changes: 19 additions & 0 deletions src/FormsySegmentedControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react'
import Formsy from 'formsy-react'

import SegmentedControl from './SegmentedControl'

const FormsySegmentedControl = React.createClass({
mixins: [Formsy.Mixin],

render() {
return (
<SegmentedControl
{...this.props}
setValue={this.setValue}
/>
)
}
})

export default FormsySegmentedControl
Loading

0 comments on commit 0273bd3

Please sign in to comment.