|
| 1 | +import { |
| 2 | + ChangeEvent, |
| 3 | + ChangeEventHandler, |
| 4 | + CSSProperties, |
| 5 | + ReactNode, |
| 6 | + useState |
| 7 | +} from 'react' |
| 8 | +import { Flex } from 'components/Layout' |
| 9 | +import Text from 'components/Typography' |
| 10 | +import BaseInput from '../Input/BaseInput' |
| 11 | +import TextArea from '../TextArea' |
| 12 | +import FileItem from './FileItem' |
| 13 | + |
| 14 | +export interface FileImageItemProps { |
| 15 | + /** |
| 16 | + * Source URL for the image preview |
| 17 | + */ |
| 18 | + src: string |
| 19 | + /** |
| 20 | + * Title of the file |
| 21 | + */ |
| 22 | + title: string |
| 23 | + /** |
| 24 | + * Description of the file |
| 25 | + */ |
| 26 | + description: string |
| 27 | + /** |
| 28 | + * Placeholder text for the title input |
| 29 | + */ |
| 30 | + titlePlaceholder?: string |
| 31 | + /** |
| 32 | + * Placeholder text for the description input |
| 33 | + */ |
| 34 | + descriptionPlaceholder?: string |
| 35 | + /** |
| 36 | + * Label for the title input |
| 37 | + */ |
| 38 | + titleLabel?: string |
| 39 | + /** |
| 40 | + * Label for the description input |
| 41 | + */ |
| 42 | + descriptionLabel?: string |
| 43 | + /** |
| 44 | + * Maximum length for the description text |
| 45 | + */ |
| 46 | + descriptionMaxLength?: number |
| 47 | + /** |
| 48 | + * Name of the file to display |
| 49 | + */ |
| 50 | + name: string |
| 51 | + /** |
| 52 | + * Type/format of the file |
| 53 | + */ |
| 54 | + type: string |
| 55 | + /** |
| 56 | + * Size of the file in KB (optional) |
| 57 | + */ |
| 58 | + fileSize?: number |
| 59 | + /** |
| 60 | + * Additional CSS class names |
| 61 | + */ |
| 62 | + className?: string |
| 63 | + /** |
| 64 | + * Custom CSS styles |
| 65 | + */ |
| 66 | + style?: CSSProperties |
| 67 | + /** |
| 68 | + * Children components (typically action buttons) |
| 69 | + */ |
| 70 | + children?: ReactNode |
| 71 | + /** |
| 72 | + * Handler for title changes |
| 73 | + */ |
| 74 | + onChangeTitle: ChangeEventHandler<HTMLInputElement> |
| 75 | + /** |
| 76 | + * Handler for description changes |
| 77 | + */ |
| 78 | + onChangeDescription: ChangeEventHandler<HTMLTextAreaElement> |
| 79 | +} |
| 80 | + |
| 81 | +interface stateType { |
| 82 | + title: string |
| 83 | + description: string |
| 84 | +} |
| 85 | + |
| 86 | +export const FileImageItem = ({ |
| 87 | + name, |
| 88 | + type, |
| 89 | + fileSize, |
| 90 | + src, |
| 91 | + title, |
| 92 | + description, |
| 93 | + titlePlaceholder, |
| 94 | + descriptionPlaceholder, |
| 95 | + titleLabel, |
| 96 | + descriptionLabel, |
| 97 | + descriptionMaxLength = 100, |
| 98 | + className, |
| 99 | + style, |
| 100 | + children, |
| 101 | + onChangeTitle, |
| 102 | + onChangeDescription |
| 103 | +}: FileImageItemProps) => { |
| 104 | + const [state, setState] = useState<stateType>({ |
| 105 | + title: title || '', |
| 106 | + description: description || '' |
| 107 | + }) |
| 108 | + |
| 109 | + const handleChange = ( |
| 110 | + e: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLTextAreaElement> |
| 111 | + ) => { |
| 112 | + setState((prev) => ({ |
| 113 | + ...prev, |
| 114 | + [e.target.name]: e.target.value |
| 115 | + })) |
| 116 | + e.target.name === 'title' && |
| 117 | + onChangeTitle(e as ChangeEvent<HTMLInputElement>) |
| 118 | + e.target.name === 'description' && |
| 119 | + onChangeDescription(e as ChangeEvent<HTMLTextAreaElement>) |
| 120 | + } |
| 121 | + |
| 122 | + return ( |
| 123 | + <Flex |
| 124 | + role="file-item-image" |
| 125 | + className="flex-col bg-white rounded-lg pb-2" |
| 126 | + gap="2" |
| 127 | + > |
| 128 | + <FileItem |
| 129 | + name={name} |
| 130 | + type={type} |
| 131 | + fileSize={fileSize} |
| 132 | + className={className} |
| 133 | + style={style} |
| 134 | + > |
| 135 | + {children} |
| 136 | + </FileItem> |
| 137 | + <div |
| 138 | + className="px-4 w-full h-full max-h-[164px] grid gap-4" |
| 139 | + style={{ |
| 140 | + gridTemplateColumns: '350px 1fr' |
| 141 | + }} |
| 142 | + > |
| 143 | + <figure |
| 144 | + className="w-full border border-gray-100 rounded-lg overflow-hidden" |
| 145 | + style={{ maxWidth: '350px', height: '164px' }} |
| 146 | + > |
| 147 | + <img |
| 148 | + role="file-image" |
| 149 | + src={src} |
| 150 | + alt="File" |
| 151 | + className="w-full h-full object-cover" |
| 152 | + /> |
| 153 | + </figure> |
| 154 | + <Flex className="flex-col" gap="4"> |
| 155 | + <BaseInput |
| 156 | + name="title" |
| 157 | + style={{ marginTop: 0 }} |
| 158 | + label={titleLabel} |
| 159 | + placeholder={titlePlaceholder} |
| 160 | + value={state.title} |
| 161 | + onChange={handleChange} |
| 162 | + /> |
| 163 | + <Flex className="flex-col gap-2"> |
| 164 | + <TextArea |
| 165 | + name="description" |
| 166 | + maxLength={descriptionMaxLength} |
| 167 | + label={descriptionLabel} |
| 168 | + placeholder={descriptionPlaceholder} |
| 169 | + value={state.description} |
| 170 | + onChange={handleChange} |
| 171 | + /> |
| 172 | + <Text |
| 173 | + role="file-description-length" |
| 174 | + size="xs" |
| 175 | + textMuted500 |
| 176 | + className="text-right" |
| 177 | + > |
| 178 | + {state.description?.length || 0}/{descriptionMaxLength} |
| 179 | + </Text> |
| 180 | + </Flex> |
| 181 | + </Flex> |
| 182 | + </div> |
| 183 | + </Flex> |
| 184 | + ) |
| 185 | +} |
| 186 | + |
| 187 | +export default FileImageItem |
0 commit comments