feat(confirm-dialog): add render function in children

This commit is contained in:
George Lima 2019-01-21 18:41:48 -03:00
parent 7a5969e783
commit d7edbb2f13
2 changed files with 43 additions and 33 deletions

View File

@ -50,21 +50,29 @@ type Props = {
renderTrigger: (() => void) => Element<*>, renderTrigger: (() => void) => Element<*>,
title: string, title: string,
onConfirm: () => void, onConfirm: () => void,
onClose?: () => void,
showButtons?: boolean, showButtons?: boolean,
width?: number, width?: number,
isLoading?: boolean, isLoading?: boolean,
children: Element<*>, children: (() => void) => Element<*>,
}; };
export const ConfirmDialogComponent = ({ export const ConfirmDialogComponent = ({
children, children,
title, title,
onConfirm, onConfirm,
onClose,
renderTrigger, renderTrigger,
showButtons, showButtons,
isLoading, isLoading,
width, width,
}: Props) => ( }: Props) => {
const handleClose = toggle => () => {
toggle();
if (onClose) onClose();
};
return (
<ModalComponent <ModalComponent
renderTrigger={renderTrigger} renderTrigger={renderTrigger}
closeOnBackdropClick={false} closeOnBackdropClick={false}
@ -73,19 +81,19 @@ export const ConfirmDialogComponent = ({
{toggle => ( {toggle => (
<Wrapper width={width}> <Wrapper width={width}>
<CloseIconWrapper> <CloseIconWrapper>
<CloseIconImg src={CloseIcon} onClick={toggle} /> <CloseIconImg src={CloseIcon} onClick={handleClose(toggle)} />
</CloseIconWrapper> </CloseIconWrapper>
<TitleWrapper> <TitleWrapper>
<TextComponent value={title} align='center' /> <TextComponent value={title} align='center' />
</TitleWrapper> </TitleWrapper>
<Divider opacity={0.3} /> <Divider opacity={0.3} />
{React.Children.map(children, _ => _)} {children(handleClose(toggle))}
{showButtons && ( {showButtons && (
<> <>
<Btn label='Confirm' onClick={onConfirm} isLoading={isLoading} /> <Btn label='Confirm' onClick={onConfirm} isLoading={isLoading} />
<Btn <Btn
label='Cancel' label='Cancel'
onClick={toggle} onClick={handleClose(toggle)}
variant='secondary' variant='secondary'
disabled={isLoading} disabled={isLoading}
/> />
@ -95,9 +103,11 @@ export const ConfirmDialogComponent = ({
)} )}
</ModalComponent> </ModalComponent>
); );
};
ConfirmDialogComponent.defaultProps = { ConfirmDialogComponent.defaultProps = {
showButtons: true, showButtons: true,
width: 460, width: 460,
isLoading: false, isLoading: false,
onClose: () => {},
}; };

View File

@ -24,7 +24,7 @@ import { DoczWrapper } from '../theme.js'
onConfirm={() => alert('Confirm')} onConfirm={() => alert('Confirm')}
renderTrigger={toggle => <button onClick={toggle}> Open! </button>} renderTrigger={toggle => <button onClick={toggle}> Open! </button>}
> >
<div>Confirm content</div> {toggle => <div>Confirm content</div>}
</ConfirmDialogComponent> </ConfirmDialogComponent>
)} )}
</DoczWrapper> </DoczWrapper>