Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185596
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T02:10:16+00:00 2022-06-10T02:10:16+00:00

javascript – How to import existing component and add/update props on top of it to create new component?

  • 0

[ad_1]

I have existing component called TextField. I would like to use this component as base to create NumberField component. Reason is I don’t want to type out exact same code when there are only minor tweaks in the NumberField component. There are 3 parts that I would like to change:

  1. For interface, TextField has type="text" | 'search', but I would like to update it to type="text" for NumberField component.
  2. I would like to add new prop inputMode="numeric" for NumberField.
  3. I would like to update handleChange function for onChange prop.

So I have coded up the following.

TextField.tsx

import React, { useState } from "react";

export interface ITextFieldProps {
  type?: "text" | "search";
  onChange?: (value: string) => void;
  required?: boolean;
}

export const TextField: React.FunctionComponent<ITextFieldProps> = React.forwardRef<
  HTMLInputElement, ITextFieldProps>((props, ref) => {
  const { type = "text", onChange, required = true } = props;
  const [value, setValue] = useState("");

  function handleChange(e: React.FormEvent<HTMLInputElement>) {
    setValue(e.currentTarget.value);
    onChange?.(e.currentTarget.value);
  }

  return (
    <input
      type={type}
      value={value}
      onChange={handleChange}
      required={required}
    />
  );
});

NumberField.tsx

import React, { useState } from "react";
import { ITextFieldProps, TextField } from "./TextField";

export interface INumberFieldProps extends ITextFieldProps {
  type?: "text";
  inputMode?: "numeric";
}

export const NumberField: React.FunctionComponent<INumberFieldProps> = React.forwardRef<
  HTMLInputElement, INumberFieldProps>((props, ref) => {
  const { inputMode = "numeric", onChange } = props;
  const [value, setValue] = useState("");

  function handleChange(e: React.FormEvent<HTMLInputElement>) {
    const updatedValue = e.currentTarget.value.replace(/\D/g, "");
    setValue(updatedValue);
    onChange?.(updatedValue);
  }

  return (
    <TextField inputMode={inputMode} onChange={handleChange} value={value} />
  );
});

App.tsx

import "./styles.css";
import { TextField } from "./TextField";
import { NumberField } from "./NumberField";

export default function App() {
  function onChange(e: any) {
    console.log(e);
  }
  return (
    <div className="App">
      <NumberField onChange={(e) => onChange(e)} />
    </div>
  );
}

With this code when I go to chrome dev tools and check input element, I only see <input type="text" />, but I expect to see <input type="text" inputmode="numeric" />, since I added inputMode prop. Also with regex in the handleChange function, I should be able to only type numbers in the input, but I get “Cannot read properties of undefined (reading ‘value’)” error. What’s the best way to solve this issue? Source: https://codesandbox.io/s/numberfield-component-ts-wohlok?file=/src/NumberField.tsx

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 396 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 370 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 361 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.