|
| 1 | +import * as React from 'react'; |
| 2 | +import { FiX } from 'react-icons/fi'; |
| 3 | +import { TouchableOpacity } from 'react-native'; |
| 4 | + |
| 5 | +import { withTheme } from '../../theme'; |
| 6 | +import { ButtonColor } from '../../theme/component-variables/buttonVariables'; |
| 7 | +import { Button } from '../Button'; |
| 8 | +import { GridBox } from '../Layout'; |
| 9 | +import { Heading } from '../Typography'; |
| 10 | +import Dialog, { IDialogProps } from './Dialog'; |
| 11 | + |
| 12 | +export interface IConfirmDialogProps |
| 13 | + extends IDialogProps, |
| 14 | + IConfirmDialogHeaderProps, |
| 15 | + IConfirmDialogFooterProps {} |
| 16 | + |
| 17 | +export interface IConfirmDialogHeaderProps { |
| 18 | + /** Title displayed in the header */ |
| 19 | + title?: string; |
| 20 | + onClose?: () => void; |
| 21 | +} |
| 22 | + |
| 23 | +const ConfirmDialogHeader = ({ title, onClose }: IConfirmDialogHeaderProps) => ( |
| 24 | + <GridBox |
| 25 | + padding={2} |
| 26 | + flexDirection="row" |
| 27 | + justifyContent="space-between" |
| 28 | + alignItems="center" |
| 29 | + > |
| 30 | + <Heading>{title}</Heading> |
| 31 | + <TouchableOpacity onPress={onClose}> |
| 32 | + <FiX size={24} /> |
| 33 | + </TouchableOpacity> |
| 34 | + </GridBox> |
| 35 | +); |
| 36 | + |
| 37 | +export interface IConfirmDialogFooterProps { |
| 38 | + color?: ButtonColor; |
| 39 | + /** Label for cancel button */ |
| 40 | + cancelLabel?: string; |
| 41 | + /** Label for confirm button */ |
| 42 | + confirmLabel?: string; |
| 43 | + /** Handler for confirm button */ |
| 44 | + onConfirm?: () => void; |
| 45 | + /** Handler for cancel button */ |
| 46 | + onClose?: () => void; |
| 47 | +} |
| 48 | + |
| 49 | +const ConfirmDialogFooter = ({ |
| 50 | + color = 'primary', |
| 51 | + onClose, |
| 52 | + onConfirm, |
| 53 | + cancelLabel, |
| 54 | + confirmLabel, |
| 55 | +}: IConfirmDialogFooterProps) => ( |
| 56 | + <GridBox padding={2} flexDirection="row" justifyContent="flex-end"> |
| 57 | + <Button appearance="minimal" onPress={onClose}> |
| 58 | + {cancelLabel} |
| 59 | + </Button> |
| 60 | + <GridBox paddingLeft={2}> |
| 61 | + <Button color={color} onPress={onConfirm}> |
| 62 | + {confirmLabel} |
| 63 | + </Button> |
| 64 | + </GridBox> |
| 65 | + </GridBox> |
| 66 | +); |
| 67 | + |
| 68 | +const ConfirmDialogWithoutTheme = (props: IConfirmDialogProps) => { |
| 69 | + const { |
| 70 | + cancelLabel = 'Cancel', |
| 71 | + children, |
| 72 | + confirmLabel = 'Confirm', |
| 73 | + footer, |
| 74 | + header, |
| 75 | + onClose, |
| 76 | + onConfirm, |
| 77 | + title, |
| 78 | + ...dialogProps |
| 79 | + } = props; |
| 80 | + |
| 81 | + return ( |
| 82 | + <Dialog |
| 83 | + header={ |
| 84 | + header === null |
| 85 | + ? null |
| 86 | + : header || <ConfirmDialogHeader onClose={onClose} title={title} /> |
| 87 | + } |
| 88 | + footer={ |
| 89 | + footer === null |
| 90 | + ? null |
| 91 | + : footer || ( |
| 92 | + <ConfirmDialogFooter |
| 93 | + onClose={onClose} |
| 94 | + onConfirm={onConfirm} |
| 95 | + cancelLabel={cancelLabel} |
| 96 | + confirmLabel={confirmLabel} |
| 97 | + /> |
| 98 | + ) |
| 99 | + } |
| 100 | + onClose={onClose} |
| 101 | + {...dialogProps} |
| 102 | + > |
| 103 | + <GridBox padding={2}>{children}</GridBox> |
| 104 | + </Dialog> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +export const ConfirmDialog = withTheme(ConfirmDialogWithoutTheme); |
| 109 | +export default ConfirmDialog; |
0 commit comments