| 1 |
import { Description, Field, Label, Textarea } from '@headlessui/react'; |
| 2 |
import { Button } from '@wordpress/components'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
import { close, Icon } from '@wordpress/icons'; |
| 5 |
|
| 6 |
export const CustomTextarea = ({ |
| 7 |
placeholder, |
| 8 |
className, |
| 9 |
title, |
| 10 |
onChange, |
| 11 |
value, |
| 12 |
description = null, |
| 13 |
required = false, |
| 14 |
hideEditor = false, |
| 15 |
setHideEditor = () => {}, |
| 16 |
id, |
| 17 |
}) => { |
| 18 |
const handleEditClick = (e) => { |
| 19 |
e.preventDefault(); |
| 20 |
setHideEditor(false); |
| 21 |
}; |
| 22 |
|
| 23 |
return ( |
| 24 |
<Field id={id} className="p-3"> |
| 25 |
<Label |
| 26 |
as="h4" |
| 27 |
className="mb-2 mt-0 text-base font-medium data-[disabled]:opacity-50" |
| 28 |
> |
| 29 |
{__(title, 'extendify-local')}{' '} |
| 30 |
{required && ( |
| 31 |
<span className="text-sm font-light text-wp-alert-red">*</span> |
| 32 |
)} |
| 33 |
</Label> |
| 34 |
|
| 35 |
{description && ( |
| 36 |
<Description className="data-[disabled]:opacity-50"> |
| 37 |
{__(description, 'extendify-local')} |
| 38 |
</Description> |
| 39 |
)} |
| 40 |
{hideEditor ? ( |
| 41 |
<div> |
| 42 |
<span className="line-clamp-4 opacity-80">{value}</span> |
| 43 |
<span className="link mt-1 flex w-full justify-end"> |
| 44 |
<Button variant="link" onClick={handleEditClick}> |
| 45 |
{__('Edit', 'extendify-local')} |
| 46 |
</Button> |
| 47 |
</span> |
| 48 |
</div> |
| 49 |
) : ( |
| 50 |
<div className="relative"> |
| 51 |
<Textarea |
| 52 |
name="description" |
| 53 |
placeholder={placeholder} |
| 54 |
value={value} |
| 55 |
onChange={onChange} |
| 56 |
className={className} |
| 57 |
/> |
| 58 |
{value ? ( |
| 59 |
<span className="absolute right-0 top-0 z-10 flex items-center pr-2 pt-2"> |
| 60 |
<button |
| 61 |
type="button" |
| 62 |
className="m-0 border-0 bg-transparent p-0 text-gray-500" |
| 63 |
aria-label={__('Clear', 'extendify-local')} |
| 64 |
onClick={() => { |
| 65 |
onChange({ currentTarget: { value: '' } }); |
| 66 |
}} |
| 67 |
> |
| 68 |
<Icon icon={close} size={16} /> |
| 69 |
</button> |
| 70 |
</span> |
| 71 |
) : null} |
| 72 |
</div> |
| 73 |
)} |
| 74 |
</Field> |
| 75 |
); |
| 76 |
}; |
| 77 |
|