Skip to content
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
76 changes: 42 additions & 34 deletions src/components/experimental/Select/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { ReactElement, useState } from 'react';
import React from 'react';
import {
Select as BaseSelect,
SelectProps as BaseSelectProps,
SelectValue,
SelectStateContext,
FieldError
FieldError,
SelectValueRenderProps
} from 'react-aria-components';
import { useIsSSR } from 'react-aria';
import { useResizeObserver } from '@react-aria/utils';
Expand Down Expand Up @@ -41,42 +42,47 @@ const FakeButton = styled(FakeInput)`
}
`;

interface SelectFieldProps extends Pick<FieldProps, 'label' | 'description' | 'errorMessage' | 'leadingIcon'> {
interface SelectFieldProps<T> extends Pick<FieldProps, 'label' | 'description' | 'errorMessage' | 'leadingIcon'> {
placeholder?: string;
renderValue?: (props: SelectValueRenderProps<T> & { defaultChildren: React.ReactNode }) => React.ReactNode;
}

const SelectTrigger = React.forwardRef<HTMLDivElement, SelectFieldProps>(
({ label, leadingIcon, placeholder }, forwardedRef) => {
const state = React.useContext(SelectStateContext);
const buttonRef = React.useRef<HTMLButtonElement>(null);
// eslint-disable-next-line @typescript-eslint/ban-types
function SelectTriggerWithRef<T extends object>(
{ label, leadingIcon, placeholder, renderValue }: SelectFieldProps<T>,
forwardedRef: React.ForwardedRef<HTMLDivElement>
) {
const state = React.useContext(SelectStateContext);
const buttonRef = React.useRef<HTMLButtonElement>(null);

return (
<FakeButton
$isVisuallyFocused={state?.isOpen}
ref={forwardedRef}
onClick={() => buttonRef.current?.click()}
>
{leadingIcon}
<InnerWrapper>
<Label $flying={Boolean(placeholder || state?.selectedItem)}>{label}</Label>
<Button ref={buttonRef}>
<SelectValue>
{({ defaultChildren, isPlaceholder }) =>
isPlaceholder
? placeholder || <VisuallyHidden>{defaultChildren}</VisuallyHidden>
: defaultChildren
}
</SelectValue>
</Button>
</InnerWrapper>
{state?.isOpen ? <DropupSelectIcon /> : <DropdownSelectIcon />}
</FakeButton>
);
}
);
return (
<FakeButton $isVisuallyFocused={state?.isOpen} ref={forwardedRef} onClick={() => buttonRef.current?.click()}>
{leadingIcon}
<InnerWrapper>
<Label $flying={Boolean(placeholder || state?.selectedItem)}>{label}</Label>
<Button ref={buttonRef}>
<SelectValue<T>>
{selectValueRenderProps =>
renderValue
? renderValue(selectValueRenderProps)
: (function defaultRenderValue({ isPlaceholder, defaultChildren }) {
return isPlaceholder
? placeholder || <VisuallyHidden>{defaultChildren}</VisuallyHidden>
: defaultChildren;
})(selectValueRenderProps)
}
</SelectValue>
</Button>
</InnerWrapper>
{state?.isOpen ? <DropupSelectIcon /> : <DropdownSelectIcon />}
</FakeButton>
);
}

const SelectTrigger = React.forwardRef(SelectTriggerWithRef);

interface SelectProps<T extends Record<string, unknown>>
extends SelectFieldProps,
extends SelectFieldProps<T>,
Omit<BaseSelectProps<T>, 'children'> {
items?: Iterable<T>;
children: React.ReactNode | ((item: T) => React.ReactNode);
Expand All @@ -89,9 +95,10 @@ function Select<T extends Record<string, unknown>>({
errorMessage,
description,
placeholder,
renderValue,
...props
}: SelectProps<T>): ReactElement {
const [menuWidth, setMenuWidth] = useState<string | null>(null);
}: SelectProps<T>): React.ReactElement {
const [menuWidth, setMenuWidth] = React.useState<string | null>(null);
const triggerRef = React.useRef<HTMLDivElement>(null);
const isSSR = useIsSSR();

Expand All @@ -117,6 +124,7 @@ function Select<T extends Record<string, unknown>>({
label={label}
leadingIcon={leadingIcon}
placeholder={placeholder}
renderValue={renderValue}
/>
<Footer>{isInvalid ? <FieldError>{errorMessage}</FieldError> : description}</Footer>
</Wrapper>
Expand Down
6 changes: 6 additions & 0 deletions src/components/experimental/Select/docs/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,9 @@ export const Invalid: Story = {
errorMessage: 'Error'
}
};

export const WithCustomValueRenderer: Story = {
args: {
renderValue: ({ selectedText, isPlaceholder }) => (isPlaceholder ? '' : `${selectedText} ♥`)
}
};