Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NativeSelect): add focus in readOnly state #2212

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-countries-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@digdir/designsystemet-react": patch
---

NativeSelect: add focus in `readOnly` state
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ describe('NativeSelect', () => {
expect(screen.getByRole('combobox')).toBeDisabled();
});

it('should be disabled when "readOnly" is true', () => {
render({ readOnly: true });
expect(screen.getByRole('combobox')).toBeDisabled();
});

it('Sets the ref on the select element if given', () => {
const ref = createRef<HTMLSelectElement>();
render({}, ref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const NativeSelect = forwardRef<HTMLSelectElement, NativeSelectProps>(
selectProps,
descriptionId,
errorId,
readOnly = false,
readOnly,
size = 'md',
} = useNativeSelect(props);

Expand Down Expand Up @@ -105,7 +105,7 @@ export const NativeSelect = forwardRef<HTMLSelectElement, NativeSelectProps>(
)}
<div className='ds-native-select__wrapper'>
<select
disabled={disabled || readOnly}
disabled={disabled}
ref={ref}
size={htmlSize}
className={cl(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { SelectHTMLAttributes } from 'react';
import type {
KeyboardEventHandler,
MouseEventHandler,
SelectHTMLAttributes,
} from 'react';
import { useContext } from 'react';

import { FieldsetContext } from '../Fieldset/FieldsetContext';
Expand All @@ -13,7 +17,13 @@ type UseNativeSelect = (props: NativeSelectProps) => Omit<
> & {
selectProps: Pick<
SelectHTMLAttributes<HTMLSelectElement>,
'name' | 'required' | 'onClick' | 'onChange' | 'id'
| 'name'
| 'required'
| 'onClick'
| 'onChange'
| 'id'
| 'onKeyDown'
| 'onMouseDown'
>;
};

Expand All @@ -22,7 +32,7 @@ export const useNativeSelect: UseNativeSelect = (props) => {
const fieldset = useContext(FieldsetContext);
const {
inputProps: selectProps,
readOnly,
readOnly = false,
size = fieldset?.size ?? 'md',
...rest
} = useFormField(props, 'select');
Expand All @@ -41,6 +51,22 @@ export const useNativeSelect: UseNativeSelect = (props) => {
}
props?.onClick?.(e);
},
onKeyDown: (e) => {
if (readOnly) {
if (e.key === 'Tab') return;
e.preventDefault();
return;
}
props?.onKeyDown?.(e);
},
onMouseDown: (e) => {
if (readOnly) {
e.preventDefault();
if (e.target instanceof HTMLElement) e.target.focus();
return;
}
props?.onMouseDown?.(e);
},
onChange: (e) => {
if (readOnly) {
e.preventDefault();
Expand Down
Loading