astro-reactive-form/packages/form/components/controls/Input.astro
Alexander Samaniego 93a8d49f0a
feat(form): implement unique IDs (#182)
* Added Short Unique ID npm library

* Added unique ID to form-control

* Added unique ID to form-group

* Unique ID added to <form>

* Added unique IDs to control components

* Added IDs to label and fieldset

* Update Form.astro with requested changes

Co-authored-by: Ayo Ayco <ayo@ayco.io>

* Adjustments for requested changes.

Co-authored-by: Ayo Ayco <ayo@ayco.io>
2022-11-08 08:53:22 +01:00

43 lines
1.1 KiB
Text

---
/**
* DEFAULT INPUT COMPONENT
*/
import type { InputType } from '@astro-reactive/common';
import type { FormControl } from '../../core/form-control';
export interface Props {
control: FormControl;
readOnly?: boolean;
}
const { control, readOnly } = Astro.props;
const { validators = [] } = control;
const hasErrors: boolean = !!control.errors?.length;
const validatorAttributes: Record<string, string> = validators?.reduce((prev, validator) => {
const split: string[] = validator.split(':');
const label: string = `data-${split[0]}` || 'invalid';
const value: string | null = split.length > 1 ? split[1] ?? null : 'true';
return {
...prev,
[label]: value,
};
}, {});
---
<input
name={control.name}
id={control.id}
type={control.type as InputType}
value={control.value?.toString()}
checked={control.value === 'checked'}
placeholder={control.placeholder}
data-label={control.label}
data-validator-haserrors={hasErrors.toString()}
readonly={readOnly || null}
disabled={(readOnly || null) && control.type === 'checkbox'}
{...validatorAttributes}
/>