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

[typings] add supported htmlFor prop to FormGroupLabelProps definition #983

Merged
merged 2 commits into from
Jan 26, 2021
Merged

[typings] add supported htmlFor prop to FormGroupLabelProps definition #983

merged 2 commits into from
Jan 26, 2021

Conversation

shiftkey
Copy link
Contributor

FormGroup.Label supports a htmlFor prop in the docs to associated the label with a text input: https://primer.style/components/FormGroup#formgrouplabel

However the TS declarations do not indicate this is a supported prop:

Omitting this value causes axe to report accessibility violations, so I added in the missing value (optional, just to match the docs).

Screenshots

N/A

Merge checklist

  • Added or updated TypeScript definitions (index.d.ts) if necessary
  • Added/updated tests
  • Added/updated documentation
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.

@changeset-bot
Copy link

changeset-bot bot commented Jan 26, 2021

🦋 Changeset detected

Latest commit: e841e15

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Jan 26, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/primer/primer-components/90fz93v37
✅ Preview: https://primer-components-git-fork-shiftkey-add-missing-prop.primer.now.sh

@vercel vercel bot temporarily deployed to Preview January 26, 2021 17:04 Inactive
@shiftkey shiftkey changed the title add supported htmlFor prop to FormGroupLabelProps type definition [typings] add supported htmlFor prop to FormGroupLabelProps definition Jan 26, 2021
Copy link
Contributor

@colebemis colebemis left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, @shiftkey! I left a suggestion on how we can write the type definition without having to list HTML attributes by hand.

Also FYI, we're in the process of rewriting all of Primer React Components in TypeScript. So hopefully typing errors like this will soon be a thing of the past 😄 #970

Comment on lines +241 to +243
export interface FormGroupLabelProps extends CommonProps, TypographyProps, Omit<React.HTMLAttributes<HTMLLabelElement>, 'color'> {
htmlFor?: string
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, it's strange that React.HTMLAttributes<HTMLLabelElement> doesn't include htmlFor. React.ComponentPropsWithRef<'label'> should do the trick though. I recently learned that ComponentPropsWithRef is the preferred way to get props of HTML elements anyway (link).

Suggested change
export interface FormGroupLabelProps extends CommonProps, TypographyProps, Omit<React.HTMLAttributes<HTMLLabelElement>, 'color'> {
htmlFor?: string
}
export interface FormGroupLabelProps extends CommonProps, TypographyProps, Omit<React.ComponentPropsWithRef<'label'>, 'color'> {}

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, I just tried this locally and I'm getting errors. I'm fine with merging the PR as-is since we're about to replace this whole index.d.ts file very soon.

Copy link
Contributor Author

@shiftkey shiftkey Jan 26, 2021

Choose a reason for hiding this comment

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

For extra context, I see the @types/react version of LabelHTMLAttributes has the field:

interface LabelHTMLAttributes<T> extends HTMLAttributes<T> {
    form?: string;
    htmlFor?: string;
}

However HTMLLabelElement in @types/react doesn't seem to have this information:

interface HTMLLabelElement extends HTMLElement { }

I think this would be the fix without duplicating htmlFor but that feels like a big hammer given the refactorings underway:

export interface FormGroupLabelProps extends CommonProps, TypographyProps, Omit<LabelHTMLAttributes, 'color'> {}

@vercel vercel bot temporarily deployed to Preview January 26, 2021 19:15 Inactive
@colebemis colebemis merged commit 091f9a2 into primer:main Jan 26, 2021
@github-actions github-actions bot mentioned this pull request Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants