@tallyui/primitives 0.1.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/dist/index.d.ts +1370 -0
- package/dist/index.js +3954 -0
- package/package.json +47 -0
- package/src/__tests__/accordion.test.tsx +370 -0
- package/src/__tests__/alert-dialog.test.tsx +127 -0
- package/src/__tests__/animated.test.tsx +184 -0
- package/src/__tests__/checkbox.test.tsx +90 -0
- package/src/__tests__/collapsible.test.tsx +149 -0
- package/src/__tests__/combobox.test.tsx +292 -0
- package/src/__tests__/context-menu.test.tsx +226 -0
- package/src/__tests__/dialog.test.tsx +81 -0
- package/src/__tests__/dropdown-menu.test.tsx +119 -0
- package/src/__tests__/hooks.test.tsx +46 -0
- package/src/__tests__/hover-card.test.tsx +77 -0
- package/src/__tests__/image.test.tsx +53 -0
- package/src/__tests__/label.test.tsx +50 -0
- package/src/__tests__/list.test.tsx +63 -0
- package/src/__tests__/popover.test.tsx +63 -0
- package/src/__tests__/portal.test.tsx +66 -0
- package/src/__tests__/progress.test.tsx +70 -0
- package/src/__tests__/radio-group.test.tsx +164 -0
- package/src/__tests__/select.test.tsx +76 -0
- package/src/__tests__/separator.test.tsx +37 -0
- package/src/__tests__/slider.test.tsx +124 -0
- package/src/__tests__/slot.test.tsx +56 -0
- package/src/__tests__/switch.test.tsx +70 -0
- package/src/__tests__/table.test.tsx +172 -0
- package/src/__tests__/tabs.test.tsx +67 -0
- package/src/__tests__/text.test.tsx +59 -0
- package/src/__tests__/toast.test.tsx +137 -0
- package/src/__tests__/toggle-group.test.tsx +167 -0
- package/src/__tests__/toggle.test.tsx +60 -0
- package/src/__tests__/tooltip.test.tsx +77 -0
- package/src/__tests__/types.test-d.ts +106 -0
- package/src/__tests__/utils.test.ts +47 -0
- package/src/accordion/accordion.tsx +232 -0
- package/src/accordion/index.ts +12 -0
- package/src/accordion/types.ts +56 -0
- package/src/alert-dialog/alert-dialog.tsx +296 -0
- package/src/alert-dialog/index.ts +25 -0
- package/src/alert-dialog/types.ts +50 -0
- package/src/animated/index.ts +3 -0
- package/src/animated/presence.tsx +27 -0
- package/src/animated/types.ts +36 -0
- package/src/animated/use-presence.ts +44 -0
- package/src/checkbox/checkbox.tsx +106 -0
- package/src/checkbox/index.ts +2 -0
- package/src/checkbox/types.ts +19 -0
- package/src/collapsible/collapsible.tsx +135 -0
- package/src/collapsible/index.ts +2 -0
- package/src/collapsible/types.ts +25 -0
- package/src/combobox/combobox.tsx +325 -0
- package/src/combobox/index.ts +24 -0
- package/src/combobox/types.ts +84 -0
- package/src/context-menu/context-menu.tsx +654 -0
- package/src/context-menu/index.ts +40 -0
- package/src/context-menu/types.ts +117 -0
- package/src/dialog/dialog.tsx +260 -0
- package/src/dialog/index.ts +23 -0
- package/src/dialog/types.ts +47 -0
- package/src/dropdown-menu/dropdown-menu.tsx +654 -0
- package/src/dropdown-menu/index.ts +40 -0
- package/src/dropdown-menu/types.ts +117 -0
- package/src/hooks/index.ts +6 -0
- package/src/hooks/use-augmented-ref.ts +32 -0
- package/src/hooks/use-controllable-state.ts +87 -0
- package/src/hooks/use-relative-position.ts +253 -0
- package/src/hooks/use-relative-position.web.ts +9 -0
- package/src/hover-card/hover-card.tsx +271 -0
- package/src/hover-card/index.ts +17 -0
- package/src/hover-card/types.ts +50 -0
- package/src/image/image.tsx +29 -0
- package/src/image/index.ts +2 -0
- package/src/image/types.ts +10 -0
- package/src/index.ts +38 -0
- package/src/label/index.ts +2 -0
- package/src/label/label.tsx +45 -0
- package/src/label/types.ts +11 -0
- package/src/list/index.ts +2 -0
- package/src/list/list.tsx +54 -0
- package/src/list/types.ts +14 -0
- package/src/popover/index.ts +19 -0
- package/src/popover/popover.tsx +314 -0
- package/src/popover/types.ts +51 -0
- package/src/portal.tsx +60 -0
- package/src/progress/index.ts +2 -0
- package/src/progress/progress.tsx +92 -0
- package/src/progress/types.ts +16 -0
- package/src/radio-group/index.ts +2 -0
- package/src/radio-group/radio-group.tsx +160 -0
- package/src/radio-group/types.ts +31 -0
- package/src/select/index.ts +34 -0
- package/src/select/select.tsx +465 -0
- package/src/select/types.ts +112 -0
- package/src/separator/index.ts +2 -0
- package/src/separator/separator.tsx +32 -0
- package/src/separator/types.ts +8 -0
- package/src/slider/index.ts +2 -0
- package/src/slider/slider.tsx +139 -0
- package/src/slider/types.ts +35 -0
- package/src/slot.tsx +160 -0
- package/src/switch/index.ts +2 -0
- package/src/switch/switch.tsx +92 -0
- package/src/switch/types.ts +18 -0
- package/src/table/index.ts +12 -0
- package/src/table/table.tsx +160 -0
- package/src/table/types.ts +30 -0
- package/src/tabs/index.ts +15 -0
- package/src/tabs/tabs.tsx +140 -0
- package/src/tabs/types.ts +36 -0
- package/src/text/index.ts +2 -0
- package/src/text/text.tsx +40 -0
- package/src/text/types.ts +5 -0
- package/src/toast/index.ts +2 -0
- package/src/toast/toast.tsx +189 -0
- package/src/toast/types.ts +35 -0
- package/src/toggle/index.ts +2 -0
- package/src/toggle/toggle.tsx +59 -0
- package/src/toggle/types.ts +11 -0
- package/src/toggle-group/index.ts +2 -0
- package/src/toggle-group/toggle-group.tsx +123 -0
- package/src/toggle-group/types.ts +36 -0
- package/src/tooltip/index.ts +17 -0
- package/src/tooltip/tooltip.tsx +272 -0
- package/src/tooltip/types.ts +49 -0
- package/src/types.ts +129 -0
- package/src/uniwind-env.d.ts +31 -0
- package/src/utils.ts +67 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { render, screen, fireEvent } from '@testing-library/react';
|
|
2
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
3
|
+
import * as Tooltip from '../tooltip';
|
|
4
|
+
import { PortalHost } from '../portal';
|
|
5
|
+
|
|
6
|
+
describe('Tooltip', () => {
|
|
7
|
+
const renderTooltip = (props = {}) =>
|
|
8
|
+
render(
|
|
9
|
+
<div>
|
|
10
|
+
<PortalHost />
|
|
11
|
+
<Tooltip.Root {...props}>
|
|
12
|
+
<Tooltip.Trigger testID="trigger">Hover me</Tooltip.Trigger>
|
|
13
|
+
<Tooltip.Portal>
|
|
14
|
+
<Tooltip.Overlay testID="overlay" />
|
|
15
|
+
<Tooltip.Content testID="content">Tooltip text</Tooltip.Content>
|
|
16
|
+
</Tooltip.Portal>
|
|
17
|
+
</Tooltip.Root>
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
it('content has tooltip role', () => {
|
|
22
|
+
renderTooltip({ defaultOpen: true });
|
|
23
|
+
expect(screen.getByTestId('content').getAttribute('role')).toBe('tooltip');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('trigger opens tooltip', () => {
|
|
27
|
+
renderTooltip();
|
|
28
|
+
fireEvent.click(screen.getByTestId('trigger'));
|
|
29
|
+
expect(screen.getByTestId('content')).toBeTruthy();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('closes on trigger re-press', () => {
|
|
33
|
+
renderTooltip({ defaultOpen: true });
|
|
34
|
+
fireEvent.click(screen.getByTestId('trigger'));
|
|
35
|
+
expect(screen.queryByTestId('content')).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('supports controlled mode', () => {
|
|
39
|
+
const onOpenChange = vi.fn();
|
|
40
|
+
renderTooltip({ open: false, onOpenChange });
|
|
41
|
+
fireEvent.click(screen.getByTestId('trigger'));
|
|
42
|
+
expect(onOpenChange).toHaveBeenCalledWith(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('supports uncontrolled with defaultOpen', () => {
|
|
46
|
+
renderTooltip({ defaultOpen: true });
|
|
47
|
+
expect(screen.getByTestId('content')).toBeTruthy();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('content not rendered when closed', () => {
|
|
51
|
+
renderTooltip();
|
|
52
|
+
expect(screen.queryByTestId('content')).toBeNull();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('trigger has button role', () => {
|
|
56
|
+
renderTooltip();
|
|
57
|
+
expect(screen.getByTestId('trigger').getAttribute('role')).toBe('button');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('asChild works on trigger', () => {
|
|
61
|
+
render(
|
|
62
|
+
<div>
|
|
63
|
+
<PortalHost />
|
|
64
|
+
<Tooltip.Root>
|
|
65
|
+
<Tooltip.Trigger asChild>
|
|
66
|
+
<div data-testid="custom-trigger">Custom</div>
|
|
67
|
+
</Tooltip.Trigger>
|
|
68
|
+
<Tooltip.Portal>
|
|
69
|
+
<Tooltip.Content testID="content">Tooltip text</Tooltip.Content>
|
|
70
|
+
</Tooltip.Portal>
|
|
71
|
+
</Tooltip.Root>
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
const trigger = screen.getByTestId('custom-trigger');
|
|
75
|
+
expect(trigger.getAttribute('role')).toBe('button');
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from 'vitest';
|
|
2
|
+
import type {
|
|
3
|
+
Prettify,
|
|
4
|
+
SlottableViewProps,
|
|
5
|
+
SlottablePressableProps,
|
|
6
|
+
SlottableTextProps,
|
|
7
|
+
SlottableImageProps,
|
|
8
|
+
Insets,
|
|
9
|
+
PositionedContentProps,
|
|
10
|
+
ForceMountable,
|
|
11
|
+
ViewRef,
|
|
12
|
+
PressableRef,
|
|
13
|
+
TextRef,
|
|
14
|
+
ImageRef,
|
|
15
|
+
PointerDownOutsideEvent,
|
|
16
|
+
FocusOutsideEvent,
|
|
17
|
+
} from '../types';
|
|
18
|
+
|
|
19
|
+
describe('Prettify', () => {
|
|
20
|
+
it('flattens intersection types', () => {
|
|
21
|
+
type A = { a: string } & { b: number };
|
|
22
|
+
type Result = Prettify<A>;
|
|
23
|
+
expectTypeOf<Result>().toEqualTypeOf<{ a: string; b: number }>();
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('SlottableViewProps', () => {
|
|
28
|
+
it('includes asChild prop', () => {
|
|
29
|
+
expectTypeOf<SlottableViewProps>().toHaveProperty('asChild');
|
|
30
|
+
});
|
|
31
|
+
it('includes children prop', () => {
|
|
32
|
+
expectTypeOf<SlottableViewProps>().toHaveProperty('children');
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('SlottablePressableProps', () => {
|
|
37
|
+
it('includes asChild prop', () => {
|
|
38
|
+
expectTypeOf<SlottablePressableProps>().toHaveProperty('asChild');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('SlottableTextProps', () => {
|
|
43
|
+
it('includes asChild prop', () => {
|
|
44
|
+
expectTypeOf<SlottableTextProps>().toHaveProperty('asChild');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('SlottableImageProps', () => {
|
|
49
|
+
it('includes asChild prop', () => {
|
|
50
|
+
expectTypeOf<SlottableImageProps>().toHaveProperty('asChild');
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('Ref types', () => {
|
|
55
|
+
it('ViewRef has a current property', () => {
|
|
56
|
+
expectTypeOf<ViewRef>().toHaveProperty('current');
|
|
57
|
+
});
|
|
58
|
+
it('PressableRef has a current property', () => {
|
|
59
|
+
expectTypeOf<PressableRef>().toHaveProperty('current');
|
|
60
|
+
});
|
|
61
|
+
it('TextRef has a current property', () => {
|
|
62
|
+
expectTypeOf<TextRef>().toHaveProperty('current');
|
|
63
|
+
});
|
|
64
|
+
it('ImageRef has a current property', () => {
|
|
65
|
+
expectTypeOf<ImageRef>().toHaveProperty('current');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('Insets', () => {
|
|
70
|
+
it('has top, bottom, left, right as optional numbers', () => {
|
|
71
|
+
expectTypeOf<Insets>().toHaveProperty('top');
|
|
72
|
+
expectTypeOf<Insets>().toHaveProperty('bottom');
|
|
73
|
+
expectTypeOf<Insets>().toHaveProperty('left');
|
|
74
|
+
expectTypeOf<Insets>().toHaveProperty('right');
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('PositionedContentProps', () => {
|
|
79
|
+
it('has positioning props', () => {
|
|
80
|
+
expectTypeOf<PositionedContentProps>().toHaveProperty('align');
|
|
81
|
+
expectTypeOf<PositionedContentProps>().toHaveProperty('side');
|
|
82
|
+
expectTypeOf<PositionedContentProps>().toHaveProperty('sideOffset');
|
|
83
|
+
expectTypeOf<PositionedContentProps>().toHaveProperty('avoidCollisions');
|
|
84
|
+
});
|
|
85
|
+
it('has insets prop', () => {
|
|
86
|
+
expectTypeOf<PositionedContentProps>().toHaveProperty('insets');
|
|
87
|
+
});
|
|
88
|
+
it('has alignOffset prop', () => {
|
|
89
|
+
expectTypeOf<PositionedContentProps>().toHaveProperty('alignOffset');
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('ForceMountable', () => {
|
|
94
|
+
it('has forceMount as optional true', () => {
|
|
95
|
+
expectTypeOf<ForceMountable>().toHaveProperty('forceMount');
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('Event types', () => {
|
|
100
|
+
it('PointerDownOutsideEvent is a CustomEvent', () => {
|
|
101
|
+
expectTypeOf<PointerDownOutsideEvent>().toMatchTypeOf<CustomEvent>();
|
|
102
|
+
});
|
|
103
|
+
it('FocusOutsideEvent is a CustomEvent', () => {
|
|
104
|
+
expectTypeOf<FocusOutsideEvent>().toMatchTypeOf<CustomEvent>();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { toggleGroupUtils } from '../utils';
|
|
3
|
+
|
|
4
|
+
describe('toggleGroupUtils', () => {
|
|
5
|
+
describe('getIsSelected', () => {
|
|
6
|
+
it('returns true for matching single value', () => {
|
|
7
|
+
expect(toggleGroupUtils.getIsSelected('a', 'a')).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
it('returns true for value in array', () => {
|
|
10
|
+
expect(toggleGroupUtils.getIsSelected(['a', 'b'], 'b')).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
it('returns false for non-matching value', () => {
|
|
13
|
+
expect(toggleGroupUtils.getIsSelected('a', 'b')).toBe(false);
|
|
14
|
+
});
|
|
15
|
+
it('returns false for undefined value', () => {
|
|
16
|
+
expect(toggleGroupUtils.getIsSelected(undefined, 'a')).toBe(false);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('getNewSingleValue', () => {
|
|
21
|
+
it('deselects when already selected', () => {
|
|
22
|
+
expect(toggleGroupUtils.getNewSingleValue('a', 'a')).toBeUndefined();
|
|
23
|
+
});
|
|
24
|
+
it('selects new value', () => {
|
|
25
|
+
expect(toggleGroupUtils.getNewSingleValue('a', 'b')).toBe('b');
|
|
26
|
+
});
|
|
27
|
+
it('selects from undefined', () => {
|
|
28
|
+
expect(toggleGroupUtils.getNewSingleValue(undefined, 'a')).toBe('a');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('getNewMultipleValue', () => {
|
|
33
|
+
it('adds value when not present', () => {
|
|
34
|
+
expect(toggleGroupUtils.getNewMultipleValue(['a'], 'b')).toEqual(['a', 'b']);
|
|
35
|
+
});
|
|
36
|
+
it('removes value when present', () => {
|
|
37
|
+
expect(toggleGroupUtils.getNewMultipleValue(['a', 'b'], 'b')).toEqual(['a']);
|
|
38
|
+
});
|
|
39
|
+
it('creates array from single string', () => {
|
|
40
|
+
const result = toggleGroupUtils.getNewMultipleValue('a', 'b');
|
|
41
|
+
expect(result).toEqual(['a', 'b']);
|
|
42
|
+
});
|
|
43
|
+
it('handles undefined original', () => {
|
|
44
|
+
expect(toggleGroupUtils.getNewMultipleValue(undefined, 'a')).toEqual(['a']);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Pressable, View, type GestureResponderEvent } from 'react-native';
|
|
3
|
+
import { useControllableState } from '../hooks';
|
|
4
|
+
import { Slot } from '../slot';
|
|
5
|
+
import type {
|
|
6
|
+
RootProps,
|
|
7
|
+
ItemProps,
|
|
8
|
+
HeaderProps,
|
|
9
|
+
TriggerProps,
|
|
10
|
+
ContentProps,
|
|
11
|
+
RootContext as RootContextType,
|
|
12
|
+
ItemContext as ItemContextType,
|
|
13
|
+
} from './types';
|
|
14
|
+
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// Context
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
const RootContext = React.createContext<RootContextType | null>(null);
|
|
20
|
+
|
|
21
|
+
function useRootContext(): RootContextType {
|
|
22
|
+
const context = React.useContext(RootContext);
|
|
23
|
+
if (!context) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
'Accordion compound components cannot be rendered outside the Accordion.Root component'
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return context;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const ItemContext = React.createContext<ItemContextType | null>(null);
|
|
32
|
+
|
|
33
|
+
function useItemContext(): ItemContextType {
|
|
34
|
+
const context = React.useContext(ItemContext);
|
|
35
|
+
if (!context) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
'Accordion.Item compound components cannot be rendered outside the Accordion.Item component'
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return context;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
// Helpers
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
|
|
47
|
+
function isItemExpanded(
|
|
48
|
+
rootValue: string | string[] | undefined,
|
|
49
|
+
itemValue: string
|
|
50
|
+
): boolean {
|
|
51
|
+
if (rootValue === undefined) return false;
|
|
52
|
+
return Array.isArray(rootValue)
|
|
53
|
+
? rootValue.includes(itemValue)
|
|
54
|
+
: rootValue === itemValue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
// Root
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
const Root = React.forwardRef<View, RootProps>(function Root(
|
|
62
|
+
{
|
|
63
|
+
asChild,
|
|
64
|
+
type,
|
|
65
|
+
value: valueProp,
|
|
66
|
+
defaultValue,
|
|
67
|
+
onValueChange: onValueChangeProp,
|
|
68
|
+
disabled = false,
|
|
69
|
+
...rest
|
|
70
|
+
},
|
|
71
|
+
ref
|
|
72
|
+
) {
|
|
73
|
+
// Pull collapsible out of rest for single type; it doesn't exist on MultipleRootProps
|
|
74
|
+
const collapsible = (rest as { collapsible?: boolean }).collapsible ?? false;
|
|
75
|
+
const { collapsible: _collapsible, ...props } = rest as typeof rest & { collapsible?: boolean };
|
|
76
|
+
|
|
77
|
+
const [value, setValue] = useControllableState({
|
|
78
|
+
prop: valueProp,
|
|
79
|
+
defaultProp: defaultValue,
|
|
80
|
+
onChange: onValueChangeProp as (state: string | string[] | undefined) => void,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const onValueChange = React.useCallback(
|
|
84
|
+
(newValue: string | string[]) => {
|
|
85
|
+
setValue(newValue);
|
|
86
|
+
},
|
|
87
|
+
[setValue]
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const Component = asChild ? Slot : View;
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<RootContext.Provider
|
|
94
|
+
value={{ type, value, onValueChange, collapsible, disabled }}
|
|
95
|
+
>
|
|
96
|
+
<Component ref={ref} {...props} />
|
|
97
|
+
</RootContext.Provider>
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
Root.displayName = 'AccordionRoot';
|
|
102
|
+
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
// Item
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
const Item = React.forwardRef<View, ItemProps>(function Item(
|
|
108
|
+
{ asChild, value: itemValue, disabled: disabledProp = false, ...props },
|
|
109
|
+
ref
|
|
110
|
+
) {
|
|
111
|
+
const { value: rootValue, disabled: rootDisabled } = useRootContext();
|
|
112
|
+
|
|
113
|
+
const expanded = isItemExpanded(rootValue, itemValue);
|
|
114
|
+
const isDisabled = rootDisabled || disabledProp;
|
|
115
|
+
|
|
116
|
+
const Component = asChild ? Slot : View;
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<ItemContext.Provider
|
|
120
|
+
value={{ value: itemValue, isExpanded: expanded, disabled: isDisabled }}
|
|
121
|
+
>
|
|
122
|
+
<Component ref={ref} {...props} />
|
|
123
|
+
</ItemContext.Provider>
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
Item.displayName = 'AccordionItem';
|
|
128
|
+
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
// Header
|
|
131
|
+
// ---------------------------------------------------------------------------
|
|
132
|
+
|
|
133
|
+
const Header = React.forwardRef<View, HeaderProps>(function Header(
|
|
134
|
+
{ asChild, ...props },
|
|
135
|
+
ref
|
|
136
|
+
) {
|
|
137
|
+
const Component = asChild ? Slot : View;
|
|
138
|
+
|
|
139
|
+
return <Component ref={ref} role="heading" {...props} />;
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
Header.displayName = 'AccordionHeader';
|
|
143
|
+
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
// Trigger
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
|
|
148
|
+
const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
149
|
+
{ asChild, onPress: onPressProp, disabled: disabledProp, ...props },
|
|
150
|
+
ref
|
|
151
|
+
) {
|
|
152
|
+
const { type, collapsible, onValueChange, value: rootValue } = useRootContext();
|
|
153
|
+
const { value: itemValue, isExpanded, disabled: itemDisabled } = useItemContext();
|
|
154
|
+
|
|
155
|
+
const isDisabled = itemDisabled || disabledProp;
|
|
156
|
+
|
|
157
|
+
const onPress = React.useCallback(
|
|
158
|
+
(ev: GestureResponderEvent) => {
|
|
159
|
+
if (isDisabled) return;
|
|
160
|
+
|
|
161
|
+
if (type === 'single') {
|
|
162
|
+
if (isExpanded && !collapsible) {
|
|
163
|
+
// Non-collapsible single: do not close the open item
|
|
164
|
+
onPressProp?.(ev);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
// Single collapsible: toggle; Single not expanded: expand
|
|
168
|
+
onValueChange(isExpanded ? '' : itemValue);
|
|
169
|
+
} else {
|
|
170
|
+
// Multiple: toggle this item in the array
|
|
171
|
+
const currentValue = (Array.isArray(rootValue) ? rootValue : []) as string[];
|
|
172
|
+
if (isExpanded) {
|
|
173
|
+
onValueChange(currentValue.filter((v) => v !== itemValue));
|
|
174
|
+
} else {
|
|
175
|
+
onValueChange([...currentValue, itemValue]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
onPressProp?.(ev);
|
|
180
|
+
},
|
|
181
|
+
[isDisabled, type, collapsible, isExpanded, itemValue, rootValue, onValueChange, onPressProp]
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const Component = asChild ? Slot : Pressable;
|
|
185
|
+
|
|
186
|
+
return (
|
|
187
|
+
<Component
|
|
188
|
+
ref={ref}
|
|
189
|
+
role="button"
|
|
190
|
+
aria-expanded={isExpanded}
|
|
191
|
+
aria-disabled={isDisabled || undefined}
|
|
192
|
+
onPress={onPress}
|
|
193
|
+
disabled={isDisabled || undefined}
|
|
194
|
+
{...props}
|
|
195
|
+
/>
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
Trigger.displayName = 'AccordionTrigger';
|
|
200
|
+
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
// Content
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
|
|
205
|
+
const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
206
|
+
{ asChild, forceMount, ...props },
|
|
207
|
+
ref
|
|
208
|
+
) {
|
|
209
|
+
const { isExpanded } = useItemContext();
|
|
210
|
+
|
|
211
|
+
if (!forceMount && !isExpanded) {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const Component = asChild ? Slot : View;
|
|
216
|
+
|
|
217
|
+
return (
|
|
218
|
+
<Component
|
|
219
|
+
ref={ref}
|
|
220
|
+
role="region"
|
|
221
|
+
{...props}
|
|
222
|
+
/>
|
|
223
|
+
);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
Content.displayName = 'AccordionContent';
|
|
227
|
+
|
|
228
|
+
// ---------------------------------------------------------------------------
|
|
229
|
+
// Exports
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
|
|
232
|
+
export { Root, Item, Header, Trigger, Content, useRootContext, useItemContext };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { Root, Item, Header, Trigger, Content, useRootContext, useItemContext } from './accordion';
|
|
2
|
+
export type {
|
|
3
|
+
RootProps,
|
|
4
|
+
SingleRootProps,
|
|
5
|
+
MultipleRootProps,
|
|
6
|
+
ItemProps,
|
|
7
|
+
HeaderProps,
|
|
8
|
+
TriggerProps,
|
|
9
|
+
ContentProps,
|
|
10
|
+
RootContext,
|
|
11
|
+
ItemContext,
|
|
12
|
+
} from './types';
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ForceMountable, SlottablePressableProps, SlottableViewProps, PressableRef, ViewRef } from '../types';
|
|
2
|
+
|
|
3
|
+
interface SingleRootProps extends SlottableViewProps {
|
|
4
|
+
type: 'single';
|
|
5
|
+
value?: string;
|
|
6
|
+
defaultValue?: string;
|
|
7
|
+
onValueChange?: (value: string) => void;
|
|
8
|
+
collapsible?: boolean;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
ref?: ViewRef;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface MultipleRootProps extends SlottableViewProps {
|
|
14
|
+
type: 'multiple';
|
|
15
|
+
value?: string[];
|
|
16
|
+
defaultValue?: string[];
|
|
17
|
+
onValueChange?: (value: string[]) => void;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
ref?: ViewRef;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type RootProps = SingleRootProps | MultipleRootProps;
|
|
23
|
+
|
|
24
|
+
interface ItemProps extends SlottableViewProps {
|
|
25
|
+
value: string;
|
|
26
|
+
disabled?: boolean;
|
|
27
|
+
ref?: ViewRef;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface HeaderProps extends SlottableViewProps {
|
|
31
|
+
ref?: ViewRef;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface TriggerProps extends SlottablePressableProps {
|
|
35
|
+
ref?: PressableRef;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ContentProps extends SlottableViewProps, ForceMountable {
|
|
39
|
+
ref?: ViewRef;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface RootContext {
|
|
43
|
+
type: 'single' | 'multiple';
|
|
44
|
+
value: string | string[] | undefined;
|
|
45
|
+
onValueChange: (value: string | string[]) => void;
|
|
46
|
+
collapsible: boolean;
|
|
47
|
+
disabled: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface ItemContext {
|
|
51
|
+
value: string;
|
|
52
|
+
isExpanded: boolean;
|
|
53
|
+
disabled: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type { RootProps, SingleRootProps, MultipleRootProps, ItemProps, HeaderProps, TriggerProps, ContentProps, RootContext, ItemContext };
|