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