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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | import { LightningElement, api } from 'lwc';
import { reflectAttribute, normalizeBoolean, normalizeInput, hasSlotText } from 'sds/utils';
import 'sds/privateThemeProvider';
export default class Select extends LightningElement {
static shadowSupportMode = 'native';
_id = 'select-id';
_options = [];
_disabled = false;
_required = false;
_name = 'select';
_labelHidden = false;
_multiple = false;
_invalid = false;
_value;
_validityMessage;
_selectElement;
_helpTextElement;
_formElement;
/**
* The id of the select element.
* @type {string}
*/
@api
get id() {
return this._id;
}
set id(value) {
this._id = normalizeInput(value);
}
/**
* If present, the select is disabled and users cannot interact with it.
* @type {boolean}
* @default false
*/
@api
get disabled() {
return this._disabled;
}
set disabled(value) {
this._disabled = normalizeBoolean(value);
reflectAttribute(this, 'disabled', this._disabled);
}
/**
* The name of the select element.
* @type {string}
* @default select
*/
@api
get name() {
return this._name;
}
set name(value) {
this._name = normalizeInput(value);
}
/**
* If present, the select must have a selected value before submitting a form.
* @type {boolean}
*/
@api
get required() {
return this._required;
}
set required(value) {
this._required = normalizeBoolean(value);
reflectAttribute(this, 'required', this._required);
}
/**
* If present, the select allows multiple selections.
* @type {boolean}
* @default false
*/
@api
get multiple() {
return this._multiple;
}
set multiple(value) {
this._multiple = normalizeBoolean(value);
reflectAttribute(this, 'multiple', this._multiple);
}
/**
* The size of the select element.
* @type {string}
*/
@api size;
/**
* Visually hides the label.
* @type {boolean}
*/
@api
get labelHidden() {
return this._labelHidden;
}
set labelHidden(value) {
this._labelHidden = normalizeBoolean(value);
reflectAttribute(this, 'label-hidden', this._labelHidden);
}
/**
* Allows customer to set invalid state on input
* An example would be if the input is required and the value is empty on form submit,
* since we are not using native form validation, we need to offer the ability set the invalid state on the input
*/
@api
get invalid() {
return this._invalid;
}
set invalid(value) {
this._invalid = normalizeBoolean(value);
reflectAttribute(this, 'invalid', this._invalid);
}
/**
* Returns the select options
* @type {array}
* @readonly
*/
@api
get options() {
return this._options;
}
/**
* Returns the selected value
* @type {string}
* @readonly
*/
@api
get value() {
return this._value;
}
/**
* Property to get the select element.
* @returns {HTMLElement} - the select element
* @private
*/
get select() {
this._selectElement = this._selectElement || this.template.querySelector('select');
return this._selectElement;
}
/**
* Property to get the help text element.
*
* @returns {HTMLElement} - the help text element
* @private
*/
get helpText() {
this._helpTextElement = this._helpTextElement || this.template.querySelector('[part="help-text"]');
return this._helpTextElement;
}
@api
get validity() {
return Promise.resolve().then(() => this.select.validity);
}
@api
get validityMessage() {
return this._validityMessage;
}
/**
* Returns the closest form element
* @returns {HTMLElement} - the form element
* @private
*/
get form() {
this._formElement = this._formElement || this.template.host.closest('form');
return this._formElement;
}
/**
* If invalid, sets the aria-invalid attribute on the input element
* and sets invalid attribute on custom element
*
* @private
*/
isInvalid() {
if (!this.select.validity.valid) {
this.invalid = true;
} else {
this.invalid = false;
}
}
/**
* Validates the input element and sets the validity state
* Sets the "invalid" attribute on the custom element if the input is invalid
* Sets the "aria-invalid" attribute on the input element if the input is invalid
*
* @private
*/
handleValidity() {
if (this.select.validity.valueMissing) {
this._validityMessage = `select: This field ${this.name} is required.`;
} else {
this._validityMessage = null;
}
}
/**
* Change handler for the select element when its value changes
* @param {*} e - the change event
*/
handleChange(e) {
if (!this._disabled) {
this._value = this.select[this.select.options.selectedIndex].value;
this.isInvalid();
this.handleValidity();
}
this.dispatchEvent(new CustomEvent('change'));
}
/**
* Blur handler for the select element when it loses focus
* @param {*} e
*/
handleBlur(e) {
this.removeAttribute('focus');
}
/**
* On click event handler
* Used to focus the input element when the custom element is clicked
* @param {Object} e - click event
*/
handleClick(e) {
if (!this._disabled) {
this.setAttribute('focus', '');
this.select.focus();
}
}
/**
* Slot change event on the default slot
* Since the select element is not supported in slots, we are using the slotchange event to get the options
* @param {*} e
*/
handleSlotChange(e) {
const childNodes = e.target.assignedNodes({ flatten: true });
const options = [...childNodes].reduce((arr, node) => {
if (node instanceof HTMLOptionElement) {
arr.push({ value: node.value, name: node.text, selected: node.selected });
}
return arr;
}, []);
options.forEach((option) => {
const optionEl = document.createElement('option');
optionEl.value = option.value;
optionEl.selected = option.selected;
optionEl.text = option.name;
if (option.selected) {
this._value = option.value;
}
this.select.appendChild(optionEl);
});
this._options = options.length > 0 && options;
e.target.style.display = 'none';
}
/**
* Slot change event on help text slot
* Used to dynamically add/remove the associated ID to the input element if help text is present
* Used to dynamically add "visible" part to the help text element if help text is found in the slot
*
* @param {*} e - slot change event
*/
handleHelpTextSlotChange(e) {
let childElements = e.target.assignedElements({ flatten: true });
if (childElements.length > 0) {
if (this.helpText) {
this.helpText.id = 'id-help-text';
this.input.setAttribute('aria-describedby', 'id-help-text');
this.helpText.setAttribute('part', 'help-text visible');
}
}
}
/**
* 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();
});
}
}
}
|