| 1 |
import {FieldHasDescriptionProps} from '@givewp/forms/propTypes'; |
| 2 |
import autoCompleteAttr from '@givewp/forms/registrars/templates/fields/utils/autoCompleteAttr'; |
| 3 |
|
| 4 |
/** |
| 5 |
* @since 4.3.0 Add autoComplete support and aria-labelledby and aria-describedby attributes. |
| 6 |
*/ |
| 7 |
export default function Text({ |
| 8 |
Label, |
| 9 |
ErrorMessage, |
| 10 |
fieldError, |
| 11 |
description, |
| 12 |
placeholder, |
| 13 |
inputProps, |
| 14 |
}: FieldHasDescriptionProps) { |
| 15 |
const autoComplete = autoCompleteAttr(inputProps?.name); |
| 16 |
const FieldDescription = window.givewp.form.templates.layouts.fieldDescription; |
| 17 |
const baseId = `givewp-fields-text-${inputProps.name}`; |
| 18 |
const inputId = `${baseId}-input`; |
| 19 |
const labelId = `${baseId}-label`; |
| 20 |
const descriptionId = `${baseId}-description`; |
| 21 |
|
| 22 |
return ( |
| 23 |
<label |
| 24 |
className={fieldError && 'givewp-field-error-label'} |
| 25 |
htmlFor={inputId} |
| 26 |
> |
| 27 |
<span id={labelId}> |
| 28 |
<Label /> |
| 29 |
</span> |
| 30 |
{description && ( |
| 31 |
<span id={descriptionId}> |
| 32 |
<FieldDescription description={description} /> |
| 33 |
</span> |
| 34 |
)} |
| 35 |
|
| 36 |
<input |
| 37 |
id={inputId} |
| 38 |
type="text" |
| 39 |
aria-invalid={fieldError ? 'true' : 'false'} |
| 40 |
aria-labelledby={labelId} |
| 41 |
aria-describedby={description ? descriptionId : undefined} |
| 42 |
placeholder={placeholder} |
| 43 |
{...inputProps} |
| 44 |
autoComplete={autoComplete} |
| 45 |
/> |
| 46 |
|
| 47 |
<ErrorMessage /> |
| 48 |
</label> |
| 49 |
); |
| 50 |
} |
| 51 |
|