-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAltTextField.tsx
More file actions
53 lines (42 loc) · 1.72 KB
/
AltTextField.tsx
File metadata and controls
53 lines (42 loc) · 1.72 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
'use client'
import type { TextareaFieldClientProps } from 'payload'
import { FieldLabel, TextareaInput, useDocumentInfo, useField } from '@payloadcms/ui'
import { matchesMimeType } from '../utilities/mimeTypes.js'
import { GenerateAltTextButton } from './GenerateAltTextButton.js'
export const AltTextField = (clientProps: TextareaFieldClientProps) => {
const { field, path } = clientProps
const supportedMimeTypes = field.admin?.custom?.supportedMimeTypes as string[] | undefined
const trackedMimeTypes = field.admin?.custom?.trackedMimeTypes as string[] | undefined
const { setValue, value } = useField<string>({ path })
const { id } = useDocumentInfo()
const { value: mimeType } = useField<string>({ path: 'mimeType' })
const isTrackedMimeType =
!trackedMimeTypes ||
trackedMimeTypes.length === 0 ||
(!!mimeType && matchesMimeType(mimeType, trackedMimeTypes))
if (!isTrackedMimeType) {
return null
}
// the field should be optional when the document is created
// (since the alt text generation can only be used once the document is created and the image uploaded)
const required = id ? field.required : false
return (
<div className="field-type textarea" style={{ flex: '1 1 auto' }}>
<FieldLabel
htmlFor={`field-${path}`}
label={field.label}
localized={field.localized}
required={required}
/>
<div className="field-type__wrap">
<TextareaInput
AfterInput={<GenerateAltTextButton supportedMimeTypes={supportedMimeTypes} />}
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => setValue(e.target.value)}
path={path}
required={required}
value={value}
/>
</div>
</div>
)
}