Skip to content

Commit

Permalink
feat(Select): Allow customisation of search input and option containe…
Browse files Browse the repository at this point in the history
…r (#1437) (#295)
  • Loading branch information
essj authored Dec 3, 2021
1 parent caabdfc commit aac1b12
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
59 changes: 42 additions & 17 deletions src/components/Select/component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import React, { forwardRef, useCallback, useEffect, useMemo, useState } from 'react';
import { FixedSizeList } from 'react-window';
import { useTheme } from 'styled-components';

Expand All @@ -10,7 +10,13 @@ import { TextInput } from '../TextInput';
import { Context } from './context';
import { DropdownSelect } from './variant/DropdownSelect';
import { ModalSelect } from './variant/ModalSelect';
import { Container, StyledCard, StyledTextInput, OptionsContainer, OptionsTitle } from './styled';
import {
Container,
StyledCard,
StyledTextInput,
OptionContainer as DefaultOptionContainer,
OptionTitle,
} from './styled';
import { useCurrentVariant } from './useCurrentVariant';

export const VARIANTS = ['responsive', 'dropdown', 'modal'] as const;
Expand All @@ -24,12 +30,14 @@ export type Props = Pick<React.HTMLProps<HTMLElement>, 'children'> &
variant?: Variant;
title?: React.ReactNode;
optionTitle?: React.ReactNode;
optionContainerElement?: React.ReactNode;
optionContainerHeight?: number;
optionItemHeight?: number;
search?: React.ComponentProps<typeof TextInput>['value'];
onChangeSearch?: React.ComponentProps<typeof TextInput>['onChange'];
searchIcon?: boolean;
searchPlaceholder?: string;
showSearch?: boolean;
target: React.ReactNode;
open?: boolean;
onClose?: () => void;
Expand All @@ -39,12 +47,14 @@ export const Component = ({
variant = 'responsive',
children,
optionTitle,
optionContainerElement: optionContainerElementProp,
optionContainerHeight,
optionItemHeight = DEFAULT_OPTION_ITEM_HEIGHT,
search: searchProp,
onChangeSearch,
searchIcon = true,
searchPlaceholder,
showSearch,
'data-testid': testId,
...otherProps
}: Props) => {
Expand Down Expand Up @@ -144,10 +154,19 @@ export const Component = ({
[items],
);

const OptionContainer = forwardRef<HTMLDivElement, React.HTMLProps<HTMLDivElement>>(
(props, ref) => {
if (React.isValidElement(optionContainerElementProp)) {
return React.cloneElement(optionContainerElementProp, { ...props, ref });
}
return <DefaultOptionContainer {...props} as="div" ref={ref} />;
},
);

const content = useMemo(() => {
return (
<Container>
{filterable && (
{(filterable || showSearch) && (
<StyledCard position="top">
<StyledTextInput
value={search}
Expand All @@ -166,34 +185,40 @@ export const Component = ({
</StyledCard>
)}
<Space size="small" />
{optionTitle && <OptionsTitle>{optionTitle}</OptionsTitle>}
{optionTitle && <OptionTitle>{optionTitle}</OptionTitle>}
<Space size="small" />
<OptionsContainer data-testid={buildTestId('options')}>
<FixedSizeList
width="100%"
height={Math.min(
optionContainerHeight ?? DEFAULT_OPTION_CONTAINER_HEIGHT,
itemCount * (optionItemHeight ?? DEFAULT_OPTION_ITEM_HEIGHT),
)}
itemCount={itemCount}
itemSize={optionItemHeight}
>
{row}
</FixedSizeList>
</OptionsContainer>
<OptionContainer data-testid={buildTestId('options')}>
{filterable ? (
<FixedSizeList
width="100%"
height={Math.min(
optionContainerHeight ?? DEFAULT_OPTION_CONTAINER_HEIGHT,
itemCount * (optionItemHeight ?? DEFAULT_OPTION_ITEM_HEIGHT),
)}
itemCount={itemCount}
itemSize={optionItemHeight}
>
{row}
</FixedSizeList>
) : (
items
)}
</OptionContainer>
</Container>
);
}, [
buildTestId,
filterable,
itemCount,
items,
optionContainerHeight,
optionItemHeight,
optionTitle,
row,
search,
searchIcon,
searchPlaceholder,
showSearch,
theme.honeycomb.size.increased,
theme.honeycomb.color.text.placeholder,
updateSearch,
Expand Down
4 changes: 2 additions & 2 deletions src/components/Select/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export const StyledTextInput = styled(TextInput)`
}
`;

export const OptionsTitle = styled.div`
export const OptionTitle = styled.div`
font-size: ${({ theme }) => em(theme.honeycomb.size.small)};
color: ${({ theme }) => theme.honeycomb.color.text.masked};
padding: 0 ${({ theme }) => em(theme.honeycomb.size.small, theme.honeycomb.size.small)};
`;

export const OptionsContainer = styled.div`
export const OptionContainer = styled.div`
background: ${({ theme }) => theme.honeycomb.color.bg.normal};
border-top-left-radius: ${({ theme }) => em(theme.honeycomb.radius.increased)};
border-top-right-radius: ${({ theme }) => em(theme.honeycomb.radius.increased)};
Expand Down

0 comments on commit aac1b12

Please sign in to comment.