Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
KonnorRogers committed Nov 4, 2024
1 parent 2363f1c commit bb0e8e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
29 changes: 14 additions & 15 deletions src/components/select/select.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { animateTo, stopAnimations } from '../../internal/animate.js';
import { classMap } from 'lit/directives/class-map.js';
import { defaultValue } from '../../internal/default-value.js';
import { FormControlController } from '../../internal/form.js';
import { getAnimation, setDefaultAnimation } from '../../utilities/animation-registry.js';
import { HasSlotController } from '../../internal/slot.js';
Expand Down Expand Up @@ -102,10 +101,10 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
/** The name of the select, submitted as a name/value pair with form data. */
@property() name = '';

private _value: string | string[] = ""
private _value: string | string[] = '';

get value () {
return this._value
get value() {
return this._value;
}

/**
Expand All @@ -120,24 +119,24 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
// toAttribute: (value: string[]) => value.join(' ')
// }
})
set value (val: string | string[]) {
set value(val: string | string[]) {
if (this.multiple) {
val = Array.isArray(val) ? val : val.split(" ")
val = Array.isArray(val) ? val : val.split(' ');
} else {
val = Array.isArray(val) ? val.join(" ") : val
val = Array.isArray(val) ? val.join(' ') : val;
}

if (this._value === val) {
return
return;
}

this.valueHasChanged = true
this.valueHasChanged = true;

this._value = val
this._value = val;
}

/** The default value of the form control. Primarily used for resetting the form control. */
@property({ attribute: "value" }) defaultValue: string | string[] = '';
@property({ attribute: 'value' }) defaultValue: string | string[] = '';

/** The select's size. */
@property({ reflect: true }) size: 'small' | 'medium' | 'large' = 'medium';
Expand Down Expand Up @@ -619,7 +618,7 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
// Update selected options cache
this.selectedOptions = options.filter(el => el.selected);

const cachedValueHasChanged = this.valueHasChanged
const cachedValueHasChanged = this.valueHasChanged;
// Update the value and display label
if (this.multiple) {
this.value = this.selectedOptions.map(el => el.value);
Expand All @@ -635,7 +634,7 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
this.value = selectedOption?.value ?? '';
this.displayLabel = selectedOption?.getTextLabel?.() ?? '';
}
this.valueHasChanged = cachedValueHasChanged
this.valueHasChanged = cachedValueHasChanged;

// Update validity
this.updateComplete.then(() => {
Expand Down Expand Up @@ -676,8 +675,8 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
@watch(['defaultValue', 'value'], { waitUntilFirstUpdate: true })
handleValueChange() {
if (!this.valueHasChanged) {
this.value = this.defaultValue
this.valueHasChanged = false
this.value = this.defaultValue;
this.valueHasChanged = false;
}

const allOptions = this.getAllOptions();
Expand Down
20 changes: 9 additions & 11 deletions src/components/select/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,20 +745,18 @@ describe('<sl-select>', () => {
* @see {https://github.com/shoelace-style/shoelace/issues/2254}
*/
it('Should account for if `value` changed before connecting', async () => {
const select = await fixture<SlSelect>(html`
<sl-select label="Search By" multiple clearable .value=${["foo", "bar"]}>
<sl-option value="foo">Foo</sl-option>
<sl-option value="bar">Bar</sl-option>
</sl-select>
`)
const select = await fixture<SlSelect>(html`
<sl-select label="Search By" multiple clearable .value=${['foo', 'bar']}>
<sl-option value="foo">Foo</sl-option>
<sl-option value="bar">Bar</sl-option>
</sl-select>
`);

// just for safe measure.
await aTimeout(10)

expect(select.value).to.deep.equal(["foo", "bar"])

})
await aTimeout(10);

expect(select.value).to.deep.equal(['foo', 'bar']);
});
});

runFormControlBaseTests('sl-select');
Expand Down

0 comments on commit bb0e8e4

Please sign in to comment.