Skip to content

Commit 7aeac9d

Browse files
committed
samples & fix for combobox & single select
1 parent a3fc352 commit 7aeac9d

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

packages/components/src/components/combobox/shadow.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ export class KolCombobox implements ComboboxAPI, FocusableElement {
155155
}
156156
}
157157

158-
private setFilteredSuggestionsByQuery(query: string) {
158+
private setFilteredSuggestionsByQuery(query: string | undefined) {
159+
if (!query) {
160+
return;
161+
}
162+
159163
if (query.trim() === '') {
160164
this._filteredSuggestions = [...this.state._suggestions];
161165
} else {
@@ -630,6 +634,7 @@ export class KolCombobox implements ComboboxAPI, FocusableElement {
630634
public validateSuggestions(value?: SuggestionsPropType): void {
631635
this.controller.validateSuggestions(value);
632636
this._filteredSuggestions = value;
637+
this.setFilteredSuggestionsByQuery(this._value);
633638
}
634639

635640
@Watch('_hasClearButton')

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ export class KolSingleSelect implements SingleSelectAPI, FocusableElement {
206206
}
207207
}
208208

209-
private setFilteredOptionsByQuery(query: string) {
209+
private setFilteredOptionsByQuery(query: string | undefined) {
210+
if (!query) {
211+
return;
212+
}
213+
210214
if (query?.trim() === '') {
211215
this._filteredOptions = [...this.state._options];
212216
} else if (Array.isArray(this.state._options) && this.state._options.length > 0 && query.length > 0) {
@@ -721,7 +725,7 @@ export class KolSingleSelect implements SingleSelectAPI, FocusableElement {
721725
public validateOptions(value?: OptionsPropType): void {
722726
this.controller.validateOptions(value);
723727
this._filteredOptions = value;
724-
this.updateInputValue(this._value);
728+
this.setFilteredOptionsByQuery(this._inputValue);
725729
}
726730

727731
@Watch('_required')
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { SuggestionsPropType } from '@public-ui/components';
2+
import { KolCombobox, KolSpin } from '@public-ui/react-v19';
3+
import type { FC } from 'react';
4+
import React, { useState } from 'react';
5+
import { COUNTRY_SUGGESTIONS } from '../../shares/country';
6+
import { SampleDescription } from '../SampleDescription';
7+
8+
const LoadingOverlayFC: FC<{
9+
show: boolean;
10+
}> = ({ show }) => {
11+
if (show) {
12+
return (
13+
<div className="loading-overlay">
14+
<KolSpin
15+
_label="loading"
16+
_show={show}
17+
_variant="cycle"
18+
style={{
19+
backgroundColor: 'transparent',
20+
}}
21+
/>
22+
</div>
23+
);
24+
} else {
25+
return null;
26+
}
27+
};
28+
29+
export const ComboboxLazyLoaded: FC = () => {
30+
const [suggestions, setSuggestions] = useState<SuggestionsPropType>([]);
31+
const [loading, setLoading] = useState<boolean>(false);
32+
33+
function loadCountries() {
34+
if (suggestions.length > 0) {
35+
return;
36+
}
37+
setLoading(true);
38+
setTimeout(() => {
39+
setSuggestions(COUNTRY_SUGGESTIONS);
40+
setLoading(false);
41+
}, 5000);
42+
}
43+
44+
return (
45+
<>
46+
<SampleDescription>
47+
<p>This combobox loads its list of countries 5 seconds after the first input of the user to simulate a call to the server.</p>
48+
</SampleDescription>
49+
50+
<section className="w-full relative p-3">
51+
<KolCombobox _label="Lazy loaded countries" _suggestions={suggestions} onInput={() => loadCountries()} />
52+
<LoadingOverlayFC show={loading} />
53+
</section>
54+
</>
55+
);
56+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { Routes } from '../../shares/types';
22
import { ComboboxBasic } from './basic';
33
import { ComboboxHtml } from './html';
4+
import { ComboboxLazyLoaded } from './lazy-loaded';
45

56
export const COMBOBOX_ROUTES: Routes = {
67
combobox: {
78
basic: ComboboxBasic,
89
html: ComboboxHtml,
10+
'lazy-loaded': ComboboxLazyLoaded,
911
},
1012
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { Option, StencilUnknown } from '@public-ui/components';
2+
import { KolSingleSelect, KolSpin } from '@public-ui/react-v19';
3+
import type { FC } from 'react';
4+
import React, { useState } from 'react';
5+
import { COUNTRY_OPTIONS } from '../../shares/country';
6+
import { SampleDescription } from '../SampleDescription';
7+
8+
const LoadingOverlayFC: FC<{
9+
show: boolean;
10+
}> = ({ show }) => {
11+
if (show) {
12+
return (
13+
<div className="loading-overlay">
14+
<KolSpin
15+
_label="loading"
16+
_show={show}
17+
_variant="cycle"
18+
style={{
19+
backgroundColor: 'transparent',
20+
}}
21+
/>
22+
</div>
23+
);
24+
} else {
25+
return null;
26+
}
27+
};
28+
29+
export const SingleSelectLazyLoaded: FC = () => {
30+
const [suggestions, setSuggestions] = useState<Option<StencilUnknown>[]>([]);
31+
const [loading, setLoading] = useState<boolean>(false);
32+
33+
function loadCountries() {
34+
if (suggestions.length > 0) {
35+
return;
36+
}
37+
setLoading(true);
38+
setTimeout(() => {
39+
setSuggestions(COUNTRY_OPTIONS as Option<StencilUnknown>[]);
40+
setLoading(false);
41+
}, 5000);
42+
}
43+
44+
return (
45+
<>
46+
<SampleDescription>
47+
<p>This combobox loads its list of countries 5 seconds after the first input of the user to simulate a call to the server.</p>
48+
</SampleDescription>
49+
50+
<section className="w-full relative p-3">
51+
<KolSingleSelect _label="Lazy loaded countries" _options={suggestions} onInput={() => loadCountries()} />
52+
<LoadingOverlayFC show={loading} />
53+
</section>
54+
</>
55+
);
56+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Routes } from '../../shares/types';
22
import { SingleSelectBasic } from './basic';
3+
import { SingleSelectLazyLoaded } from './lazy-loaded';
34

45
export const SINGLE_SELECT_ROUTES: Routes = {
56
'single-select': {
67
basic: SingleSelectBasic,
8+
'lazy-loaded': SingleSelectLazyLoaded,
79
},
810
};

0 commit comments

Comments
 (0)