Skip to content

Commit 46bf2fc

Browse files
fix: Hide autosuggest results text when not relevant (#3914)
1 parent fa86a71 commit 46bf2fc

File tree

9 files changed

+41
-24
lines changed

9 files changed

+41
-24
lines changed

pages/autosuggest/simple.page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default function AutosuggestPage() {
3131
ariaLabel={'simple autosuggest'}
3232
selectedAriaLabel="Selected"
3333
empty={empty}
34+
finishedText="Finished"
3435
filteringResultsText={matchesCount => `${matchesCount} items`}
3536
/>
3637

src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3298,7 +3298,10 @@ because fixed positioning results in a slight, visible lag when scrolling comple
32983298
"type": "boolean",
32993299
},
33003300
{
3301-
"description": "Specifies the text to display with the number of matches at the bottom of the dropdown menu while filtering.",
3301+
"description": "Specifies the text to display with the number of matches at the bottom of the dropdown menu while filtering.
3302+
3303+
Note that the \`matchesCount\` includes the \`enteredTextLabel\` ("Use \${value}") item, so in most cases you
3304+
should subtract 1 from \`matchesCount\`. If using manual filtering, you should provide your own value for \`totalCount\`.",
33023305
"inlineType": {
33033306
"name": "(matchesCount: number, totalCount: number) => string",
33043307
"parameters": [

src/autosuggest/__tests__/autosuggest-dropdown-states.test.tsx

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ const defaultProps: AutosuggestProps = {
3030
clearAriaLabel: 'clear input',
3131
};
3232

33+
const ControlledAutosuggest = (props: AutosuggestProps) => {
34+
const [value, setValue] = React.useState(props.value);
35+
return <Autosuggest {...props} value={value} onChange={event => setValue(event.detail.value)} />;
36+
};
37+
3338
function renderAutosuggest(props: Partial<AutosuggestProps>) {
34-
const { container } = render(<Autosuggest {...defaultProps} {...props} />);
39+
const { container } = render(<ControlledAutosuggest {...defaultProps} {...props} />);
3540
const wrapper = createWrapper(container).findAutosuggest()!;
3641
return { container, wrapper };
3742
}
@@ -127,8 +132,9 @@ describe('footer types', () => {
127132
});
128133

129134
test('results', async () => {
130-
renderAutosuggest({ value: 'x', filteringResultsText: () => '3 items' });
135+
const { wrapper } = renderAutosuggest({ filteringResultsText: () => '3 items' });
131136
focusInput();
137+
wrapper.setInputValue('x');
132138
expectDropdown();
133139
expectFooterSticky(true);
134140
expectFooterContent('3 items');
@@ -171,9 +177,10 @@ describe('filtering results', () => {
171177
expectFooterContent('No options');
172178
});
173179

174-
test('displays results footer when value is not empty', () => {
175-
renderAutosuggest({ value: ' ', options: [], filteringResultsText: () => '0 items' });
180+
test('displays results footer when value is entered but not filtering', () => {
181+
const { wrapper } = renderAutosuggest({ options: [], filteringResultsText: () => '0 items' });
176182
focusInput();
183+
wrapper.setInputValue(' ');
177184
expectFooterContent('0 items');
178185
});
179186
});
@@ -185,9 +192,16 @@ describe('filtering results', () => {
185192
expectNoFooter();
186193
});
187194

188-
test('displays results footer when value is not empty', () => {
195+
test('displays no footer when value is not empty', () => {
189196
renderAutosuggest({ value: ' ', statusType: 'pending', filteringResultsText: () => '3 items' });
190197
focusInput();
198+
expectNoFooter();
199+
});
200+
201+
test('displays results footer when value is entered', () => {
202+
const { wrapper } = renderAutosuggest({ statusType: 'pending', filteringResultsText: () => '3 items' });
203+
focusInput();
204+
wrapper.setInputValue(' ');
191205
expectFooterContent('3 items');
192206
});
193207
});
@@ -233,15 +247,23 @@ describe('filtering results', () => {
233247
expectFooterContent('finished!');
234248
});
235249

236-
test('displays results footer when finished w/o finished text and value is not empty', () => {
237-
renderAutosuggest({ value: ' ', finishedText: undefined, filteringResultsText: () => '3 items' });
250+
test('displays finished footer when finished w/ finished text and value is present but not filtering', () => {
251+
renderAutosuggest({ value: ' ', filteringResultsText: () => '3 items' });
252+
focusInput();
253+
expectFooterContent('finished!');
254+
});
255+
256+
test('displays results footer when finished w/o finished text and value is entered', () => {
257+
const { wrapper } = renderAutosuggest({ finishedText: undefined, filteringResultsText: () => '3 items' });
238258
focusInput();
259+
wrapper.setInputValue(' ');
239260
expectFooterContent('3 items');
240261
});
241262

242-
test('displays results footer when finished w/ finished text and value is not empty', () => {
243-
renderAutosuggest({ value: ' ', filteringResultsText: () => '3 items' });
263+
test('displays results footer when finished w/ finished text and value is entered', () => {
264+
const { wrapper } = renderAutosuggest({ filteringResultsText: () => '3 items' });
244265
focusInput();
266+
wrapper.setInputValue(' ');
245267
expectFooterContent('3 items');
246268
});
247269
});

src/autosuggest/interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export interface AutosuggestProps
8383

8484
/**
8585
* Specifies the text to display with the number of matches at the bottom of the dropdown menu while filtering.
86+
*
87+
* Note that the `matchesCount` includes the `enteredTextLabel` ("Use ${value}") item, so in most cases you
88+
* should subtract 1 from `matchesCount`. If using manual filtering, you should provide your own value for `totalCount`.
8689
*/
8790
filteringResultsText?: (matchesCount: number, totalCount: number) => string;
8891

src/autosuggest/internal.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,13 @@ const InternalAutosuggest = React.forwardRef((props: InternalAutosuggestProps, r
179179
const highlightedOptionId = autosuggestItemsState.highlightedOption ? highlightedOptionIdSource : undefined;
180180

181181
const isEmpty = !value && !autosuggestItemsState.items.length;
182-
const isFiltered = !!value && value.length !== 0;
182+
const isFiltered = !!value && value.length !== 0 && !(filteringType === 'auto' && autosuggestItemsState.showAll);
183183
const filteredText = isFiltered
184184
? filteringResultsText?.(autosuggestItemsState.items.length, options?.length ?? 0)
185185
: undefined;
186186
const dropdownStatus = useDropdownStatus({
187187
...props,
188188
isEmpty,
189-
isFiltered,
190189
recoveryText,
191190
errorIconAriaLabel,
192191
onRecoveryClick: handleRecoveryClick,

src/internal/components/dropdown-status/__tests__/dropdown-status.test.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ describe('useDropdownStatus', () => {
132132
const { getContent } = renderComponent({
133133
statusType: 'pending',
134134
filteringResultsText: '2 matches',
135-
isFiltered: true,
136135
});
137136
expect(getContent()).toBe('2 matches');
138137
});
@@ -141,7 +140,6 @@ describe('useDropdownStatus', () => {
141140
const { getContent } = renderComponent({
142141
statusType: 'loading',
143142
filteringResultsText: '2 matches',
144-
isFiltered: true,
145143
loadingText: 'Loading',
146144
});
147145
expect(getContent()).toBe('Loading');
@@ -151,7 +149,6 @@ describe('useDropdownStatus', () => {
151149
const { getContent } = renderComponent({
152150
statusType: 'error',
153151
filteringResultsText: '2 matches',
154-
isFiltered: true,
155152
errorText: 'We got a problem',
156153
recoveryText: 'do not worry',
157154
hasRecoveryCallback: true,
@@ -162,8 +159,6 @@ describe('useDropdownStatus', () => {
162159
test('render finished text when finished and not filtered', () => {
163160
const { getContent } = renderComponent({
164161
statusType: 'finished',
165-
filteringResultsText: '10 out of 10 items',
166-
isFiltered: false,
167162
finishedText: 'End of results',
168163
});
169164
expect(getContent()).toBe('End of results');
@@ -173,7 +168,6 @@ describe('useDropdownStatus', () => {
173168
const { getContent } = renderComponent({
174169
statusType: 'finished',
175170
filteringResultsText: '10 out of 10 items',
176-
isFiltered: true,
177171
finishedText: 'End of results',
178172
});
179173
expect(getContent()).toBe('10 out of 10 items');

src/internal/components/dropdown-status/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export { DropdownStatusProps };
1515
export interface DropdownStatusPropsExtended extends DropdownStatusProps {
1616
isEmpty?: boolean;
1717
isNoMatch?: boolean;
18-
isFiltered?: boolean;
1918
noMatch?: React.ReactNode;
2019
filteringResultsText?: string;
2120
/**
@@ -46,7 +45,6 @@ type UseDropdownStatus = ({
4645
recoveryText,
4746
isEmpty,
4847
isNoMatch,
49-
isFiltered,
5048
noMatch,
5149
hasRecoveryCallback,
5250
onRecoveryClick,
@@ -68,7 +66,6 @@ export const useDropdownStatus: UseDropdownStatus = ({
6866
recoveryText,
6967
isEmpty,
7068
isNoMatch,
71-
isFiltered,
7269
noMatch,
7370
onRecoveryClick,
7471
hasRecoveryCallback = false,
@@ -106,7 +103,7 @@ export const useDropdownStatus: UseDropdownStatus = ({
106103
statusResult.content = empty;
107104
} else if (isNoMatch && noMatch) {
108105
statusResult.content = noMatch;
109-
} else if (isFiltered && filteringResultsText) {
106+
} else if (filteringResultsText) {
110107
statusResult.content = filteringResultsText;
111108
} else if (statusType === 'finished' && finishedText) {
112109
statusResult.content = finishedText;

src/multiselect/use-multiselect.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ export function useMultiselect({
232232
isEmpty,
233233
isNoMatch,
234234
noMatch,
235-
isFiltered,
236235
filteringResultsText: filteredText,
237236
onRecoveryClick: handleRecoveryClick,
238237
errorIconAriaLabel: errorIconAriaLabel,

src/select/internal.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ const InternalSelect = React.forwardRef(
195195
isEmpty,
196196
isNoMatch,
197197
noMatch,
198-
isFiltered,
199198
filteringResultsText: filteredText,
200199
errorIconAriaLabel,
201200
onRecoveryClick: handleRecoveryClick,

0 commit comments

Comments
 (0)