@umituz/web-design-system 6.3.0 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-design-system",
3
- "version": "6.3.0",
3
+ "version": "6.4.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Web Design System - Tailwind v4 authored, precompiled CSS shipped. Atomic Design components (Atoms, Molecules, Organisms, Templates) for React applications.",
@@ -0,0 +1,77 @@
1
+ import { afterEach, describe, expect, it } from 'vitest';
2
+ import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
3
+ import { ConfirmProvider, useConfirm } from '../presentation/organisms/ConfirmProvider';
4
+
5
+ afterEach(cleanup);
6
+
7
+ function Probe({ onResult }: { onResult: (value: boolean) => void }) {
8
+ const confirm = useConfirm();
9
+ return (
10
+ <button
11
+ type="button"
12
+ onClick={() => {
13
+ void confirm({ title: 'Delete item?', body: 'This cannot be undone.' }).then(onResult);
14
+ }}
15
+ >
16
+ ask
17
+ </button>
18
+ );
19
+ }
20
+
21
+ describe('ConfirmProvider', () => {
22
+ it('resolves true when confirmed and closes the dialog', async () => {
23
+ let result: boolean | undefined;
24
+ render(
25
+ <ConfirmProvider>
26
+ <Probe onResult={(v) => { result = v; }} />
27
+ </ConfirmProvider>,
28
+ );
29
+ expect(screen.queryByRole('dialog')).toBeNull();
30
+ fireEvent.click(screen.getByRole('button', { name: 'ask' }));
31
+ expect(screen.getByRole('dialog')).toBeTruthy();
32
+ expect(screen.getByText('This cannot be undone.')).toBeTruthy();
33
+ fireEvent.click(screen.getByRole('button', { name: 'Confirm' }));
34
+ await waitFor(() => expect(result).toBe(true));
35
+ await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
36
+ });
37
+
38
+ it('resolves false when cancelled', async () => {
39
+ let result: boolean | undefined;
40
+ render(
41
+ <ConfirmProvider>
42
+ <Probe onResult={(v) => { result = v; }} />
43
+ </ConfirmProvider>,
44
+ );
45
+ fireEvent.click(screen.getByRole('button', { name: 'ask' }));
46
+ fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
47
+ await waitFor(() => expect(result).toBe(false));
48
+ });
49
+
50
+ it('honors custom labels and tone', () => {
51
+ function LabelledProbe() {
52
+ const confirm = useConfirm();
53
+ return (
54
+ <button
55
+ type="button"
56
+ onClick={() => {
57
+ void confirm({ title: 'Leave page?', tone: 'danger', confirmLabel: 'Leave', cancelLabel: 'Stay' });
58
+ }}
59
+ >
60
+ go
61
+ </button>
62
+ );
63
+ }
64
+ render(
65
+ <ConfirmProvider>
66
+ <LabelledProbe />
67
+ </ConfirmProvider>,
68
+ );
69
+ fireEvent.click(screen.getByRole('button', { name: 'go' }));
70
+ expect(screen.getByRole('button', { name: 'Leave' })).toBeTruthy();
71
+ expect(screen.getByRole('button', { name: 'Stay' })).toBeTruthy();
72
+ });
73
+
74
+ it('throws when used outside the provider', () => {
75
+ expect(() => render(<Probe onResult={() => undefined} />)).toThrow(/ConfirmProvider/);
76
+ });
77
+ });
@@ -0,0 +1,56 @@
1
+
2
+ import { createContext, useCallback, useContext, useRef, useState } from 'react';
3
+ import type { ReactNode } from 'react';
4
+ import { ConfirmDialog } from './ConfirmDialog';
5
+
6
+ export interface ConfirmOptions {
7
+ title: ReactNode;
8
+ body?: ReactNode;
9
+ tone?: 'danger' | 'primary' | 'success';
10
+ confirmLabel?: string;
11
+ cancelLabel?: string;
12
+ }
13
+
14
+ export type ConfirmFn = (options: ConfirmOptions) => Promise<boolean>;
15
+
16
+ const ConfirmContext = createContext<ConfirmFn | null>(null);
17
+
18
+ export function useConfirm(): ConfirmFn {
19
+ const confirm = useContext(ConfirmContext);
20
+ if (!confirm) throw new Error('useConfirm must be used within ConfirmProvider');
21
+ return confirm;
22
+ }
23
+
24
+ export function ConfirmProvider({ children }: { children: ReactNode }) {
25
+ const [options, setOptions] = useState<ConfirmOptions | null>(null);
26
+ const resolver = useRef<((value: boolean) => void) | null>(null);
27
+
28
+ const confirm = useCallback<ConfirmFn>((next) => {
29
+ setOptions(next);
30
+ return new Promise<boolean>((resolve) => {
31
+ resolver.current = resolve;
32
+ });
33
+ }, []);
34
+
35
+ const settle = (value: boolean) => {
36
+ resolver.current?.(value);
37
+ resolver.current = null;
38
+ setOptions(null);
39
+ };
40
+
41
+ return (
42
+ <ConfirmContext.Provider value={confirm}>
43
+ {children}
44
+ <ConfirmDialog
45
+ open={options !== null}
46
+ title={options?.title ?? ''}
47
+ body={options?.body}
48
+ tone={options?.tone ?? 'primary'}
49
+ confirmLabel={options?.confirmLabel}
50
+ cancelLabel={options?.cancelLabel}
51
+ onConfirm={() => settle(true)}
52
+ onClose={() => settle(false)}
53
+ />
54
+ </ConfirmContext.Provider>
55
+ );
56
+ }
@@ -170,6 +170,9 @@ export type {
170
170
  export { ConfirmDialog } from './ConfirmDialog';
171
171
  export type { ConfirmDialogProps } from './ConfirmDialog';
172
172
 
173
+ export { ConfirmProvider, useConfirm } from './ConfirmProvider';
174
+ export type { ConfirmOptions, ConfirmFn } from './ConfirmProvider';
175
+
173
176
  export { Stepper } from './Stepper';
174
177
  export type { StepperProps, StepperStep, StepperStepStatus } from './Stepper';
175
178