@ledgerhq/lumen-ui-rnative 0.1.63 → 0.1.64
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/module/lib/Components/core/AmountDisplay/AmountDisplay.js +19 -11
- package/dist/module/lib/Components/core/AmountDisplay/AmountDisplay.js.map +1 -1
- package/dist/module/lib/Components/core/AmountDisplay/AmountDisplay.test.js +113 -4
- package/dist/module/lib/Components/core/AmountDisplay/AmountDisplay.test.js.map +1 -1
- package/dist/module/lib/Components/core/BottomSheet/BottomSheet.stories.js +51 -0
- package/dist/module/lib/Components/core/BottomSheet/BottomSheet.stories.js.map +1 -1
- package/dist/module/lib/Components/core/MenuList/MenuList.js +93 -7
- package/dist/module/lib/Components/core/MenuList/MenuList.js.map +1 -1
- package/dist/module/lib/Components/core/MenuList/MenuList.mdx +14 -1
- package/dist/module/lib/Components/core/MenuList/MenuList.stories.js +39 -2
- package/dist/module/lib/Components/core/MenuList/MenuList.stories.js.map +1 -1
- package/dist/module/lib/Components/core/MenuList/MenuList.test.js +60 -2
- package/dist/module/lib/Components/core/MenuList/MenuList.test.js.map +1 -1
- package/dist/module/lib/Components/core/SegmentedControl/SegmentedControl.js +1 -1
- package/dist/module/lib/Components/core/SegmentedControl/SegmentedControl.js.map +1 -1
- package/dist/typescript/src/lib/Components/core/AmountDisplay/AmountDisplay.d.ts.map +1 -1
- package/dist/typescript/src/lib/Components/core/MenuList/MenuList.d.ts +12 -2
- package/dist/typescript/src/lib/Components/core/MenuList/MenuList.d.ts.map +1 -1
- package/dist/typescript/src/lib/Components/core/MenuList/types.d.ts +26 -7
- package/dist/typescript/src/lib/Components/core/MenuList/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/lib/Components/core/AmountDisplay/AmountDisplay.test.tsx +138 -4
- package/src/lib/Components/core/AmountDisplay/AmountDisplay.tsx +13 -6
- package/src/lib/Components/core/BottomSheet/BottomSheet.stories.tsx +51 -0
- package/src/lib/Components/core/MenuList/MenuList.mdx +14 -1
- package/src/lib/Components/core/MenuList/MenuList.stories.tsx +30 -2
- package/src/lib/Components/core/MenuList/MenuList.test.tsx +72 -2
- package/src/lib/Components/core/MenuList/MenuList.tsx +94 -11
- package/src/lib/Components/core/MenuList/types.ts +29 -7
- package/src/lib/Components/core/SegmentedControl/SegmentedControl.tsx +1 -1
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import { describe, it, expect } from '@jest/globals';
|
|
1
|
+
import { afterEach, describe, it, expect, jest } from '@jest/globals';
|
|
2
2
|
import { ledgerLiveThemes } from '@ledgerhq/lumen-design-core';
|
|
3
|
-
import { render, screen } from '@testing-library/react-native';
|
|
3
|
+
import { cleanup, render, screen } from '@testing-library/react-native';
|
|
4
|
+
import { useRef } from 'react';
|
|
4
5
|
import { ThemeProvider } from '../ThemeProvider/ThemeProvider';
|
|
5
6
|
import { AmountDisplay } from './AmountDisplay';
|
|
6
7
|
import type { FormattedValue } from './types';
|
|
7
8
|
|
|
8
|
-
const TestWrapper: React.FC<{
|
|
9
|
-
|
|
9
|
+
const TestWrapper: React.FC<{
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
themes?: typeof ledgerLiveThemes;
|
|
12
|
+
}> = ({ children, themes = ledgerLiveThemes }) => (
|
|
13
|
+
<ThemeProvider themes={themes} colorScheme='dark' locale='en'>
|
|
10
14
|
{children}
|
|
11
15
|
</ThemeProvider>
|
|
12
16
|
);
|
|
@@ -332,4 +336,134 @@ describe('AmountDisplay', () => {
|
|
|
332
336
|
});
|
|
333
337
|
});
|
|
334
338
|
});
|
|
339
|
+
|
|
340
|
+
describe('re-rendering a mounted instance', () => {
|
|
341
|
+
afterEach(() => {
|
|
342
|
+
// Unmount first: restoring a spied hook on a mounted tree breaks the
|
|
343
|
+
// hook order of any late re-render
|
|
344
|
+
cleanup();
|
|
345
|
+
jest.restoreAllMocks();
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('updates the digit strips when the value changes while animating', () => {
|
|
349
|
+
const { rerender } = render(
|
|
350
|
+
<TestWrapper>
|
|
351
|
+
<AmountDisplay
|
|
352
|
+
value={1234.56}
|
|
353
|
+
formatter={createFormatter()}
|
|
354
|
+
animate={true}
|
|
355
|
+
/>
|
|
356
|
+
</TestWrapper>,
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
expect(getDigitStripValues()).toEqual([1, 2, 3, 4, 5, 6]);
|
|
360
|
+
|
|
361
|
+
rerender(
|
|
362
|
+
<TestWrapper>
|
|
363
|
+
<AmountDisplay
|
|
364
|
+
value={9876.54}
|
|
365
|
+
formatter={createFormatter({
|
|
366
|
+
integerPart: '9876',
|
|
367
|
+
decimalPart: '54',
|
|
368
|
+
})}
|
|
369
|
+
animate={true}
|
|
370
|
+
/>
|
|
371
|
+
</TestWrapper>,
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
expect(getDigitStripValues()).toEqual([9, 8, 7, 6, 5, 4]);
|
|
375
|
+
getDigitStripWidths().forEach((width) => {
|
|
376
|
+
expect(typeof width).not.toBe('number');
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it('keeps the digit strip geometry correct when animate is toggled', () => {
|
|
381
|
+
const formatter = createFormatter();
|
|
382
|
+
const tree = (animate: boolean) => (
|
|
383
|
+
<TestWrapper>
|
|
384
|
+
<AmountDisplay
|
|
385
|
+
value={1234.56}
|
|
386
|
+
formatter={formatter}
|
|
387
|
+
animate={animate}
|
|
388
|
+
/>
|
|
389
|
+
</TestWrapper>
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
const { rerender } = render(tree(true));
|
|
393
|
+
rerender(tree(false));
|
|
394
|
+
|
|
395
|
+
expect(getDigitStripValues()).toEqual([1, 2, 3, 4, 5, 6]);
|
|
396
|
+
getDigitStripWidths().forEach((width) => {
|
|
397
|
+
expect(typeof width).toBe('number');
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
rerender(tree(true));
|
|
401
|
+
|
|
402
|
+
expect(getDigitStripValues()).toEqual([1, 2, 3, 4, 5, 6]);
|
|
403
|
+
getDigitStripWidths().forEach((width) => {
|
|
404
|
+
expect(typeof width).not.toBe('number');
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it('swaps between bullets and digits when hidden is toggled', () => {
|
|
409
|
+
const formatter = createFormatter();
|
|
410
|
+
const tree = (isHidden: boolean) => (
|
|
411
|
+
<TestWrapper>
|
|
412
|
+
<AmountDisplay
|
|
413
|
+
value={1234.56}
|
|
414
|
+
formatter={formatter}
|
|
415
|
+
hidden={isHidden}
|
|
416
|
+
/>
|
|
417
|
+
</TestWrapper>
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
const { rerender } = render(tree(false));
|
|
421
|
+
rerender(tree(true));
|
|
422
|
+
|
|
423
|
+
expect(screen.getByText('••••', hidden)).toBeTruthy();
|
|
424
|
+
expect(getDigitStripValues()).toEqual([]);
|
|
425
|
+
|
|
426
|
+
rerender(tree(false));
|
|
427
|
+
|
|
428
|
+
expect(screen.queryByText('••••', hidden)).toBeNull();
|
|
429
|
+
expect(getDigitStripValues()).toEqual([1, 2, 3, 4, 5, 6]);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it('does not restart digit animations when an equivalent theme object is passed', () => {
|
|
433
|
+
const Reanimated = jest.requireMock('react-native-reanimated') as {
|
|
434
|
+
useSharedValue: (initial: unknown) => { value: unknown };
|
|
435
|
+
withTiming: (
|
|
436
|
+
toValue: unknown,
|
|
437
|
+
config?: { duration?: number },
|
|
438
|
+
) => unknown;
|
|
439
|
+
};
|
|
440
|
+
// The global mock creates a new shared value on every render, which
|
|
441
|
+
// would re-run the digit-strip effect regardless of the timing config
|
|
442
|
+
const useStableSharedValue = (initial: unknown): { value: unknown } =>
|
|
443
|
+
useRef({ value: initial }).current;
|
|
444
|
+
jest
|
|
445
|
+
.spyOn(Reanimated, 'useSharedValue')
|
|
446
|
+
.mockImplementation(useStableSharedValue);
|
|
447
|
+
const withTiming = jest.spyOn(Reanimated, 'withTiming');
|
|
448
|
+
// Pulse also reschedules when the theme object changes, so only the
|
|
449
|
+
// digit-strip duration is tracked
|
|
450
|
+
const digitStripAnimations = () =>
|
|
451
|
+
withTiming.mock.calls.filter(([, config]) => config?.duration === 700);
|
|
452
|
+
|
|
453
|
+
const formatter = createFormatter();
|
|
454
|
+
const tree = () => (
|
|
455
|
+
<TestWrapper themes={{ ...ledgerLiveThemes }}>
|
|
456
|
+
<AmountDisplay value={1234.56} formatter={formatter} animate={true} />
|
|
457
|
+
</TestWrapper>
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
const { rerender } = render(tree());
|
|
461
|
+
expect(digitStripAnimations().length).toBeGreaterThan(0);
|
|
462
|
+
|
|
463
|
+
withTiming.mockClear();
|
|
464
|
+
rerender(tree());
|
|
465
|
+
|
|
466
|
+
expect(digitStripAnimations()).toEqual([]);
|
|
467
|
+
});
|
|
468
|
+
});
|
|
335
469
|
});
|
|
@@ -10,7 +10,7 @@ import Animated, {
|
|
|
10
10
|
import { useCommonTranslation } from '../../../../i18n';
|
|
11
11
|
import type { LumenTypographyTokenName } from '../../../../styles';
|
|
12
12
|
import { useStyleSheet } from '../../../../styles';
|
|
13
|
-
import { RuntimeConstants } from '../../../utils';
|
|
13
|
+
import { RuntimeConstants, useGet } from '../../../utils';
|
|
14
14
|
import { Pulse } from '../../animations/Pulse';
|
|
15
15
|
import { useTimingConfig } from '../../animations/useTimingConfig';
|
|
16
16
|
import { Box } from '../../primitives';
|
|
@@ -135,11 +135,13 @@ const useAnimatedDigitStrip = ({
|
|
|
135
135
|
duration: 700,
|
|
136
136
|
easing: 'easeInOut',
|
|
137
137
|
});
|
|
138
|
+
// Avoid restarting withTiming when theme rebuilds a new config object
|
|
139
|
+
const getTimingConfig = useGet(timingConfig);
|
|
138
140
|
|
|
139
141
|
useEffect(() => {
|
|
140
142
|
if (animate) {
|
|
141
|
-
translateY.value = withTiming(-value * lineHeight,
|
|
142
|
-
width.value = withTiming(targetWidth,
|
|
143
|
+
translateY.value = withTiming(-value * lineHeight, getTimingConfig());
|
|
144
|
+
width.value = withTiming(targetWidth, getTimingConfig());
|
|
143
145
|
} else {
|
|
144
146
|
translateY.value = -value * lineHeight;
|
|
145
147
|
width.value = targetWidth;
|
|
@@ -151,7 +153,7 @@ const useAnimatedDigitStrip = ({
|
|
|
151
153
|
animate,
|
|
152
154
|
width,
|
|
153
155
|
targetWidth,
|
|
154
|
-
|
|
156
|
+
getTimingConfig,
|
|
155
157
|
]);
|
|
156
158
|
|
|
157
159
|
const animatedStyle = useAnimatedStyle(
|
|
@@ -185,6 +187,7 @@ const DigitStrip = memo(
|
|
|
185
187
|
|
|
186
188
|
return (
|
|
187
189
|
<Animated.View
|
|
190
|
+
collapsable={false}
|
|
188
191
|
style={{
|
|
189
192
|
height: lineHeight,
|
|
190
193
|
width: animate ? width : targetWidth,
|
|
@@ -203,7 +206,10 @@ const DigitStrip = memo(
|
|
|
203
206
|
alignItems: 'center',
|
|
204
207
|
}}
|
|
205
208
|
>
|
|
206
|
-
<Animated.View
|
|
209
|
+
<Animated.View
|
|
210
|
+
collapsable={false}
|
|
211
|
+
style={[animatedStyle, { alignItems: 'center' }]}
|
|
212
|
+
>
|
|
207
213
|
{DIGITS.map((d) => (
|
|
208
214
|
<Text
|
|
209
215
|
allowFontScaling={false}
|
|
@@ -236,8 +242,9 @@ const DigitStripList = memo(
|
|
|
236
242
|
);
|
|
237
243
|
}
|
|
238
244
|
return (
|
|
245
|
+
// Remount when animate flips so width stays either SharedValue or number
|
|
239
246
|
<DigitStrip
|
|
240
|
-
key={key}
|
|
247
|
+
key={`${key}-${animate}`}
|
|
241
248
|
value={Number(item.value) as DigitStripProps['value']}
|
|
242
249
|
animate={animate}
|
|
243
250
|
textStyle={textStyle}
|
|
@@ -2,7 +2,9 @@ import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
|
2
2
|
import type { FC } from 'react';
|
|
3
3
|
import { useEffect, useState } from 'react';
|
|
4
4
|
import { Box, RadialGradient, Text } from '../../primitives';
|
|
5
|
+
import { Bell, Settings, Trash, Unlink } from '../../symbols';
|
|
5
6
|
import { Button } from '../Button';
|
|
7
|
+
import { MenuList, MenuListItem, MenuListSwitchItem } from '../MenuList';
|
|
6
8
|
import { SearchInput } from '../SearchInput';
|
|
7
9
|
import { Spot } from '../Spot';
|
|
8
10
|
import { BottomSheet } from './BottomSheet';
|
|
@@ -935,3 +937,52 @@ export const WithFooter: Story = {
|
|
|
935
937
|
);
|
|
936
938
|
},
|
|
937
939
|
};
|
|
940
|
+
|
|
941
|
+
export const WithMenuList: Story = {
|
|
942
|
+
args: {
|
|
943
|
+
snapPoints: null,
|
|
944
|
+
enableDynamicSizing: true,
|
|
945
|
+
},
|
|
946
|
+
render: (args) => {
|
|
947
|
+
const bottomSheetRef = useBottomSheetRef();
|
|
948
|
+
|
|
949
|
+
return (
|
|
950
|
+
<Box
|
|
951
|
+
lx={{
|
|
952
|
+
height: 's320',
|
|
953
|
+
width: 'full',
|
|
954
|
+
alignItems: 'center',
|
|
955
|
+
justifyContent: 'center',
|
|
956
|
+
paddingTop: 's32',
|
|
957
|
+
}}
|
|
958
|
+
>
|
|
959
|
+
<Button size='sm' onPress={() => bottomSheetRef.current?.present()}>
|
|
960
|
+
Toggle open
|
|
961
|
+
</Button>
|
|
962
|
+
<BottomSheet {...args} ref={bottomSheetRef}>
|
|
963
|
+
<BottomSheetView>
|
|
964
|
+
<BottomSheetHeader
|
|
965
|
+
title='Device Options'
|
|
966
|
+
density='compact'
|
|
967
|
+
description='Manage this device'
|
|
968
|
+
/>
|
|
969
|
+
<MenuList>
|
|
970
|
+
<MenuListItem label='Rename device' icon={Settings} />
|
|
971
|
+
<MenuListItem label='Unlink device' icon={Unlink} />
|
|
972
|
+
<MenuListSwitchItem
|
|
973
|
+
label='Notifications'
|
|
974
|
+
icon={Bell}
|
|
975
|
+
defaultChecked
|
|
976
|
+
/>
|
|
977
|
+
<MenuListItem
|
|
978
|
+
label='Remove device'
|
|
979
|
+
icon={Trash}
|
|
980
|
+
appearance='red'
|
|
981
|
+
/>
|
|
982
|
+
</MenuList>
|
|
983
|
+
</BottomSheetView>
|
|
984
|
+
</BottomSheet>
|
|
985
|
+
</Box>
|
|
986
|
+
);
|
|
987
|
+
},
|
|
988
|
+
};
|
|
@@ -40,9 +40,18 @@ Set `disabled` on individual `MenuListItem`s to prevent interaction. The label a
|
|
|
40
40
|
|
|
41
41
|
<Canvas of={MenuListStories.DisabledShowcase} />
|
|
42
42
|
|
|
43
|
+
### Switch item
|
|
44
|
+
|
|
45
|
+
Use `MenuListSwitchItem` for a toggleable action. It renders a label, an optional leading icon, and a trailing `Switch`. Pressing anywhere on the item toggles the switch.
|
|
46
|
+
|
|
47
|
+
`MenuListSwitchItem` supports controlled (`checked` / `onCheckedChange`) and uncontrolled (`defaultChecked`) usage, same as `Switch`.
|
|
48
|
+
|
|
49
|
+
<Canvas of={MenuListStories.SwitchItemShowcase} />
|
|
50
|
+
<Controls of={MenuListStories.SwitchItemShowcase} />
|
|
51
|
+
|
|
43
52
|
## Accessibility
|
|
44
53
|
|
|
45
|
-
Each `MenuListItem` has `accessibilityRole='button'` and sets `accessibilityState={{ disabled }}` to reflect its interaction state to assistive technologies.
|
|
54
|
+
Each `MenuListItem` has `accessibilityRole='button'` and sets `accessibilityState={{ disabled }}` to reflect its interaction state to assistive technologies. `MenuListSwitchItem` uses `accessibilityRole='switch'` and additionally reflects `checked` in its `accessibilityState`.
|
|
46
55
|
|
|
47
56
|
</Tab>
|
|
48
57
|
|
|
@@ -62,5 +71,9 @@ Each `MenuListItem` has `accessibilityRole='button'` and sets `accessibilityStat
|
|
|
62
71
|
|
|
63
72
|
<Source of={MenuListStories.DisabledShowcase} />
|
|
64
73
|
|
|
74
|
+
### Switch Item
|
|
75
|
+
|
|
76
|
+
<Source of={MenuListStories.SwitchItemShowcase} />
|
|
77
|
+
|
|
65
78
|
</Tab>
|
|
66
79
|
</CustomTabs>
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react-native-web-vite';
|
|
2
2
|
import { Box } from '../../primitives';
|
|
3
3
|
import { Settings, Trash, Unlink } from '../../symbols';
|
|
4
|
-
import { MenuList, MenuListItem } from './MenuList';
|
|
4
|
+
import { MenuList, MenuListItem, MenuListSwitchItem } from './MenuList';
|
|
5
5
|
|
|
6
6
|
const iconMap = {
|
|
7
7
|
None: undefined,
|
|
@@ -14,7 +14,7 @@ const meta = {
|
|
|
14
14
|
id: 'rnative-menulist',
|
|
15
15
|
title: 'Core/MenuList',
|
|
16
16
|
component: MenuList,
|
|
17
|
-
subcomponents: { MenuListItem },
|
|
17
|
+
subcomponents: { MenuListItem, MenuListSwitchItem },
|
|
18
18
|
parameters: {
|
|
19
19
|
docs: {
|
|
20
20
|
source: { language: 'tsx', format: true, type: 'dynamic' },
|
|
@@ -87,6 +87,34 @@ export const AppearanceShowcase: Story = {
|
|
|
87
87
|
),
|
|
88
88
|
};
|
|
89
89
|
|
|
90
|
+
export const SwitchItemShowcase: StoryObj<typeof MenuListSwitchItem> = {
|
|
91
|
+
parameters: {
|
|
92
|
+
layout: 'centered',
|
|
93
|
+
backgrounds: { default: 'light' },
|
|
94
|
+
},
|
|
95
|
+
argTypes: {
|
|
96
|
+
icon: {
|
|
97
|
+
options: Object.keys(iconMap),
|
|
98
|
+
mapping: iconMap,
|
|
99
|
+
control: { type: 'select' },
|
|
100
|
+
},
|
|
101
|
+
onCheckedChange: { action: 'checkedChange' },
|
|
102
|
+
},
|
|
103
|
+
args: {
|
|
104
|
+
label: 'Notifications',
|
|
105
|
+
disabled: false,
|
|
106
|
+
defaultChecked: false,
|
|
107
|
+
icon: Settings,
|
|
108
|
+
},
|
|
109
|
+
render: (args) => (
|
|
110
|
+
<Box lx={{ width: 's320' }}>
|
|
111
|
+
<MenuList>
|
|
112
|
+
<MenuListSwitchItem {...args} />
|
|
113
|
+
</MenuList>
|
|
114
|
+
</Box>
|
|
115
|
+
),
|
|
116
|
+
};
|
|
117
|
+
|
|
90
118
|
export const DisabledShowcase: Story = {
|
|
91
119
|
parameters: {
|
|
92
120
|
layout: 'centered',
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { describe, it, expect, jest } from '@jest/globals';
|
|
2
2
|
import { ledgerLiveThemes } from '@ledgerhq/lumen-design-core';
|
|
3
|
-
import { render, fireEvent } from '@testing-library/react-native';
|
|
3
|
+
import { render, fireEvent, waitFor } from '@testing-library/react-native';
|
|
4
4
|
import { View } from 'react-native';
|
|
5
5
|
import { ThemeProvider } from '../ThemeProvider/ThemeProvider';
|
|
6
|
-
import { MenuList, MenuListItem } from './MenuList';
|
|
6
|
+
import { MenuList, MenuListItem, MenuListSwitchItem } from './MenuList';
|
|
7
7
|
import type { IconComponent } from './types';
|
|
8
8
|
|
|
9
9
|
const TestIcon: IconComponent = ({ size, color }) => (
|
|
@@ -133,3 +133,73 @@ describe('MenuListItem', () => {
|
|
|
133
133
|
expect(getByTestId('icon').props.accessibilityLabel).toBe('error');
|
|
134
134
|
});
|
|
135
135
|
});
|
|
136
|
+
|
|
137
|
+
describe('MenuListSwitchItem', () => {
|
|
138
|
+
it('renders the label', () => {
|
|
139
|
+
const { getByText } = render(
|
|
140
|
+
<TestWrapper>
|
|
141
|
+
<MenuListSwitchItem label='Notifications' />
|
|
142
|
+
</TestWrapper>,
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
expect(getByText('Notifications')).toBeTruthy();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('uncontrolled: toggles checked state on press', async () => {
|
|
149
|
+
const { getByRole } = render(
|
|
150
|
+
<TestWrapper>
|
|
151
|
+
<MenuListSwitchItem label='Notifications' defaultChecked={false} />
|
|
152
|
+
</TestWrapper>,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const item = getByRole('switch');
|
|
156
|
+
expect(item.props.accessibilityState?.checked).toBe(false);
|
|
157
|
+
|
|
158
|
+
fireEvent.press(item);
|
|
159
|
+
|
|
160
|
+
await waitFor(() =>
|
|
161
|
+
expect(item.props.accessibilityState?.checked).toBe(true),
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('controlled: calls onCheckedChange but state is controlled by parent', async () => {
|
|
166
|
+
const onCheckedChange = jest.fn();
|
|
167
|
+
const { getByRole } = render(
|
|
168
|
+
<TestWrapper>
|
|
169
|
+
<MenuListSwitchItem
|
|
170
|
+
label='Notifications'
|
|
171
|
+
checked={false}
|
|
172
|
+
onCheckedChange={onCheckedChange}
|
|
173
|
+
/>
|
|
174
|
+
</TestWrapper>,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
fireEvent.press(getByRole('switch'));
|
|
178
|
+
|
|
179
|
+
await waitFor(() => expect(onCheckedChange).toHaveBeenCalledWith(true));
|
|
180
|
+
await waitFor(() =>
|
|
181
|
+
expect(getByRole('switch').props.accessibilityState?.checked).toBe(false),
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('does not toggle when disabled', async () => {
|
|
186
|
+
const onCheckedChange = jest.fn();
|
|
187
|
+
const { getByRole } = render(
|
|
188
|
+
<TestWrapper>
|
|
189
|
+
<MenuListSwitchItem
|
|
190
|
+
label='Notifications'
|
|
191
|
+
disabled
|
|
192
|
+
defaultChecked={false}
|
|
193
|
+
onCheckedChange={onCheckedChange}
|
|
194
|
+
/>
|
|
195
|
+
</TestWrapper>,
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
const item = getByRole('switch');
|
|
199
|
+
expect(item.props.accessibilityState?.disabled).toBe(true);
|
|
200
|
+
|
|
201
|
+
fireEvent.press(item);
|
|
202
|
+
|
|
203
|
+
await waitFor(() => expect(onCheckedChange).not.toHaveBeenCalled());
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { useDisabledContext } from '@ledgerhq/lumen-utils-shared';
|
|
2
|
-
import { StyleSheet } from 'react-native';
|
|
2
|
+
import { StyleSheet, View } from 'react-native';
|
|
3
3
|
import type { LumenTextStyle } from '../../../../styles';
|
|
4
4
|
import { useStyleSheet } from '../../../../styles';
|
|
5
|
+
import { useControllableState } from '../../../utils';
|
|
5
6
|
import { Box, Pressable, Text } from '../../primitives';
|
|
7
|
+
import { Switch } from '../Switch';
|
|
6
8
|
import type {
|
|
7
9
|
MenuListItemAppearance,
|
|
8
10
|
MenuListItemProps,
|
|
9
11
|
MenuListProps,
|
|
12
|
+
MenuListSwitchItemProps,
|
|
10
13
|
} from './types';
|
|
11
14
|
|
|
12
15
|
const resolveContentColor = (
|
|
@@ -70,16 +73,8 @@ const useItemStyles = ({
|
|
|
70
73
|
* <MenuListItem icon={Trash} label="Remove" appearance="red" onPress={handleRemove} />
|
|
71
74
|
* </MenuList>
|
|
72
75
|
*/
|
|
73
|
-
export const MenuList = ({
|
|
74
|
-
children
|
|
75
|
-
lx,
|
|
76
|
-
style,
|
|
77
|
-
ref,
|
|
78
|
-
...props
|
|
79
|
-
}: MenuListProps) => (
|
|
80
|
-
<Box lx={lx} style={style} ref={ref} {...props}>
|
|
81
|
-
{children}
|
|
82
|
-
</Box>
|
|
76
|
+
export const MenuList = ({ children, ...props }: MenuListProps) => (
|
|
77
|
+
<Box {...props}>{children}</Box>
|
|
83
78
|
);
|
|
84
79
|
|
|
85
80
|
/**
|
|
@@ -160,3 +155,91 @@ const MenuListItemInner = ({
|
|
|
160
155
|
</Box>
|
|
161
156
|
);
|
|
162
157
|
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* A toggleable action inside a `MenuList`: a label, an optional leading icon
|
|
161
|
+
* and a trailing `Switch`. Pressing anywhere on the item toggles the switch.
|
|
162
|
+
*
|
|
163
|
+
* @see {@link https://ldls-react-native.vercel.app/?path=/docs/rnative-menulist--docs Storybook}
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* <MenuListSwitchItem label="Notifications" icon={Bell} defaultChecked />
|
|
167
|
+
*/
|
|
168
|
+
export const MenuListSwitchItem = ({
|
|
169
|
+
label,
|
|
170
|
+
icon: Icon,
|
|
171
|
+
disabled: disabledProp = false,
|
|
172
|
+
checked: checkedProp,
|
|
173
|
+
defaultChecked = false,
|
|
174
|
+
onCheckedChange: onCheckedChangeProp,
|
|
175
|
+
accessibilityState,
|
|
176
|
+
lx,
|
|
177
|
+
style,
|
|
178
|
+
ref,
|
|
179
|
+
...props
|
|
180
|
+
}: MenuListSwitchItemProps) => {
|
|
181
|
+
const disabled = useDisabledContext({
|
|
182
|
+
consumerName: 'MenuListSwitchItem',
|
|
183
|
+
mergeWith: { disabled: disabledProp },
|
|
184
|
+
});
|
|
185
|
+
const [checked, onCheckedChange] = useControllableState({
|
|
186
|
+
prop: checkedProp,
|
|
187
|
+
onChange: onCheckedChangeProp,
|
|
188
|
+
defaultProp: defaultChecked,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<Pressable
|
|
193
|
+
ref={ref}
|
|
194
|
+
lx={lx}
|
|
195
|
+
style={style}
|
|
196
|
+
onPress={() => onCheckedChange(!checked)}
|
|
197
|
+
disabled={disabled}
|
|
198
|
+
accessibilityRole='switch'
|
|
199
|
+
accessibilityState={{ ...accessibilityState, disabled, checked }}
|
|
200
|
+
{...props}
|
|
201
|
+
>
|
|
202
|
+
{({ pressed }) => (
|
|
203
|
+
<MenuListSwitchItemInner
|
|
204
|
+
label={label}
|
|
205
|
+
icon={Icon}
|
|
206
|
+
pressed={pressed}
|
|
207
|
+
disabled={disabled}
|
|
208
|
+
checked={!!checked}
|
|
209
|
+
/>
|
|
210
|
+
)}
|
|
211
|
+
</Pressable>
|
|
212
|
+
);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const MenuListSwitchItemInner = ({
|
|
216
|
+
label,
|
|
217
|
+
icon: Icon,
|
|
218
|
+
pressed,
|
|
219
|
+
disabled,
|
|
220
|
+
checked,
|
|
221
|
+
}: {
|
|
222
|
+
label: string;
|
|
223
|
+
icon: MenuListSwitchItemProps['icon'];
|
|
224
|
+
pressed: boolean;
|
|
225
|
+
disabled: boolean;
|
|
226
|
+
checked: boolean;
|
|
227
|
+
}) => {
|
|
228
|
+
const styles = useItemStyles({ pressed, disabled, appearance: 'base' });
|
|
229
|
+
|
|
230
|
+
return (
|
|
231
|
+
<Box style={styles.container}>
|
|
232
|
+
{Icon && <Icon size={24} color={resolveContentColor(disabled, 'base')} />}
|
|
233
|
+
<Text style={styles.label} numberOfLines={1}>
|
|
234
|
+
{label}
|
|
235
|
+
</Text>
|
|
236
|
+
<View
|
|
237
|
+
pointerEvents='none'
|
|
238
|
+
accessibilityElementsHidden
|
|
239
|
+
importantForAccessibility='no-hide-descendants'
|
|
240
|
+
>
|
|
241
|
+
<Switch checked={checked} disabled={disabled} />
|
|
242
|
+
</View>
|
|
243
|
+
</Box>
|
|
244
|
+
);
|
|
245
|
+
};
|
|
@@ -20,7 +20,7 @@ export type MenuListProps = {
|
|
|
20
20
|
children: ReactNode;
|
|
21
21
|
} & Omit<StyledViewProps, 'children'>;
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
type MenuListItemBaseProps = {
|
|
24
24
|
/**
|
|
25
25
|
* Text describing the action, truncated to a single line.
|
|
26
26
|
*/
|
|
@@ -29,6 +29,15 @@ export type MenuListItemProps = {
|
|
|
29
29
|
* Icon rendered before the label.
|
|
30
30
|
*/
|
|
31
31
|
icon?: IconComponent;
|
|
32
|
+
/**
|
|
33
|
+
* Prevents interaction and dims the content.
|
|
34
|
+
*
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
disabled?: boolean;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type MenuListItemProps = {
|
|
32
41
|
/**
|
|
33
42
|
* Visual treatment of the label and icon. Use `red` for irreversible or
|
|
34
43
|
* dangerous actions.
|
|
@@ -37,13 +46,26 @@ export type MenuListItemProps = {
|
|
|
37
46
|
*/
|
|
38
47
|
appearance?: MenuListItemAppearance;
|
|
39
48
|
/**
|
|
40
|
-
*
|
|
41
|
-
|
|
49
|
+
* Action to run when the item is pressed.
|
|
50
|
+
*/
|
|
51
|
+
onPress?: StyledPressableProps['onPress'];
|
|
52
|
+
} & MenuListItemBaseProps &
|
|
53
|
+
Omit<StyledPressableProps, 'children' | 'onPress'>;
|
|
54
|
+
|
|
55
|
+
export type MenuListSwitchItemProps = {
|
|
56
|
+
/**
|
|
57
|
+
* The controlled checked state of the switch.
|
|
42
58
|
* @default false
|
|
43
59
|
*/
|
|
44
|
-
|
|
60
|
+
checked?: boolean;
|
|
45
61
|
/**
|
|
46
|
-
*
|
|
62
|
+
* Sets the initial checked state for uncontrolled use.
|
|
63
|
+
* @default false
|
|
47
64
|
*/
|
|
48
|
-
|
|
49
|
-
|
|
65
|
+
defaultChecked?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* The callback function called when the checked state changes.
|
|
68
|
+
*/
|
|
69
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
70
|
+
} & MenuListItemBaseProps &
|
|
71
|
+
Omit<StyledPressableProps, 'children' | 'onPress'>;
|
|
@@ -211,7 +211,7 @@ function useRootStyles({
|
|
|
211
211
|
...(tabLayout === 'fixed'
|
|
212
212
|
? { width: '100%' }
|
|
213
213
|
: { alignSelf: 'flex-start' }),
|
|
214
|
-
borderRadius: t.borderRadius.
|
|
214
|
+
borderRadius: t.borderRadius.sm,
|
|
215
215
|
backgroundColor:
|
|
216
216
|
appearance === 'background' ? t.colors.bg.surface : 'transparent',
|
|
217
217
|
},
|