Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(MultiSelector): the default count trigger text comes from the catalog (#9020)

Cindy Zhang6beb2e55aca8
diff --git a/.changeset/multiselector-count-i18n.md b/.changeset/multiselector-count-i18n.md
new file mode 100644
index 0000000000..5fe0dcc7a2
--- /dev/null
+++ b/.changeset/multiselector-count-i18n.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] MultiSelector: the default count text on the trigger (`triggerDisplay="count"`, for example "2 selected") now comes from the message catalog (`@astryx.multiSelector.selectedCount`), so it is translated and can use plural forms under any locale. `formatValue` still replaces it.
+
+@cixzhang
diff --git a/packages/core/locales/en.json b/packages/core/locales/en.json
index 22f0a6065d..6baace85a3 100644
--- a/packages/core/locales/en.json
+++ b/packages/core/locales/en.json
@@ -1643,6 +1643,10 @@
"defaultMessage": "{count, number} of {total, number} selected",
"description": "Polite screen-reader announcement of how many MultiSelector options are selected out of the total, spoken after each toggle. Live-region text, never shown on screen; example: `3 of 12 selected`."
},
+ "@astryx.multiSelector.selectedCount": {
+ "defaultMessage": "{count, plural, one {# selected} other {# selected}}",
+ "description": "Visible text on a closed MultiSelector's trigger when it shows a count of the chosen options (`triggerDisplay=\"count\"`), for example `2 selected`. `{count}` is the number of chosen options; use a plural form if your language needs one."
+ },
"@astryx.multiSelector.createOption": {
"defaultMessage": "Create \"{query}\"",
"description": "Label of the first row in a MultiSelector's search panel when `hasCreate` is set and the typed text matches no option. Picking it reports a creation to the caller. `{query}` is the trimmed text the person typed, shown in quotes."
diff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsx
index 3f27bdf9e4..a0210541be 100644
--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx
@@ -1388,6 +1388,58 @@ describe('MultiSelector', () => {
expect(within(listbox).getByText('Add a fruit first')).toBeInTheDocument();
});
+ describe('count trigger text', () => {
+ it('shows the default count in the catalog message', () => {
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={['Apple', 'Banana']}
+ onChange={() => {}}
+ triggerDisplay="count"
+ />,
+ );
+ expect(screen.getByText('2 selected')).toBeInTheDocument();
+ });
+
+ it('translates the default count, with plural forms, under another locale', () => {
+ render(
+ <InternationalizationProvider
+ locale="fr"
+ overrides={{
+ fr: {
+ '@astryx.multiSelector.selectedCount':
+ '{count, number} {count, plural, one {sélectionné} other {sélectionnés}}',
+ },
+ }}>
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={['Apple', 'Banana']}
+ onChange={() => {}}
+ triggerDisplay="count"
+ />
+ </InternationalizationProvider>,
+ );
+ expect(screen.getByText('2 sélectionnés')).toBeInTheDocument();
+ expect(screen.queryByText('2 selected')).not.toBeInTheDocument();
+ });
+
+ it('still lets formatValue replace the count text', () => {
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={defaultOptions}
+ value={['Apple', 'Banana']}
+ onChange={() => {}}
+ triggerDisplay="count"
+ formatValue={items => `${items.length} fruits`}
+ />,
+ );
+ expect(screen.getByText('2 fruits')).toBeInTheDocument();
+ });
+ });
+
describe('result announcements', () => {
it('announces the match count politely while searching', async () => {
const user = userEvent.setup();
diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsx
index 192d85440e..604ad37fae 100644
--- a/packages/core/src/MultiSelector/MultiSelector.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.tsx
@@ -905,7 +905,7 @@ export interface MultiSelectorProps<
* Not called when nothing is selected — the placeholder shows instead — and
* not called for triggerDisplay 'badges', which renders Badge elements
* rather than text.
- * @default items => `${items.length} selected` for 'count', "A, B, C, +N" for 'labels'
+ * @default the translated `@astryx.multiSelector.selectedCount` message ("2 selected") for 'count', "A, B, C, +N" for 'labels'
*/
formatValue?: (items: MultiSelectorSelectedItem[]) => string;
@@ -1890,7 +1890,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
return (
<span {...stylex.props(styles.triggerText)}>
{formatValue?.(selectedItems) ??
- `${optimisticValue.length} selected`}
+ t('@astryx.multiSelector.selectedCount', {
+ count: optimisticValue.length,
+ })}
</span>
);
@@ -1933,6 +1935,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
placeholder,
formatValue,
maxBadges,
+ t,
]);
// Render search input