Skip to content

Commit 566f2c3

Browse files
authored
[select] Block opening the popup when provided readOnly (#2717)
1 parent 5c8143d commit 566f2c3

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

packages/react/src/select/root/SelectRoot.test.tsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,78 @@ describe('<Select.Root />', () => {
806806
});
807807
});
808808

809+
describe('prop: readOnly', () => {
810+
it('sets the readOnly state', async () => {
811+
const handleOpenChange = spy();
812+
const { user } = await render(
813+
<Select.Root defaultValue="b" onOpenChange={handleOpenChange} readOnly>
814+
<Select.Trigger>
815+
<Select.Value />
816+
</Select.Trigger>
817+
</Select.Root>,
818+
);
819+
820+
const trigger = screen.getByRole('combobox');
821+
expect(trigger).to.have.attribute('aria-readonly', 'true');
822+
expect(trigger).to.have.attribute('data-readonly');
823+
824+
await user.keyboard('[Tab]');
825+
expect(trigger).toHaveFocus();
826+
827+
await user.click(trigger);
828+
expect(handleOpenChange.callCount).to.equal(0);
829+
});
830+
831+
it('should not open the select when clicked', async () => {
832+
const handleOpenChange = spy();
833+
const { user } = await render(
834+
<Select.Root onOpenChange={handleOpenChange} readOnly>
835+
<Select.Trigger>
836+
<Select.Value />
837+
</Select.Trigger>
838+
</Select.Root>,
839+
);
840+
841+
const trigger = screen.getByRole('combobox');
842+
843+
await user.click(trigger);
844+
expect(screen.queryByRole('listbox')).to.equal(null);
845+
expect(handleOpenChange.callCount).to.equal(0);
846+
});
847+
848+
it('should not open the select when using keyboard', async () => {
849+
const handleOpenChange = spy();
850+
const { user } = await render(
851+
<Select.Root onOpenChange={handleOpenChange} readOnly>
852+
<Select.Trigger>
853+
<Select.Value />
854+
</Select.Trigger>
855+
</Select.Root>,
856+
);
857+
858+
const trigger = screen.getByRole('combobox');
859+
860+
await act(async () => {
861+
trigger.focus();
862+
});
863+
864+
expect(screen.queryByRole('listbox')).to.equal(null);
865+
expect(document.activeElement).to.equal(trigger);
866+
867+
await user.keyboard('[ArrowDown]');
868+
expect(screen.queryByRole('listbox')).to.equal(null);
869+
expect(handleOpenChange.callCount).to.equal(0);
870+
871+
await user.keyboard('[Enter]');
872+
expect(screen.queryByRole('listbox')).to.equal(null);
873+
expect(handleOpenChange.callCount).to.equal(0);
874+
875+
await user.keyboard('[Space]');
876+
expect(screen.queryByRole('listbox')).to.equal(null);
877+
expect(handleOpenChange.callCount).to.equal(0);
878+
});
879+
});
880+
809881
describe('prop: id', () => {
810882
it('sets the id on the hidden input', async () => {
811883
const { container } = await render(

packages/react/src/select/trigger/SelectTrigger.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,8 @@ export const SelectTrigger = React.forwardRef(function SelectTrigger(
159159
onPointerDown({ pointerType }) {
160160
store.set('touchModality', pointerType === 'touch');
161161
},
162-
onKeyDown(event) {
162+
onKeyDown() {
163163
keyboardActiveRef.current = true;
164-
165-
if (event.key === 'ArrowDown') {
166-
setOpen(true, createBaseUIEventDetails('list-navigation', event.nativeEvent));
167-
}
168164
},
169165
onMouseDown(event) {
170166
if (open) {

0 commit comments

Comments
 (0)