cpk-ui 0.5.4 → 0.7.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.
Files changed (136) hide show
  1. package/README.md +80 -147
  2. package/components/modals/AlertDialog/AlertDialog.d.ts.map +1 -1
  3. package/components/modals/AlertDialog/AlertDialog.js +14 -7
  4. package/components/modals/Snackbar/Snackbar.d.ts.map +1 -1
  5. package/components/modals/Snackbar/Snackbar.js +20 -17
  6. package/components/uis/Accordion/AccordionItem.d.ts.map +1 -1
  7. package/components/uis/Accordion/AccordionItem.js +41 -17
  8. package/components/uis/Button/Button.d.ts +2 -1
  9. package/components/uis/Button/Button.d.ts.map +1 -1
  10. package/components/uis/Button/Button.js +11 -11
  11. package/components/uis/Card/Card.d.ts +2 -1
  12. package/components/uis/Card/Card.d.ts.map +1 -1
  13. package/components/uis/Card/Card.js +12 -3
  14. package/components/uis/Checkbox/Checkbox.d.ts +2 -1
  15. package/components/uis/Checkbox/Checkbox.d.ts.map +1 -1
  16. package/components/uis/Checkbox/Checkbox.js +2 -2
  17. package/components/uis/CustomPressable/CustomPressable.d.ts.map +1 -1
  18. package/components/uis/CustomPressable/CustomPressable.js +6 -6
  19. package/components/uis/EditText/EditText.d.ts +1 -0
  20. package/components/uis/EditText/EditText.d.ts.map +1 -1
  21. package/components/uis/EditText/EditText.js +18 -13
  22. package/components/uis/Fab/Fab.d.ts +2 -2
  23. package/components/uis/Fab/Fab.d.ts.map +1 -1
  24. package/components/uis/Fab/Fab.js +22 -4
  25. package/components/uis/Icon/Icon.d.ts.map +1 -1
  26. package/components/uis/Icon/Icon.js +6 -3
  27. package/components/uis/IconButton/IconButton.d.ts +2 -1
  28. package/components/uis/IconButton/IconButton.d.ts.map +1 -1
  29. package/components/uis/IconButton/IconButton.js +44 -42
  30. package/components/uis/LoadingIndicator/LoadingIndicator.d.ts +2 -1
  31. package/components/uis/LoadingIndicator/LoadingIndicator.d.ts.map +1 -1
  32. package/components/uis/LoadingIndicator/LoadingIndicator.js +4 -4
  33. package/components/uis/RadioGroup/RadioButton.d.ts +2 -1
  34. package/components/uis/RadioGroup/RadioButton.d.ts.map +1 -1
  35. package/components/uis/RadioGroup/RadioButton.js +2 -2
  36. package/components/uis/RadioGroup/RadioGroup.d.ts.map +1 -1
  37. package/components/uis/RadioGroup/RadioGroup.js +16 -3
  38. package/components/uis/Rating/Rating.d.ts.map +1 -1
  39. package/components/uis/Rating/Rating.js +23 -19
  40. package/components/uis/SegmentedControl/SegmentedControl.d.ts.map +1 -1
  41. package/components/uis/SegmentedControl/SegmentedControl.js +1 -1
  42. package/components/uis/StatusbarBrightness/StatusBarBrightness.d.ts +2 -2
  43. package/components/uis/StatusbarBrightness/StatusBarBrightness.d.ts.map +1 -1
  44. package/components/uis/StatusbarBrightness/StatusBarBrightness.js +1 -1
  45. package/components/uis/SwitchToggle/SwitchToggle.d.ts +2 -1
  46. package/components/uis/SwitchToggle/SwitchToggle.d.ts.map +1 -1
  47. package/components/uis/SwitchToggle/SwitchToggle.js +2 -2
  48. package/hooks/useHoverState.d.ts +8 -0
  49. package/hooks/useHoverState.d.ts.map +1 -0
  50. package/hooks/useHoverState.js +16 -0
  51. package/package.json +18 -6
  52. package/providers/ThemeProvider.d.ts.map +1 -1
  53. package/providers/ThemeProvider.js +0 -1
  54. package/providers/index.d.ts.map +1 -1
  55. package/providers/index.js +3 -2
  56. package/components/uis/Icon/selection.json +0 -1
  57. package/src/components/common/ErrorBoundary/ErrorBoundary.tsx +0 -75
  58. package/src/components/modals/AlertDialog/AlertDialog.stories.tsx +0 -216
  59. package/src/components/modals/AlertDialog/AlertDialog.test.tsx +0 -251
  60. package/src/components/modals/AlertDialog/AlertDialog.tsx +0 -356
  61. package/src/components/modals/Snackbar/Snackbar.stories.tsx +0 -182
  62. package/src/components/modals/Snackbar/Snackbar.test.tsx +0 -319
  63. package/src/components/modals/Snackbar/Snackbar.tsx +0 -260
  64. package/src/components/modals/Snackbar/const.ts +0 -4
  65. package/src/components/uis/Accordion/Accordion.stories.tsx +0 -167
  66. package/src/components/uis/Accordion/Accordion.test.tsx +0 -272
  67. package/src/components/uis/Accordion/Accordion.tsx +0 -109
  68. package/src/components/uis/Accordion/AccordionItem.tsx +0 -306
  69. package/src/components/uis/Button/Button.stories.tsx +0 -218
  70. package/src/components/uis/Button/Button.test.tsx +0 -620
  71. package/src/components/uis/Button/Button.tsx +0 -436
  72. package/src/components/uis/Card/Card.test.tsx +0 -146
  73. package/src/components/uis/Card/Card.tsx +0 -102
  74. package/src/components/uis/Checkbox/Checkbox.stories.tsx +0 -216
  75. package/src/components/uis/Checkbox/Checkbox.test.tsx +0 -173
  76. package/src/components/uis/Checkbox/Checkbox.tsx +0 -220
  77. package/src/components/uis/CustomPressable/CustomPressable.tsx +0 -48
  78. package/src/components/uis/EditText/EditText.stories.tsx +0 -124
  79. package/src/components/uis/EditText/EditText.test.tsx +0 -477
  80. package/src/components/uis/EditText/EditText.tsx +0 -590
  81. package/src/components/uis/Fab/Fab.stories.tsx +0 -133
  82. package/src/components/uis/Fab/Fab.test.tsx +0 -110
  83. package/src/components/uis/Fab/Fab.tsx +0 -168
  84. package/src/components/uis/Hr/Hr.test.tsx +0 -26
  85. package/src/components/uis/Hr/Hr.tsx +0 -19
  86. package/src/components/uis/Icon/Icon.stories.tsx +0 -98
  87. package/src/components/uis/Icon/Icon.test.tsx +0 -49
  88. package/src/components/uis/Icon/Icon.tsx +0 -3819
  89. package/src/components/uis/IconButton/IconButton.test.tsx +0 -165
  90. package/src/components/uis/IconButton/IconButton.tsx +0 -275
  91. package/src/components/uis/LoadingIndicator/LoadingIndicator.tsx +0 -126
  92. package/src/components/uis/LoadingIndicator/LodingIndicator.stories.tsx +0 -81
  93. package/src/components/uis/PinchZoom/PinchZoom.stories.tsx +0 -266
  94. package/src/components/uis/PinchZoom/PinchZoom.test.tsx +0 -113
  95. package/src/components/uis/PinchZoom/PinchZoom.tsx +0 -424
  96. package/src/components/uis/RadioGroup/RadioButton.tsx +0 -188
  97. package/src/components/uis/RadioGroup/RadioGroup.stories.tsx +0 -188
  98. package/src/components/uis/RadioGroup/RadioGroup.test.tsx +0 -287
  99. package/src/components/uis/RadioGroup/RadioGroup.tsx +0 -109
  100. package/src/components/uis/Rating/Rating.test.tsx +0 -98
  101. package/src/components/uis/Rating/Rating.tsx +0 -200
  102. package/src/components/uis/SegmentedControl/SegmentedControl.stories.tsx +0 -546
  103. package/src/components/uis/SegmentedControl/SegmentedControl.test.tsx +0 -274
  104. package/src/components/uis/SegmentedControl/SegmentedControl.tsx +0 -229
  105. package/src/components/uis/StatusbarBrightness/StatusBarBrightness.test.tsx +0 -41
  106. package/src/components/uis/StatusbarBrightness/StatusBarBrightness.tsx +0 -26
  107. package/src/components/uis/Styled/StyledComponents.tsx +0 -217
  108. package/src/components/uis/SwitchToggle/SwitchToggle.stories.tsx +0 -75
  109. package/src/components/uis/SwitchToggle/SwitchToggle.test.tsx +0 -105
  110. package/src/components/uis/SwitchToggle/SwitchToggle.tsx +0 -305
  111. package/src/components/uis/Typography/Typography.stories.tsx +0 -149
  112. package/src/components/uis/Typography/Typography.test.tsx +0 -58
  113. package/src/components/uis/Typography/Typography.tsx +0 -386
  114. package/src/hooks/index.ts +0 -2
  115. package/src/hooks/useAnimations.ts +0 -71
  116. package/src/hooks/useDebouncedColorScheme.tsx +0 -30
  117. package/src/hooks/useOptimizedStyles.ts +0 -22
  118. package/src/hooks/usePerformance.ts +0 -93
  119. package/src/index.tsx +0 -38
  120. package/src/providers/ThemeProvider.tsx +0 -177
  121. package/src/providers/index.tsx +0 -130
  122. package/src/test/testUtils.tsx +0 -101
  123. package/src/types/common.ts +0 -77
  124. package/src/types/index.ts +0 -1
  125. package/src/types/kstyled.d.ts +0 -6
  126. package/src/utils/accessibility.ts +0 -110
  127. package/src/utils/colors.ts +0 -151
  128. package/src/utils/createCtx.tsx +0 -26
  129. package/src/utils/guards.ts +0 -93
  130. package/src/utils/index.ts +0 -2
  131. package/src/utils/performanceTest.ts +0 -174
  132. package/src/utils/platform.ts +0 -69
  133. package/src/utils/theme.ts +0 -38
  134. package/test/testUtils.d.ts +0 -113
  135. package/test/testUtils.d.ts.map +0 -1
  136. package/test/testUtils.js +0 -65
@@ -1,167 +0,0 @@
1
- import type {ComponentProps} from 'react';
2
- import {css} from 'kstyled';
3
- import type {Meta, StoryObj} from '@storybook/react';
4
- import {Accordion} from './Accordion';
5
- import {withThemeProvider} from '../../../../.storybook/decorators';
6
-
7
- const ACCORDION_DOCS = `
8
- An expandable/collapsible accordion component for organizing content hierarchically.
9
-
10
- ## Features
11
- - **Smooth Animations**: Configurable animation duration for expand/collapse
12
- - **Flexible Sizing**: Preset sizes and custom numeric values
13
- - **Initial Expansion Control**: Control which items start expanded
14
- - **Item Press Handling**: Callback for item interactions
15
- - **Customizable Content**: Flexible data structure for titles and items
16
- - **Animation Control**: Can disable animations if needed
17
-
18
- ## Size Options
19
- - \`small\`: Compact accordion (title: 40px height, 14px font)
20
- - \`medium\`: Standard accordion (title: 48px height, 16px font) - default
21
- - \`large\`: Large accordion (title: 56px height, 18px font)
22
- - Custom number: Custom size with proportional scaling
23
-
24
- ## Usage
25
- \`\`\`tsx
26
- // Default: All items collapsed
27
- <Accordion data={data} />
28
-
29
- // First item expanded
30
- <Accordion data={data} defaultExpandedIndexes={[0]} />
31
-
32
- // Multiple items expanded (first and third)
33
- <Accordion data={data} defaultExpandedIndexes={[0, 2]} />
34
-
35
- // All items expanded
36
- <Accordion data={data} expandAllOnStart={true} />
37
- \`\`\`
38
-
39
- ## Initial Expansion Props
40
- - **defaultExpandedIndexes**: Array of indexes to expand on start (e.g., \`[0, 2]\`)
41
- - **expandAllOnStart**: Boolean to expand all items on start
42
- - **collapseOnStart**: **@deprecated** Use \`expandAllOnStart={false}\` instead
43
-
44
- **Priority order:** \`defaultExpandedIndexes\` > \`expandAllOnStart\` > \`collapseOnStart\` (deprecated) > default (all collapsed)
45
-
46
- ## Props
47
- - **size**: Accordion size ('small', 'medium', 'large', or number)
48
- - **data**: Array of accordion sections with title and items
49
- - **defaultExpandedIndexes**: Array of indexes to expand on start
50
- - **expandAllOnStart**: Whether all sections start expanded
51
- - **collapseOnStart**: **@deprecated** Whether sections start collapsed (use expandAllOnStart instead)
52
- - **animDuration**: Animation duration in milliseconds
53
- - **shouldAnimate**: Enable/disable animations
54
- - **onPressItem**: Callback when an item is pressed
55
- `;
56
-
57
- const meta = {
58
- title: 'Accordion',
59
- component: Accordion,
60
- parameters: {
61
- notes: ACCORDION_DOCS,
62
- docs: {
63
- description: {
64
- component: ACCORDION_DOCS,
65
- },
66
- },
67
- },
68
- argTypes: {
69
- size: {
70
- control: 'radio',
71
- options: ['small', 'medium', 'large', 16, 20, 24],
72
- description: 'Accordion size: "small" (40px title), "medium" (48px title), "large" (56px title), or custom number',
73
- },
74
- },
75
- decorators: [withThemeProvider],
76
- } satisfies Meta<typeof Accordion>;
77
-
78
- export default meta;
79
-
80
- type Story = StoryObj<typeof meta>;
81
-
82
- const defaultData = [
83
- {
84
- title: 'Item 1',
85
- items: ['User', 'Mail', 'Text'],
86
- },
87
- {
88
- title: 'Item 2',
89
- items: ['User', 'Mail', 'Text'],
90
- },
91
- {
92
- title: 'Item 3',
93
- items: ['User', 'Mail', 'Text'],
94
- },
95
- ];
96
-
97
- export const Basic: Story = {
98
- args: {
99
- size: 'medium',
100
- animDuration: 200,
101
- collapseOnStart: true,
102
- onPressItem: () => {},
103
- data: defaultData,
104
- shouldAnimate: true,
105
- },
106
- argTypes: {
107
- // @ts-expect-error - theme is for storybook control
108
- theme: {
109
- control: 'select',
110
- options: ['light', 'dark'],
111
- },
112
- },
113
- };
114
-
115
- export const FirstItemExpanded: Story = {
116
- args: {
117
- size: 'medium',
118
- animDuration: 200,
119
- defaultExpandedIndexes: [0],
120
- onPressItem: () => {},
121
- data: defaultData,
122
- shouldAnimate: true,
123
- },
124
- argTypes: {
125
- // @ts-expect-error - theme is for storybook control
126
- theme: {
127
- control: 'select',
128
- options: ['light', 'dark'],
129
- },
130
- },
131
- };
132
-
133
- export const MultipleItemsExpanded: Story = {
134
- args: {
135
- size: 'medium',
136
- animDuration: 200,
137
- defaultExpandedIndexes: [0, 2],
138
- onPressItem: () => {},
139
- data: defaultData,
140
- shouldAnimate: true,
141
- },
142
- argTypes: {
143
- // @ts-expect-error - theme is for storybook control
144
- theme: {
145
- control: 'select',
146
- options: ['light', 'dark'],
147
- },
148
- },
149
- };
150
-
151
- export const AllItemsExpanded: Story = {
152
- args: {
153
- size: 'medium',
154
- animDuration: 200,
155
- expandAllOnStart: true,
156
- onPressItem: () => {},
157
- data: defaultData,
158
- shouldAnimate: true,
159
- },
160
- argTypes: {
161
- // @ts-expect-error - theme is for storybook control
162
- theme: {
163
- control: 'select',
164
- options: ['light', 'dark'],
165
- },
166
- },
167
- };
@@ -1,272 +0,0 @@
1
- import React, {type ReactElement} from 'react';
2
- import {Text} from 'react-native';
3
- import type {RenderAPI} from '@testing-library/react-native';
4
- import {fireEvent, render} from '@testing-library/react-native';
5
-
6
- import {createComponent, createTestProps} from '../../../../test/testUtils';
7
- import {Accordion} from './Accordion';
8
-
9
- let props: any;
10
- let component: ReactElement;
11
- let testingLib: RenderAPI;
12
-
13
- const data: any[] = [
14
- {
15
- title: 'title1',
16
- items: ['body1', 'body2', 'body3'],
17
- },
18
- {
19
- title: 'title2',
20
- items: ['body1', 'body2', 'body3'],
21
- },
22
- {
23
- title: 'title3',
24
- items: ['body1', 'body2', 'body3'],
25
- },
26
- ];
27
-
28
- describe('[Accordion] render test', () => {
29
- it('should render without crashing', () => {
30
- props = createTestProps({
31
- data: data,
32
- renderTitle: (title) => <Text>{title}</Text>,
33
- renderItem: (item) => <Text>{item}</Text>,
34
- });
35
-
36
- component = createComponent(<Accordion {...props} />);
37
-
38
- testingLib = render(component);
39
-
40
- const json = testingLib.toJSON();
41
- expect(json).toBeTruthy();
42
- });
43
-
44
- it('should render collapsed when collapseOnStart props is true (deprecated)', () => {
45
- props = createTestProps({
46
- collapseOnStart: true,
47
- data: data,
48
- renderTitle: (title) => <Text>{title}</Text>,
49
- renderItem: (item) => <Text>{item}</Text>,
50
- });
51
-
52
- component = createComponent(<Accordion {...props} />);
53
- testingLib = render(component);
54
-
55
- const json = testingLib.toJSON();
56
-
57
- expect(json).toBeTruthy();
58
- });
59
-
60
- it('should render all expanded when expandAllOnStart is true', () => {
61
- props = createTestProps({
62
- expandAllOnStart: true,
63
- data: data,
64
- renderTitle: (title) => <Text>{title}</Text>,
65
- renderItem: (item) => <Text>{item}</Text>,
66
- });
67
-
68
- component = createComponent(<Accordion {...props} />);
69
- testingLib = render(component);
70
-
71
- const json = testingLib.toJSON();
72
-
73
- expect(json).toBeTruthy();
74
- });
75
-
76
- it('should render first item expanded when defaultExpandedIndexes is [0]', () => {
77
- props = createTestProps({
78
- defaultExpandedIndexes: [0],
79
- data: data,
80
- renderTitle: (title) => <Text>{title}</Text>,
81
- renderItem: (item) => <Text>{item}</Text>,
82
- });
83
-
84
- component = createComponent(<Accordion {...props} />);
85
- testingLib = render(component);
86
-
87
- const json = testingLib.toJSON();
88
-
89
- expect(json).toBeTruthy();
90
- });
91
-
92
- it('should render multiple items expanded when defaultExpandedIndexes is [0, 2]', () => {
93
- props = createTestProps({
94
- defaultExpandedIndexes: [0, 2],
95
- data: data,
96
- renderTitle: (title) => <Text>{title}</Text>,
97
- renderItem: (item) => <Text>{item}</Text>,
98
- });
99
-
100
- component = createComponent(<Accordion {...props} />);
101
- testingLib = render(component);
102
-
103
- const json = testingLib.toJSON();
104
-
105
- expect(json).toBeTruthy();
106
- });
107
-
108
- it('should prioritize defaultExpandedIndexes over expandAllOnStart', () => {
109
- props = createTestProps({
110
- defaultExpandedIndexes: [0],
111
- expandAllOnStart: true,
112
- data: data,
113
- renderTitle: (title) => <Text>{title}</Text>,
114
- renderItem: (item) => <Text>{item}</Text>,
115
- });
116
-
117
- component = createComponent(<Accordion {...props} />);
118
- testingLib = render(component);
119
-
120
- const json = testingLib.toJSON();
121
-
122
- expect(json).toBeTruthy();
123
- });
124
-
125
- it('should prioritize expandAllOnStart over collapseOnStart (deprecated)', () => {
126
- props = createTestProps({
127
- expandAllOnStart: false,
128
- collapseOnStart: false,
129
- data: data,
130
- renderTitle: (title) => <Text>{title}</Text>,
131
- renderItem: (item) => <Text>{item}</Text>,
132
- });
133
-
134
- component = createComponent(<Accordion {...props} />);
135
- testingLib = render(component);
136
-
137
- const json = testingLib.toJSON();
138
-
139
- expect(json).toBeTruthy();
140
- });
141
-
142
- it('should operate animation when shouldAnimate props is true', () => {
143
- props = createTestProps({
144
- shouldAnimate: true,
145
- data: data,
146
- renderTitle: (title) => <Text>{title}</Text>,
147
- renderItem: (item) => <Text>{item}</Text>,
148
- });
149
-
150
- component = createComponent(<Accordion {...props} />);
151
-
152
- testingLib = render(component);
153
-
154
- const json = testingLib.toJSON();
155
-
156
- expect(json).toBeTruthy();
157
- });
158
-
159
- it('should adjust duration of animation depends on animDuration props value', () => {
160
- props = createTestProps({
161
- animDuration: 500,
162
- data: data,
163
- renderTitle: (title) => <Text>{title}</Text>,
164
- renderItem: (item) => <Text>{item}</Text>,
165
- });
166
-
167
- component = createComponent(<Accordion {...props} />);
168
- testingLib = render(component);
169
-
170
- const json = testingLib.toJSON();
171
-
172
- expect(json).toBeTruthy();
173
- });
174
- });
175
-
176
- describe('[Accordion] event test', () => {
177
- beforeEach(() => {
178
- props = createTestProps({
179
- data: data,
180
- renderTitle: (title) => <Text>{title}</Text>,
181
- renderItem: (item) => <Text>{item}</Text>,
182
- });
183
-
184
- component = createComponent(<Accordion {...props} />);
185
- testingLib = render(component);
186
- });
187
-
188
- it('should trigger onLayout event when itemBody rendered', () => {
189
- const {getByTestId} = testingLib;
190
- const itemTitle = getByTestId('body-0');
191
-
192
- fireEvent(itemTitle, 'layout', {
193
- nativeEvent: {
194
- layout: {
195
- height: 300,
196
- },
197
- },
198
- });
199
-
200
- expect(itemTitle.props.style.height).toBeDefined();
201
- });
202
-
203
- it('should trigger press event when clicking title', () => {
204
- fireEvent.press(testingLib.getByTestId('title-0'));
205
-
206
- expect(
207
- testingLib.getByTestId('body-0').props.accessibilityState.expanded,
208
- ).toBeTruthy();
209
- });
210
- });
211
-
212
- describe('[Accordion] sizes', () => {
213
- it('should render with small size', () => {
214
- props = createTestProps({
215
- data: data,
216
- size: 'small',
217
- renderTitle: (title) => <Text>{title}</Text>,
218
- renderItem: (item) => <Text>{item}</Text>,
219
- });
220
-
221
- component = createComponent(<Accordion {...props} />);
222
- testingLib = render(component);
223
-
224
- const json = testingLib.toJSON();
225
- expect(json).toBeTruthy();
226
- });
227
-
228
- it('should render with medium size', () => {
229
- props = createTestProps({
230
- data: data,
231
- size: 'medium',
232
- renderTitle: (title) => <Text>{title}</Text>,
233
- renderItem: (item) => <Text>{item}</Text>,
234
- });
235
-
236
- component = createComponent(<Accordion {...props} />);
237
- testingLib = render(component);
238
-
239
- const json = testingLib.toJSON();
240
- expect(json).toBeTruthy();
241
- });
242
-
243
- it('should render with large size', () => {
244
- props = createTestProps({
245
- data: data,
246
- size: 'large',
247
- renderTitle: (title) => <Text>{title}</Text>,
248
- renderItem: (item) => <Text>{item}</Text>,
249
- });
250
-
251
- component = createComponent(<Accordion {...props} />);
252
- testingLib = render(component);
253
-
254
- const json = testingLib.toJSON();
255
- expect(json).toBeTruthy();
256
- });
257
-
258
- it('should render with custom numeric size', () => {
259
- props = createTestProps({
260
- data: data,
261
- size: 20,
262
- renderTitle: (title) => <Text>{title}</Text>,
263
- renderItem: (item) => <Text>{item}</Text>,
264
- });
265
-
266
- component = createComponent(<Accordion {...props} />);
267
- testingLib = render(component);
268
-
269
- const json = testingLib.toJSON();
270
- expect(json).toBeTruthy();
271
- });
272
- });
@@ -1,109 +0,0 @@
1
- import React, {useMemo, type ReactElement} from 'react';
2
- import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
3
- import {View} from 'react-native';
4
- import {styled} from 'kstyled';
5
-
6
- import type {AccordionItemDataType} from './AccordionItem';
7
- import {AccordionItem} from './AccordionItem';
8
-
9
- const Container = styled(View)`
10
- flex-direction: column;
11
- `;
12
-
13
- type Styles = {
14
- container?: StyleProp<ViewStyle>;
15
- titleContainer?: StyleProp<ViewStyle>;
16
- titleText?: StyleProp<TextStyle>;
17
- itemContainer?: StyleProp<ViewStyle>;
18
- itemText?: StyleProp<TextStyle>;
19
- toggleElement?: StyleProp<ViewStyle>;
20
- };
21
-
22
- export type AccordionSizeType = 'small' | 'medium' | 'large' | number;
23
-
24
- export type AccordionBaseProps<T = string, K = string> = {
25
- data: AccordionItemDataType<T, K>[];
26
- style?: StyleProp<ViewStyle>;
27
- styles?: Styles;
28
- size?: AccordionSizeType;
29
- shouldAnimate?: boolean;
30
- /** @deprecated Use expandAllOnStart instead */
31
- collapseOnStart?: boolean;
32
- /** If true, all items start expanded. Defaults to false (all items collapsed) */
33
- expandAllOnStart?: boolean;
34
- /** Array of indexes that should be expanded on start. Overrides expandAllOnStart */
35
- defaultExpandedIndexes?: number[];
36
- animDuration?: number;
37
- activeOpacity?: number;
38
- toggleElementPosition?: 'left' | 'right';
39
- toggleElement?: ReactElement | null;
40
- renderTitle?: (title: T) => ReactElement;
41
- renderItem?: (body: K) => ReactElement;
42
- onPressItem?: (title: T | string, body: K | string) => void;
43
- };
44
-
45
- export type AccordionProps<T = string, K = string> = AccordionBaseProps<T, K>;
46
-
47
- function Accordion<T, K>({
48
- style,
49
- toggleElementPosition = 'right',
50
- size = 'medium',
51
- data,
52
- collapseOnStart,
53
- expandAllOnStart,
54
- defaultExpandedIndexes,
55
- ...rest
56
- }: AccordionProps<T, K>): ReactElement {
57
- // Memoize accordion items rendering
58
- const accordionItems = useMemo(
59
- () =>
60
- data.map((datum, titleKey) => {
61
- // Determine if this item should be collapsed on start
62
- let shouldCollapse: boolean;
63
-
64
- if (
65
- defaultExpandedIndexes !== undefined &&
66
- defaultExpandedIndexes.length > 0
67
- ) {
68
- // If defaultExpandedIndexes is provided, use it
69
- shouldCollapse = !defaultExpandedIndexes.includes(titleKey);
70
- } else if (expandAllOnStart !== undefined) {
71
- // If expandAllOnStart is provided, use it
72
- shouldCollapse = !expandAllOnStart;
73
- } else if (collapseOnStart !== undefined) {
74
- // Fallback to deprecated collapseOnStart for backward compatibility
75
- shouldCollapse = collapseOnStart;
76
- } else {
77
- // Default: all items collapsed
78
- shouldCollapse = true;
79
- }
80
-
81
- return (
82
- <AccordionItem
83
- data={datum}
84
- key={titleKey}
85
- testID={`${titleKey}`}
86
- toggleElementPosition={toggleElementPosition}
87
- size={size}
88
- collapseOnStart={shouldCollapse}
89
- {...rest}
90
- />
91
- );
92
- }),
93
- [
94
- data,
95
- toggleElementPosition,
96
- size,
97
- collapseOnStart,
98
- expandAllOnStart,
99
- defaultExpandedIndexes,
100
- rest,
101
- ],
102
- );
103
-
104
- return <Container style={style}>{accordionItems}</Container>;
105
- }
106
-
107
- // Export memoized component for better performance
108
- export default React.memo(Accordion) as typeof Accordion;
109
- export {Accordion};