astro-reactive-form/packages/form/components/controls/RadioGroup.astro
Alexander Samaniego 5b539c809c
feat(form): implement FormControl and ControlConfig prop triggerValidationOn (#224)
* Added ValidationHooks and attribute to FormControl

* Event listener uses data attribute to set type

* Changed default event listener to blur

* Add validation data attribute to other components

* Adjusted querySelector to remove condition

* Added nullish operator to prevent error

* Added optional chaining to validator
2022-12-06 18:44:52 +07:00

41 lines
895 B
Text

---
/**
* RADIO GROUP COMPONENT
*/
import type { Radio, ControlOption } from '@astro-reactive/common';
export interface Props {
control: Radio;
readOnly?: boolean;
}
const { control, readOnly = false } = Astro.props;
const options = control.options.map((option: string | ControlOption) => {
if (typeof option === 'string') {
return {
label: option,
value: option,
};
}
return option;
});
---
{
options.map((option: ControlOption, index: number) => (
<div class="radio-option">
<input
type="radio"
id={control.id + '-' + index}
name={control.name}
value={option.value}
checked={option.value === control.value}
readonly={readOnly || null}
disabled={readOnly || null}
data-validation-on={control.triggerValidationOn ? control.triggerValidationOn : null}
/>
<label for={control.id + '-' + index}>{option.label}</label>
</div>
))
}