Socialify

Folder ..

Viewing textAreaWrapper.tsx
59 lines (51 loc) • 1.4 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { useEffect, useState } from 'react'

import { useDebouncedCallback } from 'use-debounce'

import ConfigType from '../../../common/types/configType'

type TextAreaProps = {
  title?: string
  alt?: string
  value: string
  placeholder?: string
  keyName: keyof ConfigType
  handleChange: (value: any, key: keyof ConfigType) => void
  disabled?: boolean
}

const TextAreaWrapper = ({
  title,
  alt,
  keyName,
  value,
  placeholder,
  handleChange,
  disabled
}: TextAreaProps) => {
  const [internalValue, setInternalValue] = useState(value)

  const debounced = useDebouncedCallback((value) => {
    handleChange({ value, editable: true, state: true }, keyName)
  }, 500)

  const processChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
    setInternalValue(e.target.value)
    debounced(e.target.value)
  }

  useEffect(() => {
    setInternalValue(value)
  }, [value])

  return (
    <div className="form-control" style={{ display: 'none' }}>
      {title && (
        <label className="label">
          <span className="label-text">{title}</span>
          {alt && <span className="label-text-alt">{alt}</span>}
        </label>
      )}
      <textarea
        className="textarea textarea-bordered h-20"
        value={internalValue}
        onChange={processChange}
        disabled={disabled}
        placeholder={placeholder}
      />
    </div>
  )
}
export default TextAreaWrapper