Skip to content

Commit 23192dd

Browse files
authored
feat(NativeSelect): add focus in readOnly state (#2212)
resolves #1659
1 parent 90ca009 commit 23192dd

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

.changeset/wise-countries-double.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@digdir/designsystemet-react": patch
3+
---
4+
5+
NativeSelect: add focus in `readOnly` state

packages/react/src/components/form/NativeSelect/NativeSelect.test.tsx

-5
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ describe('NativeSelect', () => {
8383
expect(screen.getByRole('combobox')).toBeDisabled();
8484
});
8585

86-
it('should be disabled when "readOnly" is true', () => {
87-
render({ readOnly: true });
88-
expect(screen.getByRole('combobox')).toBeDisabled();
89-
});
90-
9186
it('Sets the ref on the select element if given', () => {
9287
const ref = createRef<HTMLSelectElement>();
9388
render({}, ref);

packages/react/src/components/form/NativeSelect/NativeSelect.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const NativeSelect = forwardRef<HTMLSelectElement, NativeSelectProps>(
5858
selectProps,
5959
descriptionId,
6060
errorId,
61-
readOnly = false,
61+
readOnly,
6262
size = 'md',
6363
} = useNativeSelect(props);
6464

@@ -105,7 +105,7 @@ export const NativeSelect = forwardRef<HTMLSelectElement, NativeSelectProps>(
105105
)}
106106
<div className='ds-native-select__wrapper'>
107107
<select
108-
disabled={disabled || readOnly}
108+
disabled={disabled}
109109
ref={ref}
110110
size={htmlSize}
111111
className={cl(

packages/react/src/components/form/NativeSelect/useNativeSelect.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { SelectHTMLAttributes } from 'react';
1+
import type {
2+
KeyboardEventHandler,
3+
MouseEventHandler,
4+
SelectHTMLAttributes,
5+
} from 'react';
26
import { useContext } from 'react';
37

48
import { FieldsetContext } from '../Fieldset/FieldsetContext';
@@ -13,7 +17,13 @@ type UseNativeSelect = (props: NativeSelectProps) => Omit<
1317
> & {
1418
selectProps: Pick<
1519
SelectHTMLAttributes<HTMLSelectElement>,
16-
'name' | 'required' | 'onClick' | 'onChange' | 'id'
20+
| 'name'
21+
| 'required'
22+
| 'onClick'
23+
| 'onChange'
24+
| 'id'
25+
| 'onKeyDown'
26+
| 'onMouseDown'
1727
>;
1828
};
1929

@@ -22,7 +32,7 @@ export const useNativeSelect: UseNativeSelect = (props) => {
2232
const fieldset = useContext(FieldsetContext);
2333
const {
2434
inputProps: selectProps,
25-
readOnly,
35+
readOnly = false,
2636
size = fieldset?.size ?? 'md',
2737
...rest
2838
} = useFormField(props, 'select');
@@ -41,6 +51,22 @@ export const useNativeSelect: UseNativeSelect = (props) => {
4151
}
4252
props?.onClick?.(e);
4353
},
54+
onKeyDown: (e) => {
55+
if (readOnly) {
56+
if (e.key === 'Tab') return;
57+
e.preventDefault();
58+
return;
59+
}
60+
props?.onKeyDown?.(e);
61+
},
62+
onMouseDown: (e) => {
63+
if (readOnly) {
64+
e.preventDefault();
65+
if (e.target instanceof HTMLElement) e.target.focus();
66+
return;
67+
}
68+
props?.onMouseDown?.(e);
69+
},
4470
onChange: (e) => {
4571
if (readOnly) {
4672
e.preventDefault();

0 commit comments

Comments
 (0)