@vx-oss/heroui-v2-use-aria-multiselect 2.4.20-alpha.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/LICENSE +32 -0
- package/README.md +24 -0
- package/dist/chunk-74XVDT4G.mjs +40 -0
- package/dist/chunk-FHVPTEOP.mjs +69 -0
- package/dist/chunk-RLX3CPDX.mjs +144 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +273 -0
- package/dist/index.mjs +14 -0
- package/dist/use-multiselect-list-state.d.mts +19 -0
- package/dist/use-multiselect-list-state.d.ts +19 -0
- package/dist/use-multiselect-list-state.js +64 -0
- package/dist/use-multiselect-list-state.mjs +6 -0
- package/dist/use-multiselect-state.d.mts +35 -0
- package/dist/use-multiselect-state.d.ts +35 -0
- package/dist/use-multiselect-state.js +128 -0
- package/dist/use-multiselect-state.mjs +7 -0
- package/dist/use-multiselect.d.mts +29 -0
- package/dist/use-multiselect.d.ts +29 -0
- package/dist/use-multiselect.js +168 -0
- package/dist/use-multiselect.mjs +6 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
The MIT License (MIT)
|
|
4
|
+
|
|
5
|
+
Parameters
|
|
6
|
+
|
|
7
|
+
Creator / Maintainer : Vezham Technologies Private Limited
|
|
8
|
+
Original Author : NextUI Inc
|
|
9
|
+
Licensor : Vezham Technologies Private Limited
|
|
10
|
+
|
|
11
|
+
Copyright © 2025, Vezham Technologies Private Limited. All rights reserved.
|
|
12
|
+
Copyright (c) 2020, NextUI Inc.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
17
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
18
|
+
in the Software without restriction, including without limitation the rights
|
|
19
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
20
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
21
|
+
furnished to do so, subject to the following conditions:
|
|
22
|
+
|
|
23
|
+
The above copyright notice and this permission notice shall be included in all
|
|
24
|
+
copies or substantial portions of the Software.
|
|
25
|
+
|
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
27
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
28
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
29
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
30
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
31
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
32
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @vx-oss/heroui-v2-use-aria-multiselect
|
|
2
|
+
|
|
3
|
+
A Quick description of the component
|
|
4
|
+
|
|
5
|
+
> This is an internal utility, not intended for public usage.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
yarn add @vx-oss/heroui-v2-use-aria-multiselect
|
|
11
|
+
# or
|
|
12
|
+
npm i @vx-oss/heroui-v2-use-aria-multiselect
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Contribution
|
|
16
|
+
|
|
17
|
+
Yes please! See the
|
|
18
|
+
[contributing guidelines](https://github.com/heroui-inc/heroui/blob/master/CONTRIBUTING.md)
|
|
19
|
+
for details.
|
|
20
|
+
|
|
21
|
+
## License
|
|
22
|
+
|
|
23
|
+
This project is licensed under the terms of the
|
|
24
|
+
[MIT license](https://github.com/heroui-inc/heroui/blob/master/LICENSE).
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/use-multiselect-list-state.ts
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { useListState } from "@react-stately/list";
|
|
4
|
+
function useMultiSelectListState(props) {
|
|
5
|
+
const {
|
|
6
|
+
collection,
|
|
7
|
+
disabledKeys,
|
|
8
|
+
selectionManager,
|
|
9
|
+
selectionManager: { setSelectedKeys, selectedKeys, selectionMode }
|
|
10
|
+
} = useListState(props);
|
|
11
|
+
const missingKeys = useMemo(() => {
|
|
12
|
+
if (!props.isLoading && selectedKeys.size !== 0) {
|
|
13
|
+
return Array.from(selectedKeys).filter(Boolean).filter((key) => !collection.getItem(key));
|
|
14
|
+
}
|
|
15
|
+
return [];
|
|
16
|
+
}, [selectedKeys, collection]);
|
|
17
|
+
const selectedItems = selectedKeys.size !== 0 ? Array.from(selectedKeys).map((key) => {
|
|
18
|
+
return collection.getItem(key);
|
|
19
|
+
}).filter(Boolean) : null;
|
|
20
|
+
if (missingKeys.length) {
|
|
21
|
+
console.warn(
|
|
22
|
+
`Select: Keys "${missingKeys.join(
|
|
23
|
+
", "
|
|
24
|
+
)}" passed to "selectedKeys" are not present in the collection.`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
collection,
|
|
29
|
+
disabledKeys,
|
|
30
|
+
selectionManager,
|
|
31
|
+
selectionMode,
|
|
32
|
+
selectedKeys,
|
|
33
|
+
setSelectedKeys: setSelectedKeys.bind(selectionManager),
|
|
34
|
+
selectedItems
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export {
|
|
39
|
+
useMultiSelectListState
|
|
40
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useMultiSelectListState
|
|
3
|
+
} from "./chunk-74XVDT4G.mjs";
|
|
4
|
+
|
|
5
|
+
// src/use-multiselect-state.ts
|
|
6
|
+
import { useMenuTriggerState } from "@react-stately/menu";
|
|
7
|
+
import { useFormValidationState } from "@react-stately/form";
|
|
8
|
+
import { useState } from "react";
|
|
9
|
+
function useMultiSelectState({
|
|
10
|
+
validate,
|
|
11
|
+
validationBehavior,
|
|
12
|
+
...props
|
|
13
|
+
}) {
|
|
14
|
+
const [isFocused, setFocused] = useState(false);
|
|
15
|
+
const [focusStrategy, setFocusStrategy] = useState(null);
|
|
16
|
+
const triggerState = useMenuTriggerState(props);
|
|
17
|
+
const listState = useMultiSelectListState({
|
|
18
|
+
...props,
|
|
19
|
+
onSelectionChange: (keys) => {
|
|
20
|
+
if (props.onSelectionChange != null) {
|
|
21
|
+
if (keys === "all") {
|
|
22
|
+
props.onSelectionChange(new Set(listState.collection.getKeys()));
|
|
23
|
+
} else {
|
|
24
|
+
props.onSelectionChange(keys);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (props.selectionMode === "single") {
|
|
28
|
+
triggerState.close();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
const validationState = useFormValidationState({
|
|
33
|
+
...props,
|
|
34
|
+
validationBehavior,
|
|
35
|
+
validate: (value) => {
|
|
36
|
+
if (!validate) return;
|
|
37
|
+
const keys = Array.from(value);
|
|
38
|
+
return validate(props.selectionMode === "single" ? keys[0] : keys);
|
|
39
|
+
},
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
value: listState.selectedKeys
|
|
42
|
+
});
|
|
43
|
+
const shouldHideContent = listState.collection.size === 0 && props.hideEmptyContent;
|
|
44
|
+
return {
|
|
45
|
+
...validationState,
|
|
46
|
+
...listState,
|
|
47
|
+
...triggerState,
|
|
48
|
+
focusStrategy,
|
|
49
|
+
close() {
|
|
50
|
+
triggerState.close();
|
|
51
|
+
},
|
|
52
|
+
open(focusStrategy2 = null) {
|
|
53
|
+
if (shouldHideContent) return;
|
|
54
|
+
setFocusStrategy(focusStrategy2);
|
|
55
|
+
triggerState.open();
|
|
56
|
+
},
|
|
57
|
+
toggle(focusStrategy2 = null) {
|
|
58
|
+
if (shouldHideContent) return;
|
|
59
|
+
setFocusStrategy(focusStrategy2);
|
|
60
|
+
triggerState.toggle();
|
|
61
|
+
},
|
|
62
|
+
isFocused,
|
|
63
|
+
setFocused
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export {
|
|
68
|
+
useMultiSelectState
|
|
69
|
+
};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// src/use-multiselect.ts
|
|
2
|
+
import { useCollator } from "@react-aria/i18n";
|
|
3
|
+
import { setInteractionModality } from "@react-aria/interactions";
|
|
4
|
+
import { useField } from "@react-aria/label";
|
|
5
|
+
import { useMenuTrigger } from "@react-aria/menu";
|
|
6
|
+
import { ListKeyboardDelegate, useTypeSelect } from "@react-aria/selection";
|
|
7
|
+
import { chain, filterDOMProps, mergeProps, useId } from "@react-aria/utils";
|
|
8
|
+
import { useMemo } from "react";
|
|
9
|
+
function useMultiSelect(props, state, ref) {
|
|
10
|
+
const { disallowEmptySelection, isDisabled } = props;
|
|
11
|
+
const collator = useCollator({ usage: "search", sensitivity: "base" });
|
|
12
|
+
const delegate = useMemo(
|
|
13
|
+
() => new ListKeyboardDelegate(state.collection, state.disabledKeys, null, collator),
|
|
14
|
+
[state.collection, state.disabledKeys, collator]
|
|
15
|
+
);
|
|
16
|
+
const { menuTriggerProps, menuProps } = useMenuTrigger(
|
|
17
|
+
{
|
|
18
|
+
isDisabled,
|
|
19
|
+
type: "listbox"
|
|
20
|
+
},
|
|
21
|
+
state,
|
|
22
|
+
ref
|
|
23
|
+
);
|
|
24
|
+
const triggerOnKeyDown = (e) => {
|
|
25
|
+
if (state.selectionMode === "single") {
|
|
26
|
+
switch (e.key) {
|
|
27
|
+
case "ArrowLeft": {
|
|
28
|
+
e.preventDefault();
|
|
29
|
+
const key = state.selectedKeys.size > 0 ? delegate.getKeyAbove(state.selectedKeys.values().next().value) : delegate.getFirstKey();
|
|
30
|
+
if (key) {
|
|
31
|
+
state.setSelectedKeys([key]);
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
case "ArrowRight": {
|
|
36
|
+
e.preventDefault();
|
|
37
|
+
const key = state.selectedKeys.size > 0 ? delegate.getKeyBelow(state.selectedKeys.values().next().value) : delegate.getFirstKey();
|
|
38
|
+
if (key) {
|
|
39
|
+
state.setSelectedKeys([key]);
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const { typeSelectProps } = useTypeSelect({
|
|
47
|
+
keyboardDelegate: delegate,
|
|
48
|
+
selectionManager: state.selectionManager,
|
|
49
|
+
onTypeSelect(key) {
|
|
50
|
+
state.setSelectedKeys([key]);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
const { isInvalid, validationErrors, validationDetails } = state.displayValidation;
|
|
54
|
+
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = useField({
|
|
55
|
+
...props,
|
|
56
|
+
labelElementType: "span",
|
|
57
|
+
isInvalid,
|
|
58
|
+
errorMessage: props.errorMessage || validationErrors
|
|
59
|
+
});
|
|
60
|
+
typeSelectProps.onKeyDown = typeSelectProps.onKeyDownCapture;
|
|
61
|
+
delete typeSelectProps.onKeyDownCapture;
|
|
62
|
+
menuTriggerProps.onPressStart = (e) => {
|
|
63
|
+
if (e.pointerType !== "touch" && e.pointerType !== "keyboard" && !isDisabled) {
|
|
64
|
+
state.toggle(e.pointerType === "virtual" ? "first" : null);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const domProps = filterDOMProps(props, { labelable: true });
|
|
68
|
+
const triggerProps = mergeProps(typeSelectProps, menuTriggerProps, fieldProps);
|
|
69
|
+
const valueId = useId();
|
|
70
|
+
return {
|
|
71
|
+
labelProps: {
|
|
72
|
+
...labelProps,
|
|
73
|
+
onClick: () => {
|
|
74
|
+
var _a;
|
|
75
|
+
if (!props.isDisabled) {
|
|
76
|
+
(_a = ref.current) == null ? void 0 : _a.focus();
|
|
77
|
+
setInteractionModality("keyboard");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
triggerProps: mergeProps(domProps, {
|
|
82
|
+
...triggerProps,
|
|
83
|
+
onKeyDown: chain(triggerProps.onKeyDown, triggerOnKeyDown, props.onKeyDown),
|
|
84
|
+
onKeyUp: props.onKeyUp,
|
|
85
|
+
"aria-labelledby": [
|
|
86
|
+
valueId,
|
|
87
|
+
triggerProps["aria-labelledby"],
|
|
88
|
+
triggerProps["aria-label"] && !triggerProps["aria-labelledby"] ? triggerProps.id : null
|
|
89
|
+
].join(" "),
|
|
90
|
+
onFocus(e) {
|
|
91
|
+
if (state.isFocused) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (props.onFocus) {
|
|
95
|
+
props.onFocus(e);
|
|
96
|
+
}
|
|
97
|
+
state.setFocused(true);
|
|
98
|
+
},
|
|
99
|
+
onBlur(e) {
|
|
100
|
+
if (state.isOpen) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (props.onBlur) {
|
|
104
|
+
props.onBlur(e);
|
|
105
|
+
}
|
|
106
|
+
state.setFocused(false);
|
|
107
|
+
}
|
|
108
|
+
}),
|
|
109
|
+
valueProps: {
|
|
110
|
+
id: valueId
|
|
111
|
+
},
|
|
112
|
+
menuProps: {
|
|
113
|
+
...menuProps,
|
|
114
|
+
disallowEmptySelection,
|
|
115
|
+
autoFocus: state.focusStrategy || true,
|
|
116
|
+
shouldSelectOnPressUp: true,
|
|
117
|
+
shouldFocusOnHover: true,
|
|
118
|
+
onBlur: (e) => {
|
|
119
|
+
if (e.currentTarget.contains(e.relatedTarget)) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (props.onBlur) {
|
|
123
|
+
props.onBlur(e);
|
|
124
|
+
}
|
|
125
|
+
state.setFocused(false);
|
|
126
|
+
},
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
onFocus: menuProps == null ? void 0 : menuProps.onFocus,
|
|
129
|
+
"aria-labelledby": [
|
|
130
|
+
fieldProps["aria-labelledby"],
|
|
131
|
+
triggerProps["aria-label"] && !fieldProps["aria-labelledby"] ? triggerProps.id : null
|
|
132
|
+
].filter(Boolean).join(" ")
|
|
133
|
+
},
|
|
134
|
+
descriptionProps,
|
|
135
|
+
errorMessageProps,
|
|
136
|
+
isInvalid,
|
|
137
|
+
validationErrors,
|
|
138
|
+
validationDetails
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export {
|
|
143
|
+
useMultiSelect
|
|
144
|
+
};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { MultiSelectAria, MultiSelectProps, useMultiSelect } from './use-multiselect.mjs';
|
|
2
|
+
export { MultiSelectListProps, MultiSelectListState, useMultiSelectListState } from './use-multiselect-list-state.mjs';
|
|
3
|
+
export { MultiSelectState, useMultiSelectState } from './use-multiselect-state.mjs';
|
|
4
|
+
import '@react-types/button';
|
|
5
|
+
import '@react-aria/listbox';
|
|
6
|
+
import 'react';
|
|
7
|
+
import '@react-types/shared';
|
|
8
|
+
import '@react-types/overlays';
|
|
9
|
+
import '@react-stately/menu';
|
|
10
|
+
import '@react-stately/form';
|
|
11
|
+
import '@react-stately/list';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { MultiSelectAria, MultiSelectProps, useMultiSelect } from './use-multiselect.js';
|
|
2
|
+
export { MultiSelectListProps, MultiSelectListState, useMultiSelectListState } from './use-multiselect-list-state.js';
|
|
3
|
+
export { MultiSelectState, useMultiSelectState } from './use-multiselect-state.js';
|
|
4
|
+
import '@react-types/button';
|
|
5
|
+
import '@react-aria/listbox';
|
|
6
|
+
import 'react';
|
|
7
|
+
import '@react-types/shared';
|
|
8
|
+
import '@react-types/overlays';
|
|
9
|
+
import '@react-stately/menu';
|
|
10
|
+
import '@react-stately/form';
|
|
11
|
+
import '@react-stately/list';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useMultiSelect: () => useMultiSelect,
|
|
24
|
+
useMultiSelectListState: () => useMultiSelectListState,
|
|
25
|
+
useMultiSelectState: () => useMultiSelectState
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/use-multiselect.ts
|
|
30
|
+
var import_i18n = require("@react-aria/i18n");
|
|
31
|
+
var import_interactions = require("@react-aria/interactions");
|
|
32
|
+
var import_label = require("@react-aria/label");
|
|
33
|
+
var import_menu = require("@react-aria/menu");
|
|
34
|
+
var import_selection = require("@react-aria/selection");
|
|
35
|
+
var import_utils = require("@react-aria/utils");
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
function useMultiSelect(props, state, ref) {
|
|
38
|
+
const { disallowEmptySelection, isDisabled } = props;
|
|
39
|
+
const collator = (0, import_i18n.useCollator)({ usage: "search", sensitivity: "base" });
|
|
40
|
+
const delegate = (0, import_react.useMemo)(
|
|
41
|
+
() => new import_selection.ListKeyboardDelegate(state.collection, state.disabledKeys, null, collator),
|
|
42
|
+
[state.collection, state.disabledKeys, collator]
|
|
43
|
+
);
|
|
44
|
+
const { menuTriggerProps, menuProps } = (0, import_menu.useMenuTrigger)(
|
|
45
|
+
{
|
|
46
|
+
isDisabled,
|
|
47
|
+
type: "listbox"
|
|
48
|
+
},
|
|
49
|
+
state,
|
|
50
|
+
ref
|
|
51
|
+
);
|
|
52
|
+
const triggerOnKeyDown = (e) => {
|
|
53
|
+
if (state.selectionMode === "single") {
|
|
54
|
+
switch (e.key) {
|
|
55
|
+
case "ArrowLeft": {
|
|
56
|
+
e.preventDefault();
|
|
57
|
+
const key = state.selectedKeys.size > 0 ? delegate.getKeyAbove(state.selectedKeys.values().next().value) : delegate.getFirstKey();
|
|
58
|
+
if (key) {
|
|
59
|
+
state.setSelectedKeys([key]);
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
case "ArrowRight": {
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
const key = state.selectedKeys.size > 0 ? delegate.getKeyBelow(state.selectedKeys.values().next().value) : delegate.getFirstKey();
|
|
66
|
+
if (key) {
|
|
67
|
+
state.setSelectedKeys([key]);
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const { typeSelectProps } = (0, import_selection.useTypeSelect)({
|
|
75
|
+
keyboardDelegate: delegate,
|
|
76
|
+
selectionManager: state.selectionManager,
|
|
77
|
+
onTypeSelect(key) {
|
|
78
|
+
state.setSelectedKeys([key]);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
const { isInvalid, validationErrors, validationDetails } = state.displayValidation;
|
|
82
|
+
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = (0, import_label.useField)({
|
|
83
|
+
...props,
|
|
84
|
+
labelElementType: "span",
|
|
85
|
+
isInvalid,
|
|
86
|
+
errorMessage: props.errorMessage || validationErrors
|
|
87
|
+
});
|
|
88
|
+
typeSelectProps.onKeyDown = typeSelectProps.onKeyDownCapture;
|
|
89
|
+
delete typeSelectProps.onKeyDownCapture;
|
|
90
|
+
menuTriggerProps.onPressStart = (e) => {
|
|
91
|
+
if (e.pointerType !== "touch" && e.pointerType !== "keyboard" && !isDisabled) {
|
|
92
|
+
state.toggle(e.pointerType === "virtual" ? "first" : null);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const domProps = (0, import_utils.filterDOMProps)(props, { labelable: true });
|
|
96
|
+
const triggerProps = (0, import_utils.mergeProps)(typeSelectProps, menuTriggerProps, fieldProps);
|
|
97
|
+
const valueId = (0, import_utils.useId)();
|
|
98
|
+
return {
|
|
99
|
+
labelProps: {
|
|
100
|
+
...labelProps,
|
|
101
|
+
onClick: () => {
|
|
102
|
+
var _a;
|
|
103
|
+
if (!props.isDisabled) {
|
|
104
|
+
(_a = ref.current) == null ? void 0 : _a.focus();
|
|
105
|
+
(0, import_interactions.setInteractionModality)("keyboard");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
triggerProps: (0, import_utils.mergeProps)(domProps, {
|
|
110
|
+
...triggerProps,
|
|
111
|
+
onKeyDown: (0, import_utils.chain)(triggerProps.onKeyDown, triggerOnKeyDown, props.onKeyDown),
|
|
112
|
+
onKeyUp: props.onKeyUp,
|
|
113
|
+
"aria-labelledby": [
|
|
114
|
+
valueId,
|
|
115
|
+
triggerProps["aria-labelledby"],
|
|
116
|
+
triggerProps["aria-label"] && !triggerProps["aria-labelledby"] ? triggerProps.id : null
|
|
117
|
+
].join(" "),
|
|
118
|
+
onFocus(e) {
|
|
119
|
+
if (state.isFocused) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (props.onFocus) {
|
|
123
|
+
props.onFocus(e);
|
|
124
|
+
}
|
|
125
|
+
state.setFocused(true);
|
|
126
|
+
},
|
|
127
|
+
onBlur(e) {
|
|
128
|
+
if (state.isOpen) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (props.onBlur) {
|
|
132
|
+
props.onBlur(e);
|
|
133
|
+
}
|
|
134
|
+
state.setFocused(false);
|
|
135
|
+
}
|
|
136
|
+
}),
|
|
137
|
+
valueProps: {
|
|
138
|
+
id: valueId
|
|
139
|
+
},
|
|
140
|
+
menuProps: {
|
|
141
|
+
...menuProps,
|
|
142
|
+
disallowEmptySelection,
|
|
143
|
+
autoFocus: state.focusStrategy || true,
|
|
144
|
+
shouldSelectOnPressUp: true,
|
|
145
|
+
shouldFocusOnHover: true,
|
|
146
|
+
onBlur: (e) => {
|
|
147
|
+
if (e.currentTarget.contains(e.relatedTarget)) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (props.onBlur) {
|
|
151
|
+
props.onBlur(e);
|
|
152
|
+
}
|
|
153
|
+
state.setFocused(false);
|
|
154
|
+
},
|
|
155
|
+
// @ts-ignore
|
|
156
|
+
onFocus: menuProps == null ? void 0 : menuProps.onFocus,
|
|
157
|
+
"aria-labelledby": [
|
|
158
|
+
fieldProps["aria-labelledby"],
|
|
159
|
+
triggerProps["aria-label"] && !fieldProps["aria-labelledby"] ? triggerProps.id : null
|
|
160
|
+
].filter(Boolean).join(" ")
|
|
161
|
+
},
|
|
162
|
+
descriptionProps,
|
|
163
|
+
errorMessageProps,
|
|
164
|
+
isInvalid,
|
|
165
|
+
validationErrors,
|
|
166
|
+
validationDetails
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// src/use-multiselect-list-state.ts
|
|
171
|
+
var import_react2 = require("react");
|
|
172
|
+
var import_list = require("@react-stately/list");
|
|
173
|
+
function useMultiSelectListState(props) {
|
|
174
|
+
const {
|
|
175
|
+
collection,
|
|
176
|
+
disabledKeys,
|
|
177
|
+
selectionManager,
|
|
178
|
+
selectionManager: { setSelectedKeys, selectedKeys, selectionMode }
|
|
179
|
+
} = (0, import_list.useListState)(props);
|
|
180
|
+
const missingKeys = (0, import_react2.useMemo)(() => {
|
|
181
|
+
if (!props.isLoading && selectedKeys.size !== 0) {
|
|
182
|
+
return Array.from(selectedKeys).filter(Boolean).filter((key) => !collection.getItem(key));
|
|
183
|
+
}
|
|
184
|
+
return [];
|
|
185
|
+
}, [selectedKeys, collection]);
|
|
186
|
+
const selectedItems = selectedKeys.size !== 0 ? Array.from(selectedKeys).map((key) => {
|
|
187
|
+
return collection.getItem(key);
|
|
188
|
+
}).filter(Boolean) : null;
|
|
189
|
+
if (missingKeys.length) {
|
|
190
|
+
console.warn(
|
|
191
|
+
`Select: Keys "${missingKeys.join(
|
|
192
|
+
", "
|
|
193
|
+
)}" passed to "selectedKeys" are not present in the collection.`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
collection,
|
|
198
|
+
disabledKeys,
|
|
199
|
+
selectionManager,
|
|
200
|
+
selectionMode,
|
|
201
|
+
selectedKeys,
|
|
202
|
+
setSelectedKeys: setSelectedKeys.bind(selectionManager),
|
|
203
|
+
selectedItems
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// src/use-multiselect-state.ts
|
|
208
|
+
var import_menu2 = require("@react-stately/menu");
|
|
209
|
+
var import_form = require("@react-stately/form");
|
|
210
|
+
var import_react3 = require("react");
|
|
211
|
+
function useMultiSelectState({
|
|
212
|
+
validate,
|
|
213
|
+
validationBehavior,
|
|
214
|
+
...props
|
|
215
|
+
}) {
|
|
216
|
+
const [isFocused, setFocused] = (0, import_react3.useState)(false);
|
|
217
|
+
const [focusStrategy, setFocusStrategy] = (0, import_react3.useState)(null);
|
|
218
|
+
const triggerState = (0, import_menu2.useMenuTriggerState)(props);
|
|
219
|
+
const listState = useMultiSelectListState({
|
|
220
|
+
...props,
|
|
221
|
+
onSelectionChange: (keys) => {
|
|
222
|
+
if (props.onSelectionChange != null) {
|
|
223
|
+
if (keys === "all") {
|
|
224
|
+
props.onSelectionChange(new Set(listState.collection.getKeys()));
|
|
225
|
+
} else {
|
|
226
|
+
props.onSelectionChange(keys);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (props.selectionMode === "single") {
|
|
230
|
+
triggerState.close();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
const validationState = (0, import_form.useFormValidationState)({
|
|
235
|
+
...props,
|
|
236
|
+
validationBehavior,
|
|
237
|
+
validate: (value) => {
|
|
238
|
+
if (!validate) return;
|
|
239
|
+
const keys = Array.from(value);
|
|
240
|
+
return validate(props.selectionMode === "single" ? keys[0] : keys);
|
|
241
|
+
},
|
|
242
|
+
// @ts-ignore
|
|
243
|
+
value: listState.selectedKeys
|
|
244
|
+
});
|
|
245
|
+
const shouldHideContent = listState.collection.size === 0 && props.hideEmptyContent;
|
|
246
|
+
return {
|
|
247
|
+
...validationState,
|
|
248
|
+
...listState,
|
|
249
|
+
...triggerState,
|
|
250
|
+
focusStrategy,
|
|
251
|
+
close() {
|
|
252
|
+
triggerState.close();
|
|
253
|
+
},
|
|
254
|
+
open(focusStrategy2 = null) {
|
|
255
|
+
if (shouldHideContent) return;
|
|
256
|
+
setFocusStrategy(focusStrategy2);
|
|
257
|
+
triggerState.open();
|
|
258
|
+
},
|
|
259
|
+
toggle(focusStrategy2 = null) {
|
|
260
|
+
if (shouldHideContent) return;
|
|
261
|
+
setFocusStrategy(focusStrategy2);
|
|
262
|
+
triggerState.toggle();
|
|
263
|
+
},
|
|
264
|
+
isFocused,
|
|
265
|
+
setFocused
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
269
|
+
0 && (module.exports = {
|
|
270
|
+
useMultiSelect,
|
|
271
|
+
useMultiSelectListState,
|
|
272
|
+
useMultiSelectState
|
|
273
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useMultiSelectState
|
|
3
|
+
} from "./chunk-FHVPTEOP.mjs";
|
|
4
|
+
import {
|
|
5
|
+
useMultiSelectListState
|
|
6
|
+
} from "./chunk-74XVDT4G.mjs";
|
|
7
|
+
import {
|
|
8
|
+
useMultiSelect
|
|
9
|
+
} from "./chunk-RLX3CPDX.mjs";
|
|
10
|
+
export {
|
|
11
|
+
useMultiSelect,
|
|
12
|
+
useMultiSelectListState,
|
|
13
|
+
useMultiSelectState
|
|
14
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ListState } from '@react-stately/list';
|
|
2
|
+
import { Node, MultipleSelection, CollectionBase, AsyncLoadable } from '@react-types/shared';
|
|
3
|
+
import { Key } from 'react';
|
|
4
|
+
|
|
5
|
+
interface MultiSelectListProps<T> extends CollectionBase<T>, AsyncLoadable, MultipleSelection {
|
|
6
|
+
}
|
|
7
|
+
interface MultiSelectListState<T> extends ListState<T> {
|
|
8
|
+
/** The keys for the currently selected items. */
|
|
9
|
+
selectedKeys: Set<Key>;
|
|
10
|
+
/** Sets the selected keys. */
|
|
11
|
+
setSelectedKeys(keys: Iterable<Key>): void;
|
|
12
|
+
/** The value of the currently selected items. */
|
|
13
|
+
selectedItems: Node<T>[] | null;
|
|
14
|
+
/** The type of selection. */
|
|
15
|
+
selectionMode: MultipleSelection["selectionMode"];
|
|
16
|
+
}
|
|
17
|
+
declare function useMultiSelectListState<T extends object>(props: MultiSelectListProps<T>): MultiSelectListState<T>;
|
|
18
|
+
|
|
19
|
+
export { type MultiSelectListProps, type MultiSelectListState, useMultiSelectListState };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ListState } from '@react-stately/list';
|
|
2
|
+
import { Node, MultipleSelection, CollectionBase, AsyncLoadable } from '@react-types/shared';
|
|
3
|
+
import { Key } from 'react';
|
|
4
|
+
|
|
5
|
+
interface MultiSelectListProps<T> extends CollectionBase<T>, AsyncLoadable, MultipleSelection {
|
|
6
|
+
}
|
|
7
|
+
interface MultiSelectListState<T> extends ListState<T> {
|
|
8
|
+
/** The keys for the currently selected items. */
|
|
9
|
+
selectedKeys: Set<Key>;
|
|
10
|
+
/** Sets the selected keys. */
|
|
11
|
+
setSelectedKeys(keys: Iterable<Key>): void;
|
|
12
|
+
/** The value of the currently selected items. */
|
|
13
|
+
selectedItems: Node<T>[] | null;
|
|
14
|
+
/** The type of selection. */
|
|
15
|
+
selectionMode: MultipleSelection["selectionMode"];
|
|
16
|
+
}
|
|
17
|
+
declare function useMultiSelectListState<T extends object>(props: MultiSelectListProps<T>): MultiSelectListState<T>;
|
|
18
|
+
|
|
19
|
+
export { type MultiSelectListProps, type MultiSelectListState, useMultiSelectListState };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/use-multiselect-list-state.ts
|
|
21
|
+
var use_multiselect_list_state_exports = {};
|
|
22
|
+
__export(use_multiselect_list_state_exports, {
|
|
23
|
+
useMultiSelectListState: () => useMultiSelectListState
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_multiselect_list_state_exports);
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
var import_list = require("@react-stately/list");
|
|
28
|
+
function useMultiSelectListState(props) {
|
|
29
|
+
const {
|
|
30
|
+
collection,
|
|
31
|
+
disabledKeys,
|
|
32
|
+
selectionManager,
|
|
33
|
+
selectionManager: { setSelectedKeys, selectedKeys, selectionMode }
|
|
34
|
+
} = (0, import_list.useListState)(props);
|
|
35
|
+
const missingKeys = (0, import_react.useMemo)(() => {
|
|
36
|
+
if (!props.isLoading && selectedKeys.size !== 0) {
|
|
37
|
+
return Array.from(selectedKeys).filter(Boolean).filter((key) => !collection.getItem(key));
|
|
38
|
+
}
|
|
39
|
+
return [];
|
|
40
|
+
}, [selectedKeys, collection]);
|
|
41
|
+
const selectedItems = selectedKeys.size !== 0 ? Array.from(selectedKeys).map((key) => {
|
|
42
|
+
return collection.getItem(key);
|
|
43
|
+
}).filter(Boolean) : null;
|
|
44
|
+
if (missingKeys.length) {
|
|
45
|
+
console.warn(
|
|
46
|
+
`Select: Keys "${missingKeys.join(
|
|
47
|
+
", "
|
|
48
|
+
)}" passed to "selectedKeys" are not present in the collection.`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
collection,
|
|
53
|
+
disabledKeys,
|
|
54
|
+
selectionManager,
|
|
55
|
+
selectionMode,
|
|
56
|
+
selectedKeys,
|
|
57
|
+
setSelectedKeys: setSelectedKeys.bind(selectionManager),
|
|
58
|
+
selectedItems
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
62
|
+
0 && (module.exports = {
|
|
63
|
+
useMultiSelectListState
|
|
64
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { OverlayTriggerProps } from '@react-types/overlays';
|
|
2
|
+
import { CollectionBase, AsyncLoadable, InputBase, DOMProps, HelpTextProps, Validation, LabelableProps, TextInputBase, MultipleSelection, FocusableProps, ValidationError } from '@react-types/shared';
|
|
3
|
+
import { MenuTriggerState } from '@react-stately/menu';
|
|
4
|
+
import { FormValidationState } from '@react-stately/form';
|
|
5
|
+
import { MultiSelectListState } from './use-multiselect-list-state.mjs';
|
|
6
|
+
import '@react-stately/list';
|
|
7
|
+
import 'react';
|
|
8
|
+
|
|
9
|
+
interface MultiSelectProps<T> extends CollectionBase<T>, AsyncLoadable, Omit<InputBase, "isReadOnly">, DOMProps, HelpTextProps, Omit<Validation<T>, "validate">, LabelableProps, TextInputBase, Omit<MultipleSelection, "none">, FocusableProps, OverlayTriggerProps {
|
|
10
|
+
/**
|
|
11
|
+
* Whether the menu should automatically flip direction when space is limited.
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
shouldFlip?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* A function that returns an error message if a given value is invalid.
|
|
17
|
+
* Validation errors are displayed to the user when the form is submitted
|
|
18
|
+
* if `validationBehavior="native"`. For realtime validation, use the `isInvalid`
|
|
19
|
+
* prop instead.
|
|
20
|
+
*/
|
|
21
|
+
validate?: (value: string | string[]) => ValidationError | true | null | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Whether the menu should be hidden when there are no items.
|
|
24
|
+
*/
|
|
25
|
+
hideEmptyContent?: boolean;
|
|
26
|
+
}
|
|
27
|
+
interface MultiSelectState<T> extends MultiSelectListState<T>, MenuTriggerState, FormValidationState {
|
|
28
|
+
/** Whether the select is currently focused. */
|
|
29
|
+
isFocused: boolean;
|
|
30
|
+
/** Sets whether the select is focused. */
|
|
31
|
+
setFocused(isFocused: boolean): void;
|
|
32
|
+
}
|
|
33
|
+
declare function useMultiSelectState<T extends object>({ validate, validationBehavior, ...props }: MultiSelectProps<T>): MultiSelectState<T>;
|
|
34
|
+
|
|
35
|
+
export { type MultiSelectProps, type MultiSelectState, useMultiSelectState };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { OverlayTriggerProps } from '@react-types/overlays';
|
|
2
|
+
import { CollectionBase, AsyncLoadable, InputBase, DOMProps, HelpTextProps, Validation, LabelableProps, TextInputBase, MultipleSelection, FocusableProps, ValidationError } from '@react-types/shared';
|
|
3
|
+
import { MenuTriggerState } from '@react-stately/menu';
|
|
4
|
+
import { FormValidationState } from '@react-stately/form';
|
|
5
|
+
import { MultiSelectListState } from './use-multiselect-list-state.js';
|
|
6
|
+
import '@react-stately/list';
|
|
7
|
+
import 'react';
|
|
8
|
+
|
|
9
|
+
interface MultiSelectProps<T> extends CollectionBase<T>, AsyncLoadable, Omit<InputBase, "isReadOnly">, DOMProps, HelpTextProps, Omit<Validation<T>, "validate">, LabelableProps, TextInputBase, Omit<MultipleSelection, "none">, FocusableProps, OverlayTriggerProps {
|
|
10
|
+
/**
|
|
11
|
+
* Whether the menu should automatically flip direction when space is limited.
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
shouldFlip?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* A function that returns an error message if a given value is invalid.
|
|
17
|
+
* Validation errors are displayed to the user when the form is submitted
|
|
18
|
+
* if `validationBehavior="native"`. For realtime validation, use the `isInvalid`
|
|
19
|
+
* prop instead.
|
|
20
|
+
*/
|
|
21
|
+
validate?: (value: string | string[]) => ValidationError | true | null | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Whether the menu should be hidden when there are no items.
|
|
24
|
+
*/
|
|
25
|
+
hideEmptyContent?: boolean;
|
|
26
|
+
}
|
|
27
|
+
interface MultiSelectState<T> extends MultiSelectListState<T>, MenuTriggerState, FormValidationState {
|
|
28
|
+
/** Whether the select is currently focused. */
|
|
29
|
+
isFocused: boolean;
|
|
30
|
+
/** Sets whether the select is focused. */
|
|
31
|
+
setFocused(isFocused: boolean): void;
|
|
32
|
+
}
|
|
33
|
+
declare function useMultiSelectState<T extends object>({ validate, validationBehavior, ...props }: MultiSelectProps<T>): MultiSelectState<T>;
|
|
34
|
+
|
|
35
|
+
export { type MultiSelectProps, type MultiSelectState, useMultiSelectState };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/use-multiselect-state.ts
|
|
21
|
+
var use_multiselect_state_exports = {};
|
|
22
|
+
__export(use_multiselect_state_exports, {
|
|
23
|
+
useMultiSelectState: () => useMultiSelectState
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_multiselect_state_exports);
|
|
26
|
+
var import_menu = require("@react-stately/menu");
|
|
27
|
+
var import_form = require("@react-stately/form");
|
|
28
|
+
var import_react2 = require("react");
|
|
29
|
+
|
|
30
|
+
// src/use-multiselect-list-state.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
var import_list = require("@react-stately/list");
|
|
33
|
+
function useMultiSelectListState(props) {
|
|
34
|
+
const {
|
|
35
|
+
collection,
|
|
36
|
+
disabledKeys,
|
|
37
|
+
selectionManager,
|
|
38
|
+
selectionManager: { setSelectedKeys, selectedKeys, selectionMode }
|
|
39
|
+
} = (0, import_list.useListState)(props);
|
|
40
|
+
const missingKeys = (0, import_react.useMemo)(() => {
|
|
41
|
+
if (!props.isLoading && selectedKeys.size !== 0) {
|
|
42
|
+
return Array.from(selectedKeys).filter(Boolean).filter((key) => !collection.getItem(key));
|
|
43
|
+
}
|
|
44
|
+
return [];
|
|
45
|
+
}, [selectedKeys, collection]);
|
|
46
|
+
const selectedItems = selectedKeys.size !== 0 ? Array.from(selectedKeys).map((key) => {
|
|
47
|
+
return collection.getItem(key);
|
|
48
|
+
}).filter(Boolean) : null;
|
|
49
|
+
if (missingKeys.length) {
|
|
50
|
+
console.warn(
|
|
51
|
+
`Select: Keys "${missingKeys.join(
|
|
52
|
+
", "
|
|
53
|
+
)}" passed to "selectedKeys" are not present in the collection.`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
collection,
|
|
58
|
+
disabledKeys,
|
|
59
|
+
selectionManager,
|
|
60
|
+
selectionMode,
|
|
61
|
+
selectedKeys,
|
|
62
|
+
setSelectedKeys: setSelectedKeys.bind(selectionManager),
|
|
63
|
+
selectedItems
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/use-multiselect-state.ts
|
|
68
|
+
function useMultiSelectState({
|
|
69
|
+
validate,
|
|
70
|
+
validationBehavior,
|
|
71
|
+
...props
|
|
72
|
+
}) {
|
|
73
|
+
const [isFocused, setFocused] = (0, import_react2.useState)(false);
|
|
74
|
+
const [focusStrategy, setFocusStrategy] = (0, import_react2.useState)(null);
|
|
75
|
+
const triggerState = (0, import_menu.useMenuTriggerState)(props);
|
|
76
|
+
const listState = useMultiSelectListState({
|
|
77
|
+
...props,
|
|
78
|
+
onSelectionChange: (keys) => {
|
|
79
|
+
if (props.onSelectionChange != null) {
|
|
80
|
+
if (keys === "all") {
|
|
81
|
+
props.onSelectionChange(new Set(listState.collection.getKeys()));
|
|
82
|
+
} else {
|
|
83
|
+
props.onSelectionChange(keys);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (props.selectionMode === "single") {
|
|
87
|
+
triggerState.close();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
const validationState = (0, import_form.useFormValidationState)({
|
|
92
|
+
...props,
|
|
93
|
+
validationBehavior,
|
|
94
|
+
validate: (value) => {
|
|
95
|
+
if (!validate) return;
|
|
96
|
+
const keys = Array.from(value);
|
|
97
|
+
return validate(props.selectionMode === "single" ? keys[0] : keys);
|
|
98
|
+
},
|
|
99
|
+
// @ts-ignore
|
|
100
|
+
value: listState.selectedKeys
|
|
101
|
+
});
|
|
102
|
+
const shouldHideContent = listState.collection.size === 0 && props.hideEmptyContent;
|
|
103
|
+
return {
|
|
104
|
+
...validationState,
|
|
105
|
+
...listState,
|
|
106
|
+
...triggerState,
|
|
107
|
+
focusStrategy,
|
|
108
|
+
close() {
|
|
109
|
+
triggerState.close();
|
|
110
|
+
},
|
|
111
|
+
open(focusStrategy2 = null) {
|
|
112
|
+
if (shouldHideContent) return;
|
|
113
|
+
setFocusStrategy(focusStrategy2);
|
|
114
|
+
triggerState.open();
|
|
115
|
+
},
|
|
116
|
+
toggle(focusStrategy2 = null) {
|
|
117
|
+
if (shouldHideContent) return;
|
|
118
|
+
setFocusStrategy(focusStrategy2);
|
|
119
|
+
triggerState.toggle();
|
|
120
|
+
},
|
|
121
|
+
isFocused,
|
|
122
|
+
setFocused
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
126
|
+
0 && (module.exports = {
|
|
127
|
+
useMultiSelectState
|
|
128
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { MultiSelectProps as MultiSelectProps$1, MultiSelectState } from './use-multiselect-state.mjs';
|
|
2
|
+
import { AriaButtonProps } from '@react-types/button';
|
|
3
|
+
import { AriaListBoxOptions } from '@react-aria/listbox';
|
|
4
|
+
import { HTMLAttributes, RefObject } from 'react';
|
|
5
|
+
import { ValidationResult } from '@react-types/shared';
|
|
6
|
+
import './use-multiselect-list-state.mjs';
|
|
7
|
+
import '@react-types/overlays';
|
|
8
|
+
import '@react-stately/menu';
|
|
9
|
+
import '@react-stately/form';
|
|
10
|
+
import '@react-stately/list';
|
|
11
|
+
|
|
12
|
+
type MultiSelectProps<T> = MultiSelectProps$1<T>;
|
|
13
|
+
interface MultiSelectAria<T> extends ValidationResult {
|
|
14
|
+
/** Props for the label element. */
|
|
15
|
+
labelProps: HTMLAttributes<HTMLElement>;
|
|
16
|
+
/** Props for the popup trigger element. */
|
|
17
|
+
triggerProps: AriaButtonProps;
|
|
18
|
+
/** Props for the element representing the selected value. */
|
|
19
|
+
valueProps: HTMLAttributes<HTMLElement>;
|
|
20
|
+
/** Props for the popup. */
|
|
21
|
+
menuProps: AriaListBoxOptions<T>;
|
|
22
|
+
/** Props for the select's description element, if any. */
|
|
23
|
+
descriptionProps: HTMLAttributes<HTMLElement>;
|
|
24
|
+
/** Props for the select's error message element, if any. */
|
|
25
|
+
errorMessageProps: HTMLAttributes<HTMLElement>;
|
|
26
|
+
}
|
|
27
|
+
declare function useMultiSelect<T>(props: MultiSelectProps<T>, state: MultiSelectState<T>, ref: RefObject<HTMLElement>): MultiSelectAria<T>;
|
|
28
|
+
|
|
29
|
+
export { type MultiSelectAria, type MultiSelectProps, useMultiSelect };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { MultiSelectProps as MultiSelectProps$1, MultiSelectState } from './use-multiselect-state.js';
|
|
2
|
+
import { AriaButtonProps } from '@react-types/button';
|
|
3
|
+
import { AriaListBoxOptions } from '@react-aria/listbox';
|
|
4
|
+
import { HTMLAttributes, RefObject } from 'react';
|
|
5
|
+
import { ValidationResult } from '@react-types/shared';
|
|
6
|
+
import './use-multiselect-list-state.js';
|
|
7
|
+
import '@react-types/overlays';
|
|
8
|
+
import '@react-stately/menu';
|
|
9
|
+
import '@react-stately/form';
|
|
10
|
+
import '@react-stately/list';
|
|
11
|
+
|
|
12
|
+
type MultiSelectProps<T> = MultiSelectProps$1<T>;
|
|
13
|
+
interface MultiSelectAria<T> extends ValidationResult {
|
|
14
|
+
/** Props for the label element. */
|
|
15
|
+
labelProps: HTMLAttributes<HTMLElement>;
|
|
16
|
+
/** Props for the popup trigger element. */
|
|
17
|
+
triggerProps: AriaButtonProps;
|
|
18
|
+
/** Props for the element representing the selected value. */
|
|
19
|
+
valueProps: HTMLAttributes<HTMLElement>;
|
|
20
|
+
/** Props for the popup. */
|
|
21
|
+
menuProps: AriaListBoxOptions<T>;
|
|
22
|
+
/** Props for the select's description element, if any. */
|
|
23
|
+
descriptionProps: HTMLAttributes<HTMLElement>;
|
|
24
|
+
/** Props for the select's error message element, if any. */
|
|
25
|
+
errorMessageProps: HTMLAttributes<HTMLElement>;
|
|
26
|
+
}
|
|
27
|
+
declare function useMultiSelect<T>(props: MultiSelectProps<T>, state: MultiSelectState<T>, ref: RefObject<HTMLElement>): MultiSelectAria<T>;
|
|
28
|
+
|
|
29
|
+
export { type MultiSelectAria, type MultiSelectProps, useMultiSelect };
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/use-multiselect.ts
|
|
21
|
+
var use_multiselect_exports = {};
|
|
22
|
+
__export(use_multiselect_exports, {
|
|
23
|
+
useMultiSelect: () => useMultiSelect
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_multiselect_exports);
|
|
26
|
+
var import_i18n = require("@react-aria/i18n");
|
|
27
|
+
var import_interactions = require("@react-aria/interactions");
|
|
28
|
+
var import_label = require("@react-aria/label");
|
|
29
|
+
var import_menu = require("@react-aria/menu");
|
|
30
|
+
var import_selection = require("@react-aria/selection");
|
|
31
|
+
var import_utils = require("@react-aria/utils");
|
|
32
|
+
var import_react = require("react");
|
|
33
|
+
function useMultiSelect(props, state, ref) {
|
|
34
|
+
const { disallowEmptySelection, isDisabled } = props;
|
|
35
|
+
const collator = (0, import_i18n.useCollator)({ usage: "search", sensitivity: "base" });
|
|
36
|
+
const delegate = (0, import_react.useMemo)(
|
|
37
|
+
() => new import_selection.ListKeyboardDelegate(state.collection, state.disabledKeys, null, collator),
|
|
38
|
+
[state.collection, state.disabledKeys, collator]
|
|
39
|
+
);
|
|
40
|
+
const { menuTriggerProps, menuProps } = (0, import_menu.useMenuTrigger)(
|
|
41
|
+
{
|
|
42
|
+
isDisabled,
|
|
43
|
+
type: "listbox"
|
|
44
|
+
},
|
|
45
|
+
state,
|
|
46
|
+
ref
|
|
47
|
+
);
|
|
48
|
+
const triggerOnKeyDown = (e) => {
|
|
49
|
+
if (state.selectionMode === "single") {
|
|
50
|
+
switch (e.key) {
|
|
51
|
+
case "ArrowLeft": {
|
|
52
|
+
e.preventDefault();
|
|
53
|
+
const key = state.selectedKeys.size > 0 ? delegate.getKeyAbove(state.selectedKeys.values().next().value) : delegate.getFirstKey();
|
|
54
|
+
if (key) {
|
|
55
|
+
state.setSelectedKeys([key]);
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
case "ArrowRight": {
|
|
60
|
+
e.preventDefault();
|
|
61
|
+
const key = state.selectedKeys.size > 0 ? delegate.getKeyBelow(state.selectedKeys.values().next().value) : delegate.getFirstKey();
|
|
62
|
+
if (key) {
|
|
63
|
+
state.setSelectedKeys([key]);
|
|
64
|
+
}
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const { typeSelectProps } = (0, import_selection.useTypeSelect)({
|
|
71
|
+
keyboardDelegate: delegate,
|
|
72
|
+
selectionManager: state.selectionManager,
|
|
73
|
+
onTypeSelect(key) {
|
|
74
|
+
state.setSelectedKeys([key]);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const { isInvalid, validationErrors, validationDetails } = state.displayValidation;
|
|
78
|
+
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = (0, import_label.useField)({
|
|
79
|
+
...props,
|
|
80
|
+
labelElementType: "span",
|
|
81
|
+
isInvalid,
|
|
82
|
+
errorMessage: props.errorMessage || validationErrors
|
|
83
|
+
});
|
|
84
|
+
typeSelectProps.onKeyDown = typeSelectProps.onKeyDownCapture;
|
|
85
|
+
delete typeSelectProps.onKeyDownCapture;
|
|
86
|
+
menuTriggerProps.onPressStart = (e) => {
|
|
87
|
+
if (e.pointerType !== "touch" && e.pointerType !== "keyboard" && !isDisabled) {
|
|
88
|
+
state.toggle(e.pointerType === "virtual" ? "first" : null);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const domProps = (0, import_utils.filterDOMProps)(props, { labelable: true });
|
|
92
|
+
const triggerProps = (0, import_utils.mergeProps)(typeSelectProps, menuTriggerProps, fieldProps);
|
|
93
|
+
const valueId = (0, import_utils.useId)();
|
|
94
|
+
return {
|
|
95
|
+
labelProps: {
|
|
96
|
+
...labelProps,
|
|
97
|
+
onClick: () => {
|
|
98
|
+
var _a;
|
|
99
|
+
if (!props.isDisabled) {
|
|
100
|
+
(_a = ref.current) == null ? void 0 : _a.focus();
|
|
101
|
+
(0, import_interactions.setInteractionModality)("keyboard");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
triggerProps: (0, import_utils.mergeProps)(domProps, {
|
|
106
|
+
...triggerProps,
|
|
107
|
+
onKeyDown: (0, import_utils.chain)(triggerProps.onKeyDown, triggerOnKeyDown, props.onKeyDown),
|
|
108
|
+
onKeyUp: props.onKeyUp,
|
|
109
|
+
"aria-labelledby": [
|
|
110
|
+
valueId,
|
|
111
|
+
triggerProps["aria-labelledby"],
|
|
112
|
+
triggerProps["aria-label"] && !triggerProps["aria-labelledby"] ? triggerProps.id : null
|
|
113
|
+
].join(" "),
|
|
114
|
+
onFocus(e) {
|
|
115
|
+
if (state.isFocused) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (props.onFocus) {
|
|
119
|
+
props.onFocus(e);
|
|
120
|
+
}
|
|
121
|
+
state.setFocused(true);
|
|
122
|
+
},
|
|
123
|
+
onBlur(e) {
|
|
124
|
+
if (state.isOpen) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (props.onBlur) {
|
|
128
|
+
props.onBlur(e);
|
|
129
|
+
}
|
|
130
|
+
state.setFocused(false);
|
|
131
|
+
}
|
|
132
|
+
}),
|
|
133
|
+
valueProps: {
|
|
134
|
+
id: valueId
|
|
135
|
+
},
|
|
136
|
+
menuProps: {
|
|
137
|
+
...menuProps,
|
|
138
|
+
disallowEmptySelection,
|
|
139
|
+
autoFocus: state.focusStrategy || true,
|
|
140
|
+
shouldSelectOnPressUp: true,
|
|
141
|
+
shouldFocusOnHover: true,
|
|
142
|
+
onBlur: (e) => {
|
|
143
|
+
if (e.currentTarget.contains(e.relatedTarget)) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (props.onBlur) {
|
|
147
|
+
props.onBlur(e);
|
|
148
|
+
}
|
|
149
|
+
state.setFocused(false);
|
|
150
|
+
},
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
onFocus: menuProps == null ? void 0 : menuProps.onFocus,
|
|
153
|
+
"aria-labelledby": [
|
|
154
|
+
fieldProps["aria-labelledby"],
|
|
155
|
+
triggerProps["aria-label"] && !fieldProps["aria-labelledby"] ? triggerProps.id : null
|
|
156
|
+
].filter(Boolean).join(" ")
|
|
157
|
+
},
|
|
158
|
+
descriptionProps,
|
|
159
|
+
errorMessageProps,
|
|
160
|
+
isInvalid,
|
|
161
|
+
validationErrors,
|
|
162
|
+
validationDetails
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
166
|
+
0 && (module.exports = {
|
|
167
|
+
useMultiSelect
|
|
168
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vx-oss/heroui-v2-use-aria-multiselect",
|
|
3
|
+
"version": "2.4.20-alpha.0",
|
|
4
|
+
"description": "Provides the behavior and accessibility implementation for a multi-select component.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"use-aria-multiselect"
|
|
7
|
+
],
|
|
8
|
+
"author": "Vx OSS Devs <oss-developers@vezham.com>",
|
|
9
|
+
"homepage": "https://vezham.com",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/vezham/heroui-v2.git",
|
|
22
|
+
"directory": "packages/hooks/use-aria-multiselect"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/vezham/heroui-v2/issues"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@react-aria/i18n": "3.12.13",
|
|
29
|
+
"@react-aria/interactions": "3.25.6",
|
|
30
|
+
"@react-aria/label": "3.7.22",
|
|
31
|
+
"@react-aria/listbox": "3.15.0",
|
|
32
|
+
"@react-aria/menu": "3.19.3",
|
|
33
|
+
"@react-aria/selection": "3.26.0",
|
|
34
|
+
"@react-aria/utils": "3.31.0",
|
|
35
|
+
"@react-stately/form": "3.2.2",
|
|
36
|
+
"@react-stately/list": "3.13.1",
|
|
37
|
+
"@react-stately/menu": "3.9.8",
|
|
38
|
+
"@react-types/button": "3.14.1",
|
|
39
|
+
"@react-types/overlays": "3.9.2",
|
|
40
|
+
"@react-types/shared": "3.32.1"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": ">=18 || >=19.0.0-rc.0",
|
|
44
|
+
"react-dom": ">=18 || >=19.0.0-rc.0"
|
|
45
|
+
},
|
|
46
|
+
"clean-package": "../../../clean-package.config.json",
|
|
47
|
+
"tsup": {
|
|
48
|
+
"clean": true,
|
|
49
|
+
"target": "es2019",
|
|
50
|
+
"format": [
|
|
51
|
+
"cjs",
|
|
52
|
+
"esm"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
"module": "dist/index.mjs",
|
|
56
|
+
"types": "dist/index.d.ts",
|
|
57
|
+
"exports": {
|
|
58
|
+
".": {
|
|
59
|
+
"types": "./dist/index.d.ts",
|
|
60
|
+
"import": "./dist/index.mjs",
|
|
61
|
+
"require": "./dist/index.js"
|
|
62
|
+
},
|
|
63
|
+
"./package.json": "./package.json"
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsup src --dts",
|
|
67
|
+
"build:fast": "tsup src",
|
|
68
|
+
"dev": "pnpm build:fast --watch",
|
|
69
|
+
"clean": "rimraf dist .turbo",
|
|
70
|
+
"typecheck": "tsc --noEmit"
|
|
71
|
+
}
|
|
72
|
+
}
|