Код IT
← Каталог

TypeScript и React — Типизация UI-компонентов

Фрагмент из «TypeScript и React»: Типизация UI-компонентов.

Plain text main.tsx
type TextFieldProps = {
  label: string;
  error?: string;
} & React.InputHTMLAttributes<HTMLInputElement>;

function TextField({ label, error, id, ...inputProps }: TextFieldProps) {
  const autoId = React.useId();
  const fieldId = id ?? autoId;
  return (
    <div className="form-group">
      <label htmlFor={fieldId}>{label}</label>
      <input
        id={fieldId}
        className={error ? 'input input--error' : 'input'}
        aria-invalid={error ? true : undefined}
        aria-describedby={error ? `${fieldId}-err` : undefined}
        {...inputProps}
      />
      {error ? (
        <span id={`${fieldId}-err`} className="input-hint error" role="alert">
          {error}
        </span>
      ) : null}
    </div>
  );
}
type TextFieldProps = {
  label: string;
  error?: string;
} & React.InputHTMLAttributes<HTMLInputElement>;

function TextField({ label, error, id, ...inputProps }: TextFieldProps) {
  const autoId = React.useId();
  const fieldId = id ?? autoId;
  return (
    <div className="form-group">
      <label htmlFor={fieldId}>{label}</label>
      <input
        id={fieldId}
        className={error ? 'input input--error' : 'input'}
        aria-invalid={error ? true : undefined}
        aria-describedby={error ? `${fieldId}-err` : undefined}
        {...inputProps}
      />
      {error ? (
        <span id={`${fieldId}-err`} className="input-hint error" role="alert">
          {error}
        </span>
      ) : null}
    </div>
  );
}