You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
import React, { ReactElement, useCallback, useState } from 'react'; |
|
|
|
import { ConfirmModal } from '@grafana/ui'; |
|
|
|
interface WithConfirmProps { |
|
children: ReactElement; |
|
title?: string; |
|
body?: React.ReactNode; |
|
confirmText?: string; |
|
disabled?: boolean; |
|
} |
|
|
|
const WithConfirm = (props: WithConfirmProps) => { |
|
const { children, title = 'Are you sure to delete?', body, confirmText = 'Delete', disabled } = props; |
|
|
|
const [showConfirmation, setShowConfirmation] = useState<boolean>(false); |
|
|
|
const onClickCallback = useCallback((event: any) => { |
|
event.stopPropagation(); |
|
|
|
setShowConfirmation(true); |
|
}, []); |
|
|
|
const onConfirmCallback = useCallback(() => { |
|
if (children.props.onClick) { |
|
children.props.onClick(); |
|
} |
|
|
|
setShowConfirmation(false); |
|
}, [children]); |
|
|
|
return ( |
|
<> |
|
{showConfirmation && ( |
|
<ConfirmModal |
|
isOpen |
|
title={title} |
|
confirmText={confirmText} |
|
dismissText="Cancel" |
|
onConfirm={onConfirmCallback} |
|
body={body} |
|
onDismiss={() => { |
|
setShowConfirmation(false); |
|
}} |
|
/> |
|
)} |
|
{React.cloneElement(children, { |
|
disabled: children.props.disabled || disabled, |
|
onClick: onClickCallback, |
|
})} |
|
</> |
|
); |
|
}; |
|
|
|
export default WithConfirm;
|
|
|