diff --git a/src/components/ToggleButton/ToggleButton.stories.ts b/src/components/ToggleButton/ToggleButton.stories.ts index c3192cf5..d7598359 100644 --- a/src/components/ToggleButton/ToggleButton.stories.ts +++ b/src/components/ToggleButton/ToggleButton.stories.ts @@ -3,7 +3,9 @@ import { IconArrowDownRight, IconUser } from '@tabler/icons-react-native' import { ToggleButton } from './ToggleButton' -const icons = { IconArrowDownRight, IconUser, none: undefined } +const svgIcons = { IconArrowDownRight, IconUser, none: undefined } + +const imageIcons = { hotels: require('./hotels.png'), none: undefined } const meta: Meta = { title: 'Form/ToggleButton', @@ -16,6 +18,7 @@ const meta: Meta = { label: 'ButtonToggle', size: 'base', Icon: IconArrowDownRight, + Image: undefined, }, argTypes: { iconPos: { @@ -24,7 +27,16 @@ const meta: Meta = { mapping: { null: null }, }, size: { control: 'radio', options: ['xlarge', 'large', 'base', 'small'] }, - Icon: { control: 'radio', options: Object.keys(icons), mapping: icons }, + Icon: { + control: 'radio', + options: Object.keys(svgIcons), + mapping: svgIcons, + }, + Image: { + control: 'radio', + options: Object.keys(imageIcons), + mapping: imageIcons, + }, onPress: { action: 'onPress' }, }, } diff --git a/src/components/ToggleButton/ToggleButton.tsx b/src/components/ToggleButton/ToggleButton.tsx index b8e0e140..5e5589d9 100644 --- a/src/components/ToggleButton/ToggleButton.tsx +++ b/src/components/ToggleButton/ToggleButton.tsx @@ -1,6 +1,8 @@ import { memo, useCallback, useMemo, useState } from 'react' import { type AccessibilityProps, + Image, + type ImageSourcePropType, Pressable, type StyleProp, Text, @@ -43,8 +45,10 @@ export interface ToggleButtonProps size?: 'xlarge' | 'large' | 'base' | 'small' /** Дополнительная стилизация для контейнера компонента */ style?: StyleProp - /** SVG-иконка */ + /** SVG-иконка. Если передана, то изображение не будет отображаться */ Icon?: SvgSource + /** Изображение. Будет отображаться, только если не передана иконка Icon */ + Image?: ImageSourcePropType } /** @@ -62,6 +66,7 @@ export const ToggleButton = memo( size = 'base', style, Icon, + Image: imageSource, testID, ...rest }) => { @@ -80,6 +85,13 @@ export const ToggleButton = memo( source={Icon} testID={ToggleButtonTestId.icon} /> + ) : imageSource ? ( + ) : null const onPressIn = useCallback(() => setPressed(true), []) diff --git a/src/components/ToggleButton/__tests__/ToggleButton.test.tsx b/src/components/ToggleButton/__tests__/ToggleButton.test.tsx index 60f1df2a..a218e61a 100644 --- a/src/components/ToggleButton/__tests__/ToggleButton.test.tsx +++ b/src/components/ToggleButton/__tests__/ToggleButton.test.tsx @@ -70,6 +70,16 @@ describe('ToggleButton', () => { iconPos: 'right', }, ], + [ + 'checked = false, disabled = false, iconOnly = true, size = base, with PNG Icon', + { + checked: false, + disabled: false, + iconOnly: true, + size: 'base', + Image: require('../hotels.png'), + }, + ], ] beforeAll(() => { @@ -90,6 +100,84 @@ describe('ToggleButton', () => { }) }) + test('should render PNG Icon as Image', () => { + const { getByTestId } = render( + + ) + const icon = getByTestId(ToggleButtonTestId.icon) + + expect(icon.type).toBe('Image') + }) + + test('should render SVG Icon as Svg (uri)', async () => { + const originalFetch = global.fetch + jest + .spyOn(global, 'fetch') + .mockImplementation() + .mockResolvedValue({ + ok: true, + status: 200, + text: async () => + '', + } as Response) + + try { + const { findByTestId } = render( + + ) + const icon = await findByTestId(ToggleButtonTestId.icon) + + expect(icon.type).toBe('RNSVGSvgView') + } finally { + global.fetch = originalFetch + } + }) + + test('should render SVG Icon as Svg (xml)', () => { + const { getByTestId } = render( + ', + }} + /> + ) + const icon = getByTestId(ToggleButtonTestId.icon) + + expect(icon.type).toBe('RNSVGSvgView') + }) + + test('should render non-svg uri Icon as Image', () => { + const { getByTestId } = render( + + ) + const icon = getByTestId(ToggleButtonTestId.icon) + + expect(icon.type).toBe('Image') + }) + + test('should render numeric Icon as Image', () => { + const { getByTestId } = render( + + ) + const icon = getByTestId(ToggleButtonTestId.icon) + + expect(icon.type).toBe('Image') + }) + test('should handle press', async () => { const mockedOnPress = jest.fn() const { queryByTestId } = render() diff --git a/src/components/ToggleButton/__tests__/__snapshots__/ToggleButton.test.tsx.snap b/src/components/ToggleButton/__tests__/__snapshots__/ToggleButton.test.tsx.snap index 213d712b..d99139b2 100644 --- a/src/components/ToggleButton/__tests__/__snapshots__/ToggleButton.test.tsx.snap +++ b/src/components/ToggleButton/__tests__/__snapshots__/ToggleButton.test.tsx.snap @@ -472,6 +472,70 @@ exports[`ToggleButton snapshots checked = false, disabled = false, iconOnly = tr `; +exports[`ToggleButton snapshots checked = false, disabled = false, iconOnly = true, size = base, with PNG Icon 1`] = ` + + + + + +`; + exports[`ToggleButton snapshots checked = false, disabled = true, iconOnly = false, size = large, with Icon, with label 1`] = `