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.
Files changed (107) hide show
  1. package/AGENTS.md +11 -2
  2. package/ReadMe.md +86 -46
  3. package/dist/DatePickerPlugin-CpGuQRsi.js +1 -0
  4. package/dist/DatePickerPlugin-DB3nnslN.mjs +1 -0
  5. package/dist/index.d.mts +1069 -245
  6. package/dist/index.d.ts +1069 -245
  7. package/dist/index.js +16 -16
  8. package/dist/index.mjs +15 -15
  9. package/dist/rolldown-runtime-5iZMYShA.js +1 -0
  10. package/dist/style.css +69 -66
  11. package/package.json +4 -2
  12. package/source/Accordion.tsx +137 -0
  13. package/source/Alert.tsx +138 -0
  14. package/source/Avatar.tsx +7 -1
  15. package/source/Badge.tsx +2 -0
  16. package/source/Breadcrumb.tsx +36 -0
  17. package/source/Button.tsx +40 -5
  18. package/source/ButtonGroup.tsx +10 -1
  19. package/source/Card.tsx +73 -4
  20. package/source/Carousel/index.tsx +391 -0
  21. package/source/Carousel/model.ts +113 -0
  22. package/source/Carousel/scroll.ts +79 -0
  23. package/source/Collapse.tsx +178 -0
  24. package/source/Drawer.tsx +127 -30
  25. package/source/{CodeBlock.tsx → Extension/CodeBlock/index.tsx} +23 -5
  26. package/source/Extension/CodeBlock/style.module.css +3 -0
  27. package/source/{Dialog.tsx → Extension/Dialog.tsx} +10 -6
  28. package/source/{EditorHTML.tsx → Extension/EditorHTML.tsx} +1 -1
  29. package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.module.less +1 -3
  30. package/source/{Icon.tsx → Extension/Icon.tsx} +1 -7
  31. package/source/{Loading.tsx → Extension/Loading.tsx} +8 -3
  32. package/source/{MonthCalendar.tsx → Extension/MonthCalendar.tsx} +3 -3
  33. package/source/{Nameplate.tsx → Extension/Nameplate.tsx} +1 -1
  34. package/source/Extension/OverlayBox.tsx +280 -0
  35. package/source/{PageNav.tsx → Extension/PageNav.tsx} +27 -9
  36. package/source/{ScrollNav → Extension/ScrollNav}/index.tsx +1 -1
  37. package/source/Extension/Select.tsx +127 -0
  38. package/source/{ShareBox.tsx → Extension/ShareBox.tsx} +1 -1
  39. package/source/{SpinnerButton.tsx → Extension/SpinnerButton.tsx} +2 -2
  40. package/source/{TableSpinner.tsx → Extension/TableSpinner.tsx} +1 -1
  41. package/source/{Timeline → Extension/Timeline}/index.tsx +1 -1
  42. package/source/{UserRank → Extension/UserRank}/index.tsx +4 -4
  43. package/source/Extension/index.ts +27 -0
  44. package/source/Form/Adorn.tsx +59 -0
  45. package/source/Form/Checkbox.tsx +78 -0
  46. package/source/Form/ChipInput.tsx +125 -0
  47. package/source/Form/Chips.tsx +69 -0
  48. package/source/Form/Combobox.tsx +374 -0
  49. package/source/Form/Control.tsx +94 -0
  50. package/source/Form/DatePicker.tsx +280 -0
  51. package/source/Form/DatePickerPlugin.ts +37 -0
  52. package/source/Form/Feedback.tsx +35 -0
  53. package/source/Form/FloatingLabel.tsx +32 -0
  54. package/source/Form/InputGroup.tsx +173 -0
  55. package/source/Form/OTP.tsx +141 -0
  56. package/source/Form/PasswordStrength.tsx +183 -0
  57. package/source/Form/Plugin.tsx +53 -0
  58. package/source/Form/Primitives.tsx +88 -0
  59. package/source/Form/Radio.tsx +43 -0
  60. package/source/Form/Range.tsx +116 -0
  61. package/source/Form/Switch.tsx +58 -0
  62. package/source/Form/index.ts +16 -0
  63. package/source/Grid.tsx +4 -0
  64. package/source/ListGroup.tsx +28 -15
  65. package/source/Menu.tsx +226 -17
  66. package/source/NativeDialog/index.tsx +109 -5
  67. package/source/Nav.tsx +18 -2
  68. package/source/NavOverflow/index.tsx +307 -0
  69. package/source/NavOverflow/model.ts +21 -0
  70. package/source/Navbar.tsx +31 -6
  71. package/source/Overlay.ts +163 -1
  72. package/source/Pagination.tsx +92 -0
  73. package/source/Placeholder.tsx +66 -0
  74. package/source/Popover.tsx +7 -2
  75. package/source/Progress/index.tsx +76 -0
  76. package/source/Progress/style.module.css +15 -0
  77. package/source/ScrollSpy/index.tsx +365 -0
  78. package/source/ScrollSpy/model.ts +17 -0
  79. package/source/Spinner.tsx +10 -0
  80. package/source/Stepper.tsx +85 -0
  81. package/source/Tab.tsx +248 -0
  82. package/source/Toast.tsx +65 -27
  83. package/source/ToggleButton.tsx +21 -7
  84. package/source/Toggler.tsx +108 -0
  85. package/source/Tooltip.tsx +6 -1
  86. package/source/index.ts +16 -27
  87. package/source/OverlayBox.tsx +0 -208
  88. package/source/Select.tsx +0 -95
  89. /package/source/{ClickBoundary.tsx → Extension/ClickBoundary.tsx} +0 -0
  90. /package/source/{Countdown.tsx → Extension/Countdown.tsx} +0 -0
  91. /package/source/{Editor.tsx → Extension/Editor.tsx} +0 -0
  92. /package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.tsx +0 -0
  93. /package/source/{LiveTSX.tsx → Extension/LiveTSX.tsx} +0 -0
  94. /package/source/{ScrollNav → Extension/ScrollNav}/Scroller.tsx +0 -0
  95. /package/source/{ScrollNav → Extension/ScrollNav}/index.module.less +0 -0
  96. /package/source/{TextTruncate → Extension/TextTruncate}/Content.tsx +0 -0
  97. /package/source/{TextTruncate → Extension/TextTruncate}/index.tsx +0 -0
  98. /package/source/{Time.tsx → Extension/Time.tsx} +0 -0
  99. /package/source/{TimeDistance.tsx → Extension/TimeDistance.tsx} +0 -0
  100. /package/source/{Timeline → Extension/Timeline}/index.module.less +0 -0
  101. /package/source/{TypeEcho → Extension/TypeEcho}/Content.tsx +0 -0
  102. /package/source/{TypeEcho → Extension/TypeEcho}/index.tsx +0 -0
  103. /package/source/{UserRank → Extension/UserRank}/Address.tsx +0 -0
  104. /package/source/{UserRank → Extension/UserRank}/index.module.less +0 -0
  105. /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.module.less +0 -0
  106. /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.tsx +0 -0
  107. /package/source/{ZodiacBar.tsx → Extension/ZodiacBar.tsx} +0 -0
@@ -0,0 +1,94 @@
1
+ import classNames from 'classnames';
2
+ import { ComponentPropsWithRef, FC } from 'react';
3
+
4
+ import { FormValidationProps } from './Primitives';
5
+
6
+ const CONTROL_SIZES = {
7
+ sm: 'form-control-sm',
8
+ lg: 'form-control-lg'
9
+ } as const;
10
+
11
+ export type FormControlProps = Omit<ComponentPropsWithRef<'input'>, 'color'> &
12
+ FormValidationProps & {
13
+ plaintext?: boolean;
14
+ ghost?: boolean;
15
+ caret?: boolean;
16
+ color?: boolean;
17
+ controlSize?: 'sm' | 'lg';
18
+ };
19
+
20
+ export const FormControl: FC<FormControlProps> = ({
21
+ className,
22
+ plaintext,
23
+ ghost,
24
+ caret,
25
+ color,
26
+ controlSize,
27
+ validation,
28
+ ...props
29
+ }) => (
30
+ <input
31
+ {...props}
32
+ aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
33
+ className={classNames(
34
+ ghost ? 'form-ghost' : plaintext ? 'form-control-plaintext' : 'form-control',
35
+ !ghost && caret && 'form-control-caret',
36
+ !ghost && !plaintext && color && 'form-control-color',
37
+ !ghost && !plaintext && controlSize && CONTROL_SIZES[controlSize],
38
+ validation && `is-${validation}`,
39
+ className
40
+ )}
41
+ />
42
+ );
43
+
44
+ FormControl.displayName = 'FormControl';
45
+
46
+ export type FormTextAreaProps = ComponentPropsWithRef<'textarea'> &
47
+ FormValidationProps & {
48
+ controlSize?: 'sm' | 'lg';
49
+ };
50
+
51
+ export const FormTextArea: FC<FormTextAreaProps> = ({
52
+ className,
53
+ controlSize,
54
+ validation,
55
+ ...props
56
+ }) => (
57
+ <textarea
58
+ {...props}
59
+ aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
60
+ className={classNames(
61
+ 'form-control',
62
+ controlSize && CONTROL_SIZES[controlSize],
63
+ validation && `is-${validation}`,
64
+ className
65
+ )}
66
+ />
67
+ );
68
+
69
+ FormTextArea.displayName = 'FormTextArea';
70
+
71
+ export type FormSelectProps = ComponentPropsWithRef<'select'> &
72
+ FormValidationProps & {
73
+ controlSize?: 'sm' | 'lg';
74
+ };
75
+
76
+ export const FormSelect: FC<FormSelectProps> = ({
77
+ className,
78
+ controlSize,
79
+ validation,
80
+ ...props
81
+ }) => (
82
+ <select
83
+ {...props}
84
+ aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
85
+ className={classNames(
86
+ 'form-control',
87
+ controlSize && CONTROL_SIZES[controlSize],
88
+ validation && `is-${validation}`,
89
+ className
90
+ )}
91
+ />
92
+ );
93
+
94
+ FormSelect.displayName = 'FormSelect';
@@ -0,0 +1,280 @@
1
+ 'use client';
2
+
3
+ import Datepicker, { type DatepickerConfig } from 'bootstrap/js/src/datepicker.js';
4
+ import classNames from 'classnames';
5
+ import { ComponentPropsWithoutRef, FC, forwardRef, Ref } from 'react';
6
+
7
+ import { Button, ButtonProps } from '../Button';
8
+ import { Icon } from '../Extension/Icon';
9
+ import { FormPlugin } from './Plugin';
10
+ import { FormValidationProps } from './Primitives';
11
+
12
+ export type DatePickerOptions = Partial<DatepickerConfig>;
13
+
14
+ interface DatePickerSettings {
15
+ options?: DatePickerOptions;
16
+ inline?: boolean;
17
+ selectionMode?: DatepickerConfig['selectionMode'];
18
+ displayMonthsCount?: DatepickerConfig['displayMonthsCount'];
19
+ selectedDates?: string[];
20
+ firstWeekday?: DatepickerConfig['firstWeekday'];
21
+ placement?: DatepickerConfig['placement'];
22
+ datepickerTheme?: string;
23
+ onDatesChange?: (dates: string[]) => unknown;
24
+ }
25
+
26
+ export type DatePickerProps = Omit<ComponentPropsWithoutRef<'input'>, 'type'> &
27
+ DatePickerSettings &
28
+ FormValidationProps & {
29
+ enhanced?: boolean;
30
+ ghost?: boolean;
31
+ controlSize?: 'sm' | 'lg';
32
+ };
33
+
34
+ export type DatePickerButtonProps = Extract<ButtonProps, { href?: never }> &
35
+ DatePickerSettings & {
36
+ leadingIcon?: string;
37
+ };
38
+ export type DatePickerInlineProps = ComponentPropsWithoutRef<'div'> & DatePickerSettings;
39
+
40
+ interface DatePickerComponentProps extends DatePickerProps {
41
+ kind?: 'input' | 'button' | 'div';
42
+ hostProps?: DatePickerButtonProps | DatePickerInlineProps;
43
+ leadingIcon?: string;
44
+ forwardedRef?: Ref<HTMLElement>;
45
+ }
46
+
47
+ class DatePickerComponent extends FormPlugin<
48
+ DatePickerComponentProps,
49
+ Pick<Datepicker, 'dispose'>,
50
+ HTMLElement
51
+ > {
52
+ protected configurationChanged = (previous: DatePickerComponentProps) =>
53
+ previous.options !== this.props.options ||
54
+ previous.enhanced !== this.props.enhanced ||
55
+ previous.inline !== this.props.inline ||
56
+ previous.selectionMode !== this.props.selectionMode ||
57
+ previous.displayMonthsCount !== this.props.displayMonthsCount ||
58
+ previous.firstWeekday !== this.props.firstWeekday ||
59
+ previous.placement !== this.props.placement ||
60
+ previous.datepickerTheme !== this.props.datepickerTheme ||
61
+ previous.min !== this.props.min ||
62
+ previous.max !== this.props.max ||
63
+ JSON.stringify(previous.selectedDates) !== JSON.stringify(this.props.selectedDates);
64
+
65
+ protected load = async () => {
66
+ const { enhanced, kind = 'input' } = this.props;
67
+ if (!enhanced && kind === 'input') return () => ({ dispose: () => undefined });
68
+
69
+ const { ReactDatepicker } = await import('./DatePickerPlugin');
70
+ const {
71
+ options,
72
+ inline,
73
+ selectionMode,
74
+ min,
75
+ max,
76
+ displayMonthsCount,
77
+ selectedDates,
78
+ firstWeekday,
79
+ placement,
80
+ datepickerTheme
81
+ } = this.props;
82
+ const config: DatePickerOptions = {
83
+ ...options,
84
+ ...(inline !== undefined && { inline }),
85
+ ...(selectionMode !== undefined && { selectionMode }),
86
+ ...(min !== undefined && { dateMin: String(min) as DatepickerConfig['dateMin'] }),
87
+ ...(max !== undefined && { dateMax: String(max) as DatepickerConfig['dateMax'] }),
88
+ ...(displayMonthsCount !== undefined && { displayMonthsCount }),
89
+ ...(selectedDates !== undefined && { selectedDates }),
90
+ ...(firstWeekday !== undefined && { firstWeekday }),
91
+ ...(placement !== undefined && { placement }),
92
+ ...(datepickerTheme !== undefined && { datepickerTheme })
93
+ };
94
+
95
+ return (element: HTMLElement) => {
96
+ const children = [...element.childNodes];
97
+ const instance = ReactDatepicker.getOrCreateInstance(element, config);
98
+ element.addEventListener('change.bs.datepicker', this.handleDatesChange);
99
+
100
+ return {
101
+ dispose: () => {
102
+ element.removeEventListener('change.bs.datepicker', this.handleDatesChange);
103
+ instance.dispose();
104
+ if (this.props.kind === 'div') element.replaceChildren(...children);
105
+ }
106
+ };
107
+ };
108
+ };
109
+
110
+ private handleDatesChange = (event: Event) =>
111
+ this.props.onDatesChange?.((event as Event & { dates: string[] }).dates);
112
+
113
+ private setRef = (element: HTMLElement | null) => {
114
+ this.element.current = element;
115
+ const { forwardedRef } = this.props;
116
+
117
+ if (typeof forwardedRef === 'function') forwardedRef(element);
118
+ else if (forwardedRef) forwardedRef.current = element;
119
+ };
120
+
121
+ render() {
122
+ const {
123
+ className,
124
+ enhanced,
125
+ inline,
126
+ selectionMode,
127
+ min,
128
+ max,
129
+ options: _options,
130
+ displayMonthsCount,
131
+ selectedDates,
132
+ firstWeekday,
133
+ placement,
134
+ datepickerTheme,
135
+ onDatesChange: _onDatesChange,
136
+ ghost,
137
+ controlSize,
138
+ validation,
139
+ kind = 'input',
140
+ hostProps,
141
+ leadingIcon,
142
+ forwardedRef: _forwardedRef,
143
+ ...props
144
+ } = this.props;
145
+ const attributes = {
146
+ 'data-bs-toggle': enhanced || kind !== 'input' ? 'datepicker' : undefined,
147
+ 'data-bs-inline': inline || kind === 'div' || undefined,
148
+ 'data-bs-selection-mode': selectionMode,
149
+ 'data-bs-display-months-count': displayMonthsCount,
150
+ 'data-bs-selected-dates': selectedDates ? JSON.stringify(selectedDates) : undefined,
151
+ 'data-bs-first-weekday': firstWeekday,
152
+ 'data-bs-placement': placement,
153
+ 'data-bs-datepicker-theme': datepickerTheme,
154
+ 'data-bs-date-min': enhanced ? min : undefined,
155
+ 'data-bs-date-max': enhanced ? max : undefined
156
+ };
157
+
158
+ if (kind === 'button') {
159
+ const { children, ...buttonProps } = hostProps as ButtonProps;
160
+
161
+ return (
162
+ <Button {...buttonProps} {...attributes} ref={this.setRef}>
163
+ {leadingIcon && <Icon name={leadingIcon} aria-hidden="true" />}
164
+ <span data-bs-datepicker-display="">{children}</span>
165
+ </Button>
166
+ );
167
+ }
168
+ if (kind === 'div')
169
+ return (
170
+ <div
171
+ {...(hostProps as ComponentPropsWithoutRef<'div'>)}
172
+ {...attributes}
173
+ ref={this.setRef}
174
+ />
175
+ );
176
+
177
+ return (
178
+ <input
179
+ {...props}
180
+ {...attributes}
181
+ ref={this.setRef}
182
+ className={classNames(
183
+ ghost ? 'form-ghost' : 'form-control',
184
+ controlSize && `form-control-${controlSize}`,
185
+ validation && `is-${validation}`,
186
+ className
187
+ )}
188
+ aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
189
+ type={enhanced ? 'text' : 'date'}
190
+ min={min}
191
+ max={max}
192
+ />
193
+ );
194
+ }
195
+ }
196
+
197
+ export const DatePicker = forwardRef<HTMLInputElement, DatePickerProps>((props, forwardedRef) => (
198
+ <DatePickerComponent {...props} forwardedRef={forwardedRef} />
199
+ ));
200
+
201
+ DatePicker.displayName = 'DatePicker';
202
+
203
+ export const DatePickerButton: FC<DatePickerButtonProps> = forwardRef<
204
+ HTMLButtonElement,
205
+ DatePickerButtonProps
206
+ >(
207
+ (
208
+ {
209
+ options,
210
+ inline,
211
+ selectionMode,
212
+ displayMonthsCount,
213
+ selectedDates,
214
+ firstWeekday,
215
+ placement,
216
+ datepickerTheme,
217
+ onDatesChange,
218
+ leadingIcon,
219
+ ...hostProps
220
+ },
221
+ forwardedRef
222
+ ) => (
223
+ <DatePickerComponent
224
+ kind="button"
225
+ leadingIcon={leadingIcon}
226
+ hostProps={hostProps}
227
+ forwardedRef={forwardedRef}
228
+ {...{
229
+ options,
230
+ inline,
231
+ selectionMode,
232
+ displayMonthsCount,
233
+ selectedDates,
234
+ firstWeekday,
235
+ placement,
236
+ datepickerTheme,
237
+ onDatesChange
238
+ }}
239
+ />
240
+ )
241
+ );
242
+
243
+ DatePickerButton.displayName = 'DatePickerButton';
244
+
245
+ export const DatePickerInline = forwardRef<HTMLDivElement, DatePickerInlineProps>(
246
+ (
247
+ {
248
+ options,
249
+ selectionMode,
250
+ displayMonthsCount,
251
+ selectedDates,
252
+ firstWeekday,
253
+ placement,
254
+ datepickerTheme,
255
+ onDatesChange,
256
+ inline = true,
257
+ ...hostProps
258
+ },
259
+ forwardedRef
260
+ ) => (
261
+ <DatePickerComponent
262
+ kind="div"
263
+ hostProps={hostProps}
264
+ forwardedRef={forwardedRef}
265
+ {...{
266
+ options,
267
+ inline,
268
+ selectionMode,
269
+ displayMonthsCount,
270
+ selectedDates,
271
+ firstWeekday,
272
+ placement,
273
+ datepickerTheme,
274
+ onDatesChange
275
+ }}
276
+ />
277
+ )
278
+ );
279
+
280
+ DatePickerInline.displayName = 'DatePickerInline';
@@ -0,0 +1,37 @@
1
+ import Datepicker from 'bootstrap/js/src/datepicker.js';
2
+
3
+ export class ReactDatepicker extends Datepicker {
4
+ private changingVisibility = false;
5
+
6
+ async show() {
7
+ if (this.changingVisibility) return;
8
+
9
+ this.changingVisibility = true;
10
+ try {
11
+ await super.show();
12
+ } finally {
13
+ this.changingVisibility = false;
14
+ }
15
+ }
16
+
17
+ async hide() {
18
+ if (this.changingVisibility) return;
19
+
20
+ this.changingVisibility = true;
21
+ try {
22
+ await super.hide();
23
+ } finally {
24
+ this.changingVisibility = false;
25
+ }
26
+ }
27
+
28
+ dispose() {
29
+ // Calendar Pro normally replaces its trigger with a clone on destroy.
30
+ // Keep React's original node and its ref/event bindings intact.
31
+ const element = this._positionElement;
32
+ const replacement = this._calendar?.context.originalElement;
33
+
34
+ super.dispose();
35
+ replacement?.replaceWith(element);
36
+ }
37
+ }
@@ -0,0 +1,35 @@
1
+ import classNames from 'classnames';
2
+ import { ComponentPropsWithRef, FC } from 'react';
3
+
4
+ export type FormTextProps = ComponentPropsWithRef<'small'> & {
5
+ block?: boolean;
6
+ };
7
+
8
+ export const FormText: FC<FormTextProps> = ({ className, block, ...props }) => (
9
+ <small {...props} className={classNames('form-text', block && 'd-block', className)} />
10
+ );
11
+
12
+ FormText.displayName = 'FormText';
13
+
14
+ export type FormFeedbackProps = Omit<ComponentPropsWithRef<'div'>, 'type'> & {
15
+ type?: 'valid' | 'invalid';
16
+ tooltip?: boolean;
17
+ };
18
+
19
+ export const FormFeedback: FC<FormFeedbackProps> = ({
20
+ className,
21
+ type = 'invalid',
22
+ tooltip,
23
+ ...props
24
+ }) => (
25
+ <div
26
+ {...props}
27
+ className={classNames(
28
+ tooltip && 'tooltip',
29
+ tooltip ? `${type}-tooltip` : `${type}-feedback`,
30
+ className
31
+ )}
32
+ />
33
+ );
34
+
35
+ FormFeedback.displayName = 'FormFeedback';
@@ -0,0 +1,32 @@
1
+ import classNames from 'classnames';
2
+ import { Children, ComponentPropsWithRef, FC, isValidElement, ReactNode } from 'react';
3
+
4
+ import { FormLabel } from './Primitives';
5
+
6
+ export type FormFloatingLabelProps = ComponentPropsWithRef<'div'> & {
7
+ always?: boolean;
8
+ };
9
+
10
+ const isLabel = (child: ReactNode) =>
11
+ isValidElement(child) && (child.type === 'label' || child.type === FormLabel);
12
+
13
+ export const FormFloatingLabel: FC<FormFloatingLabelProps> = ({
14
+ className,
15
+ always,
16
+ children,
17
+ ...props
18
+ }) => {
19
+ const items = Children.toArray(children);
20
+
21
+ return (
22
+ <div
23
+ {...props}
24
+ className={classNames('form-floating', always && 'form-floating-always', className)}
25
+ >
26
+ {items.filter(isLabel)}
27
+ {items.filter(item => !isLabel(item))}
28
+ </div>
29
+ );
30
+ };
31
+
32
+ FormFloatingLabel.displayName = 'FormFloatingLabel';
@@ -0,0 +1,173 @@
1
+ 'use client';
2
+
3
+ import classNames from 'classnames';
4
+ import { observer } from 'mobx-react';
5
+ import {
6
+ cloneElement,
7
+ ComponentPropsWithRef,
8
+ FC,
9
+ FocusEvent,
10
+ KeyboardEventHandler,
11
+ ReactElement
12
+ } from 'react';
13
+
14
+ import { Button, ButtonProps } from '../Button';
15
+ import { Menu, MenuProps } from '../Menu';
16
+
17
+ export type InputGroupProps = ComponentPropsWithRef<'div'> & {
18
+ size?: 'sm' | 'lg';
19
+ };
20
+
21
+ export const InputGroup: FC<InputGroupProps> = ({ className, size, ...props }) => (
22
+ <div
23
+ {...props}
24
+ className={classNames('input-group', size && `input-group-${size}`, className)}
25
+ />
26
+ );
27
+
28
+ InputGroup.displayName = 'InputGroup';
29
+
30
+ export type InputGroupTextProps =
31
+ | (ComponentPropsWithRef<'span'> & { htmlFor?: never })
32
+ | (ComponentPropsWithRef<'label'> & { htmlFor: string });
33
+
34
+ const isLabelAddon = (
35
+ props: InputGroupTextProps
36
+ ): props is ComponentPropsWithRef<'label'> & { htmlFor: string } =>
37
+ typeof props.htmlFor === 'string';
38
+
39
+ export const InputGroupText: FC<InputGroupTextProps> = ({ className, ...props }) => {
40
+ const addonClassName = classNames('input-group-text', className);
41
+
42
+ return isLabelAddon(props) ? (
43
+ <label {...props} className={addonClassName} />
44
+ ) : (
45
+ <span {...props} className={addonClassName} />
46
+ );
47
+ };
48
+
49
+ InputGroupText.displayName = 'InputGroupText';
50
+
51
+ export type InputGroupButtonProps = ButtonProps;
52
+
53
+ export const InputGroupButton: FC<InputGroupButtonProps> = ({ className, ...props }) => (
54
+ <Button {...props} className={classNames('input-group-btn', className)} />
55
+ );
56
+
57
+ InputGroupButton.displayName = 'InputGroupButton';
58
+
59
+ export interface InputGroupIgnoreProps {
60
+ children: ReactElement<{ className?: string }>;
61
+ }
62
+
63
+ export const InputGroupIgnore: FC<InputGroupIgnoreProps> = ({ children }) =>
64
+ cloneElement(children, {
65
+ className: classNames('input-group-ignore', children.props.className)
66
+ });
67
+
68
+ InputGroupIgnore.displayName = 'InputGroupIgnore';
69
+
70
+ export interface InputGroupMenuProps extends MenuProps {
71
+ split?: boolean;
72
+ }
73
+
74
+ export class InputGroupMenu extends Menu {
75
+ declare props: InputGroupMenuProps;
76
+
77
+ static displayName = 'InputGroupMenu';
78
+
79
+ componentDidMount() {
80
+ this.root.current = this.trigger.current?.closest<HTMLDivElement>('.input-group') ?? null;
81
+ super.componentDidMount();
82
+ }
83
+
84
+ handlePanelClick: ComponentPropsWithRef<'div'>['onClick'] = event => {
85
+ this.props.onClick?.(event);
86
+
87
+ if (!event.defaultPrevented) this.handleSelect(event);
88
+ };
89
+
90
+ handleTriggerKeyDown: KeyboardEventHandler<HTMLElement> = this.handleKeyDown;
91
+
92
+ handleBlur = (event: FocusEvent<HTMLElement>) => {
93
+ this.props.onBlur?.(event as FocusEvent<HTMLDivElement>);
94
+
95
+ const { autoClose = true } = this.props;
96
+ if (
97
+ !event.defaultPrevented &&
98
+ autoClose !== false &&
99
+ autoClose !== 'inside' &&
100
+ !this.root.current?.contains(event.relatedTarget)
101
+ )
102
+ this.setOpen(false);
103
+ };
104
+
105
+ render() {
106
+ return <InputGroupMenuView {...this.props} owner={this} />;
107
+ }
108
+ }
109
+
110
+ const InputGroupMenuView = observer<FC<InputGroupMenuProps & { owner: InputGroupMenu }>>(
111
+ ({
112
+ owner: {
113
+ open,
114
+ trigger,
115
+ panel,
116
+ handleToggle,
117
+ handleTriggerKeyDown,
118
+ handleBlur,
119
+ handlePanelClick,
120
+ handleKeyDown
121
+ },
122
+ id,
123
+ label,
124
+ split,
125
+ toggleProps,
126
+ className,
127
+ children,
128
+ align = 'end',
129
+ menuClassName
130
+ }) => (
131
+ <>
132
+ <Button
133
+ {...toggleProps}
134
+ ref={trigger}
135
+ className={classNames(
136
+ 'input-group-btn',
137
+ split && 'menu-toggle-split',
138
+ toggleProps?.className,
139
+ className
140
+ )}
141
+ aria-label={
142
+ split
143
+ ? (toggleProps?.['aria-label'] ?? 'Toggle menu')
144
+ : toggleProps?.['aria-label']
145
+ }
146
+ aria-expanded={open}
147
+ aria-controls={id ? `${id}-menu` : undefined}
148
+ onClick={handleToggle}
149
+ onKeyDown={handleTriggerKeyDown}
150
+ onBlur={handleBlur}
151
+ >
152
+ {label}
153
+ </Button>
154
+
155
+ <div
156
+ ref={panel}
157
+ id={id ? `${id}-menu` : undefined}
158
+ className={classNames(
159
+ 'menu top-100',
160
+ align === 'end' ? 'end-0' : 'start-0',
161
+ open && 'show',
162
+ menuClassName
163
+ )}
164
+ hidden={!open}
165
+ onClick={handlePanelClick}
166
+ onKeyDown={handleKeyDown}
167
+ onBlur={handleBlur}
168
+ >
169
+ {children}
170
+ </div>
171
+ </>
172
+ )
173
+ );