@sixbell-telco/sdk
Version:
A collection of reusable components designed for use in Sixbell Telco Angular projects
176 lines (172 loc) • 6.72 kB
JavaScript
import * as i0 from '@angular/core';
import { input, model, output, computed, ChangeDetectionStrategy, Component } from '@angular/core';
import { FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms';
import { cn } from '@sixbell-telco/sdk/utils/cn';
import { cva } from 'class-variance-authority';
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* @internal
* Generates base radio button classes with style variants
*/
const radioComponent = cva(['radio', 'font-body'], {
variants: {
variant: {
primary: ['radio-primary'],
secondary: ['radio-secondary'],
tertiary: ['radio-tertiary'],
accent: ['radio-accent'],
success: ['radio-success'],
warning: ['radio-warning'],
info: ['radio-info'],
error: ['radio-error'],
},
size: {
xs: ['radio-xs'],
sm: ['radio-sm'],
md: ['radio-md'],
lg: ['radio-lg'],
xl: ['radio-xl'],
},
},
compoundVariants: [],
defaultVariants: {
variant: 'primary',
size: 'md',
},
});
/**
* A customizable radio button component with form integration
*
* @remarks
* Implements ControlValueAccessor for seamless Angular form integration.
* Supports grouped radio buttons through name attribute binding.
*
* @example
* ```html
* <!-- Basic usage -->
* <st-radio value="option1" [(selected)]="selectedOption"></st-radio>
* ```
*
* @example
* ```html
* <!-- Grouped radio buttons with labels -->
* <st-radio
* variant="success"
* size="lg"
* label="Option 1"
* value="opt1"
* name="options"
* [(selected)]="selectedOption"
* ></st-radio>
* <st-radio
* variant="success"
* size="lg"
* label="Option 2"
* value="opt2"
* name="options"
* [(selected)]="selectedOption"
* ></st-radio>
* ```
*/
class RadioComponent {
/**
* Radio button color variant
* @defaultValue 'primary'
*/
variant = input('primary');
/**
* Radio button size variant
* @defaultValue 'md'
*/
size = input('md');
/**
* Label text displayed next to the radio button
*/
label = input('');
/**
* Group name for radio button selection
*/
name = input(null);
/**
* The value associated with this radio button
*/
value = input.required();
/**
* Two-way bindable selected value for radio group
*/
selected = model();
/**
* Event emitted when radio button is selected
*/
valueUpdated = output();
// ControlValueAccessor implementation
onControlChange = () => { };
onControlTouch = () => { };
/**
* Whether the radio button is disabled
* @defaultValue false
*/
disabled = model(false);
/**
* @internal
* Computed class string for the radio button
*/
componentClass = computed(() => {
return cn(radioComponent({
variant: this.variant(),
size: this.size(),
}));
});
// ControlValueAccessor methods
writeValue(obj) {
this.selected.set(obj);
}
registerOnChange(fn) {
this.onControlChange = fn;
}
registerOnTouched(fn) {
this.onControlTouch = fn;
}
setDisabledState(isDisabled) {
this.disabled.set(isDisabled);
}
/**
* @internal
* Handles radio button selection changes
*/
handleChange() {
this.onControlChange(this.value());
this.valueUpdated.emit(this.value());
this.selected.set(this.value());
}
/**
* @internal
* Handles blur event
*/
handleBlur() {
this.onControlTouch();
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.0", ngImport: i0, type: RadioComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "19.2.0", type: RadioComponent, isStandalone: true, selector: "st-radio", inputs: { variant: { classPropertyName: "variant", publicName: "variant", isSignal: true, isRequired: false, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null }, name: { classPropertyName: "name", publicName: "name", isSignal: true, isRequired: false, transformFunction: null }, value: { classPropertyName: "value", publicName: "value", isSignal: true, isRequired: true, transformFunction: null }, selected: { classPropertyName: "selected", publicName: "selected", isSignal: true, isRequired: false, transformFunction: null }, disabled: { classPropertyName: "disabled", publicName: "disabled", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { selected: "selectedChange", valueUpdated: "valueUpdated", disabled: "disabledChange" }, providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: RadioComponent,
multi: true,
},
], ngImport: i0, template: "<label class=\"fieldset-label text-base-content font-body\">\n\t<input\n\t\ttype=\"radio\"\n\t\t[class]=\"componentClass()\"\n\t\t[value]=\"value()\"\n\t\t[checked]=\"selected() === value()\"\n\t\t[disabled]=\"disabled()\"\n\t\t[attr.name]=\"name()\"\n\t\t(change)=\"handleChange()\"\n\t\t(blur)=\"handleBlur()\"\n\t/>\n\t{{ label() }}\n</label>\n", dependencies: [{ kind: "ngmodule", type: FormsModule }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.0", ngImport: i0, type: RadioComponent, decorators: [{
type: Component,
args: [{ selector: 'st-radio', imports: [FormsModule], providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: RadioComponent,
multi: true,
},
], changeDetection: ChangeDetectionStrategy.OnPush, template: "<label class=\"fieldset-label text-base-content font-body\">\n\t<input\n\t\ttype=\"radio\"\n\t\t[class]=\"componentClass()\"\n\t\t[value]=\"value()\"\n\t\t[checked]=\"selected() === value()\"\n\t\t[disabled]=\"disabled()\"\n\t\t[attr.name]=\"name()\"\n\t\t(change)=\"handleChange()\"\n\t\t(blur)=\"handleBlur()\"\n\t/>\n\t{{ label() }}\n</label>\n" }]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { RadioComponent };
//# sourceMappingURL=sixbell-telco-sdk-components-forms-radio.mjs.map