hensei-web/components/common/Input/index.tsx
Justin Edmund fd6e788c4d Update input
Make a consistent height with select triggers and fix props
2023-06-30 12:28:36 -07:00

84 lines
2.1 KiB
TypeScript

import React, { useEffect, useState } from 'react'
import classNames from 'classnames'
import styles from './index.module.scss'
interface Props extends React.ComponentProps<'input'> {
bound?: boolean
hide1Password?: boolean
showCounter?: boolean
}
const defaultProps = {
bound: false,
hide1Password: true,
showCounter: false,
}
const Input = React.forwardRef<HTMLInputElement, Props>(function input(
{ value, bound, showCounter, ...props }: Props,
forwardedRef
) {
// States
const [currentCount, setCurrentCount] = useState(() =>
props.maxLength ? props.maxLength - (`${value}` || '').length : 0
)
// Classes
const wrapperClasses = classNames(
{
[styles.wrapper]: true,
[styles.accessory]: showCounter,
[styles.input]: showCounter,
[styles.bound]: showCounter && bound,
},
props.className?.split(' ').map((className) => styles[className])
)
const inputClasses = classNames(
{
[styles.input]: !showCounter,
[styles.bound]: !showCounter && bound,
},
!showCounter &&
props.className?.split(' ').map((className) => styles[className])
)
const { defaultValue, hide1Password, ...inputProps } = props
// Hooks
useEffect(() => {
if (props.maxLength)
setCurrentCount(props.maxLength - (`${value}` || '').length)
}, [props.maxLength, value])
// Event handlers
function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
if (props.onChange) props.onChange(event)
}
// Rendering
return (
<div className={wrapperClasses}>
<input
{...inputProps}
data-1p-ignore={props.hide1Password}
autoComplete={props.autoComplete}
className={inputClasses}
type={props.type}
name={props.name}
placeholder={props.placeholder}
value={value || ''}
onBlur={props.onBlur}
onChange={handleChange}
maxLength={props.maxLength}
ref={forwardedRef}
formNoValidate
/>
<span className={styles.counter}>{currentCount}</span>
</div>
)
})
Input.defaultProps = defaultProps
export default Input