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
58 changes: 58 additions & 0 deletions code/core/src/components/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1479,3 +1479,61 @@ export const ResetWithUndefinedOption = meta.story({
});
},
});

export const ShowSelectedOptionTitleTrue = meta.story({
name: 'Show Selected Option Title (prop=true)',
args: {
showSelectedOptionTitle: true,
defaultOptions: 'frog',
},
play: async ({ canvas, step }) => {
await step('Verify selected option title is shown', async () => {
const selectButton = await canvas.findByRole('button');
expect(selectButton).toHaveTextContent('Frog');
});
},
});

export const ShowSelectedOptionTitleFalse = meta.story({
name: 'Show Selected Option Title (prop=false)',
args: {
showSelectedOptionTitle: false,
defaultOptions: 'frog',
},
play: async ({ canvas, step }) => {
await step('Verify default title is shown instead of selected option', async () => {
const selectButton = await canvas.findByRole('button');
expect(selectButton).toHaveTextContent('Animal');
});
},
});

export const ShowSelectedOptionTitleFalseMulti = meta.story({
name: 'Show Selected Option Title (prop=false, multi)',
args: {
showSelectedOptionTitle: false,
multiSelect: true,
defaultOptions: ['frog', 'tadpole'],
},
play: async ({ canvas, step }) => {
await step('Verify default title is shown for multi-select', async () => {
const selectButton = await canvas.findByRole('button');
expect(selectButton).toHaveTextContent('Animal');
});
},
Comment thread
Sidnioulz marked this conversation as resolved.
});

export const ShowSelectedOptionTitleTrueMulti = meta.story({
name: 'Show Selected Option Title (prop=true, multi)',
args: {
showSelectedOptionTitle: true,
multiSelect: true,
defaultOptions: ['frog'],
},
play: async ({ canvas, step }) => {
await step('Verify option count is shown for multi-select', async () => {
const selectButton = await canvas.findByRole('button');
expect(selectButton).toHaveTextContent('1');
});
},
});
10 changes: 9 additions & 1 deletion code/core/src/components/components/Select/Select.tsx
Comment thread
Sidnioulz marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ export interface SelectProps extends Omit<
onSelect?: (option: Value) => void;
onDeselect?: (option: Value) => void;
onChange?: (selected: Value[]) => void;
/**
* Legacy option for ToolbarMenuSelect. Do not use in new code. Controls whether to show the
* selected option's title.
*
* @default true
*/
showSelectedOptionTitle?: boolean;
}

function valueToId(parentId: string, { value }: InternalOption | ResetOption): string {
Expand Down Expand Up @@ -208,6 +215,7 @@ export const Select = forwardRef<HTMLButtonElement, SelectProps>(
onChange,
tooltip,
ariaLabel,
showSelectedOptionTitle = true,
...props
},
ref
Expand Down Expand Up @@ -522,7 +530,7 @@ export const Select = forwardRef<HTMLButtonElement, SelectProps>(
{!multiSelect && (
<>
{icon}
{selectedOptions[0]?.title ?? children}
{(showSelectedOptionTitle && selectedOptions[0]?.title) || children}
</>
)}

Expand Down
10 changes: 9 additions & 1 deletion code/core/src/toolbar/components/ToolbarMenuSelect.tsx
Comment thread
Sidnioulz marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ export const ToolbarMenuSelect: FC<ToolbarMenuProps> = ({
id,
name,
description,
toolbar: { icon: _icon, items, title: _title, preventDynamicIcon, dynamicTitle, shortcuts },
toolbar: {
icon: _icon,
items,
title: _title,
preventDynamicIcon,
dynamicTitle = true,
shortcuts,
},
}) => {
const api = useStorybookApi();
const [globals, updateGlobals, storyGlobals] = useGlobals();
Expand Down Expand Up @@ -132,6 +139,7 @@ export const ToolbarMenuSelect: FC<ToolbarMenuProps> = ({
onReset={resetItem ? () => updateGlobals({ [id]: resetItem?.value }) : undefined}
onSelect={(selected) => updateGlobals({ [id]: selected })}
icon={icon && <Icons icon={icon} __suppressDeprecationWarning={true} />}
showSelectedOptionTitle={dynamicTitle}
>
{title}
</Select>
Expand Down
8 changes: 4 additions & 4 deletions docs/_snippets/storybook-preview-configure-globaltypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const preview = {
icon: 'circlehollow',
// Array of plain string values or MenuItem shape (see below)
items: ['light', 'dark'],
// Change title based on selected value
// Change title based on selected value (recommended for consistency with the Storybook UI)
dynamicTitle: true,
},
},
Expand All @@ -36,7 +36,7 @@ const preview: Preview = {
icon: 'circlehollow',
// Array of plain string values or MenuItem shape (see below)
items: ['light', 'dark'],
// Change title based on selected value
// Change title based on selected value (recommended for consistency with the Storybook UI)
dynamicTitle: true,
},
},
Expand All @@ -63,7 +63,7 @@ export default definePreview({
icon: 'circlehollow',
// Array of plain string values or MenuItem shape (see below)
items: ['light', 'dark'],
// Change title based on selected value
// Change title based on selected value (recommended for consistency with the Storybook UI)
dynamicTitle: true,
},
},
Expand All @@ -90,7 +90,7 @@ export default definePreview({
icon: 'circlehollow',
// Array of plain string values or MenuItem shape (see below)
items: ['light', 'dark'],
// Change title based on selected value
// Change title based on selected value (recommended for consistency with the Storybook UI)
dynamicTitle: true,
},
},
Expand Down