idea-react 3.0.0-alpha.1 → 3.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +11 -2
- package/ReadMe.md +86 -46
- package/dist/DatePickerPlugin-CpGuQRsi.js +1 -0
- package/dist/DatePickerPlugin-DB3nnslN.mjs +1 -0
- package/dist/index.d.mts +1069 -245
- package/dist/index.d.ts +1069 -245
- package/dist/index.js +16 -16
- package/dist/index.mjs +15 -15
- package/dist/rolldown-runtime-5iZMYShA.js +1 -0
- package/dist/style.css +69 -66
- package/package.json +4 -2
- package/source/Accordion.tsx +137 -0
- package/source/Alert.tsx +138 -0
- package/source/Avatar.tsx +7 -1
- package/source/Badge.tsx +2 -0
- package/source/Breadcrumb.tsx +36 -0
- package/source/Button.tsx +40 -5
- package/source/ButtonGroup.tsx +10 -1
- package/source/Card.tsx +73 -4
- package/source/Carousel/index.tsx +391 -0
- package/source/Carousel/model.ts +113 -0
- package/source/Carousel/scroll.ts +79 -0
- package/source/Collapse.tsx +178 -0
- package/source/Drawer.tsx +127 -30
- package/source/{CodeBlock.tsx → Extension/CodeBlock/index.tsx} +23 -5
- package/source/Extension/CodeBlock/style.module.css +3 -0
- package/source/{Dialog.tsx → Extension/Dialog.tsx} +10 -6
- package/source/{EditorHTML.tsx → Extension/EditorHTML.tsx} +1 -1
- package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.module.less +1 -3
- package/source/{Icon.tsx → Extension/Icon.tsx} +1 -7
- package/source/{Loading.tsx → Extension/Loading.tsx} +8 -3
- package/source/{MonthCalendar.tsx → Extension/MonthCalendar.tsx} +3 -3
- package/source/{Nameplate.tsx → Extension/Nameplate.tsx} +1 -1
- package/source/Extension/OverlayBox.tsx +280 -0
- package/source/{PageNav.tsx → Extension/PageNav.tsx} +27 -9
- package/source/{ScrollNav → Extension/ScrollNav}/index.tsx +1 -1
- package/source/Extension/Select.tsx +127 -0
- package/source/{ShareBox.tsx → Extension/ShareBox.tsx} +1 -1
- package/source/{SpinnerButton.tsx → Extension/SpinnerButton.tsx} +2 -2
- package/source/{TableSpinner.tsx → Extension/TableSpinner.tsx} +1 -1
- package/source/{Timeline → Extension/Timeline}/index.tsx +1 -1
- package/source/{UserRank → Extension/UserRank}/index.tsx +4 -4
- package/source/Extension/index.ts +27 -0
- package/source/Form/Adorn.tsx +59 -0
- package/source/Form/Checkbox.tsx +78 -0
- package/source/Form/ChipInput.tsx +125 -0
- package/source/Form/Chips.tsx +69 -0
- package/source/Form/Combobox.tsx +374 -0
- package/source/Form/Control.tsx +94 -0
- package/source/Form/DatePicker.tsx +280 -0
- package/source/Form/DatePickerPlugin.ts +37 -0
- package/source/Form/Feedback.tsx +35 -0
- package/source/Form/FloatingLabel.tsx +32 -0
- package/source/Form/InputGroup.tsx +173 -0
- package/source/Form/OTP.tsx +141 -0
- package/source/Form/PasswordStrength.tsx +183 -0
- package/source/Form/Plugin.tsx +53 -0
- package/source/Form/Primitives.tsx +88 -0
- package/source/Form/Radio.tsx +43 -0
- package/source/Form/Range.tsx +116 -0
- package/source/Form/Switch.tsx +58 -0
- package/source/Form/index.ts +16 -0
- package/source/Grid.tsx +4 -0
- package/source/ListGroup.tsx +28 -15
- package/source/Menu.tsx +226 -17
- package/source/NativeDialog/index.tsx +109 -5
- package/source/Nav.tsx +18 -2
- package/source/NavOverflow/index.tsx +307 -0
- package/source/NavOverflow/model.ts +21 -0
- package/source/Navbar.tsx +31 -6
- package/source/Overlay.ts +163 -1
- package/source/Pagination.tsx +92 -0
- package/source/Placeholder.tsx +66 -0
- package/source/Popover.tsx +7 -2
- package/source/Progress/index.tsx +76 -0
- package/source/Progress/style.module.css +15 -0
- package/source/ScrollSpy/index.tsx +365 -0
- package/source/ScrollSpy/model.ts +17 -0
- package/source/Spinner.tsx +10 -0
- package/source/Stepper.tsx +85 -0
- package/source/Tab.tsx +248 -0
- package/source/Toast.tsx +65 -27
- package/source/ToggleButton.tsx +21 -7
- package/source/Toggler.tsx +108 -0
- package/source/Tooltip.tsx +6 -1
- package/source/index.ts +16 -27
- package/source/OverlayBox.tsx +0 -208
- package/source/Select.tsx +0 -95
- /package/source/{ClickBoundary.tsx → Extension/ClickBoundary.tsx} +0 -0
- /package/source/{Countdown.tsx → Extension/Countdown.tsx} +0 -0
- /package/source/{Editor.tsx → Extension/Editor.tsx} +0 -0
- /package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.tsx +0 -0
- /package/source/{LiveTSX.tsx → Extension/LiveTSX.tsx} +0 -0
- /package/source/{ScrollNav → Extension/ScrollNav}/Scroller.tsx +0 -0
- /package/source/{ScrollNav → Extension/ScrollNav}/index.module.less +0 -0
- /package/source/{TextTruncate → Extension/TextTruncate}/Content.tsx +0 -0
- /package/source/{TextTruncate → Extension/TextTruncate}/index.tsx +0 -0
- /package/source/{Time.tsx → Extension/Time.tsx} +0 -0
- /package/source/{TimeDistance.tsx → Extension/TimeDistance.tsx} +0 -0
- /package/source/{Timeline → Extension/Timeline}/index.module.less +0 -0
- /package/source/{TypeEcho → Extension/TypeEcho}/Content.tsx +0 -0
- /package/source/{TypeEcho → Extension/TypeEcho}/index.tsx +0 -0
- /package/source/{UserRank → Extension/UserRank}/Address.tsx +0 -0
- /package/source/{UserRank → Extension/UserRank}/index.module.less +0 -0
- /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.module.less +0 -0
- /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.tsx +0 -0
- /package/source/{ZodiacBar.tsx → Extension/ZodiacBar.tsx} +0 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { Component, ComponentPropsWithoutRef, forwardRef, Ref } from 'react';
|
|
5
|
+
|
|
6
|
+
import type { ComponentTheme } from '../Button';
|
|
7
|
+
import { FormValidationProps } from './Primitives';
|
|
8
|
+
|
|
9
|
+
export type CheckboxProps = Omit<ComponentPropsWithoutRef<'input'>, 'type' | 'size'> &
|
|
10
|
+
FormValidationProps & {
|
|
11
|
+
size?: 'sm' | 'lg';
|
|
12
|
+
theme?: ComponentTheme;
|
|
13
|
+
indeterminate?: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
interface CheckboxComponentProps extends CheckboxProps {
|
|
17
|
+
forwardedRef?: Ref<HTMLInputElement>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class CheckboxComponent extends Component<CheckboxComponentProps> {
|
|
21
|
+
private input?: HTMLInputElement;
|
|
22
|
+
|
|
23
|
+
componentDidMount() {
|
|
24
|
+
this.setIndeterminate();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
componentDidUpdate() {
|
|
28
|
+
this.setIndeterminate();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private setIndeterminate = () => {
|
|
32
|
+
if (this.input) this.input.indeterminate = this.props.indeterminate || false;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
private setInputRef = (input: HTMLInputElement | null) => {
|
|
36
|
+
this.input = input || undefined;
|
|
37
|
+
|
|
38
|
+
const { forwardedRef } = this.props;
|
|
39
|
+
|
|
40
|
+
if (typeof forwardedRef === 'function') forwardedRef(input);
|
|
41
|
+
else if (forwardedRef) forwardedRef.current = input;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
render() {
|
|
45
|
+
const {
|
|
46
|
+
className,
|
|
47
|
+
size,
|
|
48
|
+
theme,
|
|
49
|
+
validation,
|
|
50
|
+
indeterminate,
|
|
51
|
+
forwardedRef: _forwardedRef,
|
|
52
|
+
...props
|
|
53
|
+
} = this.props;
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<input
|
|
57
|
+
{...props}
|
|
58
|
+
ref={this.setInputRef}
|
|
59
|
+
type="checkbox"
|
|
60
|
+
aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
|
|
61
|
+
aria-checked={indeterminate ? 'mixed' : props['aria-checked']}
|
|
62
|
+
className={classNames(
|
|
63
|
+
'check',
|
|
64
|
+
size && `check-${size}`,
|
|
65
|
+
theme && `theme-${theme}`,
|
|
66
|
+
validation && `is-${validation}`,
|
|
67
|
+
className
|
|
68
|
+
)}
|
|
69
|
+
/>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>((props, forwardedRef) => (
|
|
75
|
+
<CheckboxComponent {...props} forwardedRef={forwardedRef} />
|
|
76
|
+
));
|
|
77
|
+
|
|
78
|
+
Checkbox.displayName = 'Checkbox';
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type Chips from 'bootstrap/js/src/chips.js';
|
|
4
|
+
import classNames from 'classnames';
|
|
5
|
+
import { ComponentPropsWithoutRef, forwardRef, Ref } from 'react';
|
|
6
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
7
|
+
|
|
8
|
+
import { ComponentTheme } from '../Button';
|
|
9
|
+
import { Icon } from '../Extension/Icon';
|
|
10
|
+
import { FormPlugin } from './Plugin';
|
|
11
|
+
import { FormValidationProps } from './Primitives';
|
|
12
|
+
|
|
13
|
+
export type ChipInputProps = ComponentPropsWithoutRef<'div'> &
|
|
14
|
+
FormValidationProps & {
|
|
15
|
+
theme?: ComponentTheme;
|
|
16
|
+
separator?: string | null;
|
|
17
|
+
allowDuplicates?: boolean;
|
|
18
|
+
maxChips?: number | null;
|
|
19
|
+
createOnBlur?: boolean;
|
|
20
|
+
dismissible?: boolean;
|
|
21
|
+
placeholder?: string;
|
|
22
|
+
onValuesChange?: (values: string[]) => unknown;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
interface ChipInputComponentProps extends ChipInputProps {
|
|
26
|
+
forwardedRef?: Ref<HTMLDivElement>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class ChipInputComponent extends FormPlugin<ChipInputComponentProps, Pick<Chips, 'dispose'>> {
|
|
30
|
+
protected configurationChanged = (previous: ChipInputComponentProps) =>
|
|
31
|
+
previous.separator !== this.props.separator ||
|
|
32
|
+
previous.allowDuplicates !== this.props.allowDuplicates ||
|
|
33
|
+
previous.maxChips !== this.props.maxChips ||
|
|
34
|
+
previous.createOnBlur !== this.props.createOnBlur ||
|
|
35
|
+
previous.dismissible !== this.props.dismissible ||
|
|
36
|
+
previous.placeholder !== this.props.placeholder;
|
|
37
|
+
|
|
38
|
+
protected load = async () => {
|
|
39
|
+
const [{ default: Plugin }, { default: EventHandler }] = await Promise.all([
|
|
40
|
+
import('bootstrap/js/src/chips.js'),
|
|
41
|
+
import('bootstrap/js/src/dom/event-handler.js')
|
|
42
|
+
]);
|
|
43
|
+
const {
|
|
44
|
+
separator = ',',
|
|
45
|
+
allowDuplicates = false,
|
|
46
|
+
maxChips = null,
|
|
47
|
+
createOnBlur = true,
|
|
48
|
+
dismissible = true,
|
|
49
|
+
placeholder = ''
|
|
50
|
+
} = this.props;
|
|
51
|
+
|
|
52
|
+
return (element: HTMLDivElement) => {
|
|
53
|
+
const instance = Plugin.getOrCreateInstance(element, {
|
|
54
|
+
separator,
|
|
55
|
+
allowDuplicates,
|
|
56
|
+
maxChips,
|
|
57
|
+
createOnBlur,
|
|
58
|
+
dismissible,
|
|
59
|
+
placeholder,
|
|
60
|
+
dismissIcon: renderToStaticMarkup(<Icon name="x-lg" aria-hidden="true" />)
|
|
61
|
+
});
|
|
62
|
+
element.addEventListener('change.bs.chips', this.handleValuesChange);
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
dispose: () => {
|
|
66
|
+
element.removeEventListener('change.bs.chips', this.handleValuesChange);
|
|
67
|
+
EventHandler.off(element, '.bs.chips');
|
|
68
|
+
instance.dispose();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
private handleValuesChange = (event: Event) =>
|
|
75
|
+
this.props.onValuesChange?.((event as Event & { values: string[] }).values);
|
|
76
|
+
|
|
77
|
+
private setRef = (element: HTMLDivElement | null) => {
|
|
78
|
+
this.element.current = element;
|
|
79
|
+
const { forwardedRef } = this.props;
|
|
80
|
+
|
|
81
|
+
if (typeof forwardedRef === 'function') forwardedRef(element);
|
|
82
|
+
else if (forwardedRef) forwardedRef.current = element;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
render() {
|
|
86
|
+
const {
|
|
87
|
+
className,
|
|
88
|
+
theme,
|
|
89
|
+
separator,
|
|
90
|
+
allowDuplicates,
|
|
91
|
+
maxChips,
|
|
92
|
+
createOnBlur,
|
|
93
|
+
dismissible: _dismissible,
|
|
94
|
+
placeholder: _placeholder,
|
|
95
|
+
onValuesChange: _onValuesChange,
|
|
96
|
+
forwardedRef: _forwardedRef,
|
|
97
|
+
validation,
|
|
98
|
+
...props
|
|
99
|
+
} = this.props;
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<div
|
|
103
|
+
{...props}
|
|
104
|
+
ref={this.setRef}
|
|
105
|
+
className={classNames(
|
|
106
|
+
'chip-input',
|
|
107
|
+
theme && `theme-${theme}`,
|
|
108
|
+
validation && `is-${validation}`,
|
|
109
|
+
className
|
|
110
|
+
)}
|
|
111
|
+
data-bs-chips=""
|
|
112
|
+
data-bs-separator={separator}
|
|
113
|
+
data-bs-allow-duplicates={allowDuplicates}
|
|
114
|
+
data-bs-max-chips={maxChips}
|
|
115
|
+
data-bs-create-on-blur={createOnBlur}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const ChipInput = forwardRef<HTMLDivElement, ChipInputProps>((props, forwardedRef) => (
|
|
122
|
+
<ChipInputComponent {...props} forwardedRef={forwardedRef} />
|
|
123
|
+
));
|
|
124
|
+
|
|
125
|
+
ChipInput.displayName = 'ChipInput';
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { ComponentPropsWithRef, FC } from 'react';
|
|
3
|
+
|
|
4
|
+
import { ComponentTheme } from '../Button';
|
|
5
|
+
import { Icon, IconProps } from '../Extension/Icon';
|
|
6
|
+
|
|
7
|
+
export type ChipProps = ComponentPropsWithRef<'button'> & {
|
|
8
|
+
active?: boolean;
|
|
9
|
+
theme?: ComponentTheme;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const Chip: FC<ChipProps> = ({ className, active, theme, ...props }) => (
|
|
13
|
+
<button
|
|
14
|
+
{...props}
|
|
15
|
+
className={classNames('chip', active && 'active', theme && `theme-${theme}`, className)}
|
|
16
|
+
type="button"
|
|
17
|
+
aria-pressed={active}
|
|
18
|
+
/>
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
Chip.displayName = 'Chip';
|
|
22
|
+
|
|
23
|
+
export const ChipIcon: FC<IconProps> = ({ className, ...props }) => (
|
|
24
|
+
<Icon {...props} className={classNames('chip-icon', className)} />
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
ChipIcon.displayName = 'ChipIcon';
|
|
28
|
+
|
|
29
|
+
export const ChipImage: FC<ComponentPropsWithRef<'img'>> = ({ className, alt = '', ...props }) => (
|
|
30
|
+
<img {...props} alt={alt} className={classNames('chip-img', className)} />
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
ChipImage.displayName = 'ChipImage';
|
|
34
|
+
|
|
35
|
+
export const ChipDismiss: FC<ComponentPropsWithRef<'button'>> = ({
|
|
36
|
+
className,
|
|
37
|
+
children,
|
|
38
|
+
'aria-label': label = 'Remove',
|
|
39
|
+
...props
|
|
40
|
+
}) => (
|
|
41
|
+
<button
|
|
42
|
+
{...props}
|
|
43
|
+
type="button"
|
|
44
|
+
aria-label={label}
|
|
45
|
+
className={classNames('chip-dismiss', className)}
|
|
46
|
+
>
|
|
47
|
+
{children ?? <Icon name="x-lg" aria-hidden="true" />}
|
|
48
|
+
</button>
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
ChipDismiss.displayName = 'ChipDismiss';
|
|
52
|
+
|
|
53
|
+
export type ChipTokenProps = ComponentPropsWithRef<'span'> & Pick<ChipProps, 'active' | 'theme'>;
|
|
54
|
+
|
|
55
|
+
export const ChipToken: FC<ChipTokenProps> = ({ className, active, theme, ...props }) => (
|
|
56
|
+
<span
|
|
57
|
+
{...props}
|
|
58
|
+
tabIndex={props.tabIndex ?? 0}
|
|
59
|
+
className={classNames('chip', active && 'active', theme && `theme-${theme}`, className)}
|
|
60
|
+
/>
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
ChipToken.displayName = 'ChipToken';
|
|
64
|
+
|
|
65
|
+
export const ChipInputLabel: FC<ComponentPropsWithRef<'label'>> = ({ className, ...props }) => (
|
|
66
|
+
<label {...props} className={classNames('chip-input-label', className)} />
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
ChipInputLabel.displayName = 'ChipInputLabel';
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import classNames from 'classnames';
|
|
4
|
+
import { action, observable } from 'mobx';
|
|
5
|
+
import { observer } from 'mobx-react';
|
|
6
|
+
import {
|
|
7
|
+
ChangeEvent,
|
|
8
|
+
Children,
|
|
9
|
+
cloneElement,
|
|
10
|
+
Component,
|
|
11
|
+
ComponentPropsWithRef,
|
|
12
|
+
createRef,
|
|
13
|
+
FC,
|
|
14
|
+
isValidElement,
|
|
15
|
+
MouseEvent,
|
|
16
|
+
ReactElement,
|
|
17
|
+
ReactNode
|
|
18
|
+
} from 'react';
|
|
19
|
+
|
|
20
|
+
import { Icon } from '../Extension/Icon';
|
|
21
|
+
import { Menu, MenuHeader, MenuItemContent, MenuItemDescription } from '../Menu';
|
|
22
|
+
import { FormValidationProps } from './Primitives';
|
|
23
|
+
|
|
24
|
+
export interface ComboboxOptionProps extends ComponentPropsWithRef<'button'> {
|
|
25
|
+
value: string;
|
|
26
|
+
description?: ReactNode;
|
|
27
|
+
icon?: string;
|
|
28
|
+
checkedIcon?: boolean;
|
|
29
|
+
searchText?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const ComboboxOption: FC<ComboboxOptionProps> = ({
|
|
33
|
+
className,
|
|
34
|
+
value,
|
|
35
|
+
children,
|
|
36
|
+
description,
|
|
37
|
+
icon,
|
|
38
|
+
checkedIcon,
|
|
39
|
+
searchText,
|
|
40
|
+
...props
|
|
41
|
+
}) => (
|
|
42
|
+
<button
|
|
43
|
+
{...props}
|
|
44
|
+
type="button"
|
|
45
|
+
role="option"
|
|
46
|
+
aria-selected={props['aria-selected'] ?? false}
|
|
47
|
+
className={classNames('menu-item', className)}
|
|
48
|
+
data-bs-value={value}
|
|
49
|
+
data-search-text={searchText}
|
|
50
|
+
data-label={optionText(children)}
|
|
51
|
+
data-option-hidden={props.hidden || undefined}
|
|
52
|
+
>
|
|
53
|
+
{icon && <Icon name={icon} className="menu-item-icon" aria-hidden="true" />}
|
|
54
|
+
{description ? (
|
|
55
|
+
<MenuItemContent>
|
|
56
|
+
{children}
|
|
57
|
+
<MenuItemDescription>{description}</MenuItemDescription>
|
|
58
|
+
</MenuItemContent>
|
|
59
|
+
) : (
|
|
60
|
+
children
|
|
61
|
+
)}
|
|
62
|
+
{checkedIcon && <Icon name="check-lg" className="menu-item-check" aria-hidden="true" />}
|
|
63
|
+
</button>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
ComboboxOption.displayName = 'ComboboxOption';
|
|
67
|
+
|
|
68
|
+
export interface ComboboxGroupProps extends ComponentPropsWithRef<'div'> {
|
|
69
|
+
label: ReactNode;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const ComboboxGroup: FC<ComboboxGroupProps> = ({ label, children, ...props }) => (
|
|
73
|
+
<div {...props} role="group" aria-label={typeof label === 'string' ? label : undefined}>
|
|
74
|
+
<MenuHeader>{label}</MenuHeader>
|
|
75
|
+
{children}
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
ComboboxGroup.displayName = 'ComboboxGroup';
|
|
80
|
+
|
|
81
|
+
export interface ComboboxProps
|
|
82
|
+
extends Omit<ComponentPropsWithRef<'div'>, 'onChange' | 'defaultValue'>, FormValidationProps {
|
|
83
|
+
name: string;
|
|
84
|
+
value?: string | string[];
|
|
85
|
+
defaultValue?: string | string[];
|
|
86
|
+
placeholder?: string;
|
|
87
|
+
multiple?: boolean;
|
|
88
|
+
menuClassName?: string;
|
|
89
|
+
disabled?: boolean;
|
|
90
|
+
controlSize?: 'sm' | 'lg';
|
|
91
|
+
search?: boolean;
|
|
92
|
+
searchNormalize?: boolean;
|
|
93
|
+
searchPlaceholder?: string;
|
|
94
|
+
noResultsText?: string;
|
|
95
|
+
onChange?: (value: string | string[]) => unknown;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const optionText = (content: ReactNode): string =>
|
|
99
|
+
Children.toArray(content)
|
|
100
|
+
.map(child =>
|
|
101
|
+
isValidElement<{ children?: ReactNode }>(child)
|
|
102
|
+
? optionText(child.props.children)
|
|
103
|
+
: String(child)
|
|
104
|
+
)
|
|
105
|
+
.join(' ');
|
|
106
|
+
|
|
107
|
+
const collectOptions = (children: ReactNode): ReactElement<ComboboxOptionProps>[] =>
|
|
108
|
+
Children.toArray(children).flatMap(child =>
|
|
109
|
+
isValidElement<ComboboxOptionProps>(child) && child.type === ComboboxOption
|
|
110
|
+
? [child]
|
|
111
|
+
: isValidElement<{ children?: ReactNode }>(child)
|
|
112
|
+
? collectOptions(child.props.children)
|
|
113
|
+
: []
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
@observer
|
|
117
|
+
export class Combobox extends Component<ComboboxProps> {
|
|
118
|
+
static displayName = 'Combobox';
|
|
119
|
+
|
|
120
|
+
@observable accessor query = '';
|
|
121
|
+
@observable accessor current = this.props.defaultValue ?? (this.props.multiple ? [] : '');
|
|
122
|
+
@observable accessor visibleOptionCount = 0;
|
|
123
|
+
@observable accessor selectedLabel = '';
|
|
124
|
+
|
|
125
|
+
private menu = createRef<Menu>();
|
|
126
|
+
private optionObserver?: MutationObserver;
|
|
127
|
+
|
|
128
|
+
get values() {
|
|
129
|
+
const value = this.props.value ?? this.current;
|
|
130
|
+
|
|
131
|
+
return Array.isArray(value) ? value : value ? [value] : [];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
componentDidMount() {
|
|
135
|
+
this.updateAccessibility();
|
|
136
|
+
this.optionObserver = new MutationObserver(this.updateAccessibility);
|
|
137
|
+
|
|
138
|
+
const panel = this.menu.current?.panel.current;
|
|
139
|
+
|
|
140
|
+
if (panel)
|
|
141
|
+
this.optionObserver.observe(panel, {
|
|
142
|
+
childList: true,
|
|
143
|
+
subtree: true,
|
|
144
|
+
characterData: true
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
componentDidUpdate() {
|
|
149
|
+
this.updateAccessibility();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
componentWillUnmount() {
|
|
153
|
+
this.optionObserver?.disconnect();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
@action
|
|
157
|
+
private updateAccessibility = () => {
|
|
158
|
+
const panel = this.menu.current?.panel.current;
|
|
159
|
+
const options = [...(panel?.querySelectorAll<HTMLButtonElement>('[data-bs-value]') ?? [])];
|
|
160
|
+
const selected: string[] = [];
|
|
161
|
+
let visible = 0;
|
|
162
|
+
|
|
163
|
+
for (const option of options) {
|
|
164
|
+
const value = option.dataset.bsValue ?? '';
|
|
165
|
+
const text = option.dataset.searchText ?? option.textContent ?? '';
|
|
166
|
+
const matches =
|
|
167
|
+
option.dataset.optionHidden !== 'true' &&
|
|
168
|
+
this.normalize(text).includes(this.normalize(this.query));
|
|
169
|
+
const isSelected = this.values.includes(value);
|
|
170
|
+
|
|
171
|
+
option.setAttribute('role', 'option');
|
|
172
|
+
option.setAttribute('aria-selected', String(isSelected));
|
|
173
|
+
option.classList.toggle('selected', isSelected);
|
|
174
|
+
option.hidden = !matches;
|
|
175
|
+
option.style.display = matches ? '' : 'none';
|
|
176
|
+
|
|
177
|
+
if (matches) visible++;
|
|
178
|
+
if (isSelected) selected.push(option.dataset.label ?? option.textContent ?? '');
|
|
179
|
+
}
|
|
180
|
+
for (const group of panel?.querySelectorAll<HTMLElement>('[role="group"]') ?? []) {
|
|
181
|
+
const matches = [...group.querySelectorAll<HTMLButtonElement>('[data-bs-value]')].some(
|
|
182
|
+
({ hidden }) => !hidden
|
|
183
|
+
);
|
|
184
|
+
group.hidden = !matches;
|
|
185
|
+
group.style.display = matches ? '' : 'none';
|
|
186
|
+
}
|
|
187
|
+
this.visibleOptionCount = visible;
|
|
188
|
+
this.selectedLabel =
|
|
189
|
+
this.props.multiple && selected.length > 1
|
|
190
|
+
? `${selected.length} selected`
|
|
191
|
+
: (selected[0] ?? '');
|
|
192
|
+
|
|
193
|
+
panel?.setAttribute('role', 'listbox');
|
|
194
|
+
panel?.setAttribute('aria-multiselectable', String(Boolean(this.props.multiple)));
|
|
195
|
+
panel?.setAttribute(
|
|
196
|
+
'aria-label',
|
|
197
|
+
this.props['aria-label'] ?? this.props.placeholder ?? 'Options'
|
|
198
|
+
);
|
|
199
|
+
this.menu.current?.trigger.current?.setAttribute('aria-haspopup', 'listbox');
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
@action
|
|
203
|
+
private setCurrent = (value: string | string[]) => (this.current = value);
|
|
204
|
+
|
|
205
|
+
@action
|
|
206
|
+
private handleSearch = ({ currentTarget: { value } }: ChangeEvent<HTMLInputElement>) =>
|
|
207
|
+
(this.query = value);
|
|
208
|
+
|
|
209
|
+
private handleClick = (event: MouseEvent<HTMLDivElement>) => {
|
|
210
|
+
this.props.onClick?.(event);
|
|
211
|
+
|
|
212
|
+
const { target, defaultPrevented } = event;
|
|
213
|
+
if (defaultPrevented || this.props.disabled || !(target instanceof Element)) return;
|
|
214
|
+
|
|
215
|
+
const option = target.closest<HTMLButtonElement>('[data-bs-value]');
|
|
216
|
+
if (!option || option.disabled || option.getAttribute('aria-disabled') === 'true') return;
|
|
217
|
+
|
|
218
|
+
const next = option.dataset.bsValue ?? '';
|
|
219
|
+
const value = this.props.multiple
|
|
220
|
+
? this.values.includes(next)
|
|
221
|
+
? this.values.filter(item => item !== next)
|
|
222
|
+
: [...this.values, next]
|
|
223
|
+
: next;
|
|
224
|
+
this.props.onChange?.(value);
|
|
225
|
+
|
|
226
|
+
if (this.props.value === undefined) this.setCurrent(value);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
private normalize = (value: string) => {
|
|
230
|
+
const text = value.toLocaleLowerCase();
|
|
231
|
+
|
|
232
|
+
return this.props.searchNormalize ? text.normalize('NFD').replace(/\p{M}/gu, '') : text;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
private matches = (option: ReactElement<ComboboxOptionProps>) =>
|
|
236
|
+
this.normalize(option.props.searchText ?? optionText(option.props.children)).includes(
|
|
237
|
+
this.normalize(this.query)
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
private renderChild = (child: ReactNode): ReactNode => {
|
|
241
|
+
if (!isValidElement<{ children?: ReactNode }>(child)) return child;
|
|
242
|
+
|
|
243
|
+
if (child.type === ComboboxOption) {
|
|
244
|
+
const option = child as ReactElement<ComboboxOptionProps>;
|
|
245
|
+
const selected = this.values.includes(option.props.value);
|
|
246
|
+
|
|
247
|
+
return cloneElement(option, {
|
|
248
|
+
hidden: option.props.hidden || !this.matches(option),
|
|
249
|
+
className: classNames(option.props.className, selected && 'selected'),
|
|
250
|
+
'aria-selected': selected,
|
|
251
|
+
role: 'option'
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
const options = collectOptions(child.props.children);
|
|
255
|
+
|
|
256
|
+
return cloneElement(child, {
|
|
257
|
+
...(child.type === ComboboxGroup && { hidden: !options.some(this.matches) }),
|
|
258
|
+
children: Children.map(child.props.children, this.renderChild)
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
private renderHiddenInput = (value: string) => (
|
|
263
|
+
<input
|
|
264
|
+
key={value}
|
|
265
|
+
type="hidden"
|
|
266
|
+
name={this.props.name}
|
|
267
|
+
value={value}
|
|
268
|
+
disabled={this.props.disabled}
|
|
269
|
+
/>
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
render() {
|
|
273
|
+
const {
|
|
274
|
+
className,
|
|
275
|
+
id,
|
|
276
|
+
name: _name,
|
|
277
|
+
value: _value,
|
|
278
|
+
defaultValue: _defaultValue,
|
|
279
|
+
placeholder = 'Select an item…',
|
|
280
|
+
disabled,
|
|
281
|
+
multiple,
|
|
282
|
+
onChange: _onChange,
|
|
283
|
+
onClick: _onClick,
|
|
284
|
+
menuClassName,
|
|
285
|
+
children,
|
|
286
|
+
controlSize,
|
|
287
|
+
search,
|
|
288
|
+
searchNormalize: _searchNormalize,
|
|
289
|
+
searchPlaceholder = 'Search…',
|
|
290
|
+
noResultsText = 'No results found',
|
|
291
|
+
validation,
|
|
292
|
+
...props
|
|
293
|
+
} = this.props;
|
|
294
|
+
const options = collectOptions(children);
|
|
295
|
+
const selected = options.filter(option => this.values.includes(option.props.value));
|
|
296
|
+
const hasSelection = selected.length > 0 || Boolean(this.selectedLabel);
|
|
297
|
+
const label =
|
|
298
|
+
multiple && selected.length > 1
|
|
299
|
+
? `${selected.length} selected`
|
|
300
|
+
: (selected[0]?.props.children ?? this.selectedLabel);
|
|
301
|
+
const noResults = this.visibleOptionCount === 0;
|
|
302
|
+
|
|
303
|
+
return (
|
|
304
|
+
<div {...props} className={classNames('combobox', className)}>
|
|
305
|
+
<Menu
|
|
306
|
+
ref={this.menu}
|
|
307
|
+
className="w-100"
|
|
308
|
+
id={id ? `${id}-combobox` : undefined}
|
|
309
|
+
label={
|
|
310
|
+
<>
|
|
311
|
+
<span
|
|
312
|
+
className={classNames(
|
|
313
|
+
'combobox-value',
|
|
314
|
+
!hasSelection && 'combobox-placeholder'
|
|
315
|
+
)}
|
|
316
|
+
>
|
|
317
|
+
{hasSelection ? label : placeholder}
|
|
318
|
+
</span>
|
|
319
|
+
<Icon
|
|
320
|
+
name="chevron-down"
|
|
321
|
+
className="combobox-caret"
|
|
322
|
+
aria-hidden="true"
|
|
323
|
+
/>
|
|
324
|
+
</>
|
|
325
|
+
}
|
|
326
|
+
toggleProps={{
|
|
327
|
+
id,
|
|
328
|
+
className: classNames(
|
|
329
|
+
'form-control combobox-toggle',
|
|
330
|
+
controlSize && `form-control-${controlSize}`,
|
|
331
|
+
validation && `is-${validation}`
|
|
332
|
+
),
|
|
333
|
+
variant: false,
|
|
334
|
+
disabled,
|
|
335
|
+
'aria-haspopup': 'listbox',
|
|
336
|
+
'aria-label': this.props['aria-label'] ?? placeholder,
|
|
337
|
+
'aria-labelledby': this.props['aria-labelledby'],
|
|
338
|
+
'aria-describedby': this.props['aria-describedby'],
|
|
339
|
+
'aria-invalid': validation
|
|
340
|
+
? validation === 'invalid'
|
|
341
|
+
: this.props['aria-invalid']
|
|
342
|
+
}}
|
|
343
|
+
autoClose={!multiple}
|
|
344
|
+
menuClassName={classNames(menuClassName, 'combobox-menu')}
|
|
345
|
+
onClick={this.handleClick}
|
|
346
|
+
>
|
|
347
|
+
{search && (
|
|
348
|
+
<div className="combobox-search">
|
|
349
|
+
<input
|
|
350
|
+
type="search"
|
|
351
|
+
className="form-control combobox-search-input"
|
|
352
|
+
aria-label={searchPlaceholder}
|
|
353
|
+
placeholder={searchPlaceholder}
|
|
354
|
+
autoComplete="off"
|
|
355
|
+
value={this.query}
|
|
356
|
+
onChange={this.handleSearch}
|
|
357
|
+
/>
|
|
358
|
+
</div>
|
|
359
|
+
)}
|
|
360
|
+
{Children.map(children, this.renderChild)}
|
|
361
|
+
{search && noResults && (
|
|
362
|
+
<div className="combobox-empty" role="status">
|
|
363
|
+
{noResultsText}
|
|
364
|
+
</div>
|
|
365
|
+
)}
|
|
366
|
+
</Menu>
|
|
367
|
+
|
|
368
|
+
{this.values.length || multiple
|
|
369
|
+
? this.values.map(this.renderHiddenInput)
|
|
370
|
+
: this.renderHiddenInput('')}
|
|
371
|
+
</div>
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
}
|