From 7cc470fd70b65cb3d17bf8439b852a153d355b1b Mon Sep 17 00:00:00 2001 From: Greg Thompson Date: Wed, 4 Sep 2019 11:36:21 -0500 Subject: [PATCH] TS button `onClick` clean up (#2282) * button onclick clean up * clickable generics * CL --- CHANGELOG.md | 3 +- src/components/button/button.tsx | 28 +++++++++------- .../button/button_empty/button_empty.tsx | 24 ++++++++++---- .../button/button_icon/button_icon.tsx | 28 +++++++++------- src/components/common.ts | 32 ++++++++++++++++++- src/components/filter_group/index.d.ts | 20 +++++++++--- .../pagination/pagination_button.tsx | 19 ++++++++--- 7 files changed, 114 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f2a266259d..8632524c7f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ **Bug fixes** -- Removed extra right side margin in 'EuiSuperDatePicker' ([#2236](https://github.com/elastic/eui/pull/2236)) +- Removed extra right side margin in `EuiSuperDatePicker` ([#2236](https://github.com/elastic/eui/pull/2236)) +- Fix incorrect `onClick` type for `EuiButtonEmpty` ([#2282](https://github.com/elastic/eui/pull/2282)) ## [`13.7.0`](https://github.com/elastic/eui/tree/v13.7.0) diff --git a/src/components/button/button.tsx b/src/components/button/button.tsx index 3fac06d3d08..ff77079e055 100644 --- a/src/components/button/button.tsx +++ b/src/components/button/button.tsx @@ -3,12 +3,17 @@ import React, { ButtonHTMLAttributes, FunctionComponent, HTMLAttributes, - MouseEventHandler, Ref, } from 'react'; import classNames from 'classnames'; -import { CommonProps, ExclusiveUnion, keysOf } from '../common'; +import { + CommonProps, + ExclusiveUnion, + PropsForAnchor, + PropsForButton, + keysOf, +} from '../common'; import { EuiLoadingSpinner } from '../loading'; import { getSecureRelForTarget } from '../../services'; @@ -65,18 +70,19 @@ export interface EuiButtonProps extends CommonProps { textProps?: HTMLAttributes; } -type EuiButtonPropsForAnchor = EuiButtonProps & - AnchorHTMLAttributes & { - href?: string; - onClick?: MouseEventHandler; +type EuiButtonPropsForAnchor = PropsForAnchor< + EuiButtonProps, + { buttonRef?: Ref; - }; + } +>; -type EuiButtonPropsForButton = EuiButtonProps & - ButtonHTMLAttributes & { - onClick?: MouseEventHandler; +type EuiButtonPropsForButton = PropsForButton< + EuiButtonProps, + { buttonRef?: Ref; - }; + } +>; export type Props = ExclusiveUnion< EuiButtonPropsForAnchor, diff --git a/src/components/button/button_empty/button_empty.tsx b/src/components/button/button_empty/button_empty.tsx index bd5615c7103..557c08f2c8d 100644 --- a/src/components/button/button_empty/button_empty.tsx +++ b/src/components/button/button_empty/button_empty.tsx @@ -1,7 +1,13 @@ import React, { FunctionComponent, HTMLAttributes } from 'react'; import classNames from 'classnames'; -import { CommonProps, keysOf, NoArgCallback } from '../../common'; +import { + CommonProps, + ExclusiveUnion, + PropsForAnchor, + PropsForButton, + keysOf, +} from '../../common'; import { EuiLoadingSpinner } from '../../loading'; import { getSecureRelForTarget } from '../../../services'; import { IconType, EuiIcon } from '../../icon'; @@ -38,7 +44,7 @@ const flushTypeToClassNameMap = { export const FLUSH_TYPES = keysOf(flushTypeToClassNameMap); -export interface EuiButtonEmptyProps { +export interface EuiButtonEmptyProps extends CommonProps { iconType?: IconType; iconSide?: keyof typeof iconSideToClassNameMap; color?: keyof typeof colorToClassNameMap; @@ -48,7 +54,6 @@ export interface EuiButtonEmptyProps { href?: string; target?: string; rel?: string; - onClick?: NoArgCallback; /** * Adds/swaps for loading spinner & disables @@ -68,7 +73,14 @@ export interface EuiButtonEmptyProps { textProps?: Partial>; } -type Props = CommonProps & EuiButtonEmptyProps; +type EuiButtonEmptyPropsForAnchor = PropsForAnchor; + +type EuiButtonEmptyPropsForButton = PropsForButton; + +type Props = ExclusiveUnion< + EuiButtonEmptyPropsForAnchor, + EuiButtonEmptyPropsForButton +>; export const EuiButtonEmpty: FunctionComponent = ({ children, @@ -148,7 +160,7 @@ export const EuiButtonEmpty: FunctionComponent = ({ target={target} rel={secureRel} ref={buttonRef} - {...rest}> + {...rest as EuiButtonEmptyPropsForAnchor}> {innerNode} ); @@ -160,7 +172,7 @@ export const EuiButtonEmpty: FunctionComponent = ({ className={classes} type={type} ref={buttonRef} - {...rest}> + {...rest as EuiButtonEmptyPropsForButton}> {innerNode} ); diff --git a/src/components/button/button_icon/button_icon.tsx b/src/components/button/button_icon/button_icon.tsx index ec7379c6cdc..300a7746735 100644 --- a/src/components/button/button_icon/button_icon.tsx +++ b/src/components/button/button_icon/button_icon.tsx @@ -2,13 +2,18 @@ import React, { AnchorHTMLAttributes, ButtonHTMLAttributes, FunctionComponent, - MouseEventHandler, Ref, } from 'react'; import classNames from 'classnames'; import { getSecureRelForTarget } from '../../../services'; -import { CommonProps, ExclusiveUnion, keysOf } from '../../common'; +import { + CommonProps, + ExclusiveUnion, + PropsForAnchor, + PropsForButton, + keysOf, +} from '../../common'; import { IconType, IconSize, EuiIcon } from '../../icon'; @@ -34,18 +39,19 @@ export interface EuiButtonIconProps extends CommonProps { iconSize?: IconSize; } -type EuiButtonIconPropsForAnchor = EuiButtonIconProps & - AnchorHTMLAttributes & { - href: string; - onClick?: MouseEventHandler; +type EuiButtonIconPropsForAnchor = PropsForAnchor< + EuiButtonIconProps, + { buttonRef?: Ref; - }; + } +>; -export type EuiButtonIconPropsForButton = EuiButtonIconProps & - ButtonHTMLAttributes & { - onClick?: MouseEventHandler; +export type EuiButtonIconPropsForButton = PropsForButton< + EuiButtonIconProps, + { buttonRef?: Ref; - }; + } +>; type Props = ExclusiveUnion< EuiButtonIconPropsForAnchor, diff --git a/src/components/common.ts b/src/components/common.ts index 9fe52d6fc47..804f03d8256 100644 --- a/src/components/common.ts +++ b/src/components/common.ts @@ -1,4 +1,11 @@ -import { Component, FunctionComponent, SFC } from 'react'; +import { + AnchorHTMLAttributes, + ButtonHTMLAttributes, + Component, + FunctionComponent, + MouseEventHandler, + SFC, +} from 'react'; export interface CommonProps { className?: string; @@ -110,3 +117,26 @@ export type DisambiguateSet = { export type ExclusiveUnion = (T | U) extends object // if there are any shared keys between T and U ? (DisambiguateSet & U) | (DisambiguateSet & T) // otherwise the TS union is already unique : T | U; + +/** + * For components that conditionally render