Skip to content

Commit 45daa5d

Browse files
authored
fix(FilterPicker): full items prop support (#737)
1 parent 8f6e06b commit 45daa5d

File tree

6 files changed

+386
-137
lines changed

6 files changed

+386
-137
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cube-dev/ui-kit": patch
3+
---
4+
5+
Full items prop support in FilterPicker.

src/components/actions/Button/Button.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const DEFAULT_BUTTON_STYLES = {
124124
},
125125

126126
ButtonIcon: {
127-
width: 'max-content',
127+
width: 'min 1fs',
128128
},
129129

130130
'& [data-element="ButtonIcon"]:first-child:not(:last-child)': {
@@ -722,8 +722,7 @@ export const Button = forwardRef(function Button(
722722
<LoadingIcon data-element="ButtonIcon" />
723723
)
724724
) : null}
725-
{((hasIcons && children) || (!!icon && !!rightIcon)) &&
726-
typeof children === 'string' ? (
725+
{hasIcons && typeof children === 'string' ? (
727726
<Text ellipsis>{children}</Text>
728727
) : (
729728
children

src/components/fields/FilterListBox/FilterListBox.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,12 +1187,12 @@ EscapeKeyHandling.parameters = {
11871187
};
11881188

11891189
export const VirtualizedList: StoryFn<CubeFilterListBoxProps<any>> = (args) => {
1190-
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
1190+
const [selectedKeys, setSelectedKeys] = useState<string[]>(['item-2']);
11911191

11921192
// Generate a large list of items with varying content to test virtualization
11931193
// Mix items with and without descriptions to test dynamic sizing
11941194
const items = Array.from({ length: 100 }, (_, i) => ({
1195-
id: `item-${i}`,
1195+
id: `item-${i + 1}`,
11961196
name: `Item ${i + 1}${i % 7 === 0 ? ' - This is a longer item name to test dynamic sizing' : ''}`,
11971197
description:
11981198
i % 3 === 0

src/components/fields/FilterPicker/FilterPicker.stories.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,12 +1526,12 @@ export const VirtualizedList: Story = {
15261526
await userEvent.click(trigger);
15271527
},
15281528
render: (args) => {
1529-
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
1529+
const [selectedKeys, setSelectedKeys] = useState<string[]>(['item-2']);
15301530

15311531
// Generate a large list of items with varying content to trigger virtualization
15321532
// Mix items with and without descriptions to test dynamic sizing
15331533
const items = Array.from({ length: 100 }, (_, i) => ({
1534-
id: `item-${i}`,
1534+
id: `item-${i + 1}`,
15351535
name: `Item ${i + 1}${i % 7 === 0 ? ' - This is a longer item name to test dynamic sizing' : ''}`,
15361536
description:
15371537
i % 3 === 0
@@ -1584,15 +1584,15 @@ export const VirtualizedList: Story = {
15841584

15851585
export const WithSelectAll: Story = {
15861586
render: (args) => (
1587-
<FilterPicker {...args}>
1588-
{permissions.map((permission) => (
1587+
<FilterPicker items={permissions} {...args}>
1588+
{(permission: any) => (
15891589
<FilterPicker.Item
15901590
key={permission.key}
15911591
description={permission.description}
15921592
>
15931593
{permission.label}
15941594
</FilterPicker.Item>
1595-
))}
1595+
)}
15961596
</FilterPicker>
15971597
),
15981598
args: {
@@ -1603,8 +1603,7 @@ export const WithSelectAll: Story = {
16031603
showSelectAll: true,
16041604
selectAllLabel: 'All Permissions',
16051605
defaultSelectedKeys: ['read'],
1606-
type: 'outline',
1607-
size: 'medium',
1606+
width: '30x',
16081607
},
16091608
parameters: {
16101609
docs: {

src/components/fields/FilterPicker/FilterPicker.test.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,4 +843,75 @@ describe('<FilterPicker />', () => {
843843
expect(options[1]).toHaveTextContent('Apple');
844844
});
845845
});
846+
847+
describe('Items prop functionality', () => {
848+
const itemsWithLabels = [
849+
{ key: 'apple', label: 'Red Apple' },
850+
{ key: 'banana', label: 'Yellow Banana' },
851+
{ key: 'cherry', label: 'Sweet Cherry' },
852+
];
853+
854+
it('should display labels correctly when using items prop with label property', async () => {
855+
const { getByRole } = renderWithRoot(
856+
<FilterPicker
857+
label="Select fruits"
858+
placeholder="Choose fruits..."
859+
selectionMode="single"
860+
items={itemsWithLabels}
861+
selectedKey="apple"
862+
>
863+
{(item) => (
864+
<FilterPicker.Item key={item.key}>{item.label}</FilterPicker.Item>
865+
)}
866+
</FilterPicker>,
867+
);
868+
869+
// Wait for component to render
870+
await act(async () => {
871+
await new Promise((resolve) => setTimeout(resolve, 50));
872+
});
873+
874+
const trigger = getByRole('button');
875+
876+
// Should display the label, not the key
877+
expect(trigger).toHaveTextContent('Red Apple');
878+
expect(trigger).not.toHaveTextContent('apple');
879+
});
880+
881+
it('should display correct labels in multiple selection mode with items prop', async () => {
882+
const renderSummary = jest.fn(
883+
({ selectedLabels }) => `Selected: ${selectedLabels.join(', ')}`,
884+
);
885+
886+
const { getByRole } = renderWithRoot(
887+
<FilterPicker
888+
label="Select fruits"
889+
placeholder="Choose fruits..."
890+
selectionMode="multiple"
891+
items={itemsWithLabels}
892+
selectedKeys={['apple', 'cherry']}
893+
renderSummary={renderSummary}
894+
>
895+
{(item) => (
896+
<FilterPicker.Item key={item.key}>{item.label}</FilterPicker.Item>
897+
)}
898+
</FilterPicker>,
899+
);
900+
901+
// Wait for component to render
902+
await act(async () => {
903+
await new Promise((resolve) => setTimeout(resolve, 50));
904+
});
905+
906+
// Check that renderSummary was called with correct labels
907+
expect(renderSummary).toHaveBeenCalledWith({
908+
selectedLabels: ['Red Apple', 'Sweet Cherry'],
909+
selectedKeys: ['apple', 'cherry'],
910+
selectionMode: 'multiple',
911+
});
912+
913+
const trigger = getByRole('button');
914+
expect(trigger).toHaveTextContent('Selected: Red Apple, Sweet Cherry');
915+
});
916+
});
846917
});

0 commit comments

Comments
 (0)