
* feat: initial validator component * chore: fix eslint for validator * chore: update package info for validator * chore: remove vscode settings for docs * chore: put docs and demo into apps * chore: move package scope @astro-reactive * test: update tests for validator * feat: validator functions, hooks * feat: validator sets haserrors attribute * feat: use data-validator attributes * feat: showValidationHints * feature: add logic for all validators * refactor: remove Validator component usage * docs(validator): initial readme * chore: comment out unsupported validator * docs(validator): update installation * chore: package docs and publish * chore: update deps * docs: update npm info on docs * docs(validator): update docs for validator * fix(form): handle undefined form * test(validator): update tests * chore: organize files; update deps * chore: fix build scripts
69 lines
1.5 KiB
Text
69 lines
1.5 KiB
Text
---
|
|
import type { FormControl } from '../core/form-control';
|
|
|
|
export interface Props {
|
|
control: FormControl;
|
|
showValidationHints: boolean;
|
|
}
|
|
|
|
const { control, showValidationHints } = Astro.props;
|
|
|
|
const { validators = [] } = control;
|
|
|
|
const isRequired = showValidationHints && validators.includes('validator-required');
|
|
|
|
const validatorAttirbutes: 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,
|
|
};
|
|
},
|
|
|
|
{}
|
|
);
|
|
---
|
|
|
|
<div class="field" data-validator-hints={showValidationHints.toString()}>
|
|
{
|
|
control.label && control.labelPosition === 'left' && (
|
|
<label for={control.name}>
|
|
{isRequired && <span>*</span>}
|
|
{control.label}
|
|
</label>
|
|
)
|
|
}
|
|
|
|
<input
|
|
name={control.name}
|
|
id={control.name}
|
|
type={control.type}
|
|
value={control.value}
|
|
checked={control.value === 'checked'}
|
|
placeholder={control.placeholder}
|
|
data-label={control.label}
|
|
data-label-position={control.labelPosition}
|
|
{...validatorAttirbutes}
|
|
/>
|
|
|
|
{
|
|
control.label && control.labelPosition === 'right' && (
|
|
<label for={control.name}>
|
|
{isRequired && <span>*</span>}
|
|
{control.label}
|
|
</label>
|
|
)
|
|
}
|
|
</div>
|
|
|
|
<style>
|
|
[data-validator-hints='true'][data-validator-haserrors='true'],
|
|
[data-validator-hints='true'] [data-validator-haserrors='true'] {
|
|
color: red;
|
|
border-color: red;
|
|
}
|
|
</style>
|