Skip to content

Commit

Permalink
fix: proper support for id attr overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
maxholman committed Jan 17, 2023
1 parent 11d9ec9 commit d8566c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/forms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const FormInput = forwardRef<HTMLInputElement, FormInputProps>(
},
ref,
) => {
const id = useId();
const id = useIdWithDefault(props.id);

const inputTypeProps = formInputProps(props);

Expand All @@ -157,11 +157,11 @@ export const FormInput = forwardRef<HTMLInputElement, FormInputProps>(
{description}
<input
ref={ref}
id={id}
className={clsx(formInputClassName, formInputNotCheckRadio)}
{...(props.readOnly && { tabIndex: -1 })}
{...inputTypeProps}
{...props}
id={id}
/>
{message && (
<Inline>
Expand Down Expand Up @@ -197,7 +197,7 @@ export const FormSelect: FC<FormSelectProps> = ({
message,
...props
}) => {
const id = useId();
const id = useIdWithDefault(props.id);
const isStringLike = useStringLikeDetector();

return (
Expand Down Expand Up @@ -248,17 +248,17 @@ const FormInputCheckRadio: FC<
>
>
> = ({ className, message, label, ...props }) => {
const id = useId();
const id = useIdWithDefault(props.id);
const isStringLike = useStringLikeDetector();

return (
<Block space="small" className={formInputCheckRadioWrapper}>
<input
id={id}
className={
props.type === 'radio' ? formInputRadioInput : formInputCheckboxInput
}
{...props}
id={id}
/>
{label &&
(isStringLike(label) ? (
Expand Down Expand Up @@ -400,7 +400,7 @@ export const FormTextArea = forwardRef<HTMLTextAreaElement, FormTextAreaProps>(
},
ref,
) => {
const id = useId();
const id = useIdWithDefault(props.id);

return (
<Block className={className} space="small">
Expand All @@ -416,10 +416,10 @@ export const FormTextArea = forwardRef<HTMLTextAreaElement, FormTextAreaProps>(
{description}
<textarea
ref={ref}
id={id}
className={clsx(formInputClassName, formInputNotCheckRadio)}
{...(props.readOnly && { tabIndex: -1 })}
{...props}
id={id}
/>
{message && (
<Inline>
Expand Down
6 changes: 6 additions & 0 deletions lib/hooks/use-id-with-default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useId } from 'react';

export function useIdWithDefault(id: string | undefined): string {
const generatedId = useId();
return id || generatedId;
}

0 comments on commit d8566c2

Please sign in to comment.