Skip to content

Commit beb224e

Browse files
committed
fix combobox placeholder warning
1 parent 4fc6630 commit beb224e

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

packages/@react-spectrum/s2/src/placeholder-utils.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@ export function usePlaceholderWarning(placeholder: string | undefined, component
2323
});
2424

2525
useEffect(() => {
26-
checkPlaceholder(inputRef.current);
27-
}, [checkPlaceholder, inputRef]);
26+
let timer;
27+
if (componentType === 'ComboBox') {
28+
timer = setTimeout(() => {
29+
checkPlaceholder(inputRef.current);
30+
}, 50);
31+
} else {
32+
checkPlaceholder(inputRef.current);
33+
}
34+
35+
return () => clearTimeout(timer);
36+
}, [checkPlaceholder, inputRef, componentType]);
2837

2938
useEvent(inputRef, 'blur', (e) => checkPlaceholder(e.target as HTMLInputElement));
3039
}

packages/@react-spectrum/s2/stories/ComboBox.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const meta: Meta<typeof ComboBox<any>> = {
3636
placeholder: {control: {type: 'text'}}
3737
},
3838
args: {
39-
// placeholder: 'Select a value'
39+
placeholder: 'Select a value'
4040
},
4141
title: 'ComboBox'
4242
};
@@ -46,7 +46,7 @@ type Story = StoryObj<typeof ComboBox<any>>;
4646

4747
export const Example: Story = {
4848
render: (args: ComboBoxProps<any>) => (
49-
<ComboBox selectedKey="1">
49+
<ComboBox {...args}>
5050
<ComboBoxItem id="1">Chocolate</ComboBoxItem>
5151
<ComboBoxItem>Mint</ComboBoxItem>
5252
<ComboBoxItem>Strawberry</ComboBoxItem>

packages/@react-spectrum/s2/test/Combobox.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ describe('Combobox', () => {
218218
let {rerender} = render(
219219
<DynamicCombobox items={items} />
220220
);
221+
// Since Combobox takes multiple renders to populate its input value if a selected key is provided
222+
// we have a delay for the placeholder warning
223+
act(() => {jest.runAllTimers();});
221224

222225
expect(warnSpy).toHaveBeenCalledWith('Your ComboBox is empty and not focused but doesn\'t have a placeholder. Please add one.');
223226
warnSpy.mockClear();

0 commit comments

Comments
 (0)