From a93ed00ee5d4d32d866a5b028f454b88d19cbf02 Mon Sep 17 00:00:00 2001 From: Christian Andolf Date: Thu, 10 Apr 2025 14:37:52 +0200 Subject: [PATCH] fix(SW-1509): removed support for plain array items in order to handle proper props extending various fixes for supporting default selected --- .../lib/components/Select/Select.stories.tsx | 42 ++++++----- .../lib/components/Select/Select.tsx | 73 ++++++++----------- .../lib/components/Select/SelectFilter.tsx | 44 +++++------ .../lib/components/Select/select.module.css | 2 +- .../lib/components/Select/types.ts | 17 +++-- 5 files changed, 83 insertions(+), 95 deletions(-) diff --git a/packages/design-system/lib/components/Select/Select.stories.tsx b/packages/design-system/lib/components/Select/Select.stories.tsx index 5f42f9dcc..6ac4240e9 100644 --- a/packages/design-system/lib/components/Select/Select.stories.tsx +++ b/packages/design-system/lib/components/Select/Select.stories.tsx @@ -14,9 +14,15 @@ export default meta type Story = StoryObj +const items = [ + { label: 'Foo', value: 'foo' }, + { label: 'Bar', value: 'bar' }, + { label: 'Baz', value: 'baz' }, +] + export const Default: Story = { args: { - items: ['Foo', 'Bar', 'Baz'], + items, label: 'Select an item', name: 'foo', }, @@ -24,40 +30,38 @@ export const Default: Story = { export const DefaultSelected: Story = { args: { - items: ['Foo', 'Bar', 'Baz'], - label: 'Select an item', - name: 'foo', - defaultSelectedKey: 'Foo', - }, -} - -export const ObjectItem: Story = { - args: { - items: [ - { label: 'Foo', value: 'foo' }, - { label: 'Bar', value: 'bar' }, - { label: 'Baz', value: 'baz' }, - ], + items, label: 'Select an item', name: 'foo', + defaultSelectedKey: 'foo', }, } export const Icons: Story = { args: { - icon: 'star', - itemIcon: 'check', - items: ['Foo', 'Bar', 'Baz'], + items, label: 'Select an item', name: 'foo', + icon: 'star', + itemIcon: 'check', }, } export const Filtering: Story = { args: { - items: ['Foo', 'Bar', 'Baz'], + items, label: 'Select an item', name: 'foo', enableFiltering: true, }, } + +export const FilteringSelected: Story = { + args: { + items, + label: 'Select an item', + name: 'foo', + enableFiltering: true, + defaultSelectedKey: 'foo', + }, +} diff --git a/packages/design-system/lib/components/Select/Select.tsx b/packages/design-system/lib/components/Select/Select.tsx index dd918c7c8..0682939b2 100644 --- a/packages/design-system/lib/components/Select/Select.tsx +++ b/packages/design-system/lib/components/Select/Select.tsx @@ -12,7 +12,7 @@ import { Typography } from '../Typography' import { SelectItem } from './SelectItem' import { SelectFilter } from './SelectFilter' -import type { SelectProps } from './types' +import type { SelectProps, SelectFilterProps } from './types' import styles from './select.module.css' @@ -23,10 +23,9 @@ export function Select({ isDisabled, icon, itemIcon, - enableFiltering, ...props -}: SelectProps) { - if (enableFiltering) { +}: SelectProps | SelectFilterProps) { + if ('enableFiltering' in props) { return ( ) @@ -59,22 +57,26 @@ export function Select({ /> ) : null} - {({ isPlaceholder, selectedText }) => ( - <> - - {label} - - {selectedText ? ( - - {selectedText} + {({ selectedText }) => { + return ( + <> + + {label} - ) : null} - - )} + {selectedText ? ( + + {selectedText} + + ) : null} + + ) + }} - {items.map((item) => - typeof item === 'object' ? ( - - {item.label} - - ) : ( - - {item.toString()} - - ) - )} + {items.map((item) => ( + + {item.label} + + ))} diff --git a/packages/design-system/lib/components/Select/SelectFilter.tsx b/packages/design-system/lib/components/Select/SelectFilter.tsx index 469ea4abb..e6541e530 100644 --- a/packages/design-system/lib/components/Select/SelectFilter.tsx +++ b/packages/design-system/lib/components/Select/SelectFilter.tsx @@ -6,7 +6,6 @@ import { ListBox, Popover, } from 'react-aria-components' -import { cx } from 'class-variance-authority' import { useState } from 'react' import { MaterialIcon } from '../Icons/MaterialIcon' @@ -21,13 +20,14 @@ export function SelectFilter({ name, label, items, - isRequired, isDisabled, icon, itemIcon, + defaultSelectedKey, + ...props }: SelectFilterProps) { const [focus, setFocus] = useState(false) - const [value, setValue] = useState(null) + const [value, setValue] = useState(defaultSelectedKey ?? null) const iconColor = isDisabled ? 'Icon/Interactive/Disabled' : 'Icon/Default' return ( @@ -35,11 +35,12 @@ export function SelectFilter({ className={styles.select} name={name} aria-label={label} - isRequired={isRequired} isDisabled={isDisabled} - onSelectionChange={(val) => setValue(val)} + onSelectionChange={setValue} onFocus={() => setFocus(true)} onBlur={() => setFocus(false)} + defaultSelectedKey={defaultSelectedKey} + {...props} >