File size: 1,853 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
import { Field, Label, Textarea } from '@headlessui/react';
import { ExclamationCircleIcon } from '@heroicons/react/24/outline';
import { ChangeEvent, useState } from 'react';

type TextAreaEditorProps = {
  label: string;
  rows: number;
  value: string | string[];
  id: string;
  name: string;
  placeholder: string;
  onChange: (content: string) => void;
  requiredAlert?: boolean;
  requiredField?: boolean;
};

const TextArea: React.FC<TextAreaEditorProps> = ({
  label,
  rows,
  value,
  id,
  name,
  placeholder,
  onChange,
  requiredAlert = false,
  requiredField = false,
}) => {
  const [isFocused, setIsFocused] = useState(false);

  return (
    <div className="w-full rounded-md border-0 placeholder:text-gray-400 sm:text-sm sm:leading-6">
      <Field aria-required={requiredField}>
        <Label className="block font-medium leading-6 text-gray-300">
          {label + ' '}
          {requiredField ? <span className="text-red-500 text-lg">*</span> : ''}
        </Label>
        <div className="relative">
          <Textarea
            className={`${requiredAlert && 'empty:ring empty:ring-red-500 empty:ring-1'} bg-gray-800 w-full mt-2 p-2 rounded-lg shadow-sm`}
            id={id}
            name={name}
            onBlur={() => setIsFocused(false)}
            onChange={(e: ChangeEvent<HTMLTextAreaElement>) =>
              onChange(e.target.value)
            }
            onFocus={() => setIsFocused(true)}
            placeholder={placeholder}
            rows={rows}
            value={value}
          />
          {!isFocused && requiredAlert && value === '' ? (
            <span className="absolute right-3 top-2 mt-2 ml-2 text-red-500">
              <ExclamationCircleIcon className="size-5" />
            </span>
          ) : null}
        </div>
      </Field>
    </div>
  );
};

export default TextArea;