Skip to content

Commit ebeb3a7

Browse files
authored
feat(Form, Select, MenuItem): support dataAttributes (#1387)
WEB-2253
1 parent e857c3f commit ebeb3a7

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/form.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import * as styles from './form.css';
77
import * as tokens from './text-tokens';
88
import ScreenReaderOnly from './screen-reader-only';
99
import {isIos} from './utils/platform';
10+
import {getPrefixedDataAttributes} from './utils/dom';
1011

12+
import type {DataAttributes} from './utils/types';
1113
import type {FormStatus, FormErrors, FieldRegistration} from './form-context';
1214

1315
if (
@@ -33,6 +35,7 @@ type FormProps = {
3335
onValidationErrors?: (errors: FormErrors) => void;
3436
getErrorMessageForScreenReader?: (errors: FormErrors) => string;
3537
className?: string;
38+
dataAttributes?: DataAttributes;
3639
};
3740

3841
const Form = ({
@@ -44,6 +47,7 @@ const Form = ({
4447
onValidationErrors,
4548
getErrorMessageForScreenReader,
4649
id: idProp,
50+
dataAttributes,
4751
}: FormProps): JSX.Element => {
4852
const isMountedRef = React.useRef(true); // https://github.com/facebook/react/issues/14369#issuecomment-468305796
4953
const [values, setValues] = React.useState(initialValues);
@@ -260,6 +264,7 @@ const Form = ({
260264
ref={formRef}
261265
className={classnames(styles.form, className)}
262266
noValidate
267+
{...getPrefixedDataAttributes(dataAttributes, 'Form')}
263268
>
264269
{hasMultipleFormErrors ? (
265270
<ScreenReaderOnly>

src/menu.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ interface MenuItemBaseProps {
5757
destructive?: boolean;
5858
disabled?: boolean;
5959
onPress: (item: number) => void;
60+
dataAttributes?: DataAttributes;
6061
}
6162

6263
interface MenuItemWithoutControlProps extends MenuItemBaseProps {
@@ -79,6 +80,7 @@ export const MenuItem = ({
7980
onPress,
8081
controlType,
8182
checked,
83+
dataAttributes,
8284
}: MenuItemProps): JSX.Element => {
8385
const {focusedItem, setFocusedItem, closeMenu, isMenuOpen} = useMenuContext();
8486
const itemRef = React.useRef<HTMLDivElement | null>(null);
@@ -89,6 +91,8 @@ export const MenuItem = ({
8991
const menu: HTMLElement | null = item?.closest('[role=menu]') || null;
9092
const itemIndex = getItemIndexInMenu(menu, item);
9193

94+
const menuItemDataAttributes = {testid: 'MenuItem', ...dataAttributes};
95+
9296
const renderContent = () =>
9397
controlType === 'checkbox' ? (
9498
<Checkbox
@@ -103,6 +107,7 @@ export const MenuItem = ({
103107
}}
104108
disabled={disabled}
105109
role="menuitemcheckbox"
110+
dataAttributes={menuItemDataAttributes}
106111
render={({controlElement}) => (
107112
<Box paddingX={8} paddingY={12}>
108113
<Inline space="between" alignItems="center">
@@ -132,6 +137,7 @@ export const MenuItem = ({
132137
}}
133138
disabled={disabled}
134139
role="menuitem"
140+
dataAttributes={menuItemDataAttributes}
135141
>
136142
<Box paddingX={8} paddingY={12}>
137143
<div className={styles.itemContent}>

src/select.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {applyCssVars, pxToRem} from './utils/css';
1818
import {ThemeVariant} from './theme-variant-context';
1919
import {vars} from './skins/skin-contract.css';
2020

21+
import type {DataAttributes} from './utils/types';
22+
2123
export type SelectProps = {
2224
disabled?: boolean;
2325
error?: boolean;
@@ -40,6 +42,7 @@ export type SelectProps = {
4042
fullWidth?: boolean;
4143
native?: boolean;
4244
children?: void;
45+
dataAttributes?: DataAttributes;
4346
};
4447

4548
const Select = ({
@@ -59,6 +62,7 @@ const Select = ({
5962
onBlur,
6063
autoFocus = false,
6164
native,
65+
dataAttributes,
6266
}: SelectProps): JSX.Element => {
6367
const inputRef = React.useRef<HTMLSelectElement | HTMLInputElement>(null);
6468
const focusableRef = React.useRef<HTMLSelectElement | HTMLDivElement>(null);
@@ -309,13 +313,14 @@ const Select = ({
309313
const selectedValue = valueState ?? value;
310314

311315
return (
312-
<ThemeVariant isInverse={false}>
316+
<ThemeVariant variant="default">
313317
{shouldUseNative || isServerSide ? (
314318
<FieldContainer
315319
disabled={disabled}
316320
helperText={<HelperText error={error} leftText={helperText} />}
317321
fieldRef={fieldRef}
318322
fullWidth={fullWidth}
323+
dataAttributes={{testid: 'Select', ...dataAttributes}}
319324
>
320325
{label && (
321326
<Label
@@ -422,6 +427,7 @@ const Select = ({
422427
error={error}
423428
inputRef={inputRef}
424429
fieldRef={fieldRef}
430+
dataAttributes={{testid: 'Select', ...dataAttributes}}
425431
/>
426432

427433
<div

0 commit comments

Comments
 (0)