Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(suite-native): Create generic picker header component #16613

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Button, ButtonProps } from '@suite-native/atoms';
import { Translation } from '@suite-native/intl';

export type PickerCloseButtonProps = Omit<
ButtonProps,
'children' | 'colorScheme' | 'viewLeft' | 'viewRight'
>;

export const PickerCloseButton = (props: PickerCloseButtonProps) => (
<Button colorScheme="tertiaryElevation0" {...props}>
<Translation id="generic.buttons.close" />
</Button>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ReactNode } from 'react';

import { HStack, SearchInput, Text, VStack } from '@suite-native/atoms';

type PickerHeaderSearchInputProps = {
onSearchInputChange: (value: string) => void;
searchInputPlaceholder?: string;
isSearchInputDisabled?: boolean;
maxSearchInputLength?: number;
};

export type PickerHeaderProps = {
title: ReactNode;
children?: ReactNode;
} & (
| { isSearchInputVisible?: false }
| ({ isSearchInputVisible: true } & PickerHeaderSearchInputProps)
);

const PickerHeaderSearchInput = ({
onSearchInputChange,
searchInputPlaceholder,
isSearchInputDisabled,
maxSearchInputLength,
}: PickerHeaderSearchInputProps) => (
<SearchInput
onChange={onSearchInputChange}
maxLength={maxSearchInputLength}
placeholder={searchInputPlaceholder}
isDisabled={isSearchInputDisabled}
/>
);

export const PickerHeader = ({
title,
isSearchInputVisible,
children,
...searchInputProps
}: PickerHeaderProps) => (
<VStack spacing="sp16">
<HStack justifyContent="space-between" alignItems="center">
<Text variant="titleMedium" textAlign="left">
{title}
</Text>
{children}
</HStack>
{isSearchInputVisible && (
<PickerHeaderSearchInput {...(searchInputProps as PickerHeaderSearchInputProps)} />
)}
</VStack>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render, fireEvent } from '@suite-native/test-utils';
import { Text } from '@suite-native/atoms';

import { PickerHeader } from '../PickerHeader';

describe('PickerHeader', () => {
it('should render without children', () => {
const { getByText } = render(<PickerHeader title="Title" />);
expect(getByText('Title')).toBeDefined();
});

it('should render with children', () => {
const { getByText } = render(
<PickerHeader title="Title">
<Text>Child</Text>
</PickerHeader>,
);
expect(getByText('Title')).toBeDefined();
expect(getByText('Child')).toBeDefined();
});

it('should render search input when `isSearchInputVisible`', () => {
const searchInputSpy = jest.fn();
const { getByPlaceholderText } = render(
<PickerHeader
title="Title"
isSearchInputVisible
onSearchInputChange={searchInputSpy}
searchInputPlaceholder="Search placeholder"
/>,
);
const searchInput = getByPlaceholderText('Search placeholder');

fireEvent.changeText(searchInput, 'search');

expect(searchInputSpy).toHaveBeenCalledWith('search');
});
});
Loading