Skip to content

Commit ab7fb31

Browse files
committed
refactor(select): Code clean-up and undefined as a default value
1 parent 4697dec commit ab7fb31

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

src/components/select/select.ts

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
} from '../common/mixins/forms/form-value.js';
4444
import {
4545
findElementFromEventPath,
46+
isEmpty,
4647
isString,
4748
partNameMap,
4849
} from '../common/util.js';
@@ -128,7 +129,7 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
128129
);
129130
}
130131

131-
protected override _formValue: FormValue<string>;
132+
protected override _formValue: FormValue<string | undefined>;
132133

133134
private _searchTerm = '';
134135
private _lastKeyTime = 0;
@@ -157,45 +158,33 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
157158
}
158159

159160
@query(IgcInputComponent.tagName, true)
160-
protected input!: IgcInputComponent;
161+
protected _input!: IgcInputComponent;
161162

162163
@queryAssignedElements({ slot: 'suffix' })
163-
protected inputSuffix!: Array<HTMLElement>;
164+
protected _inputSuffix!: Array<HTMLElement>;
164165

165166
@queryAssignedElements({ slot: 'prefix' })
166-
protected inputPrefix!: Array<HTMLElement>;
167+
protected _inputPrefix!: Array<HTMLElement>;
167168

168169
@queryAssignedElements({ slot: 'toggle-icon-expanded' })
169170
protected _expandedIconSlot!: Array<HTMLElement>;
170171

171-
protected get hasExpandedIcon() {
172-
return this._expandedIconSlot.length > 0;
173-
}
174-
175-
protected get hasPrefixes() {
176-
return this.inputPrefix.length > 0;
177-
}
178-
179-
protected get hasSuffixes() {
180-
return this.inputSuffix.length > 0;
181-
}
182-
172+
/* @tsTwoWayProperty(true, "igcChange", "detail.value", false) */
183173
/**
184174
* The value attribute of the control.
185175
* @attr
186176
*/
187177
@property()
188-
public get value(): string {
189-
return this._formValue.value;
190-
}
191-
192-
/* @tsTwoWayProperty(true, "igcChange", "detail.value", false) */
193-
public set value(value: string) {
178+
public set value(value: string | undefined) {
194179
this._updateValue(value);
195-
const item = this.getItem(this._formValue.value);
180+
const item = this.getItem(this._formValue.value!);
196181
item ? this.setSelectedItem(item) : this.clearSelectedItem();
197182
}
198183

184+
public get value(): string | undefined {
185+
return this._formValue.value;
186+
}
187+
199188
/**
200189
* The outlined attribute of the control.
201190
* @attr
@@ -278,7 +267,14 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
278267
constructor() {
279268
super();
280269

281-
this._formValue = createFormValueState(this, { initialValue: '' });
270+
this._formValue = createFormValueState<string | undefined>(this, {
271+
initialValue: undefined,
272+
transformers: {
273+
setValue: (value) => value || undefined,
274+
setDefaultValue: (value) => value || undefined,
275+
},
276+
});
277+
282278
this._rootClickController.update({ hideCallback: this.handleClosing });
283279

284280
addKeybindings(this, {
@@ -420,13 +416,13 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
420416

421417
private async altArrowUp() {
422418
if (this.open && (await this._hide(true))) {
423-
this.input.focus();
419+
this._input.focus();
424420
}
425421
}
426422

427423
protected async onEscapeKey() {
428424
if (await this._hide(true)) {
429-
this.input.focus();
425+
this._input.focus();
430426
}
431427
}
432428

@@ -483,7 +479,7 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
483479
const shouldHide = emit && !this.keepOpenOnSelect;
484480

485481
if (this._selectedItem === item) {
486-
if (shouldFocus) this.input.focus();
482+
if (shouldFocus) this._input.focus();
487483
return this._selectedItem;
488484
}
489485

@@ -492,7 +488,7 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
492488
this._updateValue(newItem.value);
493489

494490
if (emit) this.handleChange(newItem);
495-
if (shouldFocus) this.input.focus();
491+
if (shouldFocus) this._input.focus();
496492
if (shouldHide) this._hide(true);
497493

498494
return this._selectedItem;
@@ -530,19 +526,19 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
530526
/* alternateName: focusComponent */
531527
/** Sets focus on the component. */
532528
public override focus(options?: FocusOptions) {
533-
this.input.focus(options);
529+
this._input.focus(options);
534530
}
535531

536532
/* alternateName: blurComponent */
537533
/** Removes focus from the component. */
538534
public override blur() {
539-
this.input.blur();
535+
this._input.blur();
540536
}
541537

542538
/** Checks the validity of the control and moves the focus to it if it is not valid. */
543539
public override reportValidity() {
544540
const valid = super.reportValidity();
545-
if (!valid) this.input.focus();
541+
if (!valid) this._input.focus();
546542
return valid;
547543
}
548544

@@ -584,8 +580,8 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
584580
}
585581

586582
protected renderInputSlots() {
587-
const prefixName = this.hasPrefixes ? 'prefix' : '';
588-
const suffixName = this.hasSuffixes ? 'suffix' : '';
583+
const prefixName = isEmpty(this._inputPrefix) ? '' : 'prefix';
584+
const suffixName = isEmpty(this._inputSuffix) ? '' : 'suffix';
589585

590586
return html`
591587
<span slot=${prefixName}>
@@ -600,8 +596,8 @@ export default class IgcSelectComponent extends FormAssociatedRequiredMixin(
600596

601597
protected renderToggleIcon() {
602598
const parts = partNameMap({ 'toggle-icon': true, filled: this.value! });
603-
const iconHidden = this.open && this.hasExpandedIcon;
604-
const iconExpandedHidden = !(this.hasExpandedIcon && this.open);
599+
const iconHidden = this.open && !isEmpty(this._expandedIconSlot);
600+
const iconExpandedHidden = !iconHidden;
605601

606602
return html`
607603
<span slot="suffix" part=${parts} aria-hidden="true">

0 commit comments

Comments
 (0)