From 1db1421085e3ce9681e4853c74a199de8320e294 Mon Sep 17 00:00:00 2001 From: Andrey Medvedev Date: Wed, 20 Mar 2024 14:02:22 +0300 Subject: [PATCH 1/3] Pass close callback to action --- packages/vkui/src/components/Alert/Alert.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vkui/src/components/Alert/Alert.tsx b/packages/vkui/src/components/Alert/Alert.tsx index 705236ccc7..d3298a4c90 100644 --- a/packages/vkui/src/components/Alert/Alert.tsx +++ b/packages/vkui/src/components/Alert/Alert.tsx @@ -24,7 +24,7 @@ export interface AlertActionInterface AnchorHTMLAttributesOnly, HasDataAttribute { title: string; - action?: VoidFunction; + action: (args: { close: VoidFunction }) => void; /** * По умолчанию клик на опцию вызывает переданную в `Alert` функцию `onClose`, данное свойство * позволяет отключить такое поведение @@ -116,16 +116,16 @@ export const Alert = ({ (e?: TransitionEvent) => { if (!e || e.propertyName === 'opacity') { onClose(); - action && action(); + action && action({ close }); } }, timeout, ); } else { - action && action(); + action && action({ close }); } }, - [elementRef, waitTransitionFinish, onClose, timeout], + [elementRef, waitTransitionFinish, onClose, close, timeout], ); useScrollLock(); From 95fda6e4db3fc427d0fd4f990cadc150a315845d Mon Sep 17 00:00:00 2001 From: Andrey Medvedev Date: Wed, 20 Mar 2024 18:17:53 +0300 Subject: [PATCH 2/3] Make action optional again --- packages/vkui/src/components/Alert/Alert.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vkui/src/components/Alert/Alert.tsx b/packages/vkui/src/components/Alert/Alert.tsx index d3298a4c90..58944aa23c 100644 --- a/packages/vkui/src/components/Alert/Alert.tsx +++ b/packages/vkui/src/components/Alert/Alert.tsx @@ -24,7 +24,7 @@ export interface AlertActionInterface AnchorHTMLAttributesOnly, HasDataAttribute { title: string; - action: (args: { close: VoidFunction }) => void; + action?: (args: { close: VoidFunction }) => void; /** * По умолчанию клик на опцию вызывает переданную в `Alert` функцию `onClose`, данное свойство * позволяет отключить такое поведение From ddc39cde50b6140b4ad5dd4dd1081a90d521458a Mon Sep 17 00:00:00 2001 From: Andrey Medvedev Date: Tue, 26 Mar 2024 12:24:14 +0300 Subject: [PATCH 3/3] Prefer callback and set it only when autoCloseDisabled --- .../vkui/src/components/Alert/Alert.test.tsx | 18 +++++++++++++++++- packages/vkui/src/components/Alert/Alert.tsx | 9 +++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/vkui/src/components/Alert/Alert.test.tsx b/packages/vkui/src/components/Alert/Alert.test.tsx index ee7d4c5fde..c22e5adfb8 100644 --- a/packages/vkui/src/components/Alert/Alert.test.tsx +++ b/packages/vkui/src/components/Alert/Alert.test.tsx @@ -1,5 +1,6 @@ import * as React from 'react'; import { act, render, screen } from '@testing-library/react'; +import { noop } from '@vkontakte/vkjs'; import { ViewWidth } from '../../lib/adaptivity'; import { Platform } from '../../lib/platform'; import { baselineComponent, fakeTimers, userEvent } from '../../testing/utils'; @@ -35,7 +36,15 @@ describe('Alert', () => { describe('calls action and do not calls onClose with autoCloseDisabled=true', () => { it.each([Platform.ANDROID, Platform.IOS])('%s', async (platform) => { - const action = jest.fn(); + const action = jest + .fn() + .mockImplementationOnce(noop) + .mockImplementationOnce((args) => { + if (args && args.close) { + args.close(); + } + }); + const onClose = jest.fn(); render( @@ -50,6 +59,13 @@ describe('Alert', () => { expect(onClose).not.toHaveBeenCalled(); act(jest.runAllTimers); expect(onClose).not.toHaveBeenCalled(); + + // второй клик закроет Alert, так как в action был вызван метод close() + await userEvent.click(screen.getByText('__action__')); + expect(action).toHaveBeenCalledTimes(2); + expect(onClose).toHaveBeenCalledTimes(0); + act(jest.runAllTimers); + expect(onClose).toHaveBeenCalledTimes(1); }); }); diff --git a/packages/vkui/src/components/Alert/Alert.tsx b/packages/vkui/src/components/Alert/Alert.tsx index 58944aa23c..779b3fc027 100644 --- a/packages/vkui/src/components/Alert/Alert.tsx +++ b/packages/vkui/src/components/Alert/Alert.tsx @@ -24,7 +24,12 @@ export interface AlertActionInterface AnchorHTMLAttributesOnly, HasDataAttribute { title: string; - action?: (args: { close: VoidFunction }) => void; + /** + * Обработчик клика на опцию. Если свойство `autoCloseDisabled` включено, + * то в аргументы `action` передаётся объект с функцией close, + * вызвав которую можно закрыть `action` вручную. + */ + action?: (args?: { close?: VoidFunction }) => void; /** * По умолчанию клик на опцию вызывает переданную в `Alert` функцию `onClose`, данное свойство * позволяет отключить такое поведение @@ -116,7 +121,7 @@ export const Alert = ({ (e?: TransitionEvent) => { if (!e || e.propertyName === 'opacity') { onClose(); - action && action({ close }); + action && action(); } }, timeout,