정의하고싶은 것
- 이미 정의된 Input Component Props에 필요한 타입 추가하기
- React에서 정의한
interface
에서 가져오기
해결 방법
HTMLInputElement
를 cmd + 클릭
해보면 아래와 같이 react에서 input 태그에 대한 속성, 메소드 등이 정의되어 있는 걸 볼 수 있다.
interface HTMLInputElement extends HTMLElement {
accept: string;
align: string;
alt: string;
autocomplete: string;
capture: string;
checked: boolean;
defaultChecked: boolean;
defaultValue: string;
dirName: string;
disabled: boolean;
files: FileList | null;
readonly form: HTMLFormElement | null;
... 생략
- 정의된 속성 중 원하는 속성을 가져와서 props로 정의하면 된다.
interface InputProps {
css: CSS
size: keyof typeof sizeVariants.inner & keyof typeof sizeVariants.outer
isDisabled: boolean
placeholder: string
leftSocket: ReactNode
rightSocket: ReactNode
onChange: (event: ChangeEvent<HTMLInputElement>) => void
value: string | number
type: HTMLInputElement['type']
multiple: HTMLInputElement['multiple']
accept: HTMLInputElement['accept']
}
[input type=file]
에 id 필드를 추가하고 싶은데 HTMLInputElement
에는 id가 정의되어 있지 않고 HTMLAttributes
에서 확인이 가능하다. HTMLAttributes
를 cmd + 클릭
해보면 아래와 같이 정의되어 있다.
interface HTMLAttributes<T> extends AriaAttributes, DOMAttributes<T> {
defaultChecked?: boolean | undefined;
defaultValue?: string | number | ReadonlyArray<string> | undefined;
suppressContentEditableWarning?: boolean | undefined;
suppressHydrationWarning?: boolean | undefined;
accessKey?: string | undefined;
className?: string | undefined;
contentEditable?: Booleanish | "inherit" | undefined;
contextMenu?: string | undefined;
dir?: string | undefined;
draggable?: Booleanish | undefined;
hidden?: boolean | undefined;
id?: string | undefined;
lang?: string | undefined;
placeholder?: string | undefined;
slot?: string | undefined;
spellCheck?: Booleanish | undefined;
style?: CSSProperties | undefined;
tabIndex?: number | undefined;
... 생략
- id도 추가로 정의하면 아래와 같이 정의할 수 있다.
interface InputProps {
css: CSS
size: keyof typeof sizeVariants.inner & keyof typeof sizeVariants.outer
isDisabled: boolean
placeholder: string
leftSocket: ReactNode
rightSocket: ReactNode
onChange: (event: ChangeEvent<HTMLInputElement>) => void
value: string | number
type: HTMLInputElement['type']
multiple: HTMLInputElement['multiple']
accept: HTMLInputElement['accept']
id: HTMLAttributes<HTMLInputElement>['id']
}