Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/Switcher/Switcher.jsx
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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

даваем ему сделаем внутреннее состояние isChecked и надо не забыть про componentWillReceiveProps

Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}

Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
});
24 changes: 24 additions & 0 deletions src/Switcher/Switcher.md
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>
1 change: 1 addition & 0 deletions src/Switcher/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Switcher';
23 changes: 23 additions & 0 deletions src/Switcher/story.jsx
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', () => (
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
));