Skip to content

Commit bb3407d

Browse files
authored
7758 allow single value in kolselect (#8045)
2 parents c63f0db + 964888b commit bb3407d

File tree

11 files changed

+1076
-33
lines changed

11 files changed

+1076
-33
lines changed

packages/components/src/components/pagination/component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class KolPaginationWc implements PaginationAPI {
174174
_on={{
175175
onChange: this.onChangePageSize,
176176
}}
177-
_value={[this.state._pageSize]}
177+
_value={this.state._pageSize}
178178
></KolSelectTag>
179179
)}
180180
</Host>
@@ -278,7 +278,7 @@ export class KolPaginationWc implements PaginationAPI {
278278
};
279279

280280
private onChangePageSize = (event: Event, value: unknown) => {
281-
value = parseInt((value as string[])[0]);
281+
value = parseInt(value as string);
282282
if (typeof value === 'number' && value > 0 && this._pageSize !== value) {
283283
this._pageSize = value;
284284
const timeout = setTimeout(() => {

packages/components/src/components/select/controller.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export class SelectController extends InputIconController implements SelectWatch
5050
};
5151

5252
private readonly beforePatchOptions = (_value: unknown, nextState: Map<string, unknown>): void => {
53+
const raw = nextState.get('_value');
54+
if (raw !== undefined && !Array.isArray(raw)) {
55+
nextState.set('_value', [raw]);
56+
}
5357
const options = nextState.has('_options') ? nextState.get('_options') : this.component.state._options;
5458
if (Array.isArray(options) && options.length > 0) {
5559
this.keyOptionMap.clear();
@@ -83,6 +87,7 @@ export class SelectController extends InputIconController implements SelectWatch
8387
}
8488

8589
public validateMultiple(value?: boolean): void {
90+
this.assertComponentValueMatchesMultiplicity(value === true);
8691
watchBoolean(this.component, '_multiple', value, {
8792
hooks: {
8893
afterPatch: this.afterPatchOptions,
@@ -105,8 +110,9 @@ export class SelectController extends InputIconController implements SelectWatch
105110
validateRows(this.component, value);
106111
}
107112

108-
public validateValue(value?: Stringified<StencilUnknown[]>): void {
109-
watchJsonArrayString(this.component, '_value', () => true, value, undefined, {
113+
public validateValue(value?: Stringified<StencilUnknown[]> | Stringified<StencilUnknown>): void {
114+
this.assertValueMatchesMultiplicity(value);
115+
watchJsonArrayString(this.component, '_value', () => true, value === undefined ? [] : Array.isArray(value) ? value : [value], undefined, {
110116
hooks: {
111117
afterPatch: this.afterPatchOptions,
112118
beforePatch: this.beforePatchOptions,
@@ -122,4 +128,41 @@ export class SelectController extends InputIconController implements SelectWatch
122128
this.validateRows(this.component._rows);
123129
this.validateValue(this.component._value);
124130
}
131+
132+
private assertValueMatchesMultiplicity(value?: Stringified<StencilUnknown[]> | Stringified<StencilUnknown>): void {
133+
const isArray = Array.isArray(value);
134+
const isMultiple = this.component._multiple === true;
135+
136+
if (isMultiple) {
137+
if (value !== undefined && !isArray) {
138+
throw new Error(
139+
`↑ The schema for the property (_value) is not valid for multiple mode. Expected an array. The value will not be changed. (received = ${JSON.stringify(value)})`,
140+
);
141+
}
142+
} else {
143+
if (isArray) {
144+
throw new Error(
145+
`↑ The schema for the property (_value) is not valid for single mode. Expected a single value. The value will not be changed. (received = ${JSON.stringify(value)})`,
146+
);
147+
}
148+
}
149+
}
150+
151+
private assertComponentValueMatchesMultiplicity(isMultiple: boolean): void {
152+
const rawValue = this.component._value;
153+
154+
if (isMultiple) {
155+
if (rawValue !== undefined && !Array.isArray(rawValue)) {
156+
throw new Error(
157+
`↑ The schema for the property (_value) is not valid for multiple mode. Expected an array. The value will not be changed. (current = ${JSON.stringify(rawValue)})`,
158+
);
159+
}
160+
} else {
161+
if (Array.isArray(rawValue)) {
162+
throw new Error(
163+
`↑ The schema for the property (_value) is not valid for single mode. Expected a single value. The value will not be changed. (current = ${JSON.stringify(rawValue)})`,
164+
);
165+
}
166+
}
167+
}
125168
}

packages/components/src/components/select/select.e2e.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Page } from '@playwright/test';
66
import { testInputMessage } from '../../e2e/input-msg';
77

88
const COMPONENT_NAME = 'kol-select';
9-
const TEST_VALUE = ['E'];
9+
const TEST_VALUE = 'E';
1010
const TEST_LABEL = 'East';
1111
const OPTIONS = [
1212
{ label: 'North', value: 'N' },

packages/components/src/components/select/shadow.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ export class KolSelect implements SelectAPI, FocusableElement {
4949

5050
@Method()
5151
// eslint-disable-next-line @typescript-eslint/require-await
52-
public async getValue(): Promise<StencilUnknown[]> {
53-
return this.state._value;
52+
public async getValue(): Promise<StencilUnknown[] | StencilUnknown> {
53+
if (this._multiple) {
54+
return this.state._value;
55+
} else {
56+
return Array.isArray(this.state._value) && this.state._value.length > 0 ? this.state._value[0] : this.state._value;
57+
}
5458
}
5559

5660
@Method()
@@ -223,7 +227,7 @@ export class KolSelect implements SelectAPI, FocusableElement {
223227
/**
224228
* Defines the value of the input.
225229
*/
226-
@Prop({ mutable: true, reflect: true }) public _value?: Stringified<StencilUnknown[]>;
230+
@Prop({ mutable: true, reflect: true }) public _value?: Stringified<StencilUnknown[]> | Stringified<StencilUnknown>;
227231

228232
@State() public state: SelectStates = {
229233
_hasValue: false,
@@ -341,7 +345,7 @@ export class KolSelect implements SelectAPI, FocusableElement {
341345
}
342346

343347
@Watch('_value')
344-
public validateValue(value?: Stringified<StencilUnknown[]>): void {
348+
public validateValue(value?: Stringified<StencilUnknown[]> | Stringified<StencilUnknown>): void {
345349
this.controller.validateValue(value);
346350
}
347351

@@ -354,14 +358,25 @@ export class KolSelect implements SelectAPI, FocusableElement {
354358
}
355359

356360
private onInput(event: Event): void {
357-
this._value = Array.from(this.selectRef?.options || [])
358-
.filter((option) => option.selected === true)
361+
const selectedValues = Array.from(this.selectRef?.options || [])
362+
.filter((option) => option.selected)
359363
.map((option) => this.controller.getOptionByKey(option.value)?.value as string);
360364

361-
this.controller.onFacade.onInput(event, true, this._value);
365+
if (this._multiple) {
366+
this._value = selectedValues;
367+
this.controller.onFacade.onInput(event, true, selectedValues);
368+
} else {
369+
const singleValue: StencilUnknown = selectedValues.length > 0 ? selectedValues[0] : undefined;
370+
this._value = singleValue;
371+
this.controller.onFacade.onInput(event, true, singleValue);
372+
}
362373
}
363374

364375
private onChange(event: Event): void {
365-
this.controller.onFacade.onChange(event, this._value);
376+
if (this._multiple) {
377+
this.controller.onFacade.onChange(event, this._value as StencilUnknown[]);
378+
} else {
379+
this.controller.onFacade.onChange(event, this._value as StencilUnknown);
380+
}
366381
}
367382
}

0 commit comments

Comments
 (0)