From 39dbcf69adca4b5f6181e97d56b49e5fb04c1e86 Mon Sep 17 00:00:00 2001 From: Greg Thompson Date: Wed, 21 Aug 2019 14:36:19 -0500 Subject: [PATCH] Convert `EuiSwitch` to TS (#2243) * convert euiswitch to ts * remove test file * euiswitchevent; store id in state * change interface to type * CL --- CHANGELOG.md | 2 + src/components/form/index.d.ts | 1 - ...itch.test.js.snap => switch.test.tsx.snap} | 10 ++- src/components/form/switch/index.d.ts | 21 ----- src/components/form/switch/index.js | 1 - src/components/form/switch/index.ts | 1 + src/components/form/switch/switch.js | 86 ------------------- .../{switch.test.js => switch.test.tsx} | 12 ++- src/components/form/switch/switch.tsx | 86 +++++++++++++++++++ 9 files changed, 107 insertions(+), 113 deletions(-) rename src/components/form/switch/__snapshots__/{switch.test.js.snap => switch.test.tsx.snap} (95%) delete mode 100644 src/components/form/switch/index.d.ts delete mode 100644 src/components/form/switch/index.js create mode 100644 src/components/form/switch/index.ts delete mode 100644 src/components/form/switch/switch.js rename src/components/form/switch/{switch.test.js => switch.test.tsx} (65%) create mode 100644 src/components/form/switch/switch.tsx 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 ( +
+ + + +
+ ); +};