-
Notifications
You must be signed in to change notification settings - Fork 526
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
Conversation
🦋 Changeset detectedLatest commit: e841e15 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/primer/primer-components/90fz93v37 |
There was a problem hiding this 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
export interface FormGroupLabelProps extends CommonProps, TypographyProps, Omit<React.HTMLAttributes<HTMLLabelElement>, 'color'> { | ||
htmlFor?: string | ||
} |
There was a problem hiding this comment.
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).
export interface FormGroupLabelProps extends CommonProps, TypographyProps, Omit<React.HTMLAttributes<HTMLLabelElement>, 'color'> { | |
htmlFor?: string | |
} | |
export interface FormGroupLabelProps extends CommonProps, TypographyProps, Omit<React.ComponentPropsWithRef<'label'>, 'color'> {} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'> {}
FormGroup.Label
supports ahtmlFor
prop in the docs to associated the label with a text input: https://primer.style/components/FormGroup#formgrouplabelHowever 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
index.d.ts
) if necessaryTake a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.