diff --git a/packages/grafana-ui/src/components/ConfirmButton/ConfirmButton.mdx b/packages/grafana-ui/src/components/ConfirmButton/ConfirmButton.mdx index 6762a0d5691..ec468e467c2 100644 --- a/packages/grafana-ui/src/components/ConfirmButton/ConfirmButton.mdx +++ b/packages/grafana-ui/src/components/ConfirmButton/ConfirmButton.mdx @@ -29,4 +29,22 @@ Apart from the button variant, you can also modify the button size and the butto ``` +## With a custom button + +You can pass a custom button as a child to the `ConfirmButton`. The child will automatically receive the correct `onClick` from `ConfirmButton`. + +```jsx + { + console.log('Action confirmed!'); + }} +> + Click me + +``` + diff --git a/packages/grafana-ui/src/components/ConfirmButton/ConfirmButton.tsx b/packages/grafana-ui/src/components/ConfirmButton/ConfirmButton.tsx index 1ba0d5eb823..18ce89de782 100644 --- a/packages/grafana-ui/src/components/ConfirmButton/ConfirmButton.tsx +++ b/packages/grafana-ui/src/components/ConfirmButton/ConfirmButton.tsx @@ -1,5 +1,5 @@ import { cx, css } from '@emotion/css'; -import React, { PureComponent, SyntheticEvent } from 'react'; +import React, { PureComponent, ReactElement } from 'react'; import { GrafanaTheme2 } from '@grafana/data'; @@ -11,6 +11,7 @@ import { Button, ButtonVariant } from '../Button'; export interface Props extends Themeable2 { /** Confirm action callback */ onConfirm(): void; + children: string | ReactElement; /** Custom button styles */ className?: string; /** Button size */ @@ -36,14 +37,14 @@ interface State { showConfirm: boolean; } -class UnThemedConfirmButton extends PureComponent, State> { +class UnThemedConfirmButton extends PureComponent { mainButtonRef = React.createRef(); confirmButtonRef = React.createRef(); state: State = { showConfirm: false, }; - onClickButton = (event: SyntheticEvent) => { + onClickButton = (event: React.MouseEvent) => { if (event) { event.preventDefault(); } @@ -64,7 +65,7 @@ class UnThemedConfirmButton extends PureComponent } }; - onClickCancel = (event: SyntheticEvent) => { + onClickCancel = (event: React.MouseEvent) => { if (event) { event.preventDefault(); } @@ -80,7 +81,7 @@ class UnThemedConfirmButton extends PureComponent this.props.onCancel(); } }; - onConfirm = (event: SyntheticEvent) => { + onConfirm = (event: React.MouseEvent) => { if (event) { event.preventDefault(); } @@ -118,17 +119,15 @@ class UnThemedConfirmButton extends PureComponent return ( - {typeof children === 'string' ? ( - + + {typeof children === 'string' ? ( {children} - - ) : ( - - {children} - - )} + ) : ( + React.cloneElement(children, { onClick, ref: this.mainButtonRef }) + )} +