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

fix: move testId to inputProps for all input components #2096

Merged
merged 5 commits into from
Jan 29, 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
12 changes: 8 additions & 4 deletions packages/odyssey-react-mui/src/@types/react-augment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
*/

import { FC } from "react";

export interface ForwardRefWithType extends FC<WithForwardRefProps<Option>> {
<T extends Option>(props: WithForwardRefProps<T>): ReturnType<
FC<WithForwardRefProps<T>>
>;
}

export type FocusHandle = {
focus: () => void;
};
declare module "react" {
type DataAttributeKey = `data-${string}`;
interface InputHTMLAttributes<T> extends HTMLAttributes<T> {
// Allows data-* props to be passed to inputProps in nested MUI components
// see: https://github.com/mui/material-ui/issues/20160
[dataAttribute: DataAttributeKey]: string | undefined;
}
}
3 changes: 2 additions & 1 deletion packages/odyssey-react-mui/src/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ const Autocomplete = <
...params.inputProps,
"aria-errormessage": errorMessageElementId,
"aria-labelledby": labelElementId,
"data-se": testId,
}}
aria-describedby={ariaDescribedBy}
id={id}
Expand All @@ -284,6 +285,7 @@ const Autocomplete = <
isOptional,
label,
nameOverride,
testId,
]
);
const onChange = useCallback<
Expand Down Expand Up @@ -324,7 +326,6 @@ const Autocomplete = <
{...inputValueProp}
// AutoComplete is wrapped in a div within MUI which does not get the disabled attr. So this aria-disabled gets set in the div
aria-disabled={isDisabled}
data-se={testId}
disableCloseOnSelect={hasMultipleChoices}
disabled={isDisabled}
freeSolo={isCustomValueAllowed}
Expand Down
17 changes: 8 additions & 9 deletions packages/odyssey-react-mui/src/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { MuiPropsContext, useMuiProps } from "./MuiPropsContext";
import { Tooltip } from "./Tooltip";
import type { AllowedProps } from "./AllowedProps";
import { FocusHandle } from "./@types/react-augment";
import { FocusHandle } from "./inputUtils";

export const buttonSizeValues = ["small", "medium", "large"] as const;
export const buttonTypeValues = ["button", "submit", "reset"] as const;
Expand All @@ -49,9 +49,9 @@ export type ButtonProps = {
*/
ariaDescribedBy?: string;
/**
* The ref forwarded to the Button to expose focus()
* The ref forwarded to the Button
*/
buttonFocusRef?: React.RefObject<FocusHandle>;
buttonRef?: React.RefObject<FocusHandle>;
/**
* The icon element to display at the end of the Button
*/
Expand Down Expand Up @@ -119,7 +119,7 @@ const Button = ({
ariaDescribedBy,
ariaLabel,
ariaLabelledBy,
buttonFocusRef,
buttonRef,
endIcon,
id,
isDisabled,
Expand All @@ -136,14 +136,13 @@ const Button = ({
}: ButtonProps) => {
const muiProps = useMuiProps();

const ref = useRef<HTMLButtonElement>(null);
const localButtonRef = useRef<HTMLButtonElement>(null);
useImperativeHandle(
buttonFocusRef,
buttonRef,
() => {
const element = ref.current;
return {
focus: () => {
element && element.focus();
localButtonRef.current?.focus();
},
};
},
Expand All @@ -163,7 +162,7 @@ const Button = ({
fullWidth={isFullWidth}
id={id}
onClick={onClick}
ref={ref}
ref={localButtonRef}
size={size}
startIcon={startIcon}
translate={translate}
Expand Down
26 changes: 15 additions & 11 deletions packages/odyssey-react-mui/src/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import {
import { FieldComponentProps } from "./FieldComponentProps";
import { Typography } from "./Typography";
import type { AllowedProps } from "./AllowedProps";
import { ComponentControlledState, getControlState } from "./inputUtils";
import {
ComponentControlledState,
FocusHandle,
getControlState,
} from "./inputUtils";
import { CheckedFieldProps } from "./FormCheckedProps";
import { FocusHandle } from "./@types/react-augment";

export const checkboxValidityValues = ["valid", "invalid", "inherit"] as const;

Expand All @@ -43,9 +46,9 @@ export type CheckboxProps = {
*/
id?: string;
/**
* The ref forwarded to the Checkbox to expose focus()
* The ref forwarded to the Checkbox
*/
inputFocusRef?: React.RefObject<FocusHandle>;
inputRef?: React.RefObject<FocusHandle>;
/**
* Determines whether the Checkbox is disabled
*/
Expand Down Expand Up @@ -86,7 +89,7 @@ const Checkbox = ({
ariaLabel,
ariaLabelledBy,
id: idOverride,
inputFocusRef,
inputRef,
isChecked,
isDefaultChecked,
isDisabled,
Expand Down Expand Up @@ -116,14 +119,13 @@ const Checkbox = ({
return { defaultChecked: isDefaultChecked };
}, [isDefaultChecked, isChecked]);

const inputRef = useRef<HTMLInputElement>(null);
const localInputRef = useRef<HTMLInputElement>(null);
useImperativeHandle(
inputFocusRef,
inputRef,
() => {
const element = inputRef.current;
return {
focus: () => {
element && element.focus();
localInputRef.current?.focus();
},
};
},
Expand Down Expand Up @@ -179,13 +181,15 @@ const Checkbox = ({
indeterminate={isIndeterminate}
onChange={onChange}
required={isRequired}
inputRef={inputRef}
inputProps={{
"data-se": testId,
}}
inputRef={localInputRef}
sx={() => ({
marginBlockStart: "2px",
})}
/>
}
data-se={testId}
disabled={isDisabled}
id={idOverride}
label={label}
Expand Down
17 changes: 8 additions & 9 deletions packages/odyssey-react-mui/src/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import { memo, ReactElement, useImperativeHandle, useRef } from "react";
import { ExternalLinkIcon } from "./icons.generated";
import type { AllowedProps } from "./AllowedProps";
import { FocusHandle } from "./inputUtils";

import { Link as MuiLink, LinkProps as MuiLinkProps } from "@mui/material";
import { FocusHandle } from "./@types/react-augment";

export const linkVariantValues = ["default", "monochrome"] as const;

Expand All @@ -33,9 +33,9 @@ export type LinkProps = {
*/
icon?: ReactElement;
/**
* The ref forwarded to the TextField to expose focus()
* The ref forwarded to the TextField
*/
linkFocusRef?: React.RefObject<FocusHandle>;
linkRef?: React.RefObject<FocusHandle>;
/**
* The click event handler for the Link
*/
Expand Down Expand Up @@ -63,22 +63,21 @@ const Link = ({
children,
href,
icon,
linkFocusRef,
linkRef,
rel,
target,
testId,
translate,
variant,
onClick,
}: LinkProps) => {
const ref = useRef<HTMLAnchorElement>(null);
const localLinkRef = useRef<HTMLAnchorElement>(null);
useImperativeHandle(
linkFocusRef,
linkRef,
() => {
const element = ref.current;
return {
focus: () => {
element && element.focus();
localLinkRef.current?.focus();
},
};
},
Expand All @@ -89,7 +88,7 @@ const Link = ({
<MuiLink
data-se={testId}
href={href}
ref={ref}
ref={localLinkRef}
rel={rel}
target={target}
translate={translate}
Expand Down
35 changes: 33 additions & 2 deletions 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice documentation comment! 👍

*/
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
*/
inputRef?: 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,
inputRef,
isDisabled = false,
isFullWidth = false,
isMultiSelect,
Expand All @@ -126,6 +140,20 @@ const NativeSelect: ForwardRefWithType = forwardRef(
uncontrolledValue: defaultValue,
})
);
const localInputRef = useRef<HTMLSelectElement>(null);

useImperativeHandle(
inputRef,
() => {
return {
focus: () => {
localInputRef.current?.focus();
},
};
},
[]
);

const inputValues = useInputValues({
defaultValue,
value,
Expand Down Expand Up @@ -153,13 +181,15 @@ const NativeSelect: ForwardRefWithType = forwardRef(
<MuiSelect
{...inputValues}
aria-describedby={ariaDescribedBy}
autoComplete={autoCompleteType}
children={children}
data-se={testId}
id={idOverride}
inputProps={{
"aria-errormessage": errorMessageElementId,
"aria-labelledby": labelElementId,
"data-se": testId,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}}
inputRef={localInputRef}
name={idOverride}
multiple={hasMultipleChoices}
native={true}
Expand All @@ -171,6 +201,7 @@ const NativeSelect: ForwardRefWithType = forwardRef(
/>
),
[
autoCompleteType,
children,
idOverride,
inputValues,
Expand Down
Loading
Loading