0

How can I define input attributes in typescript? I have an AddUser Component and TextInput Component, I want to import the TextInput component inside the AddUser component and then pass props to the TextInput component.

AddUser.tsx

import Button from '../Shared/Form/Button/Button';
import Form from '../Shared/Form/Form';
import TextArea from '../Shared/Form/TextArea/TextArea';
import TextInput from '../Shared/Form/TextInput/TextInput';

const AddUser = () => {
    return (
        <div>
            <h1>Add User</h1>
            <Form method="post" className={'user-form'}>
                <TextInput label={'Name'} type="text" name="name" required />
                <TextInput label={'Email'} type="email" name="email" required />
                <TextInput label={'Country'} type="text" name="country" required />
                <TextInput label={'Phone'} type="text" name="phone" required />
                <TextArea label={'Address'} name="address" cols="30" rows="4" />
                <div className="form-button">
                    <Button type={'submit'} className={'btn-add'}>
                        Add
                    </Button>
                    <Button type={'submit'} className={'btn-close'}>
                        Cancel
                    </Button>
                </div>
            </Form>
        </div>
    );
};

export default AddUser;

TextInput.tsx

const TextInput = ({ className, label, ...rest }: { className: string; label: string }) => {
    return (
        <div className={`${className} form-field`}>
            <label htmlFor={label}>{label}</label>
            <input {...rest} />
        </div>
    );
};

export default TextInput;

2 Answers 2

2

You can extend HTMLProps which comes with react:

import { HTMLProps } from "react";

interface MyCOmponentProps extends HTMLProps<HTMLInputElement> {
  {...}
}
Sign up to request clarification or add additional context in comments.

Comments

0

This is how we can make a reusable component. maybe I missed something in onChangeAction according to type script. please let me know me in the comment section so that i can help you better

Example codesandbox

const AddUser = () => {
  return (
    <div>
      <h1>Add User</h1>
      <form method="post" className={"user-form"}>
        <TextInput
          label={"Name"}
          placeholder="Name"
          type="text"
          name="name"
          required
        />
        <TextInput
          label={"Email"}
          placeholder="Email"
          type="email"
          name="email"
          required
        />
        <TextInput
          label={"Country"}
          placeholder="Country"
          type="text"
          name="country"
          required
        />
        <TextInput
          label={"Phone"}
          placeholder="Phone"
          type="text"
          name="phone"
          required
        />
      </form>
    </div>
  );
};

export default AddUser;

const TextInput = ({
  className,
  label,
  value,
  type,
  onChangeAction,
  placeholder
}) => {
  return (
    <div className={`${className} form-field`}>
      <label htmlFor={label}>{label}</label>
      <input
        placeholder={placeholder || "Text"}
        type={type || "text"}
        value={value}
        onChange={onChangeAction}
      />
    </div>
  );
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.