I have an input component that can either render a textarea component (from a library) or a regular input. This is the component:
import React, { useEffect, useRef, useState } from 'react'
import './AppInput.css'
interface Props {
placeholder: string,
value: string | number,
setValue: React.Dispatch<React.SetStateAction<string>>,
text: string,
shouldFocusOnKey: boolean,
type: string,
name: string,
className: string,
Component: string
}
export const AppInput: React.FC<Props> = (props) => {
const {
placeholder,
value,
setValue,
text,
shouldFocusOnKey,
type = 'text',
name,
className,
Component='input' } = props
let inputRef = useRef<HTMLInputElement>(null)
useEffect(() => {
if (shouldFocusOnKey) {
window.onkeydown = (e) => {
if (e.ctrlKey && e.key === 'm') {
inputRef.current?.focus()
}
}
}
}, [shouldFocusOnKey])
return (
<div className={`appinput ${className}`}>
<span>{text}</span>
<div className="flexcol">
<Component
ref={inputRef}
placeholder={placeholder}
value={value}
onChange={(e) => {
setValue(e.target.value)
}}
type={type}
name={name} />
</div>
</div>
)
}
In the case that Component === 'ReactTextareaAutosize' Then I want to render that component as depicted.This works perfectly well in ReactJS. But when using ReactTS I get type errors etc as typescript does not understand what I am trying to do or doing. Any idea how I can solve this?