Skip to content

LG-4899: getLgIds #2786

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

Merged
merged 56 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
e584c08
checkboc lgids
shaneeza Mar 28, 2025
d44a0cf
confirmation modal lgids
shaneeza Mar 28, 2025
6a99af1
checkbox specs
shaneeza Mar 28, 2025
e755469
checkbox testUtils type
shaneeza Mar 28, 2025
17645b5
form-field lgids
shaneeza Mar 28, 2025
576a2aa
confirmation modal use root for lgids
shaneeza Mar 28, 2025
b55f264
form footer getLgIds
shaneeza Mar 28, 2025
f842ab0
add DEFAULT_LGID_ROOT import
shaneeza Mar 28, 2025
9b86e59
passwordInput getLgIds
shaneeza Mar 28, 2025
454e56e
tabs getLgIds
shaneeza Mar 31, 2025
19ef955
textArea getLgIds
shaneeza Mar 31, 2025
9e96a8a
textInput getLgIds
shaneeza Mar 31, 2025
c9b60f1
toggle getLgIds
shaneeza Mar 31, 2025
113f1ae
table getLgIds
shaneeza Mar 31, 2025
6ebbdcc
menu getLgIds
shaneeza Mar 31, 2025
3d36ccb
splitButton getLgIds
shaneeza Mar 31, 2025
f760242
select getLgIds
shaneeza Mar 31, 2025
a49c5a8
typography getLgIds
shaneeza Mar 31, 2025
a286f9b
fix failing tests
shaneeza Mar 31, 2025
3f69213
some clean up
shaneeza Mar 31, 2025
8413e2d
merge conflict
shaneeza Apr 2, 2025
4d97726
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4899-…
shaneeza Apr 3, 2025
1616519
fix test
shaneeza Apr 4, 2025
5d2ab30
lint
shaneeza Apr 4, 2025
b38c4f9
some cleanup
shaneeza Apr 4, 2025
8aa3033
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4899-…
shaneeza Apr 10, 2025
1460aa5
fix failing tests
shaneeza Apr 10, 2025
d55ae33
styleguide
shaneeza Apr 10, 2025
55d7af8
update code and select
shaneeza Apr 21, 2025
5140d06
remove constants folder and update exports
shaneeza Apr 21, 2025
7fa641e
changeset for date picker
shaneeza Apr 21, 2025
711cd8f
update styleguide
shaneeza Apr 22, 2025
40e641e
fix lint and update styleguide
shaneeza Apr 22, 2025
b924e48
styleguide
shaneeza Apr 22, 2025
ac23442
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4899-…
shaneeza Apr 22, 2025
a13ff1b
add LgIdString type and update styleguide
shaneeza May 2, 2025
b0bb942
use root as default
shaneeza May 2, 2025
aa268ad
fix build error
shaneeza May 2, 2025
95b3b93
remove memo
shaneeza May 2, 2025
05a49da
fix failing text-area tests
shaneeza May 4, 2025
3c4cdc6
update code lgIds to use root
shaneeza May 4, 2025
ff564be
update changeset
shaneeza May 4, 2025
2a3427b
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4899-…
shaneeza May 5, 2025
cf5741e
update label to description in typography
shaneeza May 5, 2025
7065ae8
merge conflicts
shaneeza Jun 9, 2025
3a1ad90
lint
shaneeza Jun 9, 2025
e67df1b
remove default dataLgId
shaneeza Jun 9, 2025
7f32cf0
no longer passing IDs to copyButton spec
shaneeza Jun 9, 2025
161f668
add drawer changeset
shaneeza Jun 9, 2025
c195e3a
update changesets related to getLgIds
shaneeza Jun 9, 2025
92e79eb
merge conflict
shaneeza Jun 20, 2025
096d819
add data-testid
shaneeza Jun 23, 2025
04776b8
fix failing tests
shaneeza Jun 23, 2025
b579f9e
move getLgIds in modal, move test ids in confirmation moda
shaneeza Jun 24, 2025
935ec43
fix failing tests
shaneeza Jun 24, 2025
b556bca
Merge branch 'main' into LG-4899-getLgIds
shaneeza Jun 24, 2025
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/big-stars-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/toggle': patch
---

Updates `data-lgid` and `data-testid` to use scope based test IDs. These test IDs are generated using the helper utility `getLgIds`. For more information [check out the section in the styleguide about getLgIds](https://github.com/mongodb/leafygreen-ui/blob/main/STYLEGUIDE.md#getlgids).
5 changes: 5 additions & 0 deletions .changeset/breezy-paws-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/lib': patch
---

Export `LgIdString` type
42 changes: 42 additions & 0 deletions .changeset/old-mangos-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
'@leafygreen-ui/code': major
---

- Move `lgIds` to context, use `LgIdString` type, and remove setting `DEFAULT_LGID_ROOT`.
- Update all IDs to return the unique root identifier passed to `data-lgid`:

**Before**
```tsx
export const getLgIds = (root: LgIdString = DEFAULT_LGID_ROOT) => {
const ids = {
root,
panel: `${DEFAULT_LGID_ROOT}-panel`,
select: `${root}-select`,
copyButton: `${DEFAULT_LGID_ROOT}-copy_button`,
copyTooltip: `${DEFAULT_LGID_ROOT}-copy_tooltip`,
expandButton: `${root}-expand_button`,
skeleton: `${root}-skeleton`,
pre: `${root}-pre`,
title: `${DEFAULT_LGID_ROOT}-title`,
} as const;
return ids;
};
```

**After**
```tsx
export const getLgIds = (root: LgIdString = DEFAULT_LGID_ROOT) => {
const ids = {
root,
panel: `${root}-panel`, // Updated to use unique root identifier
select: `${root}-select`,
copyButton: `${root}-copy_button`, // Updated to use unique root identifier
copyTooltip: `${root}-copy_tooltip`, // Updated to use unique root identifier
expandButton: `${root}-expand_button`,
skeleton: `${root}-skeleton`,
pre: `${root}-pre`,
title: `${root}-title`, // Updated to use unique root identifier
} as const;
return ids;
};
```
6 changes: 6 additions & 0 deletions .changeset/short-baboons-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@leafygreen-ui/button': patch
'@leafygreen-ui/drawer': patch
---

Use `LgIdString` type and remove setting `DEFAULT_LGID_ROOT`.
26 changes: 26 additions & 0 deletions .changeset/shy-gorillas-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'@leafygreen-ui/confirmation-modal': major
'@leafygreen-ui/gallery-indicator': major
'@leafygreen-ui/password-input': major
'@leafygreen-ui/split-button': major
'@leafygreen-ui/form-footer': major
'@leafygreen-ui/form-field': major
'@leafygreen-ui/text-input': major
'@leafygreen-ui/typography': major
'@leafygreen-ui/text-area': major
'@leafygreen-ui/checkbox': major
'@leafygreen-ui/select': major
'@leafygreen-ui/table': major
'@leafygreen-ui/modal': major
'@leafygreen-ui/menu': major
'@leafygreen-ui/tabs': major
---

Updates `data-lgid` and `data-testid` to use scope based test IDs. These test IDs are generated using the helper utility `getLgIds`. For more information [check out the section in the styleguide about getLgIds](https://github.com/mongodb/leafygreen-ui/blob/main/STYLEGUIDE.md#getlgids).

Removes public exports for:
- `LGIDs`
- `LGIDS_CHECKBOX`
- `LGIDS_FORM_FIELD`
- `LGIDS_SELECT`
- `LGIDS_TYPOGRAPHY`
5 changes: 5 additions & 0 deletions .changeset/tough-dryers-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/date-picker': patch
---

Updates spec file to use updated test ids for `@leafygreen-ui/form-field` components
77 changes: 77 additions & 0 deletions STYLEGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- [Passing darkMode to children](#passing-darkMode-to-children)
- [File Structure](https://github.com/mongodb/leafygreen-ui/blob/main/stories/Folder-Structure.stories.mdx)
- [API Patterns](#api-patterns)
- [Input errors](#input-errors)
- [getLgIds](#getlgids)
- [References](#references)

# Contribution Guide
Expand Down Expand Up @@ -637,6 +639,81 @@ return (
- Use `errorMessage` prop to set the error message that is displayed next to the input.
- If `state='error'` but `errorMessage` is not defined, require `aria-describedby`

## getLgIds

The `getLgIds` utility function generates test IDs for components in the LeafyGreen UI library.

The function accepts an optional root identifier, allowing consumers to set a unique base ID for each instance of a component. This base is then used to generate, scoped test IDs, ensuring predictable targeting in tests.

#### Usage

Each component includes a `getLgIds.ts` file that exports the `getLgIds` utility. This function returns an object containing IDs for each trackable element within the component.

For more information on naming test IDs, [check out the section on BEM-ish patterns](#follow-bem-ish-patterns-when-hard-coding-a-data-testid-or-data-lgid).

```js
// getLgIds.ts

export const DEFAULT_LGID_ROOT = 'lg-component';

export const getLgIds = (root: `lg-${string}` = DEFAULT_LGID_ROOT) => {
const ids = {
root,
button: `${root}-button`,
input: `${root}-input`,
} as const;
return ids;
};

export type GetLgIdsReturnType = ReturnType<typeof getLgIds>;
```

Inside the component you can use the test IDs by calling `getLgIds()`:

```js
// Component.tsx

const lgIds = getLgIds();

// lgIds.root = 'lg-component'
// lgIds.button = 'lg-component-button'
// lgIds.input -> 'lg-component-input'
```

#### Component.tsx

```js
export const Component = forwardRef<
HTMLDivElement,
ComponentProps,
>(
(
{
'data-lgid': dataLgId,
...rest
},
fwdRef: React.Ref<any>,
) => {
const lgIds = getLgIds(dataLgId);

return (
<div
data-testid={lgIds.root}
data-lgid={lgIds.root}
ref={fwdRef}
>
<button
data-testid={lgIds.button}
data-lgid={lgIds.button}
>
Click Me I'm Irish
</button>
</div>
);
},
);
```
## References
- [Airbnb Javascript Style Guide](https://github.com/airbnb/javascript)
Expand Down
4 changes: 2 additions & 2 deletions packages/button/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { BaseFontSize } from '@leafygreen-ui/tokens';
import { ButtonContent } from '../ButtonContent/ButtonContent';
import { ButtonClassName } from '../styles';
import { BaseButtonProps, Size, Variant } from '../types';
import { DEFAULT_LGID_ROOT, getLgIds } from '../utils';
import { getLgIds } from '../utils';

import { getClassName } from './Button.styles';

Expand All @@ -24,7 +24,7 @@ export const Button = InferredPolymorphic<BaseButtonProps, 'button'>(
variant = Variant.Default,
size = Size.Default,
darkMode: darkModeProp,
'data-lgid': dataLgId = DEFAULT_LGID_ROOT,
'data-lgid': dataLgId,
baseFontSize = BaseFontSize.Body1,
disabled = false,
onClick,
Expand Down
4 changes: 3 additions & 1 deletion packages/button/src/utils/getLgIds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { LgIdString } from '@leafygreen-ui/lib';

export const DEFAULT_LGID_ROOT = 'lg-button';

export const getLgIds = (root: `lg-${string}` = DEFAULT_LGID_ROOT) => {
export const getLgIds = (root: LgIdString = DEFAULT_LGID_ROOT) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not necessary here, but it might be worth creating a GetLgIdsFunction function type, and maybe a return type?

type GetLgIdsFunction = (root?: LgIdString) => {
  root: LgIdString,
  ...
}

return {
root,
} as const;
Expand Down
6 changes: 3 additions & 3 deletions packages/button/src/utils/getTestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// import { queryByLgId } from '@lg-tools/test-harnesses';
// import { findByLgId } from '@lg-tools/test-harnesses';
import { findByLgId, getByLgId, queryByLgId } from '@lg-tools/test-harnesses';

import { LgIdString } from '@leafygreen-ui/lib';

import { DEFAULT_LGID_ROOT, getLgIds } from './getLgIds';
import { GetTestUtilsReturnType } from './getTestUtils.types';

export const getTestUtils = <T extends HTMLElement = HTMLElement>(
lgId: `lg-${string}` = DEFAULT_LGID_ROOT,
lgId: LgIdString = DEFAULT_LGID_ROOT,
): GetTestUtilsReturnType<T> => {
const lgIds = getLgIds(lgId);

Expand Down
18 changes: 14 additions & 4 deletions packages/checkbox/src/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@leafygreen-ui/typography';

import { Check } from '../Check';
import { LGIDS_CHECKBOX } from '../constants';
import { getLgIds } from '../utils/getLgIds';

import {
checkWrapperClassName,
Expand Down Expand Up @@ -45,7 +45,7 @@ const Checkbox = React.forwardRef(
checked: checkedProp,
className,
darkMode: darkModeProp,
'data-lgid': dataLgId = LGIDS_CHECKBOX.root,
'data-lgid': dataLgId,
description,
disabled = false,
id: idProp,
Expand All @@ -63,6 +63,8 @@ const Checkbox = React.forwardRef(
const { darkMode, theme } = useDarkMode(darkModeProp);
const baseFontSize = useUpdatedBaseFontSize(baseFontSizeProp);

const lgIds = getLgIds(dataLgId);

const [checked, setChecked] = React.useState(defaultChecked);
const isChecked = React.useMemo(
() => (checkedProp != null ? checkedProp : checked),
Expand Down Expand Up @@ -124,7 +126,8 @@ const Checkbox = React.forwardRef(
},
className,
)}
data-lgid={dataLgId}
data-lgid={lgIds.root}
data-testid={lgIds.root}
style={style}
>
<Label
Expand All @@ -134,6 +137,8 @@ const Checkbox = React.forwardRef(
className={cx(labelStyle, labelHoverStyle[theme], {
[disabledLabelStyle]: disabled,
})}
data-lgid={lgIds.root}
data-testid={lgIds.root}
>
<input
{...rest}
Expand Down Expand Up @@ -178,7 +183,12 @@ const Checkbox = React.forwardRef(
</Label>

{description && (
<Description className={descriptionStyle} disabled={disabled}>
<Description
className={descriptionStyle}
disabled={disabled}
data-lgid={lgIds.root}
data-testid={lgIds.root}
>
{description}
</Description>
)}
Expand Down
6 changes: 0 additions & 6 deletions packages/checkbox/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,3 @@ export const insetPct = 100 - hypotenusePct;
export const rippleScale = 2.25;
export const rippleTransitionScale = 4;
export const rippleTransitionDelay = -rippleTransitionScale / rippleScale;

const LGID_ROOT = 'lg-checkbox';

export const LGIDS_CHECKBOX = {
root: LGID_ROOT,
} as const;
8 changes: 6 additions & 2 deletions packages/checkbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export { type CheckboxProps, checkWrapperClassName, default } from './Checkbox';
export { LGIDS_CHECKBOX } from './constants';
export { getTestUtils } from './utils';
export {
DEFAULT_LGID_ROOT,
getLgIds,
type GetLgIdsReturnType,
getTestUtils,
} from './utils';
12 changes: 12 additions & 0 deletions packages/checkbox/src/utils/getLgIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LgIdString } from '@leafygreen-ui/lib';

export const DEFAULT_LGID_ROOT = 'lg-checkbox';

export const getLgIds = (root: LgIdString = DEFAULT_LGID_ROOT) => {
const ids = {
root,
} as const;
return ids;
};

export type GetLgIdsReturnType = ReturnType<typeof getLgIds>;
16 changes: 9 additions & 7 deletions packages/checkbox/src/utils/getTestUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
import { getByLgId, queryBySelector } from '@lg-tools/test-harnesses';

import { LGIDS_TYPOGRAPHY } from '@leafygreen-ui/typography';

import { LGIDS_CHECKBOX } from '../constants';
import { LgIdString } from '@leafygreen-ui/lib';
import { getLgIds as getLgTypographyLgIds } from '@leafygreen-ui/typography';

import { DEFAULT_LGID_ROOT, getLgIds } from './getLgIds';
import { CheckboxTestUtilsReturnType } from './getTestUtils.types';

export const getTestUtils = (
lgId: string = LGIDS_CHECKBOX.root,
lgId: LgIdString = DEFAULT_LGID_ROOT,
): CheckboxTestUtilsReturnType => {
const lgIds = getLgIds(lgId);
const typographyLgIds = getLgTypographyLgIds(lgIds.root);
/**
* Queries the DOM for the element using the `data-lgid` data attribute.
* Will throw if no element is found.
*/
const element = getByLgId!(lgId);
const element = getByLgId!(lgIds.root);

/**
* Queries the `element` for the label element. Will return `null` if the label is not found.
*/
const getLabel = queryBySelector<HTMLLabelElement>(
element,
`[data-lgid=${LGIDS_TYPOGRAPHY.label}]`,
`[data-lgid=${typographyLgIds.label}]`,
);

/**
* Queries the `element` for the description element. Will return `null` if the desription is not found.
*/
const getDescription = queryBySelector<HTMLElement>(
element,
`[data-lgid=${LGIDS_TYPOGRAPHY.description}]`,
`[data-lgid=${typographyLgIds.description}]`,
);

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/checkbox/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export {
DEFAULT_LGID_ROOT,
getLgIds,
type GetLgIdsReturnType,
} from './getLgIds';
export { getTestUtils } from './getTestUtils';
Loading