diff --git a/CHANGELOG.md b/CHANGELOG.md index e048ae4e797..beb5cdf503a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## [`master`](https://github.com/elastic/eui/tree/master) +- Converted `EuiRadio` and `EuiRadioGroup` to TypeScript ([#2438](https://github.com/elastic/eui/pull/2438)) - Improved a11y in `EuiImage` ([#2447](https://github.com/elastic/eui/pull/2447)) - Made EuiIcon a PureComponent, to speed up React re-render performance ([#2448](https://github.com/elastic/eui/pull/2448)) - Added ability for `EuiColorStops` to accept user-defined range bounds ([#2396](https://github.com/elastic/eui/pull/2396)) diff --git a/src/components/form/index.d.ts b/src/components/form/index.d.ts index 366419d36f9..fc5c4e83c43 100644 --- a/src/components/form/index.d.ts +++ b/src/components/form/index.d.ts @@ -5,7 +5,6 @@ import { CommonProps } from '../common'; /// /// /// -/// /// /// /// diff --git a/src/components/form/radio/__snapshots__/radio.test.js.snap b/src/components/form/radio/__snapshots__/radio.test.tsx.snap similarity index 100% rename from src/components/form/radio/__snapshots__/radio.test.js.snap rename to src/components/form/radio/__snapshots__/radio.test.tsx.snap diff --git a/src/components/form/radio/__snapshots__/radio_group.test.js.snap b/src/components/form/radio/__snapshots__/radio_group.test.tsx.snap similarity index 100% rename from src/components/form/radio/__snapshots__/radio_group.test.js.snap rename to src/components/form/radio/__snapshots__/radio_group.test.tsx.snap diff --git a/src/components/form/radio/index.d.ts b/src/components/form/radio/index.d.ts deleted file mode 100644 index 432612057fe..00000000000 --- a/src/components/form/radio/index.d.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { CommonProps, Omit } from '../../common'; - -import { - FunctionComponent, - ChangeEventHandler, - HTMLAttributes, - ReactNode, -} from 'react'; - -declare module '@elastic/eui' { - /** - * @see './radio_group.js' - */ - export interface EuiRadioGroupOption { - id: string; - label?: ReactNode; - } - - export type EuiRadioGroupChangeCallback = (id: string, value: string) => void; - - export type EuiRadioGroupProps = CommonProps & - Omit, 'onChange'> & { - disabled?: boolean; - compressed?: boolean; - name?: string; - options?: EuiRadioGroupOption[]; - idSelected?: string; - onChange: EuiRadioGroupChangeCallback; - }; - - export type x = EuiRadioGroupProps['onChange']; - - export const EuiRadioGroup: FunctionComponent; - - export interface EuiRadioProps { - autoFocus?: boolean; - compressed?: boolean; - label?: ReactNode; - name?: string; - value?: string; - checked?: boolean; - disabled?: boolean; - onChange: ChangeEventHandler; // overriding to make it required - } - - export const EuiRadio: FunctionComponent< - CommonProps & HTMLAttributes & EuiRadioProps - >; -} diff --git a/src/components/form/radio/index.js b/src/components/form/radio/index.js deleted file mode 100644 index 4e12ae9073f..00000000000 --- a/src/components/form/radio/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export { EuiRadio } from './radio'; - -export { EuiRadioGroup } from './radio_group'; diff --git a/src/components/form/radio/index.ts b/src/components/form/radio/index.ts new file mode 100644 index 00000000000..f71f4453a11 --- /dev/null +++ b/src/components/form/radio/index.ts @@ -0,0 +1,8 @@ +export { EuiRadio, EuiRadioProps } from './radio'; + +export { + EuiRadioGroup, + EuiRadioGroupProps, + EuiRadioGroupOption, + EuiRadioGroupChangeCallback, +} from './radio_group'; diff --git a/src/components/form/radio/radio.test.js b/src/components/form/radio/radio.test.tsx similarity index 100% rename from src/components/form/radio/radio.test.js rename to src/components/form/radio/radio.test.tsx diff --git a/src/components/form/radio/radio.js b/src/components/form/radio/radio.tsx similarity index 58% rename from src/components/form/radio/radio.js rename to src/components/form/radio/radio.tsx index 30438a68a28..e4dd1ef95ba 100644 --- a/src/components/form/radio/radio.js +++ b/src/components/form/radio/radio.tsx @@ -1,8 +1,32 @@ -import React from 'react'; -import PropTypes from 'prop-types'; +import React, { + FunctionComponent, + ChangeEventHandler, + HTMLAttributes, + ReactNode, +} from 'react'; import classNames from 'classnames'; +import { CommonProps, Omit } from '../../common'; -export const EuiRadio = ({ +export interface RadioProps { + autoFocus?: boolean; + /** + * When `true` creates a shorter height radio row + */ + compressed?: boolean; + label?: ReactNode; + name?: string; + value?: string; + checked?: boolean; + disabled?: boolean; + onChange: ChangeEventHandler; +} + +export interface EuiRadioProps + extends CommonProps, + Omit, 'onChange'>, + RadioProps {} + +export const EuiRadio: FunctionComponent = ({ className, id, name, @@ -54,24 +78,3 @@ export const EuiRadio = ({ ); }; - -EuiRadio.propTypes = { - className: PropTypes.string, - id: PropTypes.string.isRequired, - checked: PropTypes.bool.isRequired, - label: PropTypes.node, - value: PropTypes.string, - onChange: PropTypes.func.isRequired, - disabled: PropTypes.bool, - /** - * when `true` creates a shorter height radio row - */ - compressed: PropTypes.bool, - autoFocus: PropTypes.bool, -}; - -EuiRadio.defaultProps = { - checked: false, - disabled: false, - compressed: false, -}; diff --git a/src/components/form/radio/radio_group.js b/src/components/form/radio/radio_group.js deleted file mode 100644 index 712b4932964..00000000000 --- a/src/components/form/radio/radio_group.js +++ /dev/null @@ -1,57 +0,0 @@ -import React from 'react'; -import PropTypes from 'prop-types'; - -import { EuiRadio } from './radio'; - -export const EuiRadioGroup = ({ - options, - idSelected, - onChange, - name, - className, - disabled, - compressed, - ...rest -}) => ( -
- {options.map((option, index) => { - const { disabled: isOptionDisabled, ...optionRest } = option; - return ( - - ); - })} -
-); - -EuiRadioGroup.propTypes = { - disabled: PropTypes.bool, - name: PropTypes.string, - options: PropTypes.arrayOf( - PropTypes.shape({ - id: PropTypes.string.isRequired, - label: PropTypes.node, - value: PropTypes.string, - disabled: PropTypes.bool, - }) - ).isRequired, - idSelected: PropTypes.string, - onChange: PropTypes.func.isRequired, - /** - * Tightens up the spacing between radio rows and sends down the - * compressed prop to the radio itself - */ - compressed: PropTypes.bool, -}; - -EuiRadioGroup.defaultProps = { - options: [], -}; diff --git a/src/components/form/radio/radio_group.test.js b/src/components/form/radio/radio_group.test.tsx similarity index 97% rename from src/components/form/radio/radio_group.test.js rename to src/components/form/radio/radio_group.test.tsx index 653d757872c..fa9271a8469 100644 --- a/src/components/form/radio/radio_group.test.js +++ b/src/components/form/radio/radio_group.test.tsx @@ -9,7 +9,7 @@ jest.mock('../radio', () => ({ EuiRadio: 'eui_radio' })); describe('EuiRadioGroup', () => { test('is rendered', () => { const component = render( - {}} /> + {}} /> ); expect(component).toMatchSnapshot(); diff --git a/src/components/form/radio/radio_group.tsx b/src/components/form/radio/radio_group.tsx new file mode 100644 index 00000000000..ec6dda906c0 --- /dev/null +++ b/src/components/form/radio/radio_group.tsx @@ -0,0 +1,54 @@ +import React, { FunctionComponent, HTMLAttributes } from 'react'; +import { CommonProps, Omit } from '../../common'; + +import { EuiRadio, RadioProps } from './radio'; + +export interface EuiRadioGroupOption + extends Omit { + id: string; +} + +export type EuiRadioGroupChangeCallback = (id: string, value?: string) => void; + +export type EuiRadioGroupProps = CommonProps & + Omit, 'onChange'> & { + disabled?: boolean; + /** + * Tightens up the spacing between radio rows and sends down the + * compressed prop to the radio itself + */ + compressed?: boolean; + name?: string; + options: EuiRadioGroupOption[]; + idSelected?: string; + onChange: EuiRadioGroupChangeCallback; + }; + +export const EuiRadioGroup: FunctionComponent = ({ + options = [], + idSelected, + onChange, + name, + className, + disabled, + compressed, + ...rest +}) => ( +
+ {options.map((option, index) => { + const { disabled: isOptionDisabled, ...optionRest } = option; + return ( + + ); + })} +
+);