react-science 0.37.0 → 1.0.0
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/lib/components/hooks/useSelect.d.ts +14 -3
- package/lib/components/hooks/useSelect.d.ts.map +1 -1
- package/lib/components/hooks/useSelect.js +33 -7
- package/lib/components/hooks/useSelect.js.map +1 -1
- package/lib-esm/components/hooks/useSelect.d.ts +14 -3
- package/lib-esm/components/hooks/useSelect.d.ts.map +1 -1
- package/lib-esm/components/hooks/useSelect.js +33 -7
- package/lib-esm/components/hooks/useSelect.js.map +1 -1
- package/package.json +1 -1
- package/src/components/hooks/useSelect.tsx +72 -21
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { ItemRenderer } from '@blueprintjs/select';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
3
|
+
type FilterType<SourceType, Type> = Pick<SourceType, {
|
|
4
|
+
[K in keyof SourceType]: SourceType[K] extends Type ? K : never;
|
|
5
|
+
}[keyof SourceType]>;
|
|
6
|
+
interface BaseOptions<T> {
|
|
7
|
+
itemTextKey: keyof FilterType<T, string>;
|
|
8
|
+
defaultSelectedItem?: T;
|
|
9
|
+
}
|
|
10
|
+
interface RenderOptions<T> {
|
|
11
|
+
getItemText: (item: T) => string;
|
|
12
|
+
defaultSelectedItem?: T;
|
|
13
|
+
}
|
|
14
|
+
type SelectOptions<T> = BaseOptions<T> | RenderOptions<T>;
|
|
15
|
+
export declare function useSelect<T>(options: SelectOptions<T>): {
|
|
6
16
|
value: T | null;
|
|
7
17
|
setValue: import("react").Dispatch<import("react").SetStateAction<T | null>>;
|
|
8
18
|
itemRenderer: ItemRenderer<T>;
|
|
@@ -18,4 +28,5 @@ export declare function useSelect<T extends {
|
|
|
18
28
|
itemPredicate: (query: string, item: T) => boolean;
|
|
19
29
|
itemListPredicate: (query: string, items: T[]) => T[];
|
|
20
30
|
};
|
|
31
|
+
export {};
|
|
21
32
|
//# sourceMappingURL=useSelect.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSelect.d.ts","sourceRoot":"","sources":["../../../src/components/hooks/useSelect.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGnD,
|
|
1
|
+
{"version":3,"file":"useSelect.d.ts","sourceRoot":"","sources":["../../../src/components/hooks/useSelect.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGnD,KAAK,UAAU,CAAC,UAAU,EAAE,IAAI,IAAI,IAAI,CACtC,UAAU,EACV;KACG,CAAC,IAAI,MAAM,UAAU,GAAG,UAAU,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,CAAC,GAAG,KAAK;CAChE,CAAC,MAAM,UAAU,CAAC,CACpB,CAAC;AAEF,UAAU,WAAW,CAAC,CAAC;IACrB,WAAW,EAAE,MAAM,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IACzC,mBAAmB,CAAC,EAAE,CAAC,CAAC;CACzB;AAED,UAAU,aAAa,CAAC,CAAC;IACvB,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC;IACjC,mBAAmB,CAAC,EAAE,CAAC,CAAC;CACzB;AAED,KAAK,aAAa,CAAC,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;AAsB1D,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;;;;;;yBAOjC,WAAW;;;;;;;2BAWA,MAAM,QAAQ,CAAC;+BAKX,MAAM,SAAS,CAAC,EAAE;EAiBrD"}
|
|
@@ -4,9 +4,23 @@ exports.useSelect = void 0;
|
|
|
4
4
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
5
|
const core_1 = require("@blueprintjs/core");
|
|
6
6
|
const react_1 = require("react");
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
function isAccessLabelByKey(options) {
|
|
8
|
+
return 'itemTextKey' in options;
|
|
9
|
+
}
|
|
10
|
+
function getLabel(item, options) {
|
|
11
|
+
const isAccessLByLabelKey = isAccessLabelByKey(options);
|
|
12
|
+
if (!item || (isAccessLByLabelKey && !options.itemTextKey)) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
if (isAccessLByLabelKey) {
|
|
16
|
+
return item[options.itemTextKey];
|
|
17
|
+
}
|
|
18
|
+
return options.getItemText(item);
|
|
19
|
+
}
|
|
20
|
+
function useSelect(options) {
|
|
21
|
+
const { defaultSelectedItem = null } = options;
|
|
22
|
+
const [value, setValue] = (0, react_1.useState)(defaultSelectedItem);
|
|
23
|
+
const itemRenderer = getItemRenderer(value, options);
|
|
10
24
|
const onItemSelect = setValue;
|
|
11
25
|
const popoverProps = {
|
|
12
26
|
onOpened: (node) => {
|
|
@@ -21,10 +35,18 @@ function useSelect() {
|
|
|
21
35
|
style: { display: 'inline-block' },
|
|
22
36
|
};
|
|
23
37
|
const itemPredicate = (query, item) => {
|
|
24
|
-
|
|
38
|
+
const label = getLabel(item, options);
|
|
39
|
+
if (!label)
|
|
40
|
+
return false;
|
|
41
|
+
return label.toLowerCase().includes(query.toLowerCase());
|
|
25
42
|
};
|
|
26
43
|
const itemListPredicate = (query, items) => {
|
|
27
|
-
return items.filter((item) =>
|
|
44
|
+
return items.filter((item) => {
|
|
45
|
+
const label = getLabel(item, options);
|
|
46
|
+
if (!label)
|
|
47
|
+
return false;
|
|
48
|
+
return label.toLowerCase().includes(query.toLowerCase());
|
|
49
|
+
});
|
|
28
50
|
};
|
|
29
51
|
return {
|
|
30
52
|
value,
|
|
@@ -38,8 +60,12 @@ function useSelect() {
|
|
|
38
60
|
};
|
|
39
61
|
}
|
|
40
62
|
exports.useSelect = useSelect;
|
|
41
|
-
function getItemRenderer(value) {
|
|
42
|
-
const
|
|
63
|
+
function getItemRenderer(value, options) {
|
|
64
|
+
const selectedLabel = getLabel(value, options);
|
|
65
|
+
const render = (item, { handleClick, handleFocus, modifiers, index }) => {
|
|
66
|
+
const label = getLabel(item, options);
|
|
67
|
+
return ((0, jsx_runtime_1.jsx)(core_1.MenuItem, { active: modifiers.active, disabled: modifiers.disabled, selected: selectedLabel === label, onClick: handleClick, onFocus: handleFocus, roleStructure: "listoption", text: label }, index));
|
|
68
|
+
};
|
|
43
69
|
return render;
|
|
44
70
|
}
|
|
45
71
|
//# sourceMappingURL=useSelect.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSelect.js","sourceRoot":"","sources":["../../../src/components/hooks/useSelect.tsx"],"names":[],"mappings":";;;;AAAA,4CAA6C;AAE7C,iCAAiC;
|
|
1
|
+
{"version":3,"file":"useSelect.js","sourceRoot":"","sources":["../../../src/components/hooks/useSelect.tsx"],"names":[],"mappings":";;;;AAAA,4CAA6C;AAE7C,iCAAiC;AAqBjC,SAAS,kBAAkB,CACzB,OAAyB;IAEzB,OAAO,aAAa,IAAI,OAAO,CAAC;AAClC,CAAC;AAED,SAAS,QAAQ,CAAI,IAAO,EAAE,OAAyB;IACrD,MAAM,mBAAmB,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAExD,IAAI,CAAC,IAAI,IAAI,CAAC,mBAAmB,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,CAAC;QAC3D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,mBAAmB,EAAE,CAAC;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,CAAW,CAAC;IAC7C,CAAC;IAED,OAAO,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC;AAED,SAAgB,SAAS,CAAI,OAAyB;IACpD,MAAM,EAAE,mBAAmB,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IAE/C,MAAM,CAAC,KAAK,EAAE,QAAQ,C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,YAAY;QACZ,YAAY;QACZ,kBAAkB;QAClB,aAAa;QACb,iBAAiB;KAClB,CAAC;AACJ,CAAC;AAxCD,8BAwCC;AAED,SAAS,eAAe,CAAI,KAAQ,EAAE,OAAyB;IAC7D,MAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAE/C,MAAM,MAAM,GAAoB,CAC9B,IAAI,EACJ,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,EAAE,EAC9C,EAAE;QACF,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACtC,OAAO,CACL,uBAAC,eAAQ,IACP,MAAM,EAAE,SAAS,CAAC,MAAM,EACxB,QAAQ,EAAE,SAAS,CAAC,QAAQ,EAC5B,QAAQ,EAAE,aAAa,KAAK,KAAK,EAEjC,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,WAAW,EACpB,aAAa,EAAC,YAAY,EAC1B,IAAI,EAAE,KAAK,IAJN,KAAK,CAKV,CACH,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { ItemRenderer } from '@blueprintjs/select';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
3
|
+
type FilterType<SourceType, Type> = Pick<SourceType, {
|
|
4
|
+
[K in keyof SourceType]: SourceType[K] extends Type ? K : never;
|
|
5
|
+
}[keyof SourceType]>;
|
|
6
|
+
interface BaseOptions<T> {
|
|
7
|
+
itemTextKey: keyof FilterType<T, string>;
|
|
8
|
+
defaultSelectedItem?: T;
|
|
9
|
+
}
|
|
10
|
+
interface RenderOptions<T> {
|
|
11
|
+
getItemText: (item: T) => string;
|
|
12
|
+
defaultSelectedItem?: T;
|
|
13
|
+
}
|
|
14
|
+
type SelectOptions<T> = BaseOptions<T> | RenderOptions<T>;
|
|
15
|
+
export declare function useSelect<T>(options: SelectOptions<T>): {
|
|
6
16
|
value: T | null;
|
|
7
17
|
setValue: import("react").Dispatch<import("react").SetStateAction<T | null>>;
|
|
8
18
|
itemRenderer: ItemRenderer<T>;
|
|
@@ -18,4 +28,5 @@ export declare function useSelect<T extends {
|
|
|
18
28
|
itemPredicate: (query: string, item: T) => boolean;
|
|
19
29
|
itemListPredicate: (query: string, items: T[]) => T[];
|
|
20
30
|
};
|
|
31
|
+
export {};
|
|
21
32
|
//# sourceMappingURL=useSelect.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSelect.d.ts","sourceRoot":"","sources":["../../../src/components/hooks/useSelect.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGnD,
|
|
1
|
+
{"version":3,"file":"useSelect.d.ts","sourceRoot":"","sources":["../../../src/components/hooks/useSelect.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGnD,KAAK,UAAU,CAAC,UAAU,EAAE,IAAI,IAAI,IAAI,CACtC,UAAU,EACV;KACG,CAAC,IAAI,MAAM,UAAU,GAAG,UAAU,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,CAAC,GAAG,KAAK;CAChE,CAAC,MAAM,UAAU,CAAC,CACpB,CAAC;AAEF,UAAU,WAAW,CAAC,CAAC;IACrB,WAAW,EAAE,MAAM,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IACzC,mBAAmB,CAAC,EAAE,CAAC,CAAC;CACzB;AAED,UAAU,aAAa,CAAC,CAAC;IACvB,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC;IACjC,mBAAmB,CAAC,EAAE,CAAC,CAAC;CACzB;AAED,KAAK,aAAa,CAAC,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;AAsB1D,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;;;;;;yBAOjC,WAAW;;;;;;;2BAWA,MAAM,QAAQ,CAAC;+BAKX,MAAM,SAAS,CAAC,EAAE;EAiBrD"}
|
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { MenuItem } from '@blueprintjs/core';
|
|
3
3
|
import { useState } from 'react';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
function isAccessLabelByKey(options) {
|
|
5
|
+
return 'itemTextKey' in options;
|
|
6
|
+
}
|
|
7
|
+
function getLabel(item, options) {
|
|
8
|
+
const isAccessLByLabelKey = isAccessLabelByKey(options);
|
|
9
|
+
if (!item || (isAccessLByLabelKey && !options.itemTextKey)) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
if (isAccessLByLabelKey) {
|
|
13
|
+
return item[options.itemTextKey];
|
|
14
|
+
}
|
|
15
|
+
return options.getItemText(item);
|
|
16
|
+
}
|
|
17
|
+
export function useSelect(options) {
|
|
18
|
+
const { defaultSelectedItem = null } = options;
|
|
19
|
+
const [value, setValue] = useState(defaultSelectedItem);
|
|
20
|
+
const itemRenderer = getItemRenderer(value, options);
|
|
7
21
|
const onItemSelect = setValue;
|
|
8
22
|
const popoverProps = {
|
|
9
23
|
onOpened: (node) => {
|
|
@@ -18,10 +32,18 @@ export function useSelect() {
|
|
|
18
32
|
style: { display: 'inline-block' },
|
|
19
33
|
};
|
|
20
34
|
const itemPredicate = (query, item) => {
|
|
21
|
-
|
|
35
|
+
const label = getLabel(item, options);
|
|
36
|
+
if (!label)
|
|
37
|
+
return false;
|
|
38
|
+
return label.toLowerCase().includes(query.toLowerCase());
|
|
22
39
|
};
|
|
23
40
|
const itemListPredicate = (query, items) => {
|
|
24
|
-
return items.filter((item) =>
|
|
41
|
+
return items.filter((item) => {
|
|
42
|
+
const label = getLabel(item, options);
|
|
43
|
+
if (!label)
|
|
44
|
+
return false;
|
|
45
|
+
return label.toLowerCase().includes(query.toLowerCase());
|
|
46
|
+
});
|
|
25
47
|
};
|
|
26
48
|
return {
|
|
27
49
|
value,
|
|
@@ -34,8 +56,12 @@ export function useSelect() {
|
|
|
34
56
|
itemListPredicate,
|
|
35
57
|
};
|
|
36
58
|
}
|
|
37
|
-
function getItemRenderer(value) {
|
|
38
|
-
const
|
|
59
|
+
function getItemRenderer(value, options) {
|
|
60
|
+
const selectedLabel = getLabel(value, options);
|
|
61
|
+
const render = (item, { handleClick, handleFocus, modifiers, index }) => {
|
|
62
|
+
const label = getLabel(item, options);
|
|
63
|
+
return (_jsx(MenuItem, { active: modifiers.active, disabled: modifiers.disabled, selected: selectedLabel === label, onClick: handleClick, onFocus: handleFocus, roleStructure: "listoption", text: label }, index));
|
|
64
|
+
};
|
|
39
65
|
return render;
|
|
40
66
|
}
|
|
41
67
|
//# sourceMappingURL=useSelect.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSelect.js","sourceRoot":"","sources":["../../../src/components/hooks/useSelect.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"useSelect.js","sourceRoot":"","sources":["../../../src/components/hooks/useSelect.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAqBjC,SAAS,kBAAkB,CACzB,OAAyB;IAEzB,OAAO,aAAa,IAAI,OAAO,CAAC;AAClC,CAAC;AAED,SAAS,QAAQ,CAAI,IAAO,EAAE,OAAyB;IACrD,MAAM,mBAAmB,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAExD,IAAI,CAAC,IAAI,IAAI,CAAC,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Aa,EAAE,KAAU,EAAE,EAAE;QACtD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;YAC3B,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YACtC,IAAI,CAAC,KAAK;gBAAE,OAAO,KAAK,CAAC;YACzB,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC;QAC3D,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IACF,OAAO;QACL,KAAK;QACL,QAAQ;QACR,YAAY;QACZ,YAAY;QACZ,YAAY;QACZ,kBAAkB;QAClB,aAAa;QACb,iBAAiB;KAClB,CAAC;AACJ,CAAC;AAED,SAAS,eAAe,CAAI,KAAQ,EAAE,OAAyB;IAC7D,MAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAE/C,MAAM,MAAM,GAAoB,CAC9B,IAAI,EACJ,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,EAAE,EAC9C,EAAE;QACF,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACtC,OAAO,CACL,KAAC,QAAQ,IACP,MAAM,EAAE,SAAS,CAAC,MAAM,EACxB,QAAQ,EAAE,SAAS,CAAC,QAAQ,EAC5B,QAAQ,EAAE,aAAa,KAAK,KAAK,EAEjC,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,WAAW,EACpB,aAAa,EAAC,YAAY,EAC1B,IAAI,EAAE,KAAK,IAJN,KAAK,CAKV,CACH,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -2,9 +2,50 @@ import { MenuItem } from '@blueprintjs/core';
|
|
|
2
2
|
import { ItemRenderer } from '@blueprintjs/select';
|
|
3
3
|
import { useState } from 'react';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
type FilterType<SourceType, Type> = Pick<
|
|
6
|
+
SourceType,
|
|
7
|
+
{
|
|
8
|
+
[K in keyof SourceType]: SourceType[K] extends Type ? K : never;
|
|
9
|
+
}[keyof SourceType]
|
|
10
|
+
>;
|
|
11
|
+
|
|
12
|
+
interface BaseOptions<T> {
|
|
13
|
+
itemTextKey: keyof FilterType<T, string>;
|
|
14
|
+
defaultSelectedItem?: T;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface RenderOptions<T> {
|
|
18
|
+
getItemText: (item: T) => string;
|
|
19
|
+
defaultSelectedItem?: T;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type SelectOptions<T> = BaseOptions<T> | RenderOptions<T>;
|
|
23
|
+
|
|
24
|
+
function isAccessLabelByKey<T>(
|
|
25
|
+
options: SelectOptions<T>,
|
|
26
|
+
): options is BaseOptions<T> {
|
|
27
|
+
return 'itemTextKey' in options;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getLabel<T>(item: T, options: SelectOptions<T>) {
|
|
31
|
+
const isAccessLByLabelKey = isAccessLabelByKey(options);
|
|
32
|
+
|
|
33
|
+
if (!item || (isAccessLByLabelKey && !options.itemTextKey)) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (isAccessLByLabelKey) {
|
|
38
|
+
return item[options.itemTextKey] as string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return options.getItemText(item);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function useSelect<T>(options: SelectOptions<T>) {
|
|
45
|
+
const { defaultSelectedItem = null } = options;
|
|
46
|
+
|
|
47
|
+
const [value, setValue] = useState<T | null>(defaultSelectedItem);
|
|
48
|
+
const itemRenderer = getItemRenderer(value as T, options);
|
|
8
49
|
const onItemSelect = setValue;
|
|
9
50
|
const popoverProps = {
|
|
10
51
|
onOpened: (node: HTMLElement) => {
|
|
@@ -19,12 +60,16 @@ export function useSelect<T extends { label: string }>() {
|
|
|
19
60
|
style: { display: 'inline-block' },
|
|
20
61
|
};
|
|
21
62
|
const itemPredicate = (query: string, item: T) => {
|
|
22
|
-
|
|
63
|
+
const label = getLabel(item, options);
|
|
64
|
+
if (!label) return false;
|
|
65
|
+
return label.toLowerCase().includes(query.toLowerCase());
|
|
23
66
|
};
|
|
24
67
|
const itemListPredicate = (query: string, items: T[]) => {
|
|
25
|
-
return items.filter((item) =>
|
|
26
|
-
|
|
27
|
-
|
|
68
|
+
return items.filter((item) => {
|
|
69
|
+
const label = getLabel(item, options);
|
|
70
|
+
if (!label) return false;
|
|
71
|
+
return label.toLowerCase().includes(query.toLowerCase());
|
|
72
|
+
});
|
|
28
73
|
};
|
|
29
74
|
return {
|
|
30
75
|
value,
|
|
@@ -38,21 +83,27 @@ export function useSelect<T extends { label: string }>() {
|
|
|
38
83
|
};
|
|
39
84
|
}
|
|
40
85
|
|
|
41
|
-
function getItemRenderer<T
|
|
86
|
+
function getItemRenderer<T>(value: T, options: SelectOptions<T>) {
|
|
87
|
+
const selectedLabel = getLabel(value, options);
|
|
88
|
+
|
|
42
89
|
const render: ItemRenderer<T> = (
|
|
43
|
-
|
|
90
|
+
item,
|
|
44
91
|
{ handleClick, handleFocus, modifiers, index },
|
|
45
|
-
) =>
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
92
|
+
) => {
|
|
93
|
+
const label = getLabel(item, options);
|
|
94
|
+
return (
|
|
95
|
+
<MenuItem
|
|
96
|
+
active={modifiers.active}
|
|
97
|
+
disabled={modifiers.disabled}
|
|
98
|
+
selected={selectedLabel === label}
|
|
99
|
+
key={index}
|
|
100
|
+
onClick={handleClick}
|
|
101
|
+
onFocus={handleFocus}
|
|
102
|
+
roleStructure="listoption"
|
|
103
|
+
text={label}
|
|
104
|
+
/>
|
|
105
|
+
);
|
|
106
|
+
};
|
|
107
|
+
|
|
57
108
|
return render;
|
|
58
109
|
}
|