Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
feat(UIKIT-1781,ConfirmDialog): Исправлено отображение на мобильных у…
Browse files Browse the repository at this point in the history
…стройствах (#1137)
  • Loading branch information
pan1caisreal authored Sep 17, 2024
1 parent f3b8f7d commit 4c4b826
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const Example = () => {
onClose={handleClose}
actions={{
confirm: { text: 'Да', onClick: handleConfirm, loading },
cancel: { text: 'Нет', variant: 'text' },
cancel: { text: 'Нет' },
}}
/>
</div>
Expand Down
42 changes: 19 additions & 23 deletions packages/components/src/ConfirmDialog/ConfirmDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { type ReactNode, useMemo } from 'react';
import { type ReactNode } from 'react';

import { Button, type ButtonProps } from '../Button';
import { Dialog, type DialogProps } from '../Dialog';
import { DialogContent } from '../DialogContent';
import { DialogContentText } from '../DialogContentText';
import { DialogActions } from '../DialogActions';

import { CancelButton } from './styles';
import { useLogic } from './useLogic';

export type ConfirmDialogProps = {
/**
* Заголовок диалога
Expand Down Expand Up @@ -46,30 +49,19 @@ export type ConfirmDialogProps = {
| 'open'
>;

export const ConfirmDialog = ({
open,
title,
description,
actions,
onClose,
...restProps
}: ConfirmDialogProps) => {
const { text: confirmText, ...confirmButtonProps } = actions.confirm;
export const ConfirmDialog = (props: ConfirmDialogProps) => {
const { isShowCancelButton, cancelButtonProps } = useLogic(props);

const renderCancelButton = useMemo(() => {
if (actions.cancel) {
const { text: cancelText, ...cancelButtonProps } = actions.cancel;
const handleCancelClick = actions.cancel?.onClick || onClose;
const { open, title, description, actions, onClose, ...restProps } = props;

return (
<Button {...cancelButtonProps} onClick={handleCancelClick}>
{cancelText}
</Button>
);
}
const { text: confirmText, ...confirmButtonProps } = actions.confirm;

return null;
}, [actions.cancel, onClose]);
const {
text: cancelText,
variant,
onClick,
...restCancelProps
} = actions.cancel || {};

return (
<Dialog title={title} open={open} onClose={onClose} {...restProps}>
Expand All @@ -79,7 +71,11 @@ export const ConfirmDialog = ({
</DialogContent>
)}
<DialogActions>
{renderCancelButton}
{isShowCancelButton && (
<CancelButton {...restCancelProps} {...cancelButtonProps}>
{cancelText}
</CancelButton>
)}
<Button {...confirmButtonProps}>{confirmText}</Button>
</DialogActions>
</Dialog>
Expand Down
8 changes: 8 additions & 0 deletions packages/components/src/ConfirmDialog/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { styled } from '../styles';
import { Button } from '../Button';

export const CancelButton = styled(Button)`
${({ theme }) => theme.breakpoints.down('sm')} {
order: 1;
}
`;
1 change: 1 addition & 0 deletions packages/components/src/ConfirmDialog/useLogic/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useLogic';
23 changes: 23 additions & 0 deletions packages/components/src/ConfirmDialog/useLogic/useLogic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useViewportType } from '../../hooks';
import { type ConfirmDialogProps } from '../ConfirmDialog';

type UseLogicParams = ConfirmDialogProps;

export const useLogic = ({ actions, onClose }: UseLogicParams) => {
const { isMobile } = useViewportType();
const { cancel } = actions;

const isShowCancelButton = Boolean(cancel);

const cancelVariant = isMobile ? 'light' : 'text';
const handleCancelClick = cancel?.onClick || onClose;

const { variant = cancelVariant } = cancel || {};

const cancelButtonProps = {
variant,
onClick: handleCancelClick,
};

return { isShowCancelButton, cancelButtonProps };
};

0 comments on commit 4c4b826

Please sign in to comment.