webapp/src/components/_shared/GrowingTextarea/GrowingTextarea.tsx
Ilya Y cd83807204
Feature/audio upload (#120)
* Audio upload 
* Audio player Article View
2023-07-14 16:06:21 +03:00

61 lines
1.8 KiB
TypeScript

import { clsx } from 'clsx'
import styles from './GrowingTextarea.module.scss'
import { createSignal, Show } from 'solid-js'
type Props = {
class?: string
placeholder: string
initialValue?: string
value: (string) => void
maxLength?: number
allowEnterKey: boolean
}
export const GrowingTextarea = (props: Props) => {
const [value, setValue] = createSignal<string>(props.initialValue ?? '')
const [isFocused, setIsFocused] = createSignal(false)
const handleChangeValue = (event) => {
setValue(event.target.value)
}
const handleKeyDown = async (event) => {
if (event.key === 'Enter' && event.shiftKey) {
return
}
if (event.key === 'Enter' && !event.shiftKey && value()?.trim().length > 0) {
event.preventDefault()
}
}
return (
<div class={clsx(styles.GrowingTextarea)}>
<div class={clsx(styles.growWrap, props.class)} data-replicated-value={value()}>
<textarea
rows={1}
maxlength={props.maxLength}
autocomplete="off"
class={clsx(styles.textInput, props.class)}
value={props.initialValue}
onKeyDown={props.allowEnterKey ? handleKeyDown : null}
onInput={(event) => handleChangeValue(event)}
onChange={(event) => props.value(event.target.value)}
placeholder={props.placeholder}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
/>
</div>
<Show when={props.maxLength && value() && isFocused()}>
<div
class={clsx(styles.maxLength, {
[styles.visible]: isFocused(),
[styles.limited]: value().length === props.maxLength
})}
>
{`${value().length} / ${props.maxLength}`}
</div>
</Show>
</div>
)
}