diff --git a/src/scripts/Checkbox.js b/src/scripts/Checkbox.js deleted file mode 100644 index 5c42a910e..000000000 --- a/src/scripts/Checkbox.js +++ /dev/null @@ -1,67 +0,0 @@ -import React, { Component } from 'react'; -import PropTypes from 'prop-types'; -import classnames from 'classnames'; -import { FormElement } from './FormElement'; - -export default class Checkbox extends Component { - componentWillReceiveProps(nextProps) { - const input = this.node.getElementsByTagName('input')[0]; - if (nextProps.defaultChecked !== input.checked) { - input.checked = nextProps.defaultChecked; - } - } - - renderCheckbox({ className, label, checkboxRef, ...props }) { - const checkClassNames = classnames(className, 'slds-checkbox'); - return ( - - ); - } - - render() { - const { grouped, required, error, totalCols, cols, ...props } = this.props; - const formElemProps = { required, error, totalCols, cols }; - return grouped ? ( - this.renderCheckbox(props) - ) : ( - (this.node = node)} - {...formElemProps} - > - {this.renderCheckbox(props)} - - ); - } -} - -Checkbox.propTypes = { - className: PropTypes.string, - label: PropTypes.string, - required: PropTypes.bool, - // FormElement.propTypes.error - error: PropTypes.oneOfType([ - PropTypes.bool, - PropTypes.string, - PropTypes.shape({ - message: PropTypes.string, - }), - ]), - totalCols: PropTypes.number, - cols: PropTypes.number, - grouped: PropTypes.bool, - checkboxRef: PropTypes.func, - name: PropTypes.string, - value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), - checked: PropTypes.bool, - defaultChecked: PropTypes.bool, -}; diff --git a/src/scripts/Checkbox.tsx b/src/scripts/Checkbox.tsx new file mode 100644 index 000000000..53fd949b8 --- /dev/null +++ b/src/scripts/Checkbox.tsx @@ -0,0 +1,66 @@ +import React, { Component, InputHTMLAttributes } from 'react'; +import classnames from 'classnames'; +import { FormElement, FormElementProps } from './FormElement'; + +export type CheckboxProps = { + className?: string; + label?: string; + required?: boolean; + error?: FormElementProps['error']; + totalCols?: number; + cols?: number; + grouped?: boolean; + name?: string; + value?: string | number; + checked?: boolean; + defaultChecked?: boolean; + checkboxRef?: (node: HTMLLabelElement | null) => void; +} & InputHTMLAttributes; + +export class Checkbox extends Component { + node: HTMLDivElement | HTMLLabelElement | null = null; + + componentWillReceiveProps(nextProps: Readonly) { + if (this.node) { + const input = this.node.getElementsByTagName('input')[0]; + if ( + nextProps.defaultChecked !== undefined && + nextProps.defaultChecked !== input.checked + ) { + input.checked = nextProps.defaultChecked; + } + } + } + + renderCheckbox({ className, label, checkboxRef, ...props }: CheckboxProps) { + const checkClassNames = classnames(className, 'slds-checkbox'); + return ( + + ); + } + + render() { + const { grouped, required, error, totalCols, cols, ...props } = this.props; + const formElemProps = { required, error, totalCols, cols }; + return grouped ? ( + this.renderCheckbox(props) + ) : ( + (this.node = node)} + {...formElemProps} + > + {this.renderCheckbox(props)} + + ); + } +} diff --git a/src/scripts/index.js b/src/scripts/index.js index 0857dcafb..4eec73a38 100644 --- a/src/scripts/index.js +++ b/src/scripts/index.js @@ -5,7 +5,6 @@ import Datepicker from './Datepicker'; import Tabs, { Tab } from './Tabs'; import SalesPath from './SalesPath'; import Modal, { ModalHeader, ModalContent, ModalFooter } from './Modal'; -import Checkbox from './Checkbox'; import CheckboxGroup from './CheckboxGroup'; import Select, { Option } from './Select'; import DateInput from './DateInput'; @@ -41,7 +40,6 @@ export { ModalContent, ModalFooter, SalesPath, - Checkbox, CheckboxGroup, Select, Option, @@ -75,6 +73,7 @@ export * from './BreadCrumbs'; export * from './Button'; export * from './ButtonGroup'; export * from './Container'; +export * from './Checkbox'; export * from './DropdownMenu'; export * from './DropdownButton'; export * from './Icon'; diff --git a/stories/CheckboxStories.js b/stories/CheckboxStories.tsx similarity index 96% rename from stories/CheckboxStories.js rename to stories/CheckboxStories.tsx index ed42ac543..e90aff0da 100644 --- a/stories/CheckboxStories.js +++ b/stories/CheckboxStories.tsx @@ -10,8 +10,8 @@ storiesOf('Checkbox', module) () => (