Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | import { LightningElement, api } from 'lwc';
import { reflectAttribute, normalizeBoolean, normalizeInput, hasSlotText } from 'sds/utils';
import 'sds/privateThemeProvider';
export default class InputRadio extends LightningElement {
static shadowSupportMode = 'native';
static delegatesFocus = true;
_name = 'radio-name';
_value = 'radio-value';
_disabled = false;
_checked = false;
_required = false;
_invalid = false;
_validityMessage;
@api id = 'radio-id';
/**
* Makes the radio not mutable, interactable, or focusable.
* @type {boolean}
* @default false
*/
@api
get disabled() {
return this._disabled;
}
set disabled(value) {
this._disabled = normalizeBoolean(value);
reflectAttribute(this, 'disabled', this._disabled);
}
/**
* Determines if the form control is required or not.
* @type {boolean}
* @default false
*/
@api
get required() {
return this._required;
}
set required(value) {
this._required = normalizeBoolean(value);
reflectAttribute(this, 'required', this._required);
}
/**
* The name of the input element, used on form submit.
* Name attribute is removed from custom element and delegated to the input element
* @type {string}
*/
@api
get name() {
return this._name;
}
set name(value) {
this._name = normalizeInput(value);
}
/**
* The value of the input element
* Value attribute is removed from custom element and delegated to the input element
* @type {string}
*/
@api
get value() {
return this._value;
}
set value(value) {
this._value = normalizeInput(value);
}
/**
* Determines if the checkbox is checked or not.
* @type {boolean}
* @default false
*/
@api
get checked() {
return this._checked;
}
set checked(value) {
this._checked = normalizeBoolean(value);
reflectAttribute(this, 'checked', this._checked);
}
/**
* Determines if the checkbox is invalid or not.
* Invalid attribute is removed from custom element and delegated to the input element
* @type {boolean}
*/
@api
get invalid() {
return this._invalid;
}
set invalid(value) {
this._invalid = normalizeBoolean(value);
reflectAttribute(this, 'invalid', this._invalid);
}
/**
* Sets help text for screen readers
* @type {string}
*/
@api helpMessage;
/**
* Property to get the radio element.
* @returns {HTMLElement} - the radio element
*/
get radio() {
if (!this._cachedRadio) {
this._cachedRadio = this.template.querySelector('input[type="radio"]');
}
return this._cachedRadio;
}
/**
* Returns the validity error of the input element
*/
@api
get validity() {
return Promise.resolve().then(() => this.radio.validity);
}
/**
* Returns the validity error of the input element
*/
@api
get validityMessage() {
return this._validityMessage;
}
/**
* If invalid, sets the aria-invalid attribute on the input element
* and sets invalid attribute on custom element
* @private
*/
isInvalid() {
if (!this.radio.validity.valid) {
this.invalid = true;
} else {
this.invalid = false;
}
}
/**
* Returns the closest form element
* @returns {HTMLElement} - the form element
* @private
*/
get form() {
return this.template.host.closest('form');
}
/**
* Store validity message in private variable
* @private
*/
handleValidity() {
if (this.radio.validity.valueMissing) {
this._validityMessage = `inputRadio: This field "${this.value}" is required.`;
} else {
this._validityMessage = null;
}
}
/**
* Event handler for the radio change event.
* Used to dynmically set the checked attribute on the host element
* @param {*} e
*/
handleChange(e) {
if (this.hasAttribute('checked')) {
this._checked = false;
this.removeAttribute('checked');
} else {
this._checked = true;
this.setAttribute('checked', '');
}
this.dispatchEvent(new CustomEvent('change'));
}
/**
* Event handler when the radio is blurred and loses focus.
* @param {*} e
*/
handleBlur(e) {
this.dispatchEvent(new CustomEvent('blur'));
this._value = e.target.value;
}
/**
* Slot change event on the label slot
* Used to check if the label has a slotted value, if not, we throw an error that one must be included
* @param {*} e - slot change event
*/
handleLabelSlotChange(e) {
!hasSlotText(e) && console.error('A label value must be included for all form elements.');
}
renderedCallback() {
if (this.form) {
this.form.addEventListener('submit', () => {
this.handleValidity();
this.isInvalid();
});
}
}
}
|