@dktunited-techoff/techoff-suite-ui 1.6.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/components/TsCheckbox/TsCheckbox.css +0 -3
- package/esm/components/TsDropdown/TsDropdown/TsDropdown.css +11 -2
- package/esm/components/TsDropdown/TsDropdown/TsDropdown.d.ts +1 -1
- package/esm/components/TsDropdown/TsDropdown/TsDropdown.js +31 -2
- package/esm/components/TsDropdown/TsDropdown/TsDropdown.js.map +1 -1
- package/esm/components/TsDropdown/TsDropdown/TsDropdown.tsx +47 -4
- package/esm/components/TsDropdown/TsDropdown/TsDropdown.types.d.ts +1 -0
- package/esm/components/TsDropdown/TsDropdown/TsDropdown.types.ts +1 -0
- package/lib/components/TsCheckbox/TsCheckbox.css +0 -3
- package/lib/components/TsDropdown/TsDropdown/TsDropdown.css +11 -2
- package/lib/components/TsDropdown/TsDropdown/TsDropdown.d.ts +1 -1
- package/lib/components/TsDropdown/TsDropdown/TsDropdown.js +31 -2
- package/lib/components/TsDropdown/TsDropdown/TsDropdown.js.map +1 -1
- package/lib/components/TsDropdown/TsDropdown/TsDropdown.tsx +47 -4
- package/lib/components/TsDropdown/TsDropdown/TsDropdown.types.d.ts +1 -0
- package/lib/components/TsDropdown/TsDropdown/TsDropdown.types.ts +1 -0
- package/package.json +1 -1
- package/src/components/TsCheckbox/TsCheckbox.css +0 -3
- package/src/components/TsDropdown/TsDropdown/TsDropdown.css +11 -2
- package/src/components/TsDropdown/TsDropdown/TsDropdown.tsx +47 -4
- package/src/components/TsDropdown/TsDropdown/TsDropdown.types.ts +1 -0
|
@@ -43,8 +43,7 @@
|
|
|
43
43
|
top: calc(100%+8px);
|
|
44
44
|
display: flex;
|
|
45
45
|
flex-direction: column;
|
|
46
|
-
width: 100%;
|
|
47
|
-
min-width: 320px;
|
|
46
|
+
min-width: 100%;
|
|
48
47
|
max-height: 250px;
|
|
49
48
|
padding: 12px;
|
|
50
49
|
box-shadow: 0px 8px 9px 0px #1010101a;
|
|
@@ -60,6 +59,16 @@
|
|
|
60
59
|
.ts-dropdown-menu {
|
|
61
60
|
overflow: auto;
|
|
62
61
|
}
|
|
62
|
+
.ts-dropdown-menu-loader {
|
|
63
|
+
display: flex;
|
|
64
|
+
justify-content: center;
|
|
65
|
+
}
|
|
66
|
+
.ts-dropdown-menu-error-text {
|
|
67
|
+
display: flex;
|
|
68
|
+
justify-content: center;
|
|
69
|
+
color: #7a7a7a;
|
|
70
|
+
font-style: italic;
|
|
71
|
+
}
|
|
63
72
|
.ts-dropdown-menu-item {
|
|
64
73
|
display: flex;
|
|
65
74
|
align-items: center;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { TsDropdownProps } from './TsDropdown.types';
|
|
3
3
|
import './TsDropdown.css';
|
|
4
|
-
export declare const TsDropdown: <T extends unknown>({ label, options, placeholder, searchPlaceholder, value, disabled, searchable, getOptionValue, getOptionLabel, onChange, }: TsDropdownProps<T>) => React.JSX.Element;
|
|
4
|
+
export declare const TsDropdown: <T extends unknown>({ label, options, placeholder, searchPlaceholder, value, disabled, searchable, getOptionValue, getOptionLabel, loadOptions, onChange, }: TsDropdownProps<T>) => React.JSX.Element;
|
|
@@ -5,13 +5,28 @@ import { TsInput } from '../../TsInput/TsInput/TsInput';
|
|
|
5
5
|
import { useClickOutside } from '../../../hooks/use-click-outside';
|
|
6
6
|
import { capitalize } from '../../../utils/string.utils';
|
|
7
7
|
import './TsDropdown.css';
|
|
8
|
-
|
|
8
|
+
import { TsLoader } from '../../TsLoader/TsLoader';
|
|
9
|
+
export const TsDropdown = ({ label, options, placeholder, searchPlaceholder, value, disabled, searchable, getOptionValue, getOptionLabel, loadOptions, onChange, }) => {
|
|
9
10
|
const [showDropdownMenu, setShowDropdownMenu] = useState(false);
|
|
10
11
|
const [searchValue, setSearchValue] = useState('');
|
|
11
12
|
const [items, setItems] = useState(options ?? []);
|
|
12
13
|
const [selectedItem, setSelectedItem] = useState(value);
|
|
14
|
+
const [loading, setLoading] = useState(false);
|
|
15
|
+
const [isFetchError, setIsFetchError] = useState(false);
|
|
13
16
|
const ref = useClickOutside(() => setShowDropdownMenu(false));
|
|
14
17
|
// ########
|
|
18
|
+
// Fetchers
|
|
19
|
+
const fetchOptions = () => {
|
|
20
|
+
if (!options && loadOptions) {
|
|
21
|
+
setLoading(true);
|
|
22
|
+
setIsFetchError(false);
|
|
23
|
+
loadOptions(searchValue)
|
|
24
|
+
.then(setItems)
|
|
25
|
+
.catch(() => setIsFetchError(true))
|
|
26
|
+
.finally(() => setLoading(false));
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
// ########
|
|
15
30
|
// Handlers
|
|
16
31
|
const handleSelectItem = (item) => {
|
|
17
32
|
setSelectedItem(item);
|
|
@@ -21,10 +36,16 @@ export const TsDropdown = ({ label, options, placeholder, searchPlaceholder, val
|
|
|
21
36
|
const handleShowDropdownMenu = () => !disabled && setShowDropdownMenu(true);
|
|
22
37
|
// ########
|
|
23
38
|
// Watchers
|
|
39
|
+
useEffect(fetchOptions, []);
|
|
24
40
|
useEffect(() => {
|
|
25
41
|
if (searchable && options) {
|
|
26
42
|
setItems(options.filter(option => getOptionLabel(option).toLowerCase().includes(searchValue.toLowerCase())));
|
|
27
43
|
}
|
|
44
|
+
if (searchable && !options && loadOptions) {
|
|
45
|
+
const timeOutId = setTimeout(fetchOptions, 500);
|
|
46
|
+
return () => clearTimeout(timeOutId);
|
|
47
|
+
}
|
|
48
|
+
return () => { };
|
|
28
49
|
}, [searchValue]);
|
|
29
50
|
// #########
|
|
30
51
|
// Rendering
|
|
@@ -39,6 +60,14 @@ export const TsDropdown = ({ label, options, placeholder, searchPlaceholder, val
|
|
|
39
60
|
!disabled && showDropdownMenu && (React.createElement("div", { className: "ts-dropdown-menu-container" },
|
|
40
61
|
searchable && (React.createElement("div", { className: "ts-dropdown-search" },
|
|
41
62
|
React.createElement(TsInput, { icon: "search", placeholder: searchPlaceholder, value: searchValue, autoFocus: true, onChange: setSearchValue }))),
|
|
42
|
-
React.createElement("div", { className: "ts-dropdown-menu" },
|
|
63
|
+
React.createElement("div", { className: "ts-dropdown-menu" },
|
|
64
|
+
loading && (React.createElement("div", { className: "ts-dropdown-menu-loader" },
|
|
65
|
+
React.createElement(TsLoader, { size: "sm" }))),
|
|
66
|
+
!loading && isFetchError && (React.createElement("div", { className: "ts-dropdown-menu-error-text" }, "An error occured, try again later.")),
|
|
67
|
+
!loading && !isFetchError && items.length === 0 && (React.createElement("div", { className: "ts-dropdown-menu-error-text" }, "No options found.")),
|
|
68
|
+
!loading &&
|
|
69
|
+
!isFetchError &&
|
|
70
|
+
items.length > 0 &&
|
|
71
|
+
items.map(item => (React.createElement("div", { key: getOptionValue(item), className: "ts-dropdown-menu-item", onClick: () => handleSelectItem(item) }, capitalize(getOptionLabel(item))))))))));
|
|
43
72
|
};
|
|
44
73
|
//# sourceMappingURL=TsDropdown.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TsDropdown.js","sourceRoot":"","sources":["../../../../src/components/TsDropdown/TsDropdown/TsDropdown.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE5C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AACzD,OAAO,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"TsDropdown.js","sourceRoot":"","sources":["../../../../src/components/TsDropdown/TsDropdown/TsDropdown.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B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CAAC;oBAChB,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAChB,6BACE,GAAG,EAAE,cAAc,CAAC,IAAI,CAAC,EACzB,SAAS,EAAC,uBAAuB,EACjC,OAAO,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAEpC,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,CAC7B,CACP,CAAC,CACA,CACF,CACP,CACG,CACP,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -6,6 +6,7 @@ import { TsInput } from '../../TsInput/TsInput/TsInput';
|
|
|
6
6
|
import { useClickOutside } from '../../../hooks/use-click-outside';
|
|
7
7
|
import { capitalize } from '../../../utils/string.utils';
|
|
8
8
|
import './TsDropdown.css';
|
|
9
|
+
import { TsLoader } from '../../TsLoader/TsLoader';
|
|
9
10
|
|
|
10
11
|
export const TsDropdown = <T extends unknown>({
|
|
11
12
|
label,
|
|
@@ -17,14 +18,30 @@ export const TsDropdown = <T extends unknown>({
|
|
|
17
18
|
searchable,
|
|
18
19
|
getOptionValue,
|
|
19
20
|
getOptionLabel,
|
|
21
|
+
loadOptions,
|
|
20
22
|
onChange,
|
|
21
23
|
}: TsDropdownProps<T>) => {
|
|
22
24
|
const [showDropdownMenu, setShowDropdownMenu] = useState<boolean>(false);
|
|
23
25
|
const [searchValue, setSearchValue] = useState<string>('');
|
|
24
26
|
const [items, setItems] = useState<T[]>(options ?? []);
|
|
25
27
|
const [selectedItem, setSelectedItem] = useState<T | undefined>(value);
|
|
28
|
+
const [loading, setLoading] = useState<boolean>(false);
|
|
29
|
+
const [isFetchError, setIsFetchError] = useState<boolean>(false);
|
|
26
30
|
const ref = useClickOutside(() => setShowDropdownMenu(false));
|
|
27
31
|
|
|
32
|
+
// ########
|
|
33
|
+
// Fetchers
|
|
34
|
+
const fetchOptions = () => {
|
|
35
|
+
if (!options && loadOptions) {
|
|
36
|
+
setLoading(true);
|
|
37
|
+
setIsFetchError(false);
|
|
38
|
+
loadOptions(searchValue)
|
|
39
|
+
.then(setItems)
|
|
40
|
+
.catch(() => setIsFetchError(true))
|
|
41
|
+
.finally(() => setLoading(false));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
28
45
|
// ########
|
|
29
46
|
// Handlers
|
|
30
47
|
const handleSelectItem = (item: T) => {
|
|
@@ -36,10 +53,18 @@ export const TsDropdown = <T extends unknown>({
|
|
|
36
53
|
|
|
37
54
|
// ########
|
|
38
55
|
// Watchers
|
|
56
|
+
useEffect(fetchOptions, []);
|
|
39
57
|
useEffect(() => {
|
|
40
58
|
if (searchable && options) {
|
|
41
59
|
setItems(options.filter(option => getOptionLabel(option).toLowerCase().includes(searchValue.toLowerCase())));
|
|
42
60
|
}
|
|
61
|
+
|
|
62
|
+
if (searchable && !options && loadOptions) {
|
|
63
|
+
const timeOutId = setTimeout(fetchOptions, 500);
|
|
64
|
+
return () => clearTimeout(timeOutId);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return () => {};
|
|
43
68
|
}, [searchValue]);
|
|
44
69
|
|
|
45
70
|
// #########
|
|
@@ -75,11 +100,29 @@ export const TsDropdown = <T extends unknown>({
|
|
|
75
100
|
)}
|
|
76
101
|
|
|
77
102
|
<div className="ts-dropdown-menu">
|
|
78
|
-
{
|
|
79
|
-
<div
|
|
80
|
-
|
|
103
|
+
{loading && (
|
|
104
|
+
<div className="ts-dropdown-menu-loader">
|
|
105
|
+
<TsLoader size="sm" />
|
|
81
106
|
</div>
|
|
82
|
-
)
|
|
107
|
+
)}
|
|
108
|
+
{!loading && isFetchError && (
|
|
109
|
+
<div className="ts-dropdown-menu-error-text">An error occured, try again later.</div>
|
|
110
|
+
)}
|
|
111
|
+
{!loading && !isFetchError && items.length === 0 && (
|
|
112
|
+
<div className="ts-dropdown-menu-error-text">No options found.</div>
|
|
113
|
+
)}
|
|
114
|
+
{!loading &&
|
|
115
|
+
!isFetchError &&
|
|
116
|
+
items.length > 0 &&
|
|
117
|
+
items.map(item => (
|
|
118
|
+
<div
|
|
119
|
+
key={getOptionValue(item)}
|
|
120
|
+
className="ts-dropdown-menu-item"
|
|
121
|
+
onClick={() => handleSelectItem(item)}
|
|
122
|
+
>
|
|
123
|
+
{capitalize(getOptionLabel(item))}
|
|
124
|
+
</div>
|
|
125
|
+
))}
|
|
83
126
|
</div>
|
|
84
127
|
</div>
|
|
85
128
|
)}
|
|
@@ -43,8 +43,7 @@
|
|
|
43
43
|
top: calc(100%+8px);
|
|
44
44
|
display: flex;
|
|
45
45
|
flex-direction: column;
|
|
46
|
-
width: 100%;
|
|
47
|
-
min-width: 320px;
|
|
46
|
+
min-width: 100%;
|
|
48
47
|
max-height: 250px;
|
|
49
48
|
padding: 12px;
|
|
50
49
|
box-shadow: 0px 8px 9px 0px #1010101a;
|
|
@@ -60,6 +59,16 @@
|
|
|
60
59
|
.ts-dropdown-menu {
|
|
61
60
|
overflow: auto;
|
|
62
61
|
}
|
|
62
|
+
.ts-dropdown-menu-loader {
|
|
63
|
+
display: flex;
|
|
64
|
+
justify-content: center;
|
|
65
|
+
}
|
|
66
|
+
.ts-dropdown-menu-error-text {
|
|
67
|
+
display: flex;
|
|
68
|
+
justify-content: center;
|
|
69
|
+
color: #7a7a7a;
|
|
70
|
+
font-style: italic;
|
|
71
|
+
}
|
|
63
72
|
.ts-dropdown-menu-item {
|
|
64
73
|
display: flex;
|
|
65
74
|
align-items: center;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { TsDropdownProps } from './TsDropdown.types';
|
|
3
3
|
import './TsDropdown.css';
|
|
4
|
-
export declare const TsDropdown: <T extends unknown>({ label, options, placeholder, searchPlaceholder, value, disabled, searchable, getOptionValue, getOptionLabel, onChange, }: TsDropdownProps<T>) => React.JSX.Element;
|
|
4
|
+
export declare const TsDropdown: <T extends unknown>({ label, options, placeholder, searchPlaceholder, value, disabled, searchable, getOptionValue, getOptionLabel, loadOptions, onChange, }: TsDropdownProps<T>) => React.JSX.Element;
|
|
@@ -8,13 +8,28 @@ const TsInput_1 = require("../../TsInput/TsInput/TsInput");
|
|
|
8
8
|
const use_click_outside_1 = require("../../../hooks/use-click-outside");
|
|
9
9
|
const string_utils_1 = require("../../../utils/string.utils");
|
|
10
10
|
require("./TsDropdown.css");
|
|
11
|
-
const
|
|
11
|
+
const TsLoader_1 = require("../../TsLoader/TsLoader");
|
|
12
|
+
const TsDropdown = ({ label, options, placeholder, searchPlaceholder, value, disabled, searchable, getOptionValue, getOptionLabel, loadOptions, onChange, }) => {
|
|
12
13
|
const [showDropdownMenu, setShowDropdownMenu] = (0, react_1.useState)(false);
|
|
13
14
|
const [searchValue, setSearchValue] = (0, react_1.useState)('');
|
|
14
15
|
const [items, setItems] = (0, react_1.useState)(options ?? []);
|
|
15
16
|
const [selectedItem, setSelectedItem] = (0, react_1.useState)(value);
|
|
17
|
+
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
18
|
+
const [isFetchError, setIsFetchError] = (0, react_1.useState)(false);
|
|
16
19
|
const ref = (0, use_click_outside_1.useClickOutside)(() => setShowDropdownMenu(false));
|
|
17
20
|
// ########
|
|
21
|
+
// Fetchers
|
|
22
|
+
const fetchOptions = () => {
|
|
23
|
+
if (!options && loadOptions) {
|
|
24
|
+
setLoading(true);
|
|
25
|
+
setIsFetchError(false);
|
|
26
|
+
loadOptions(searchValue)
|
|
27
|
+
.then(setItems)
|
|
28
|
+
.catch(() => setIsFetchError(true))
|
|
29
|
+
.finally(() => setLoading(false));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
// ########
|
|
18
33
|
// Handlers
|
|
19
34
|
const handleSelectItem = (item) => {
|
|
20
35
|
setSelectedItem(item);
|
|
@@ -24,10 +39,16 @@ const TsDropdown = ({ label, options, placeholder, searchPlaceholder, value, dis
|
|
|
24
39
|
const handleShowDropdownMenu = () => !disabled && setShowDropdownMenu(true);
|
|
25
40
|
// ########
|
|
26
41
|
// Watchers
|
|
42
|
+
(0, react_1.useEffect)(fetchOptions, []);
|
|
27
43
|
(0, react_1.useEffect)(() => {
|
|
28
44
|
if (searchable && options) {
|
|
29
45
|
setItems(options.filter(option => getOptionLabel(option).toLowerCase().includes(searchValue.toLowerCase())));
|
|
30
46
|
}
|
|
47
|
+
if (searchable && !options && loadOptions) {
|
|
48
|
+
const timeOutId = setTimeout(fetchOptions, 500);
|
|
49
|
+
return () => clearTimeout(timeOutId);
|
|
50
|
+
}
|
|
51
|
+
return () => { };
|
|
31
52
|
}, [searchValue]);
|
|
32
53
|
// #########
|
|
33
54
|
// Rendering
|
|
@@ -42,7 +63,15 @@ const TsDropdown = ({ label, options, placeholder, searchPlaceholder, value, dis
|
|
|
42
63
|
!disabled && showDropdownMenu && (React.createElement("div", { className: "ts-dropdown-menu-container" },
|
|
43
64
|
searchable && (React.createElement("div", { className: "ts-dropdown-search" },
|
|
44
65
|
React.createElement(TsInput_1.TsInput, { icon: "search", placeholder: searchPlaceholder, value: searchValue, autoFocus: true, onChange: setSearchValue }))),
|
|
45
|
-
React.createElement("div", { className: "ts-dropdown-menu" },
|
|
66
|
+
React.createElement("div", { className: "ts-dropdown-menu" },
|
|
67
|
+
loading && (React.createElement("div", { className: "ts-dropdown-menu-loader" },
|
|
68
|
+
React.createElement(TsLoader_1.TsLoader, { size: "sm" }))),
|
|
69
|
+
!loading && isFetchError && (React.createElement("div", { className: "ts-dropdown-menu-error-text" }, "An error occured, try again later.")),
|
|
70
|
+
!loading && !isFetchError && items.length === 0 && (React.createElement("div", { className: "ts-dropdown-menu-error-text" }, "No options found.")),
|
|
71
|
+
!loading &&
|
|
72
|
+
!isFetchError &&
|
|
73
|
+
items.length > 0 &&
|
|
74
|
+
items.map(item => (React.createElement("div", { key: getOptionValue(item), className: "ts-dropdown-menu-item", onClick: () => handleSelectItem(item) }, (0, string_utils_1.capitalize)(getOptionLabel(item))))))))));
|
|
46
75
|
};
|
|
47
76
|
exports.TsDropdown = TsDropdown;
|
|
48
77
|
//# sourceMappingURL=TsDropdown.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TsDropdown.js","sourceRoot":"","sources":["../../../../src/components/TsDropdown/TsDropdown/TsDropdown.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,iCAA4C;AAE5C,gDAA6C;AAC7C,2DAAwD;AACxD,wEAAmE;AACnE,8DAAyD;AACzD,4BAA0B;
|
|
1
|
+
{"version":3,"file":"TsDropdown.js","sourceRoot":"","sources":["../../../../src/components/TsDropdown/TsDropdown/TsDropdown.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,iCAA4C;AAE5C,gDAA6C;AAC7C,2DAAwD;AACxD,wEAAmE;AACnE,8DAAyD;AACzD,4BAA0B;AAC1B,sDAAmD;AAE5C,MAAM,UAAU,GAAG,CAAoB,EAC5C,KAAK,EACL,OAAO,EACP,WAAW,EACX,iBAAiB,EACjB,KAAK,EACL,QAAQ,EACR,UAAU,EACV,cAAc,EACd,cAAc,EACd,WAAW,EACX,QAAQ,GACW,EAAE,EAAE;IACvB,MAAM,CAAC,gBAAgB,EAAE,mBAAmB,CAAC,GAAG,IAAA,gBAAQ,EAAU,KAAK,CAAC,CAAC;IACzE,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,IAAA,gBAAQ,EAAS,EAAE,CAAC,CAAC;IAC3D,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAM,OAAO,IAAI,EAAE,CAAC,CAAC;IACvD,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,EAAgB,KAAK,CAAC,CAAC;IACvE,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,IAAA,gBAAQ,EAAU,KAAK,CAAC,CAAC;IACvD,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,EAAU,KAAK,CAAC,CAAC;IACjE,MAAM,GAAG,GAAG,IAA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yC,CACtF;gBACA,CAAC,OAAO,IAAI,CAAC,YAAY,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,CAClD,6BAAK,SAAS,EAAC,6BAA6B,wBAAwB,CACrE;gBACA,CAAC,OAAO;oBACP,CAAC,YAAY;oBACb,KAAK,CAAC,MAAM,GAAG,CAAC;oBAChB,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAChB,6BACE,GAAG,EAAE,cAAc,CAAC,IAAI,CAAC,EACzB,SAAS,EAAC,uBAAuB,EACjC,OAAO,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAEpC,IAAA,yBAAU,EAAC,cAAc,CAAC,IAAI,CAAC,CAAC,CAC7B,CACP,CAAC,CACA,CACF,CACP,CACG,CACP,CAAC;AACJ,CAAC,CAAC;AAxHW,QAAA,UAAU,cAwHrB"}
|
|
@@ -6,6 +6,7 @@ import { TsInput } from '../../TsInput/TsInput/TsInput';
|
|
|
6
6
|
import { useClickOutside } from '../../../hooks/use-click-outside';
|
|
7
7
|
import { capitalize } from '../../../utils/string.utils';
|
|
8
8
|
import './TsDropdown.css';
|
|
9
|
+
import { TsLoader } from '../../TsLoader/TsLoader';
|
|
9
10
|
|
|
10
11
|
export const TsDropdown = <T extends unknown>({
|
|
11
12
|
label,
|
|
@@ -17,14 +18,30 @@ export const TsDropdown = <T extends unknown>({
|
|
|
17
18
|
searchable,
|
|
18
19
|
getOptionValue,
|
|
19
20
|
getOptionLabel,
|
|
21
|
+
loadOptions,
|
|
20
22
|
onChange,
|
|
21
23
|
}: TsDropdownProps<T>) => {
|
|
22
24
|
const [showDropdownMenu, setShowDropdownMenu] = useState<boolean>(false);
|
|
23
25
|
const [searchValue, setSearchValue] = useState<string>('');
|
|
24
26
|
const [items, setItems] = useState<T[]>(options ?? []);
|
|
25
27
|
const [selectedItem, setSelectedItem] = useState<T | undefined>(value);
|
|
28
|
+
const [loading, setLoading] = useState<boolean>(false);
|
|
29
|
+
const [isFetchError, setIsFetchError] = useState<boolean>(false);
|
|
26
30
|
const ref = useClickOutside(() => setShowDropdownMenu(false));
|
|
27
31
|
|
|
32
|
+
// ########
|
|
33
|
+
// Fetchers
|
|
34
|
+
const fetchOptions = () => {
|
|
35
|
+
if (!options && loadOptions) {
|
|
36
|
+
setLoading(true);
|
|
37
|
+
setIsFetchError(false);
|
|
38
|
+
loadOptions(searchValue)
|
|
39
|
+
.then(setItems)
|
|
40
|
+
.catch(() => setIsFetchError(true))
|
|
41
|
+
.finally(() => setLoading(false));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
28
45
|
// ########
|
|
29
46
|
// Handlers
|
|
30
47
|
const handleSelectItem = (item: T) => {
|
|
@@ -36,10 +53,18 @@ export const TsDropdown = <T extends unknown>({
|
|
|
36
53
|
|
|
37
54
|
// ########
|
|
38
55
|
// Watchers
|
|
56
|
+
useEffect(fetchOptions, []);
|
|
39
57
|
useEffect(() => {
|
|
40
58
|
if (searchable && options) {
|
|
41
59
|
setItems(options.filter(option => getOptionLabel(option).toLowerCase().includes(searchValue.toLowerCase())));
|
|
42
60
|
}
|
|
61
|
+
|
|
62
|
+
if (searchable && !options && loadOptions) {
|
|
63
|
+
const timeOutId = setTimeout(fetchOptions, 500);
|
|
64
|
+
return () => clearTimeout(timeOutId);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return () => {};
|
|
43
68
|
}, [searchValue]);
|
|
44
69
|
|
|
45
70
|
// #########
|
|
@@ -75,11 +100,29 @@ export const TsDropdown = <T extends unknown>({
|
|
|
75
100
|
)}
|
|
76
101
|
|
|
77
102
|
<div className="ts-dropdown-menu">
|
|
78
|
-
{
|
|
79
|
-
<div
|
|
80
|
-
|
|
103
|
+
{loading && (
|
|
104
|
+
<div className="ts-dropdown-menu-loader">
|
|
105
|
+
<TsLoader size="sm" />
|
|
81
106
|
</div>
|
|
82
|
-
)
|
|
107
|
+
)}
|
|
108
|
+
{!loading && isFetchError && (
|
|
109
|
+
<div className="ts-dropdown-menu-error-text">An error occured, try again later.</div>
|
|
110
|
+
)}
|
|
111
|
+
{!loading && !isFetchError && items.length === 0 && (
|
|
112
|
+
<div className="ts-dropdown-menu-error-text">No options found.</div>
|
|
113
|
+
)}
|
|
114
|
+
{!loading &&
|
|
115
|
+
!isFetchError &&
|
|
116
|
+
items.length > 0 &&
|
|
117
|
+
items.map(item => (
|
|
118
|
+
<div
|
|
119
|
+
key={getOptionValue(item)}
|
|
120
|
+
className="ts-dropdown-menu-item"
|
|
121
|
+
onClick={() => handleSelectItem(item)}
|
|
122
|
+
>
|
|
123
|
+
{capitalize(getOptionLabel(item))}
|
|
124
|
+
</div>
|
|
125
|
+
))}
|
|
83
126
|
</div>
|
|
84
127
|
</div>
|
|
85
128
|
)}
|
package/package.json
CHANGED
|
@@ -43,8 +43,7 @@
|
|
|
43
43
|
top: calc(100%+8px);
|
|
44
44
|
display: flex;
|
|
45
45
|
flex-direction: column;
|
|
46
|
-
width: 100%;
|
|
47
|
-
min-width: 320px;
|
|
46
|
+
min-width: 100%;
|
|
48
47
|
max-height: 250px;
|
|
49
48
|
padding: 12px;
|
|
50
49
|
box-shadow: 0px 8px 9px 0px #1010101a;
|
|
@@ -60,6 +59,16 @@
|
|
|
60
59
|
.ts-dropdown-menu {
|
|
61
60
|
overflow: auto;
|
|
62
61
|
}
|
|
62
|
+
.ts-dropdown-menu-loader {
|
|
63
|
+
display: flex;
|
|
64
|
+
justify-content: center;
|
|
65
|
+
}
|
|
66
|
+
.ts-dropdown-menu-error-text {
|
|
67
|
+
display: flex;
|
|
68
|
+
justify-content: center;
|
|
69
|
+
color: #7a7a7a;
|
|
70
|
+
font-style: italic;
|
|
71
|
+
}
|
|
63
72
|
.ts-dropdown-menu-item {
|
|
64
73
|
display: flex;
|
|
65
74
|
align-items: center;
|
|
@@ -6,6 +6,7 @@ import { TsInput } from '../../TsInput/TsInput/TsInput';
|
|
|
6
6
|
import { useClickOutside } from '../../../hooks/use-click-outside';
|
|
7
7
|
import { capitalize } from '../../../utils/string.utils';
|
|
8
8
|
import './TsDropdown.css';
|
|
9
|
+
import { TsLoader } from '../../TsLoader/TsLoader';
|
|
9
10
|
|
|
10
11
|
export const TsDropdown = <T extends unknown>({
|
|
11
12
|
label,
|
|
@@ -17,14 +18,30 @@ export const TsDropdown = <T extends unknown>({
|
|
|
17
18
|
searchable,
|
|
18
19
|
getOptionValue,
|
|
19
20
|
getOptionLabel,
|
|
21
|
+
loadOptions,
|
|
20
22
|
onChange,
|
|
21
23
|
}: TsDropdownProps<T>) => {
|
|
22
24
|
const [showDropdownMenu, setShowDropdownMenu] = useState<boolean>(false);
|
|
23
25
|
const [searchValue, setSearchValue] = useState<string>('');
|
|
24
26
|
const [items, setItems] = useState<T[]>(options ?? []);
|
|
25
27
|
const [selectedItem, setSelectedItem] = useState<T | undefined>(value);
|
|
28
|
+
const [loading, setLoading] = useState<boolean>(false);
|
|
29
|
+
const [isFetchError, setIsFetchError] = useState<boolean>(false);
|
|
26
30
|
const ref = useClickOutside(() => setShowDropdownMenu(false));
|
|
27
31
|
|
|
32
|
+
// ########
|
|
33
|
+
// Fetchers
|
|
34
|
+
const fetchOptions = () => {
|
|
35
|
+
if (!options && loadOptions) {
|
|
36
|
+
setLoading(true);
|
|
37
|
+
setIsFetchError(false);
|
|
38
|
+
loadOptions(searchValue)
|
|
39
|
+
.then(setItems)
|
|
40
|
+
.catch(() => setIsFetchError(true))
|
|
41
|
+
.finally(() => setLoading(false));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
28
45
|
// ########
|
|
29
46
|
// Handlers
|
|
30
47
|
const handleSelectItem = (item: T) => {
|
|
@@ -36,10 +53,18 @@ export const TsDropdown = <T extends unknown>({
|
|
|
36
53
|
|
|
37
54
|
// ########
|
|
38
55
|
// Watchers
|
|
56
|
+
useEffect(fetchOptions, []);
|
|
39
57
|
useEffect(() => {
|
|
40
58
|
if (searchable && options) {
|
|
41
59
|
setItems(options.filter(option => getOptionLabel(option).toLowerCase().includes(searchValue.toLowerCase())));
|
|
42
60
|
}
|
|
61
|
+
|
|
62
|
+
if (searchable && !options && loadOptions) {
|
|
63
|
+
const timeOutId = setTimeout(fetchOptions, 500);
|
|
64
|
+
return () => clearTimeout(timeOutId);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return () => {};
|
|
43
68
|
}, [searchValue]);
|
|
44
69
|
|
|
45
70
|
// #########
|
|
@@ -75,11 +100,29 @@ export const TsDropdown = <T extends unknown>({
|
|
|
75
100
|
)}
|
|
76
101
|
|
|
77
102
|
<div className="ts-dropdown-menu">
|
|
78
|
-
{
|
|
79
|
-
<div
|
|
80
|
-
|
|
103
|
+
{loading && (
|
|
104
|
+
<div className="ts-dropdown-menu-loader">
|
|
105
|
+
<TsLoader size="sm" />
|
|
81
106
|
</div>
|
|
82
|
-
)
|
|
107
|
+
)}
|
|
108
|
+
{!loading && isFetchError && (
|
|
109
|
+
<div className="ts-dropdown-menu-error-text">An error occured, try again later.</div>
|
|
110
|
+
)}
|
|
111
|
+
{!loading && !isFetchError && items.length === 0 && (
|
|
112
|
+
<div className="ts-dropdown-menu-error-text">No options found.</div>
|
|
113
|
+
)}
|
|
114
|
+
{!loading &&
|
|
115
|
+
!isFetchError &&
|
|
116
|
+
items.length > 0 &&
|
|
117
|
+
items.map(item => (
|
|
118
|
+
<div
|
|
119
|
+
key={getOptionValue(item)}
|
|
120
|
+
className="ts-dropdown-menu-item"
|
|
121
|
+
onClick={() => handleSelectItem(item)}
|
|
122
|
+
>
|
|
123
|
+
{capitalize(getOptionLabel(item))}
|
|
124
|
+
</div>
|
|
125
|
+
))}
|
|
83
126
|
</div>
|
|
84
127
|
</div>
|
|
85
128
|
)}
|