@takashi145/react-multi-select-buttons 0.1.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 +21 -0
- package/README.md +162 -0
- package/dist/components/DefaultMultiSelectButton.d.ts +6 -0
- package/dist/components/MultiSelectButtons.d.ts +2 -0
- package/dist/components/NumberMultiSelectButtons.d.ts +2 -0
- package/dist/hooks/useSelectablePicker.d.ts +9 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +329 -0
- package/dist/types.d.ts +42 -0
- package/dist/utils/selection.d.ts +26 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 takashu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# @takashi145/react-multi-select-buttons
|
|
2
|
+
|
|
3
|
+
Button-based multi-select components for React.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @takashi145/@takashi145/react-multi-select-buttons
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Components
|
|
12
|
+
|
|
13
|
+
### `MultiSelectButtons`
|
|
14
|
+
|
|
15
|
+
A generic multi-select component that works with any value type.
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { useState } from 'react'
|
|
19
|
+
import { MultiSelectButtons } from '@takashi145/react-multi-select-buttons'
|
|
20
|
+
import type { MultiSelectButtonItem } from '@takashi145/react-multi-select-buttons'
|
|
21
|
+
|
|
22
|
+
const items: MultiSelectButtonItem<string>[] = [
|
|
23
|
+
{ key: 'apple', label: 'Apple', value: 'apple' },
|
|
24
|
+
{ key: 'banana', label: 'Banana', value: 'banana' },
|
|
25
|
+
{ key: 'orange', label: 'Orange', value: 'orange' },
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
function App() {
|
|
29
|
+
const [selected, setSelected] = useState<MultiSelectButtonItem<string>[]>([])
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<MultiSelectButtons
|
|
33
|
+
items={items}
|
|
34
|
+
selectedItems={selected}
|
|
35
|
+
maxSelection={2}
|
|
36
|
+
onChange={setSelected}
|
|
37
|
+
/>
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
#### Props
|
|
43
|
+
|
|
44
|
+
| Prop | Type | Required | Description |
|
|
45
|
+
|------|------|----------|-------------|
|
|
46
|
+
| `items` | `MultiSelectButtonItem<T>[]` | Yes | List of selectable items |
|
|
47
|
+
| `selectedItems` | `MultiSelectButtonItem<T>[]` | Yes | Currently selected items |
|
|
48
|
+
| `maxSelection` | `number` | Yes | Maximum number of items that can be selected |
|
|
49
|
+
| `onChange` | `(items: MultiSelectButtonItem<T>[]) => void` | Yes | Called when selection changes |
|
|
50
|
+
| `renderValue` | `(params: RenderValueParams<T>) => ReactNode` | No | Custom button renderer |
|
|
51
|
+
| `onSelectionLimitReached` | `(item: MultiSelectButtonItem<T>) => void` | No | Called when user taps an unselected item but the limit is already reached |
|
|
52
|
+
| `onDisabledClick` | `(item: MultiSelectButtonItem<T>) => void` | No | Called when user taps a disabled item |
|
|
53
|
+
| `className` | `string` | No | Class name for the container element |
|
|
54
|
+
| `style` | `CSSProperties` | No | Inline style for the container element |
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
### `NumberMultiSelectButtons`
|
|
59
|
+
|
|
60
|
+
A convenience component for selecting numbers within a range.
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import { useState } from 'react'
|
|
64
|
+
import { NumberMultiSelectButtons } from '@takashi145/react-multi-select-buttons'
|
|
65
|
+
|
|
66
|
+
function App() {
|
|
67
|
+
const [selected, setSelected] = useState<number[]>([])
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<NumberMultiSelectButtons
|
|
71
|
+
min={1}
|
|
72
|
+
max={10}
|
|
73
|
+
selectedValues={selected}
|
|
74
|
+
maxSelection={3}
|
|
75
|
+
onChange={setSelected}
|
|
76
|
+
/>
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Props
|
|
82
|
+
|
|
83
|
+
| Prop | Type | Required | Description |
|
|
84
|
+
|------|------|----------|-------------|
|
|
85
|
+
| `min` | `number` | Yes | Minimum value (inclusive) |
|
|
86
|
+
| `max` | `number` | Yes | Maximum value (inclusive) |
|
|
87
|
+
| `step` | `number` | No | Step between values (default: `1`) |
|
|
88
|
+
| `selectedValues` | `number[]` | Yes | Currently selected values |
|
|
89
|
+
| `maxSelection` | `number` | Yes | Maximum number of values that can be selected |
|
|
90
|
+
| `onChange` | `(values: number[]) => void` | Yes | Called when selection changes |
|
|
91
|
+
| `getValueLabel` | `(value: number, index: number) => string` | No | Custom label for each value |
|
|
92
|
+
| `isValueDisabled` | `(value: number, index: number) => boolean` | No | Whether a value should be disabled |
|
|
93
|
+
| `renderValue` | `(params: RenderValueParams<number>) => ReactNode` | No | Custom button renderer |
|
|
94
|
+
| `onSelectionLimitReached` | `(value: number) => void` | No | Called when user taps an unselected value but the limit is already reached |
|
|
95
|
+
| `onDisabledClick` | `(value: number) => void` | No | Called when user taps a disabled value |
|
|
96
|
+
| `className` | `string` | No | Class name for the container element |
|
|
97
|
+
| `style` | `CSSProperties` | No | Inline style for the container element |
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Types
|
|
102
|
+
|
|
103
|
+
### `MultiSelectButtonItem<T>`
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
type MultiSelectButtonItem<T> = {
|
|
107
|
+
key: Key
|
|
108
|
+
label: string
|
|
109
|
+
value: T
|
|
110
|
+
disabled?: boolean
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `RenderValueParams<T>`
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
type RenderValueParams<T> = {
|
|
118
|
+
item: MultiSelectButtonItem<T>
|
|
119
|
+
value: T
|
|
120
|
+
isSelected: boolean
|
|
121
|
+
isDisabled: boolean
|
|
122
|
+
isInteractive: boolean
|
|
123
|
+
selectedOrder: number | null
|
|
124
|
+
onClick: () => void
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Custom Rendering
|
|
131
|
+
|
|
132
|
+
Use the `renderValue` prop to fully replace the default button appearance.
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
<MultiSelectButtons
|
|
136
|
+
items={items}
|
|
137
|
+
selectedItems={selected}
|
|
138
|
+
maxSelection={3}
|
|
139
|
+
onChange={setSelected}
|
|
140
|
+
renderValue={({ item, isSelected, isDisabled, onClick }) => (
|
|
141
|
+
<button
|
|
142
|
+
type="button"
|
|
143
|
+
onClick={onClick}
|
|
144
|
+
disabled={isDisabled}
|
|
145
|
+
style={{
|
|
146
|
+
width: '100%',
|
|
147
|
+
padding: '14px 16px',
|
|
148
|
+
borderRadius: 999,
|
|
149
|
+
border: `1px solid ${isSelected ? '#111827' : '#d1d5db'}`,
|
|
150
|
+
background: isSelected ? '#111827' : '#ffffff',
|
|
151
|
+
color: isSelected ? '#ffffff' : '#111827',
|
|
152
|
+
}}
|
|
153
|
+
>
|
|
154
|
+
{item.label}
|
|
155
|
+
</button>
|
|
156
|
+
)}
|
|
157
|
+
/>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
MIT
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { RenderValueParams } from '../types';
|
|
2
|
+
type DefaultMultiSelectButtonProps<T> = RenderValueParams<T> & {
|
|
3
|
+
label: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function DefaultMultiSelectButton<T>({ isSelected, isDisabled, isInteractive, onClick, label, }: DefaultMultiSelectButtonProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { NumberMultiSelectButtonsProps } from '../types';
|
|
2
|
+
export declare function NumberMultiSelectButtons({ min, max, step, selectedValues, maxSelection, onChange, getValueLabel, isValueDisabled, renderValue, onSelectionLimitReached, onDisabledClick, className, style, }: NumberMultiSelectButtonsProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Key } from 'react';
|
|
2
|
+
import { MultiSelectButtonsProps, RenderValueParams } from '../types';
|
|
3
|
+
type PickerItemModel<T> = {
|
|
4
|
+
key: Key;
|
|
5
|
+
index: number;
|
|
6
|
+
renderParams: RenderValueParams<T>;
|
|
7
|
+
};
|
|
8
|
+
export declare function useSelectablePicker<T>({ items, selectedItems, maxSelection, onChange, onSelectionLimitReached, onDisabledClick, }: MultiSelectButtonsProps<T>): PickerItemModel<T>[];
|
|
9
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { MultiSelectButtons } from './components/MultiSelectButtons';
|
|
2
|
+
export { NumberMultiSelectButtons } from './components/NumberMultiSelectButtons';
|
|
3
|
+
export type { MultiSelectButtonItem, MultiSelectButtonsProps, NumberMultiSelectButtonsProps, RenderValueParams, } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
(function(){try{if(typeof document<`u`){var e=document.createElement(`style`);e.appendChild(document.createTextNode(`.multi-select-picker{grid-template-columns:repeat(auto-fit,minmax(96px,1fr));gap:8px;display:grid}.multi-select-picker__cell{min-width:0}.multi-select-picker__item{color:#111827;cursor:pointer;background:#fff;border:1px solid #d0d7de;border-radius:10px;justify-content:space-between;align-items:center;width:100%;min-height:44px;padding:10px 12px;display:flex}.multi-select-picker__item.is-selected{background:#eff6ff;border-color:#2563eb}.multi-select-picker__item:active{background:#f3f4f6;transform:scale(.96)}.multi-select-picker__item.is-disabled:active,.multi-select-picker__item.is-limit:active{background:#fff;transform:none}.multi-select-picker__item.is-disabled,.multi-select-picker__item.is-limit{cursor:not-allowed;opacity:.55}.multi-select-picker__label,.multi-select-picker__order{font:inherit}
|
|
2
|
+
/*$vite$:1*/`)),document.head.appendChild(e)}}catch(e){console.error(`vite-plugin-css-injected-by-js`,e)}})();import { useMemo as e } from "react";
|
|
3
|
+
//#region \0rolldown/runtime.js
|
|
4
|
+
var t = (e, t) => () => (t || e((t = { exports: {} }).exports, t), t.exports), n = /* @__PURE__ */ ((e) => typeof require < "u" ? require : typeof Proxy < "u" ? new Proxy(e, { get: (e, t) => (typeof require < "u" ? require : e)[t] }) : e)(function(e) {
|
|
5
|
+
if (typeof require < "u") return require.apply(this, arguments);
|
|
6
|
+
throw Error("Calling `require` for \"" + e + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
|
|
7
|
+
}), r = /* @__PURE__ */ t(((e) => {
|
|
8
|
+
var t = Symbol.for("react.transitional.element"), n = Symbol.for("react.fragment");
|
|
9
|
+
function r(e, n, r) {
|
|
10
|
+
var i = null;
|
|
11
|
+
if (r !== void 0 && (i = "" + r), n.key !== void 0 && (i = "" + n.key), "key" in n) for (var a in r = {}, n) a !== "key" && (r[a] = n[a]);
|
|
12
|
+
else r = n;
|
|
13
|
+
return n = r.ref, {
|
|
14
|
+
$$typeof: t,
|
|
15
|
+
type: e,
|
|
16
|
+
key: i,
|
|
17
|
+
ref: n === void 0 ? null : n,
|
|
18
|
+
props: r
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
e.Fragment = n, e.jsx = r, e.jsxs = r;
|
|
22
|
+
})), i = /* @__PURE__ */ t(((e) => {
|
|
23
|
+
process.env.NODE_ENV !== "production" && (function() {
|
|
24
|
+
function t(e) {
|
|
25
|
+
if (e == null) return null;
|
|
26
|
+
if (typeof e == "function") return e.$$typeof === k ? null : e.displayName || e.name || null;
|
|
27
|
+
if (typeof e == "string") return e;
|
|
28
|
+
switch (e) {
|
|
29
|
+
case v: return "Fragment";
|
|
30
|
+
case b: return "Profiler";
|
|
31
|
+
case y: return "StrictMode";
|
|
32
|
+
case w: return "Suspense";
|
|
33
|
+
case T: return "SuspenseList";
|
|
34
|
+
case O: return "Activity";
|
|
35
|
+
}
|
|
36
|
+
if (typeof e == "object") switch (typeof e.tag == "number" && console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."), e.$$typeof) {
|
|
37
|
+
case _: return "Portal";
|
|
38
|
+
case S: return e.displayName || "Context";
|
|
39
|
+
case x: return (e._context.displayName || "Context") + ".Consumer";
|
|
40
|
+
case C:
|
|
41
|
+
var n = e.render;
|
|
42
|
+
return e = e.displayName, e ||= (e = n.displayName || n.name || "", e === "" ? "ForwardRef" : "ForwardRef(" + e + ")"), e;
|
|
43
|
+
case E: return n = e.displayName || null, n === null ? t(e.type) || "Memo" : n;
|
|
44
|
+
case D:
|
|
45
|
+
n = e._payload, e = e._init;
|
|
46
|
+
try {
|
|
47
|
+
return t(e(n));
|
|
48
|
+
} catch {}
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
function r(e) {
|
|
53
|
+
return "" + e;
|
|
54
|
+
}
|
|
55
|
+
function i(e) {
|
|
56
|
+
try {
|
|
57
|
+
r(e);
|
|
58
|
+
var t = !1;
|
|
59
|
+
} catch {
|
|
60
|
+
t = !0;
|
|
61
|
+
}
|
|
62
|
+
if (t) {
|
|
63
|
+
t = console;
|
|
64
|
+
var n = t.error, i = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
|
|
65
|
+
return n.call(t, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", i), r(e);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function a(e) {
|
|
69
|
+
if (e === v) return "<>";
|
|
70
|
+
if (typeof e == "object" && e && e.$$typeof === D) return "<...>";
|
|
71
|
+
try {
|
|
72
|
+
var n = t(e);
|
|
73
|
+
return n ? "<" + n + ">" : "<...>";
|
|
74
|
+
} catch {
|
|
75
|
+
return "<...>";
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function o() {
|
|
79
|
+
var e = A.A;
|
|
80
|
+
return e === null ? null : e.getOwner();
|
|
81
|
+
}
|
|
82
|
+
function s() {
|
|
83
|
+
return Error("react-stack-top-frame");
|
|
84
|
+
}
|
|
85
|
+
function c(e) {
|
|
86
|
+
if (j.call(e, "key")) {
|
|
87
|
+
var t = Object.getOwnPropertyDescriptor(e, "key").get;
|
|
88
|
+
if (t && t.isReactWarning) return !1;
|
|
89
|
+
}
|
|
90
|
+
return e.key !== void 0;
|
|
91
|
+
}
|
|
92
|
+
function l(e, t) {
|
|
93
|
+
function n() {
|
|
94
|
+
P || (P = !0, console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)", t));
|
|
95
|
+
}
|
|
96
|
+
n.isReactWarning = !0, Object.defineProperty(e, "key", {
|
|
97
|
+
get: n,
|
|
98
|
+
configurable: !0
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function u() {
|
|
102
|
+
var e = t(this.type);
|
|
103
|
+
return F[e] || (F[e] = !0, console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")), e = this.props.ref, e === void 0 ? null : e;
|
|
104
|
+
}
|
|
105
|
+
function d(e, t, n, r, i, a) {
|
|
106
|
+
var o = n.ref;
|
|
107
|
+
return e = {
|
|
108
|
+
$$typeof: g,
|
|
109
|
+
type: e,
|
|
110
|
+
key: t,
|
|
111
|
+
props: n,
|
|
112
|
+
_owner: r
|
|
113
|
+
}, (o === void 0 ? null : o) === null ? Object.defineProperty(e, "ref", {
|
|
114
|
+
enumerable: !1,
|
|
115
|
+
value: null
|
|
116
|
+
}) : Object.defineProperty(e, "ref", {
|
|
117
|
+
enumerable: !1,
|
|
118
|
+
get: u
|
|
119
|
+
}), e._store = {}, Object.defineProperty(e._store, "validated", {
|
|
120
|
+
configurable: !1,
|
|
121
|
+
enumerable: !1,
|
|
122
|
+
writable: !0,
|
|
123
|
+
value: 0
|
|
124
|
+
}), Object.defineProperty(e, "_debugInfo", {
|
|
125
|
+
configurable: !1,
|
|
126
|
+
enumerable: !1,
|
|
127
|
+
writable: !0,
|
|
128
|
+
value: null
|
|
129
|
+
}), Object.defineProperty(e, "_debugStack", {
|
|
130
|
+
configurable: !1,
|
|
131
|
+
enumerable: !1,
|
|
132
|
+
writable: !0,
|
|
133
|
+
value: i
|
|
134
|
+
}), Object.defineProperty(e, "_debugTask", {
|
|
135
|
+
configurable: !1,
|
|
136
|
+
enumerable: !1,
|
|
137
|
+
writable: !0,
|
|
138
|
+
value: a
|
|
139
|
+
}), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
|
|
140
|
+
}
|
|
141
|
+
function f(e, n, r, a, s, u) {
|
|
142
|
+
var f = n.children;
|
|
143
|
+
if (f !== void 0) if (a) if (M(f)) {
|
|
144
|
+
for (a = 0; a < f.length; a++) p(f[a]);
|
|
145
|
+
Object.freeze && Object.freeze(f);
|
|
146
|
+
} else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");
|
|
147
|
+
else p(f);
|
|
148
|
+
if (j.call(n, "key")) {
|
|
149
|
+
f = t(e);
|
|
150
|
+
var m = Object.keys(n).filter(function(e) {
|
|
151
|
+
return e !== "key";
|
|
152
|
+
});
|
|
153
|
+
a = 0 < m.length ? "{key: someKey, " + m.join(": ..., ") + ": ...}" : "{key: someKey}", R[f + a] || (m = 0 < m.length ? "{" + m.join(": ..., ") + ": ...}" : "{}", console.error("A props object containing a \"key\" prop is being spread into JSX:\n let props = %s;\n <%s {...props} />\nReact keys must be passed directly to JSX without using spread:\n let props = %s;\n <%s key={someKey} {...props} />", a, f, m, f), R[f + a] = !0);
|
|
154
|
+
}
|
|
155
|
+
if (f = null, r !== void 0 && (i(r), f = "" + r), c(n) && (i(n.key), f = "" + n.key), "key" in n) for (var h in r = {}, n) h !== "key" && (r[h] = n[h]);
|
|
156
|
+
else r = n;
|
|
157
|
+
return f && l(r, typeof e == "function" ? e.displayName || e.name || "Unknown" : e), d(e, f, r, o(), s, u);
|
|
158
|
+
}
|
|
159
|
+
function p(e) {
|
|
160
|
+
m(e) ? e._store && (e._store.validated = 1) : typeof e == "object" && e && e.$$typeof === D && (e._payload.status === "fulfilled" ? m(e._payload.value) && e._payload.value._store && (e._payload.value._store.validated = 1) : e._store && (e._store.validated = 1));
|
|
161
|
+
}
|
|
162
|
+
function m(e) {
|
|
163
|
+
return typeof e == "object" && !!e && e.$$typeof === g;
|
|
164
|
+
}
|
|
165
|
+
var h = n("react"), g = Symbol.for("react.transitional.element"), _ = Symbol.for("react.portal"), v = Symbol.for("react.fragment"), y = Symbol.for("react.strict_mode"), b = Symbol.for("react.profiler"), x = Symbol.for("react.consumer"), S = Symbol.for("react.context"), C = Symbol.for("react.forward_ref"), w = Symbol.for("react.suspense"), T = Symbol.for("react.suspense_list"), E = Symbol.for("react.memo"), D = Symbol.for("react.lazy"), O = Symbol.for("react.activity"), k = Symbol.for("react.client.reference"), A = h.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, j = Object.prototype.hasOwnProperty, M = Array.isArray, N = console.createTask ? console.createTask : function() {
|
|
166
|
+
return null;
|
|
167
|
+
};
|
|
168
|
+
h = { react_stack_bottom_frame: function(e) {
|
|
169
|
+
return e();
|
|
170
|
+
} };
|
|
171
|
+
var P, F = {}, I = h.react_stack_bottom_frame.bind(h, s)(), L = N(a(s)), R = {};
|
|
172
|
+
e.Fragment = v, e.jsx = function(e, t, n) {
|
|
173
|
+
var r = 1e4 > A.recentlyCreatedOwnerStacks++;
|
|
174
|
+
return f(e, t, n, !1, r ? Error("react-stack-top-frame") : I, r ? N(a(e)) : L);
|
|
175
|
+
}, e.jsxs = function(e, t, n) {
|
|
176
|
+
var r = 1e4 > A.recentlyCreatedOwnerStacks++;
|
|
177
|
+
return f(e, t, n, !0, r ? Error("react-stack-top-frame") : I, r ? N(a(e)) : L);
|
|
178
|
+
};
|
|
179
|
+
})();
|
|
180
|
+
})), a = (/* @__PURE__ */ t(((e, t) => {
|
|
181
|
+
process.env.NODE_ENV === "production" ? t.exports = r() : t.exports = i();
|
|
182
|
+
})))();
|
|
183
|
+
function o({ isSelected: e, isDisabled: t, isInteractive: n, onClick: r, label: i }) {
|
|
184
|
+
return /* @__PURE__ */ (0, a.jsx)("button", {
|
|
185
|
+
type: "button",
|
|
186
|
+
className: [
|
|
187
|
+
"multi-select-picker__item",
|
|
188
|
+
e ? "is-selected" : "",
|
|
189
|
+
t ? "is-disabled" : "",
|
|
190
|
+
!n && !t ? "is-limit" : ""
|
|
191
|
+
].filter(Boolean).join(" "),
|
|
192
|
+
onClick: r,
|
|
193
|
+
"aria-disabled": t,
|
|
194
|
+
"aria-pressed": e,
|
|
195
|
+
children: /* @__PURE__ */ (0, a.jsx)("span", {
|
|
196
|
+
className: "multi-select-picker__label",
|
|
197
|
+
children: i
|
|
198
|
+
})
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
202
|
+
//#region src/utils/selection.ts
|
|
203
|
+
function s(e, t) {
|
|
204
|
+
return c(e, t) >= 0;
|
|
205
|
+
}
|
|
206
|
+
function c(e, t) {
|
|
207
|
+
return t.findIndex((t) => Object.is(e, t));
|
|
208
|
+
}
|
|
209
|
+
function l({ item: e, selectedItemKeys: t, selectedItems: n, maxSelection: r, isDisabled: i }) {
|
|
210
|
+
if (i) return {
|
|
211
|
+
type: "disabled",
|
|
212
|
+
nextSelectedItems: n
|
|
213
|
+
};
|
|
214
|
+
let a = c(e.key, t);
|
|
215
|
+
return a >= 0 ? {
|
|
216
|
+
type: "deselected",
|
|
217
|
+
nextSelectedItems: n.filter((e, t) => t !== a)
|
|
218
|
+
} : n.length >= r ? {
|
|
219
|
+
type: "limit",
|
|
220
|
+
nextSelectedItems: n
|
|
221
|
+
} : {
|
|
222
|
+
type: "selected",
|
|
223
|
+
nextSelectedItems: [...n, e]
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/hooks/useSelectablePicker.ts
|
|
228
|
+
function u({ items: t, selectedItems: n, maxSelection: r, onChange: i, onSelectionLimitReached: a, onDisabledClick: o }) {
|
|
229
|
+
return e(() => {
|
|
230
|
+
d(t.map((e) => e.key), "items");
|
|
231
|
+
let e = n.map((e) => e.key);
|
|
232
|
+
return d(e, "selectedItems"), t.map((t, u) => {
|
|
233
|
+
let { key: d, value: f } = t, p = c(d, e), m = p >= 0, h = t.disabled ?? !1;
|
|
234
|
+
return {
|
|
235
|
+
key: d,
|
|
236
|
+
index: u,
|
|
237
|
+
renderParams: {
|
|
238
|
+
item: t,
|
|
239
|
+
value: f,
|
|
240
|
+
isSelected: m,
|
|
241
|
+
isDisabled: h,
|
|
242
|
+
isInteractive: s(d, e) || !h && n.length < r,
|
|
243
|
+
selectedOrder: m ? p + 1 : null,
|
|
244
|
+
onClick: () => {
|
|
245
|
+
let s = l({
|
|
246
|
+
item: t,
|
|
247
|
+
selectedItemKeys: e,
|
|
248
|
+
selectedItems: n,
|
|
249
|
+
maxSelection: r,
|
|
250
|
+
isDisabled: h
|
|
251
|
+
});
|
|
252
|
+
if (s.type === "disabled") {
|
|
253
|
+
o?.(t);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (s.type === "limit") {
|
|
257
|
+
a?.(t);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
i(s.nextSelectedItems);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
});
|
|
265
|
+
}, [
|
|
266
|
+
t,
|
|
267
|
+
r,
|
|
268
|
+
i,
|
|
269
|
+
o,
|
|
270
|
+
a,
|
|
271
|
+
n
|
|
272
|
+
]);
|
|
273
|
+
}
|
|
274
|
+
function d(e, t) {
|
|
275
|
+
let n = /* @__PURE__ */ new Set();
|
|
276
|
+
for (let r of e) {
|
|
277
|
+
if (n.has(r)) throw Error(`MultiSelectButtons received duplicate key in ${t}: ${String(r)}`);
|
|
278
|
+
n.add(r);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/components/MultiSelectButtons.tsx
|
|
283
|
+
function f(e) {
|
|
284
|
+
let { className: t, items: n, style: r, renderValue: i } = e, s = u(e);
|
|
285
|
+
return /* @__PURE__ */ (0, a.jsx)("div", {
|
|
286
|
+
className: ["multi-select-picker", t].filter(Boolean).join(" "),
|
|
287
|
+
style: r,
|
|
288
|
+
children: s.map(({ key: e, index: t, renderParams: r }) => /* @__PURE__ */ (0, a.jsx)("div", {
|
|
289
|
+
className: "multi-select-picker__cell",
|
|
290
|
+
children: i ? i(r) : /* @__PURE__ */ (0, a.jsx)(o, {
|
|
291
|
+
...r,
|
|
292
|
+
label: n[t]?.label ?? String(r.value)
|
|
293
|
+
})
|
|
294
|
+
}, e))
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
//#endregion
|
|
298
|
+
//#region src/components/NumberMultiSelectButtons.tsx
|
|
299
|
+
function p({ min: t, max: n, step: r = 1, selectedValues: i, maxSelection: o, onChange: s, getValueLabel: c, isValueDisabled: l, renderValue: u, onSelectionLimitReached: d, onDisabledClick: p, className: m, style: h }) {
|
|
300
|
+
let g = e(() => {
|
|
301
|
+
let e = [];
|
|
302
|
+
for (let i = t; i <= n; i += r) e.push({
|
|
303
|
+
key: i,
|
|
304
|
+
label: c?.(i, e.length) ?? String(i),
|
|
305
|
+
value: i,
|
|
306
|
+
disabled: l?.(i, e.length) ?? !1
|
|
307
|
+
});
|
|
308
|
+
return e;
|
|
309
|
+
}, [
|
|
310
|
+
c,
|
|
311
|
+
l,
|
|
312
|
+
n,
|
|
313
|
+
t,
|
|
314
|
+
r
|
|
315
|
+
]);
|
|
316
|
+
return /* @__PURE__ */ (0, a.jsx)(f, {
|
|
317
|
+
items: g,
|
|
318
|
+
selectedItems: e(() => i.flatMap((e) => g.find((t) => Object.is(t.value, e)) ?? []), [g, i]),
|
|
319
|
+
maxSelection: o,
|
|
320
|
+
onChange: (e) => s(e.map((e) => e.value)),
|
|
321
|
+
renderValue: u,
|
|
322
|
+
onSelectionLimitReached: (e) => d?.(e.value),
|
|
323
|
+
onDisabledClick: (e) => p?.(e.value),
|
|
324
|
+
className: m,
|
|
325
|
+
style: h
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
//#endregion
|
|
329
|
+
export { f as MultiSelectButtons, p as NumberMultiSelectButtons };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { CSSProperties, Key, ReactNode } from 'react';
|
|
2
|
+
export type MultiSelectButtonItem<T> = {
|
|
3
|
+
key: Key;
|
|
4
|
+
label: string;
|
|
5
|
+
value: T;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export type RenderValueParams<T> = {
|
|
9
|
+
item: MultiSelectButtonItem<T>;
|
|
10
|
+
value: T;
|
|
11
|
+
isSelected: boolean;
|
|
12
|
+
isDisabled: boolean;
|
|
13
|
+
isInteractive: boolean;
|
|
14
|
+
selectedOrder: number | null;
|
|
15
|
+
onClick: () => void;
|
|
16
|
+
};
|
|
17
|
+
export type MultiSelectButtonsProps<T> = {
|
|
18
|
+
items: MultiSelectButtonItem<T>[];
|
|
19
|
+
selectedItems: MultiSelectButtonItem<T>[];
|
|
20
|
+
maxSelection: number;
|
|
21
|
+
onChange: (items: MultiSelectButtonItem<T>[]) => void;
|
|
22
|
+
renderValue?: (params: RenderValueParams<T>) => ReactNode;
|
|
23
|
+
onSelectionLimitReached?: (item: MultiSelectButtonItem<T>) => void;
|
|
24
|
+
onDisabledClick?: (item: MultiSelectButtonItem<T>) => void;
|
|
25
|
+
className?: string;
|
|
26
|
+
style?: CSSProperties;
|
|
27
|
+
};
|
|
28
|
+
export type NumberMultiSelectButtonsProps = {
|
|
29
|
+
min: number;
|
|
30
|
+
max: number;
|
|
31
|
+
step?: number;
|
|
32
|
+
selectedValues: number[];
|
|
33
|
+
maxSelection: number;
|
|
34
|
+
onChange: (values: number[]) => void;
|
|
35
|
+
getValueLabel?: (value: number, index: number) => string;
|
|
36
|
+
isValueDisabled?: (value: number, index: number) => boolean;
|
|
37
|
+
renderValue?: (params: RenderValueParams<number>) => ReactNode;
|
|
38
|
+
onSelectionLimitReached?: (value: number) => void;
|
|
39
|
+
onDisabledClick?: (value: number) => void;
|
|
40
|
+
className?: string;
|
|
41
|
+
style?: CSSProperties;
|
|
42
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MultiSelectButtonItem } from '../types';
|
|
2
|
+
import { Key } from 'react';
|
|
3
|
+
export type ToggleSelectionResult<T> = {
|
|
4
|
+
type: 'disabled';
|
|
5
|
+
nextSelectedItems: MultiSelectButtonItem<T>[];
|
|
6
|
+
} | {
|
|
7
|
+
type: 'limit';
|
|
8
|
+
nextSelectedItems: MultiSelectButtonItem<T>[];
|
|
9
|
+
} | {
|
|
10
|
+
type: 'selected';
|
|
11
|
+
nextSelectedItems: MultiSelectButtonItem<T>[];
|
|
12
|
+
} | {
|
|
13
|
+
type: 'deselected';
|
|
14
|
+
nextSelectedItems: MultiSelectButtonItem<T>[];
|
|
15
|
+
};
|
|
16
|
+
type ToggleSelectionParams<T> = {
|
|
17
|
+
item: MultiSelectButtonItem<T>;
|
|
18
|
+
selectedItemKeys: Key[];
|
|
19
|
+
selectedItems: MultiSelectButtonItem<T>[];
|
|
20
|
+
maxSelection: number;
|
|
21
|
+
isDisabled: boolean;
|
|
22
|
+
};
|
|
23
|
+
export declare function isItemSelected(itemKey: Key, selectedItemKeys: Key[]): boolean;
|
|
24
|
+
export declare function getSelectedIndex(itemKey: Key, selectedItemKeys: Key[]): number;
|
|
25
|
+
export declare function toggleSelection<T>({ item, selectedItemKeys, selectedItems, maxSelection, isDisabled, }: ToggleSelectionParams<T>): ToggleSelectionResult<T>;
|
|
26
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@takashi145/react-multi-select-buttons",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Button-based multi-select components for React.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"dev": "vite",
|
|
19
|
+
"build": "tsc -b && vite build",
|
|
20
|
+
"lint": "eslint .",
|
|
21
|
+
"preview": "vite preview"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^18 || ^19",
|
|
25
|
+
"react-dom": "^18 || ^19"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@eslint/js": "^9.39.4",
|
|
29
|
+
"@types/node": "^24.12.2",
|
|
30
|
+
"@types/react": "^19.2.14",
|
|
31
|
+
"@types/react-dom": "^19.2.3",
|
|
32
|
+
"@vitejs/plugin-react": "^6.0.1",
|
|
33
|
+
"eslint": "^9.39.4",
|
|
34
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
35
|
+
"eslint-plugin-react-refresh": "^0.5.2",
|
|
36
|
+
"globals": "^17.4.0",
|
|
37
|
+
"typescript": "~6.0.2",
|
|
38
|
+
"typescript-eslint": "^8.58.0",
|
|
39
|
+
"vite": "^8.0.4",
|
|
40
|
+
"vite-plugin-css-injected-by-js": "^4.0.1",
|
|
41
|
+
"vite-plugin-dts": "^4.5.4"
|
|
42
|
+
}
|
|
43
|
+
}
|