-
Notifications
You must be signed in to change notification settings - Fork 5
Switcher #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Switcher #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| 'use strict'; | ||
|
|
||
| import React, { PropTypes } from 'react'; | ||
|
|
||
| import { COLOR } from '../const/theme'; | ||
|
|
||
| import { StyleSheet, css } from '../helpers/styles'; | ||
|
|
||
| export default class Switcher extends React.Component { | ||
| static propTypes = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай добавил onChange и isDisabled |
||
| /** | ||
| * Is switcher in checked state | ||
| */ | ||
| isChecked: PropTypes.bool, | ||
| /** | ||
| * Is switcher in disabled state | ||
| */ | ||
| isDisabled: PropTypes.bool, | ||
| /** | ||
| * On click event handler | ||
| */ | ||
| onClick: PropTypes.func, | ||
| /** | ||
| * On change event handler | ||
| */ | ||
| onChange: PropTypes.func | ||
| }; | ||
|
|
||
| static defaultProps = { | ||
| isChecked: false | ||
| }; | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| isChecked: props.isChecked | ||
| }; | ||
|
|
||
| this._onClickHandler = this._onClickHandler.bind(this); | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нужно добавить обновление стейта на componentWillReceiveProps |
||
| componentWillReceiveProps(newProps) { | ||
| if (this.state.isChecked !== newProps.isChecked) { | ||
| this.setState({ isChecked: newProps.isChecked }); | ||
| } | ||
| } | ||
|
|
||
| _onClickHandler(event) { | ||
| const props = this.props; | ||
|
|
||
| if (!props.isDisabled) { | ||
| this.setState({ isChecked: !this.state.isChecked }, () => { | ||
| props.onClick && props.onClick(event); | ||
| props.onChange && props.onChange(this.state.isChecked); | ||
| }) | ||
| }; | ||
| } | ||
|
|
||
| render() { | ||
| const props = this.props; | ||
|
|
||
| return ( | ||
| <div onClick={this._onClickHandler} > | ||
| <div className={css(STYLE.main, this.state.isChecked && STYLE.on, props.isDisabled && STYLE.isDisabled )}> | ||
| <div className={css(STYLE.toggle, this.state.isChecked && STYLE.toggleOn, props.isDisabled && STYLE.isDisabled )}> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| const STYLE = StyleSheet.create({ | ||
| main: { | ||
| width: '32px', | ||
| height: '16px', | ||
| borderRadius: '50px', | ||
| cursor: 'pointer', | ||
| display: 'inline-block', | ||
| float: 'left', | ||
| boxShadow: 'rgba(27, 42, 48, 0.329412) 0px 1px 2px inset', | ||
| background: 'rgb(195, 200, 203)' | ||
| }, | ||
| toggle: { | ||
| boxShadow: 'rgba(27, 42, 48, 0.4) 0px 2px 3px', | ||
| width: '12px', | ||
| height: '12px', | ||
| left: '2px', | ||
| top: '2px', | ||
| borderRadius: '100%', | ||
| background: '#ffffff', | ||
| position: 'relative', | ||
| transition: 'left .1s ease-in-out' | ||
| }, | ||
| on: { | ||
| background: 'rgb(112, 195, 22)' | ||
| }, | ||
| toggleOn: { | ||
| left: '19px', | ||
| color: '#70c316' | ||
| }, | ||
| isDisabled: { | ||
| opacity: 0.5 | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| ### Usage | ||
|
|
||
| ```js | ||
| import Switcher from '@roistat/ui/lib/Switcher'; | ||
| ``` | ||
| const View = require('../View').default; | ||
|
|
||
| *Switcher* | ||
|
|
||
| <View> | ||
| <Switcher /> | ||
| </View> | ||
|
|
||
| *Switcher is disabled* | ||
|
|
||
| <View> | ||
| <Switcher isDisabled={true} /> | ||
| </View> | ||
|
|
||
| *Switcher is checked* | ||
|
|
||
| <View> | ||
| <Switcher isChecked={true} /> | ||
| </View> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default } from './Switcher'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import React from 'react'; | ||
| import { storiesOf, action } from '@kadira/storybook'; | ||
| import Switcher from './Switcher'; | ||
| import View from '../View'; | ||
|
|
||
| storiesOf('Switcher', module) | ||
| .add('Switcher', () => ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. добавить кейс где он без внешнего состояния переключается |
||
| <View> | ||
| <Switcher /> | ||
| </View> | ||
| )) | ||
|
|
||
| .add('Switcher is disabled', () => ( | ||
| <View> | ||
| <Switcher isDisabled={true} /> | ||
| </View> | ||
| )) | ||
|
|
||
| .add('Switcher is checked', () => ( | ||
| <View> | ||
| <Switcher isChecked={true} /> | ||
| </View> | ||
| )); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
даваем ему сделаем внутреннее состояние isChecked и надо не забыть про componentWillReceiveProps