diff --git a/CHANGELOG.md b/CHANGELOG.md index 31f8fa0f268..91df2d6e522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## [`master`](https://github.com/elastic/eui/tree/master) +- Converted `EuiSwitch` to TypeScript ([#2243](https://github.com/elastic/eui/pull/2243)) + **Bug fixes** - Added missing `viewBox` attribute to Docker, Kubernetes, and Redis logos ([#2240](https://github.com/elastic/eui/pull/2240)) diff --git a/src/components/form/index.d.ts b/src/components/form/index.d.ts index 366419d36f9..a31e554069b 100644 --- a/src/components/form/index.d.ts +++ b/src/components/form/index.d.ts @@ -9,7 +9,6 @@ import { CommonProps } from '../common'; /// /// /// -/// /// import { FunctionComponent, FormHTMLAttributes, ReactNode } from 'react'; diff --git a/src/components/form/switch/__snapshots__/switch.test.js.snap b/src/components/form/switch/__snapshots__/switch.test.tsx.snap similarity index 95% rename from src/components/form/switch/__snapshots__/switch.test.js.snap rename to src/components/form/switch/__snapshots__/switch.test.tsx.snap index 642f2dce792..b05767ccc4d 100644 --- a/src/components/form/switch/__snapshots__/switch.test.js.snap +++ b/src/components/form/switch/__snapshots__/switch.test.tsx.snap @@ -5,6 +5,7 @@ exports[`EuiSwitch assigns automatically generated ID to label 1`] = ` class="euiSwitch" > - - - - ); - } -} - -EuiSwitch.propTypes = { - name: PropTypes.string, - id: PropTypes.string, - label: PropTypes.node, - checked: PropTypes.bool, - onChange: PropTypes.func, - disabled: PropTypes.bool, - compressed: PropTypes.bool, -}; diff --git a/src/components/form/switch/switch.test.js b/src/components/form/switch/switch.test.tsx similarity index 65% rename from src/components/form/switch/switch.test.js rename to src/components/form/switch/switch.test.tsx index 50927c7fa6e..a564df98ca1 100644 --- a/src/components/form/switch/switch.test.js +++ b/src/components/form/switch/switch.test.tsx @@ -4,17 +4,25 @@ import { requiredProps } from '../../../test/required_props'; import { EuiSwitch } from './switch'; +const props = { + checked: false, + label: 'Label', + onChange: () => {}, +}; + jest.mock('../form_row/make_id', () => () => 'generated-id'); describe('EuiSwitch', () => { test('is rendered', () => { - const component = render(); + const component = render( + + ); expect(component).toMatchSnapshot(); }); test('assigns automatically generated ID to label', () => { - const component = render(); + const component = render(); expect(component).toMatchSnapshot(); }); diff --git a/src/components/form/switch/switch.tsx b/src/components/form/switch/switch.tsx new file mode 100644 index 00000000000..334b7394bb6 --- /dev/null +++ b/src/components/form/switch/switch.tsx @@ -0,0 +1,86 @@ +import React, { + ButtonHTMLAttributes, + FunctionComponent, + ReactNode, + useState, +} from 'react'; +import classNames from 'classnames'; + +import { CommonProps, Omit } from '../../common'; +import makeId from '../../form/form_row/make_id'; +import { EuiIcon } from '../../icon'; + +export type EuiSwitchEvent = React.BaseSyntheticEvent< + React.MouseEvent, + HTMLButtonElement, + EventTarget & { + checked: boolean; + } +>; + +export type EuiSwitchProps = CommonProps & + Omit, 'onChange'> & { + label: ReactNode; + checked: boolean; + onChange: (event: EuiSwitchEvent) => void; + disabled?: boolean; + compressed?: boolean; + }; + +export const EuiSwitch: FunctionComponent = ({ + label, + id, + name, + checked, + disabled, + compressed, + onChange, + className, + ...rest +}) => { + const [switchId] = useState(id || makeId()); + + const onClick = (e: React.MouseEvent) => { + const event = (e as unknown) as EuiSwitchEvent; + event.target.checked = !checked; + onChange(event); + }; + + const classes = classNames( + 'euiSwitch', + { + 'euiSwitch--compressed': compressed, + }, + className + ); + + return ( +
+ + + +
+ ); +};