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