Skip to content

Reusable Field component best practices #11939

Answered by giorgio324
giorgio324 asked this question in General
Discussion options

You must be logged in to vote

ok so this is how I rewrote the component

import { useId, forwardRef } from 'react';

interface Props {
  label: string;
  type?: 'text' | 'email' | 'password';
  error?: string;
  placeholder?: string;
}

const UncontrolledInputElement = forwardRef<HTMLInputElement, Props>(
  ({ label, error, placeholder, type, ...props }, ref) => {
    const id = useId();
    return (
      <div className='flex flex-col gap-4 border p-2'>
        <label className='font-bold' id={id}>
          {label}
        </label>
        <input
          type={type}
          placeholder={placeholder}
          id={id}
          ref={ref}
          {...props}
        />
        {error && <span className='text-red-5…

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
2 replies
@giorgio324
Comment options

@giorgio324
Comment options

Answer selected by giorgio324
Comment options

You must be logged in to vote
4 replies
@Phibee
Comment options

@giorgio324
Comment options

@crazytweeks
Comment options

@BrunoBorges12
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants