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 21, 2024
1 parent 7a93a52 commit 24ae0e2
Show file tree
Hide file tree
Showing 7 changed files with 164 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/Confirm @kseniya57
11 changes: 11 additions & 0 deletions src/components/Confirm/Confirm.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@use '@gravity-ui/uikit/styles/mixins';
@use '../../components/variables';

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

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

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

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

import './Confirm.scss';

const b = block('confirm');

export type ConfirmProps = {
title?: string;
message?: string;
content?: React.ReactNode;
confirmButtonText?: string;
cancelButtonText?: string;
onConfirm: () => void;
onCancel: () => void;
confirmButtonProps?: ButtonProps;
cancelButtonProps?: ButtonProps;
onClose?: DialogProps['onClose'];
} & Omit<DialogProps, 'children' | 'onClose'>;

export const Confirm = ({
title,
message = '',
content,
confirmButtonText,
cancelButtonText,
onConfirm,
onCancel,
confirmButtonProps,
cancelButtonProps,
onClose,
...dialogProps
}: ConfirmProps) => {
return (
<Dialog {...dialogProps} onClose={onClose ?? onCancel}>
<Dialog.Header caption={title} />
<Dialog.Body>
{content}
{message && !content && <p className={b('message')}>{message}</p>}
</Dialog.Body>
<Dialog.Footer
preset="default"
showError={false}
listenKeyEnter
textButtonApply={confirmButtonText}
textButtonCancel={cancelButtonText}
onClickButtonApply={onConfirm}
onClickButtonCancel={onCancel}
propsButtonCancel={cancelButtonProps}
propsButtonApply={confirmButtonProps}
/>
</Dialog>
);
};
54 changes: 54 additions & 0 deletions src/components/Confirm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!--GITHUB_BLOCK-->

# Confirm

<!--/GITHUB_BLOCK-->

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

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

## Properties

| Name | Description | Type | Required |
| :----------------- | :------------------------------------------------------------- | :-----------: | :------: |
| title | The confirm dialog title | `string` | Yes |
| cancelButtonText | The cancel button text | `string` | Yes |
| confirmButtonText | The ok button text | `string` | Yes |
| message | The confirmation message (used if the content is not provided) | `string` | |
| content | The confirmation custom content | `ReactNode` | |
| confirmButtonProps | The ok button props | `ButtonProps` | |
| cancelButtonProps | The cancel buttonProps | `ButtonProps` | |

And other Dialog props

## Usage

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

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

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

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

import {Confirm} from '../Confirm';
import type {ConfirmProps} from '../Confirm';

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

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

return (
<React.Fragment>
<Button view="normal" onClick={() => setOpen(true)}>
Show confirm
</Button>
<Confirm
{...args}
title="Do you want to confirm?"
onConfirm={() => {
alert('Confirmed');
setOpen(false);
}}
onCancel={() => setOpen(false)}
cancelButtonText="No"
confirmButtonText="Yes"
open={open}
aria-labelledby="app-confirmation-dialog-title"
/>
</React.Fragment>
);
};
export const Default = DefaultTemplate.bind({});
1 change: 1 addition & 0 deletions src/components/Confirm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Confirm';
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 './Confirm';

0 comments on commit 24ae0e2

Please sign in to comment.