Skip to content

Commit

Permalink
fix: add support for autocomplete and inputref in NativeSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
trevoring-okta committed Jan 27, 2024
1 parent 7c50d3d commit d8a3bdc
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/odyssey-react-mui/src/NativeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
*/

import React, {
InputHTMLAttributes,
ReactElement,
forwardRef,
memo,
useCallback,
useImperativeHandle,
useMemo,
useRef,
} from "react";
Expand All @@ -25,7 +27,7 @@ import {
import { Field } from "./Field";
import { FieldComponentProps } from "./FieldComponentProps";
import type { AllowedProps } from "./AllowedProps";
import { getControlState, useInputValues } from "./inputUtils";
import { FocusHandle, getControlState, useInputValues } from "./inputUtils";
import { ForwardRefWithType } from "./@types/react-augment";

export type NativeSelectOption = {
Expand All @@ -41,6 +43,12 @@ export type NativeSelectProps<
Value extends NativeSelectValueType<HasMultipleChoices>,
HasMultipleChoices extends boolean
> = {
/**
* This prop helps users to fill forms faster, especially on mobile devices.
* The name can be confusing, as it's more like an autofill.
* @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
*/
autoCompleteType?: InputHTMLAttributes<HTMLInputElement>["autoComplete"];
/**
* The options or optgroup elements within the NativeSelect
*/
Expand All @@ -53,6 +61,10 @@ export type NativeSelectProps<
* If `true`, the Select allows multiple selections
*/
hasMultipleChoices?: HasMultipleChoices;
/**
* The ref forwarded to the NativeSelect to expose focus()
*/
inputFocusRef?: React.RefObject<FocusHandle>;
/**
* @deprecated Use `hasMultipleChoices` instead
*/
Expand Down Expand Up @@ -98,13 +110,15 @@ const NativeSelect: ForwardRefWithType = forwardRef(
HasMultipleChoices extends boolean
>(
{
autoCompleteType,
defaultValue,
errorMessage,
errorMessageList,
hasMultipleChoices: hasMultipleChoicesProp,
hint,
HintLinkComponent,
id: idOverride,
inputFocusRef,
isDisabled = false,
isFullWidth = false,
isMultiSelect,
Expand All @@ -126,6 +140,21 @@ const NativeSelect: ForwardRefWithType = forwardRef(
uncontrolledValue: defaultValue,
})
);
const inputRef = useRef<HTMLSelectElement>(null);

useImperativeHandle(
inputFocusRef,
() => {
const element = inputRef.current;
return {
focus: () => {
element && element.focus();
},
};
},
[]
);

const inputValues = useInputValues({
defaultValue,
value,
Expand Down Expand Up @@ -153,13 +182,15 @@ const NativeSelect: ForwardRefWithType = forwardRef(
<MuiSelect
{...inputValues}
aria-describedby={ariaDescribedBy}
autoComplete={autoCompleteType}
children={children}
id={idOverride}
inputProps={{
"aria-errormessage": errorMessageElementId,
"aria-labelledby": labelElementId,
"data-se": testId,
}}
inputRef={inputRef}
name={idOverride}
multiple={hasMultipleChoices}
native={true}
Expand All @@ -171,6 +202,7 @@ const NativeSelect: ForwardRefWithType = forwardRef(
/>
),
[
autoCompleteType,
children,
idOverride,
inputValues,
Expand Down

0 comments on commit d8a3bdc

Please sign in to comment.