Skip to content

Commit 1ccf77d

Browse files
authored
feat: add hideLabel property on experimental Select (#551)
1 parent 3426ac7 commit 1ccf77d

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/components/experimental/Field/Label.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const flyingLabelStyles = css`
1111
line-height: var(--wave-exp-typescale-label-2-line-height);
1212
`;
1313

14-
export const Label = styled(BaseLabel)<{ $flying: boolean }>`
14+
export const Label = styled(BaseLabel)<{ $flying?: boolean }>`
1515
position: absolute;
1616
top: 50%;
1717
color: currentColor;

src/components/experimental/Select/Select.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ const FakeButton = styled(FakeInput)`
4040

4141
interface SelectFieldProps<T> extends Pick<FieldProps, 'label' | 'description' | 'errorMessage' | 'leadingIcon'> {
4242
label: string;
43+
hideLabel?: boolean;
4344
placeholder?: string;
4445
renderValue?: (props: SelectValueRenderProps<T> & { defaultChildren: React.ReactNode }) => React.ReactNode;
4546
}
4647

4748
// eslint-disable-next-line @typescript-eslint/ban-types
4849
function SelectTriggerWithRef<T extends object>(
49-
{ label, leadingIcon, placeholder, renderValue }: SelectFieldProps<T>,
50+
{ label, hideLabel, leadingIcon, placeholder, renderValue }: SelectFieldProps<T>,
5051
forwardedRef: React.ForwardedRef<HTMLDivElement>
5152
) {
5253
const state = React.useContext(SelectStateContext);
@@ -56,7 +57,13 @@ function SelectTriggerWithRef<T extends object>(
5657
<FakeButton $isVisuallyFocused={state?.isOpen} ref={forwardedRef} onClick={() => buttonRef.current?.click()}>
5758
{leadingIcon}
5859
<InnerWrapper>
59-
<Label $flying={Boolean(placeholder || state?.selectedItem)}>{label}</Label>
60+
{hideLabel ? (
61+
<VisuallyHidden>
62+
<Label>{label}</Label>
63+
</VisuallyHidden>
64+
) : (
65+
<Label $flying={Boolean(placeholder || state?.selectedItem)}>{label}</Label>
66+
)}
6067
<Button ref={buttonRef}>
6168
<SelectValue<T>>
6269
{selectValueRenderProps =>
@@ -83,6 +90,7 @@ interface SelectProps<T extends Record<string, unknown>>
8390
Omit<BaseSelectProps<T>, 'children'> {
8491
items?: Iterable<T>;
8592
children: React.ReactNode | ((item: T) => React.ReactNode);
93+
hideLabel?: boolean;
8694
}
8795

8896
function Select<T extends Record<string, unknown>>({
@@ -93,6 +101,7 @@ function Select<T extends Record<string, unknown>>({
93101
description,
94102
placeholder,
95103
renderValue,
104+
hideLabel,
96105
...props
97106
}: SelectProps<T>): React.ReactElement {
98107
const [menuWidth, setMenuWidth] = React.useState<string | null>(null);
@@ -119,16 +128,14 @@ function Select<T extends Record<string, unknown>>({
119128
<SelectTrigger
120129
ref={isSSR ? null : triggerRef}
121130
label={label}
131+
hideLabel={hideLabel}
122132
leadingIcon={leadingIcon}
123133
placeholder={placeholder}
124134
renderValue={renderValue}
125135
/>
126136
<Footer>{isInvalid ? <FieldError>{errorMessage}</FieldError> : description}</Footer>
127137
</Wrapper>
128-
<Popover
129-
triggerRef={triggerRef}
130-
style={{ '--trigger-width': menuWidth } as React.CSSProperties}
131-
>
138+
<Popover triggerRef={triggerRef} style={{ '--trigger-width': menuWidth } as React.CSSProperties}>
132139
<ListBox items={props.items}>{children}</ListBox>
133140
</Popover>
134141
</>

src/components/experimental/Select/docs/Select.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ type Story = StoryObj<typeof Select>;
4040

4141
export const Default: Story = {};
4242

43+
export const WithHideLabel: Story = {
44+
args: {
45+
hideLabel: true
46+
}
47+
};
48+
4349
export const WithIcon: Story = {
4450
args: {
4551
leadingIcon: <DogIcon />

0 commit comments

Comments
 (0)