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

View File

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