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
Switch component added #910
Changes from 11 commits
1bc7014
0f79524
c31b56e
a4ddfe5
220cefc
a61bb52
97b3735
3eb7e0a
e043a9d
2ccdf16
ad24dfc
4dc7a13
889d300
01c2f63
6107268
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,33 @@ | ||
// @flow | ||
import React from 'react'; | ||
import css from './Switch.scss'; | ||
|
||
type Props = { | ||
id?: string | ||
} | ||
|
||
type State = { | ||
checked: boolean | ||
}; | ||
|
||
export default class Switch extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
this.state = { checked: this.props.checked || false }; | ||
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. You could do 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. In line 16, why is it 'this.props.checked' and not 'this.state.checked'? 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. Ooh, you're right it should be |
||
} | ||
|
||
handleChange = () => { | ||
this.setState({ checked: !this.state.checked }); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<label className={css.switch}> | ||
<input type="checkbox" onClick={this.handleChange} /> | ||
<div className={css.slider}>{}</div> | ||
</label> | ||
</div> | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
@import "_legacy.scss"; | ||
@import "_base.scss"; | ||
|
||
.switch { | ||
position: relative; | ||
display: inline-block; | ||
width: 119px; | ||
height: 49px; | ||
} | ||
|
||
.switch input { | ||
position: absolute; | ||
display: none; | ||
} | ||
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. Ahh, while this works, I think you can get away with just using |
||
|
||
.slider { | ||
position: absolute; | ||
font-size: $small-font; | ||
font-family: $font-family; | ||
text-align: center; | ||
vertical-align: middle; | ||
|
||
line-height: 40px; | ||
color: $mulberry-wood; | ||
letter-spacing: 9.44%; | ||
|
||
cursor: pointer; | ||
top: 0; left: 0; right: 0; bottom: 0; | ||
background-color: $mulberry-wood; | ||
border: 0px solid rgba(255, 255, 255, 0.7); | ||
border-radius: 5px; | ||
transition: .2s; | ||
} | ||
|
||
input:checked + .slider { | ||
background-color: $yellow-green; | ||
} | ||
|
||
input:focus + .slider { | ||
box-shadow: 0 0 1px $yellow-green; | ||
} | ||
|
||
.slider:after { | ||
position: absolute; | ||
content: 'OFF'; | ||
height: 43px; | ||
width: 71.4px; | ||
left: 2.3px; | ||
top: 3.5px; | ||
background-color: rgba(255, 255, 255, 0.7); | ||
border: 0px solid rgba(255, 255, 255, 0.7); | ||
border-radius: 5px; | ||
transition: .2s; | ||
} | ||
|
||
input:checked + .slider:after { | ||
content: 'ON'; | ||
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. Let's pass in an i18n key here 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. @julianguyen How do I pass an i18n key here? I'm unable to understand. |
||
-webkit-transform: translateX(43.5px); | ||
-ms-transform: translateX(43.5px); | ||
transform: translateX(43.5px); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// @flow | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import Switch from '../Switch'; | ||
|
||
describe('Switch', () => { | ||
it('always renders the switch', () => { | ||
let wrapper = shallow(<Switch />); | ||
expect(wrapper.find('input').exists()).toEqual(true); | ||
}); | ||
it('checks if switch toggles on click', () => { | ||
let wrapper = shallow(<Switch />); | ||
expect(wrapper.find('input').exists()).toEqual(true); | ||
wrapper.find('input').simulate('click'); | ||
expect(wrapper.find('input').exists()).toEqual(true); | ||
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. I would test the value of checked i.e. |
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react'; | ||
import { withInfo } from '@storybook/addon-info'; | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import Switch from 'bundles/shared/components/Switch'; | ||
|
||
storiesOf('Switch', module) | ||
.add('Switch ON/OFF', withInfo({})(() => | ||
<Switch /> | ||
)); |
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.
It would be great to create a unit test for this component and test toggling it.
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.
I've tested this ! Let me know if it is the right way !