Skip to content

Commit

Permalink
feat(Confirm): add confirm component
Browse files Browse the repository at this point in the history
  • Loading branch information
kseniyakuzina committed Mar 22, 2024
1 parent 7a93a52 commit 38c368f
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
/src/components/SharePopover @niktverd
/src/components/StoreBadge @NikitaCG
/src/components/Stories @darkgenius

/src/components/ConfirmDialog @kseniya57
10 changes: 10 additions & 0 deletions src/components/ConfirmDialog/ConfirmDialog.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@use '@gravity-ui/uikit/styles/mixins';
@use '../../components/variables';

$block: '.#{variables.$ns}confirm-dialog';

#{$block} {
&__body {
@include mixins.text-body-2;
}
}
54 changes: 54 additions & 0 deletions src/components/ConfirmDialog/ConfirmDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';

import type {DialogProps} from '@gravity-ui/uikit';
import {Dialog, DialogFooterProps} from '@gravity-ui/uikit';

import {block} from '../utils/cn';

import './ConfirmDialog.scss';

const b = block('confirm-dialog');

export type ConfirmDialogProps = {
title?: string;
message?: React.ReactNode;
} & Omit<DialogProps, 'children'> &
Pick<
DialogFooterProps,
| 'textButtonApply'
| 'textButtonCancel'
| 'onClickButtonCancel'
| 'onClickButtonApply'
| 'propsButtonCancel'
| 'propsButtonApply'
>;

export const ConfirmDialog = ({
title,
message,
textButtonApply,
textButtonCancel,
onClickButtonApply,
onClickButtonCancel,
propsButtonCancel,
propsButtonApply,
...dialogProps
}: ConfirmDialogProps) => {
return (
<Dialog {...dialogProps}>
<Dialog.Header caption={title} />
<Dialog.Body className={b('body')}>{message}</Dialog.Body>
<Dialog.Footer
preset="default"
showError={false}
listenKeyEnter
textButtonApply={textButtonApply}
textButtonCancel={textButtonCancel}
onClickButtonApply={onClickButtonApply}
onClickButtonCancel={onClickButtonCancel}
propsButtonCancel={propsButtonCancel}
propsButtonApply={propsButtonApply}
/>
</Dialog>
);
};
60 changes: 60 additions & 0 deletions src/components/ConfirmDialog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!--GITHUB_BLOCK-->

# ConfirmDialog

<!--/GITHUB_BLOCK-->

```tsx
import {ConfirmDialog} from '@gravity-ui/components';
```

`ConfirmDialog` is a utility component, which renders confirmation dialogs

## Properties

| Name | Description | Type | Required |
| :------------------ | :------------------------------ | :--------------------------------------------------------------------------------------------: | :------: |
| title | The confirm dialog title | `string` | Yes |
| message | The confirmation message | `ReactNode` | Yes |
| textButtonCancel | The cancel button text | `string` | Yes |
| propsButtonCancel | The cancel buttonProps | `ButtonProps` | |
| onClickButtonCancel | The cancel button click handler | `(event: React.MouseEvent<HTMLElement, MouseEvent>) => void` | Yes |
| textButtonApply | The ok button text | `string` | Yes |
| propsButtonApply | The ok button props | `ButtonProps` | |
| onClickButtonApply | The ok button click handler | `(event: React.MouseEvent<HTMLElement, MouseEvent>) => void` | Yes |
| onClose | The dialog close handler | `(event: MouseEvent or KeyboardEvent, reason: ModalCloseReason or 'closeButtonClick') => void` | Yes |

And other Dialog props

## Usage

```tsx
import {ConfirmDialog} from '@gravity-ui/components';

const [open, setOpen] = React.useState(false);

return (
<React.Fragment>
<Button view="normal" onClick={() => setOpen(true)}>
Show confirm
</Button>
<ConfirmDialog
{...args}
title="Do you want to confirm?"
onClickButtonApply={() => {
alert('Confirmed');
setOpen(false);
}}
onClickButtonCancel={() => {
alert('Cancelled');
setOpen(false);
}}
onClose={() => setOpen(false)}
textButtonCancel="No"
textButtonApply="Yes"
open={open}
aria-labelledby="app-confirmation-dialog-title"
/>
</React.Fragment>
);
```
45 changes: 45 additions & 0 deletions src/components/ConfirmDialog/__stories__/ConfirmDialog.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';

import {Button} from '@gravity-ui/uikit';
import type {Meta, StoryFn} from '@storybook/react';

import {ConfirmDialog} from '../ConfirmDialog';
import type {ConfirmDialogProps} from '../ConfirmDialog';

export default {
title: 'Components/ConfirmDialog',
component: ConfirmDialog,
} as Meta<ConfirmDialogProps>;

const DefaultTemplate: StoryFn<ConfirmDialogProps> = (args) => {
const [open, setOpen] = React.useState(false);

return (
<React.Fragment>
<Button view="normal" onClick={() => setOpen(true)}>
Show confirm
</Button>
<ConfirmDialog
{...args}
onClickButtonApply={() => {
alert('Confirmed');
setOpen(false);
}}
onClickButtonCancel={() => {
alert('Cancelled');
setOpen(false);
}}
onClose={() => setOpen(false)}
open={open}
/>
</React.Fragment>
);
};
export const Default = DefaultTemplate.bind({});

Default.args = {
title: 'Do you want to confirm?',
textButtonCancel: 'No',
textButtonApply: 'Yes',
'aria-labelledby': 'app-confirmation-dialog-title',
};
1 change: 1 addition & 0 deletions src/components/ConfirmDialog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ConfirmDialog';
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from './SharePopover';
export * from './StoreBadge';
export * from './Stories';
export * from './StoriesGroup';
export * from './ConfirmDialog';

0 comments on commit 38c368f

Please sign in to comment.