add hw 14-15 for design

This commit is contained in:
neko
2022-11-08 14:01:28 +03:00
parent b25f4b4f5b
commit 7a91352d47
7 changed files with 362 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import React, {DetailedHTMLProps, InputHTMLAttributes, ReactNode, useState} from 'react'
import SuperInputText from '../../../hw04/common/c1-SuperInputText/SuperInputText'
// тип пропсов обычного инпута
type DefaultInputPropsType = DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>,
HTMLInputElement>
// здесь мы говорим что у нашего инпута будут такие же пропсы как у обычного инпута, кроме type
// (чтоб не писать value: string, onChange: ...; они уже все описаны в DefaultInputPropsType)
export type SuperDebouncedInputPropsType = Omit<DefaultInputPropsType, 'type'> & {
// и + ещё пропсы которых нет в стандартном инпуте
onChangeText?: (value: string) => void
onEnter?: () => void
error?: ReactNode
spanClassName?: string
} // илм экспортировать тип SuperInputTextPropsType
& { // плюс специальный пропс SuperPagination
onDebouncedChange?: (value: string) => void
}
const SuperDebouncedInput: React.FC<SuperDebouncedInputPropsType> = (
{
onChangeText,
onDebouncedChange,
...restProps // все остальные пропсы попадут в объект restProps
}
) => {
const [timerId, setTimerId] = useState<number | undefined>(undefined)
const onChangeTextCallback = (value: string) => {
onChangeText?.(value)
if (onDebouncedChange) {
// делает студент
timerId && clearTimeout(timerId)
const id = +setTimeout(() => {
onDebouncedChange(value)
setTimerId(undefined)
}, 1500)
setTimerId(id)
//
}
}
return (
<SuperInputText onChangeText={onChangeTextCallback} {...restProps}/>
)
}
export default SuperDebouncedInput