diff --git a/src/Checkbox.tsx b/src/Checkbox.tsx index db3fd256..0b469652 100644 --- a/src/Checkbox.tsx +++ b/src/Checkbox.tsx @@ -1,9 +1,9 @@ +import noop = require('lodash/noop'); import * as React from 'react'; import { Box } from './Box'; import { Flex } from './Flex'; import { Icon } from './Icon'; -import { Input } from './Input'; import { styled } from './utils'; export interface ICheckboxProps { @@ -16,63 +16,48 @@ export interface ICheckboxProps { onChange?: (checked: boolean) => void; } -export interface ICheckboxState { - checked?: boolean; -} - -export class BasicCheckbox extends React.Component { - constructor(props: ICheckboxProps) { - super(props); - this.state = { checked: this.props.checked }; - } +export const BasicCheckbox = (props: ICheckboxProps) => { + const { id, className, width, height, disabled, onChange = noop } = props; - private onChange = (event: React.ChangeEvent) => { - this.setState({ checked: event.target.checked }); + const [checked, setValue] = React.useState(props.checked || false); + const isChecked = props.hasOwnProperty('checked') ? props.checked : checked; - if (this.props.onChange) { - this.props.onChange(event.target.checked); - } + const handleChange = (event: React.ChangeEvent) => { + setValue(event.target.checked); + onChange(event.target.checked); }; - public render() { - const { checked: stateChecked } = this.state; - const { id, className, width, height, disabled, checked: propsChecked } = this.props; - - const checked = this.props.hasOwnProperty('checked') ? propsChecked : stateChecked; - - return ( - - - - {checked && } - - - ); - } -} + return ( + + + + {isChecked && } + + + ); +}; export const Checkbox = styled(BasicCheckbox as any)``; diff --git a/stories/Checkbox.tsx b/stories/Checkbox.tsx index 624bc2d8..11d2476b 100644 --- a/stories/Checkbox.tsx +++ b/stories/Checkbox.tsx @@ -20,7 +20,6 @@ export const checkboxKnobs = (tabName = 'Checkbox') => { storiesOf('Checkbox', module) .addDecorator(withKnobs) .add('uncontrolled', () => ) - .add('controlled checked', () => ) .addDecorator(StateDecorator(store)) .add('controlled', () => ( - )); + )) + .add('controlled set', () => );