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,65 +1,134 @@
1
1
  'use client';
2
2
 
3
+ import type Range from 'bootstrap/js/src/range.js';
3
4
  import classNames from 'classnames';
4
- import { ComponentPropsWithRef, CSSProperties, FC, InputEvent } from 'react';
5
-
6
- import type { ComponentTheme } from '../Button';
7
-
8
- export type FormRangeProps = Omit<ComponentPropsWithRef<'input'>, 'type'> & {
9
- bubble?: boolean;
10
- theme?: ComponentTheme;
11
- };
12
-
13
- export const FormRange: FC<FormRangeProps> = ({
14
- className,
15
- min = 0,
16
- max = 100,
17
- value,
18
- defaultValue,
19
- bubble,
20
- theme,
21
- onInput,
22
- style,
23
- ...props
24
- }) => {
25
- const currentValue = Number(value ?? defaultValue ?? min);
26
- const fill =
27
- Number(max) <= Number(min)
28
- ? 0
29
- : Math.max(0, Math.min(1, (currentValue - Number(min)) / (Number(max) - Number(min))));
30
-
31
- const handleInput = (event: InputEvent<HTMLInputElement>) => {
32
- const input = event.currentTarget;
33
- const range = Number(input.value);
34
- const value =
35
- Number(max) <= Number(min)
36
- ? 0
37
- : Math.max(0, Math.min(1, (range - Number(min)) / (Number(max) - Number(min))));
5
+ import { ComponentPropsWithoutRef, CSSProperties, forwardRef, Ref } from 'react';
6
+
7
+ import { ComponentTheme } from '../Button';
8
+ import { AsProps, polymorphic } from '../Polymorphic';
9
+ import { FormPlugin } from './Plugin';
10
+ import { FormValidationProps } from './Primitives';
11
+
12
+ export type FormRangeProps = Omit<ComponentPropsWithoutRef<'input'>, 'type'> &
13
+ FormValidationProps & {
14
+ bubble?: boolean;
15
+ theme?: ComponentTheme;
16
+ formatter?: (value: number) => string;
17
+ wrapperClassName?: string;
18
+ onValueChange?: (value: number) => unknown;
19
+ };
20
+
21
+ interface FormRangeComponentProps extends FormRangeProps, AsProps {
22
+ forwardedRef?: Ref<HTMLElement>;
23
+ }
24
+
25
+ class FormRangeComponent extends FormPlugin<FormRangeComponentProps, Range> {
26
+ protected configurationChanged = (previous: FormRangeComponentProps) =>
27
+ previous.bubble !== this.props.bubble ||
28
+ previous.formatter !== this.props.formatter ||
29
+ previous.list !== this.props.list ||
30
+ previous.min !== this.props.min ||
31
+ previous.max !== this.props.max;
32
+
33
+ protected load = async () => {
34
+ const { default: Plugin } = await import('bootstrap/js/src/range.js');
35
+ const { bubble = false, formatter = null } = this.props;
36
+
37
+ return (element: HTMLDivElement) => {
38
+ element.addEventListener('changed.bs.range', this.handleValueChange);
39
+
40
+ return Plugin.getOrCreateInstance(element, { bubble, formatter });
41
+ };
42
+ };
43
+
44
+ protected updatePlugin = () => this.instance?.update();
45
+
46
+ protected dispose() {
47
+ this.element.current?.removeEventListener('changed.bs.range', this.handleValueChange);
48
+ super.dispose();
49
+ }
50
+
51
+ private handleValueChange = (event: Event) =>
52
+ this.props.onValueChange?.((event as Event & { value: number }).value);
53
+
54
+ private forwardRef = (element: HTMLElement | null) => {
55
+ const { forwardedRef } = this.props;
38
56
 
39
- input.parentElement?.style.setProperty('--range-fill', value + '');
57
+ if (typeof forwardedRef === 'function') forwardedRef(element);
58
+ else if (forwardedRef) forwardedRef.current = element;
59
+ };
60
+
61
+ private setRootRef = (element: HTMLDivElement | null) => {
62
+ this.element.current = element;
40
63
 
41
- onInput?.(event);
64
+ if (this.props.as) this.forwardRef(element);
42
65
  };
43
66
 
44
- return (
45
- <div
46
- className={classNames('form-range', theme && `theme-${theme}`)}
47
- data-bs-bubble={bubble || undefined}
48
- style={{ '--range-fill': fill } as CSSProperties}
49
- >
50
- <input
51
- {...props}
52
- type="range"
53
- min={min}
54
- max={max}
55
- value={value}
56
- defaultValue={defaultValue}
57
- className={classNames('form-range-input', className)}
58
- style={style}
59
- onInput={handleInput}
60
- />
61
- </div>
62
- );
63
- };
67
+ render() {
68
+ const {
69
+ as: Root = 'div',
70
+ className,
71
+ wrapperClassName,
72
+ min = 0,
73
+ max = 100,
74
+ value,
75
+ defaultValue,
76
+ bubble,
77
+ theme,
78
+ formatter: _formatter,
79
+ onValueChange: _onValueChange,
80
+ forwardedRef: _forwardedRef,
81
+ validation,
82
+ style,
83
+ ...props
84
+ } = this.props;
85
+ const currentValue = Number(value ?? defaultValue ?? (Number(min) + Number(max)) / 2);
86
+ const fill =
87
+ Number(max) <= Number(min)
88
+ ? 0
89
+ : Math.max(
90
+ 0,
91
+ Math.min(1, (currentValue - Number(min)) / (Number(max) - Number(min)))
92
+ );
93
+
94
+ return (
95
+ <Root
96
+ {...(this.props.as ? props : {})}
97
+ ref={this.setRootRef}
98
+ className={classNames(
99
+ 'form-range',
100
+ theme && `theme-${theme}`,
101
+ validation && `is-${validation}`,
102
+ wrapperClassName
103
+ )}
104
+ data-bs-bubble={bubble || undefined}
105
+ style={{ '--bs-range-fill': fill } as CSSProperties}
106
+ >
107
+ <input
108
+ {...(this.props.as ? {} : props)}
109
+ ref={this.props.as ? undefined : this.forwardRef}
110
+ name={props.name}
111
+ type="range"
112
+ min={min}
113
+ max={max}
114
+ value={value}
115
+ defaultValue={defaultValue}
116
+ aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
117
+ className={classNames(
118
+ 'form-range-input',
119
+ validation && `is-${validation}`,
120
+ className
121
+ )}
122
+ style={style}
123
+ />
124
+ </Root>
125
+ );
126
+ }
127
+ }
64
128
 
129
+ export const FormRange = polymorphic<FormRangeProps & { ref?: Ref<HTMLInputElement> }>(
130
+ forwardRef<HTMLInputElement, FormRangeProps & AsProps>((props, forwardedRef) => (
131
+ <FormRangeComponent {...props} forwardedRef={forwardedRef} />
132
+ ))
133
+ );
65
134
  FormRange.displayName = 'FormRange';
@@ -1,35 +1,61 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC, PropsWithChildren } from 'react';
2
+ import { ComponentPropsWithRef, CSSProperties, PropsWithChildren } from 'react';
3
3
 
4
4
  import type { ComponentTheme } from '../Button';
5
+ import { polymorphic } from '../Polymorphic';
6
+ import { FormValidationProps } from './Primitives';
5
7
 
6
8
  export type FormSwitchProps = Omit<ComponentPropsWithRef<'label'>, 'children'> &
7
- PropsWithChildren<{
8
- inputProps?: Omit<ComponentPropsWithRef<'input'>, 'type'>;
9
- size?: 'sm' | 'lg';
10
- theme?: ComponentTheme;
11
- }>;
9
+ PropsWithChildren<
10
+ FormValidationProps & {
11
+ inputProps?: Omit<ComponentPropsWithRef<'input'>, 'type'>;
12
+ size?: 'sm' | 'lg';
13
+ theme?: ComponentTheme;
14
+ width?: string;
15
+ indicatorWidth?: string;
16
+ }
17
+ >;
12
18
 
13
- export const FormSwitch: FC<FormSwitchProps> = ({
14
- className,
15
- inputProps,
16
- size,
17
- theme,
18
- children,
19
- ...props
20
- }) => (
21
- <label
22
- {...props}
23
- className={classNames(
24
- 'switch',
25
- size && `switch-${size}`,
26
- theme && `theme-${theme}`,
27
- className
28
- )}
29
- >
30
- <input {...inputProps} type="checkbox" />
31
- {children}
32
- </label>
19
+ export const FormSwitch = polymorphic<FormSwitchProps>(
20
+ ({
21
+ as: Root = 'label',
22
+ className,
23
+ inputProps,
24
+ size,
25
+ theme,
26
+ validation,
27
+ width,
28
+ indicatorWidth,
29
+ style,
30
+ children,
31
+ ...props
32
+ }) => (
33
+ <Root
34
+ {...props}
35
+ className={classNames(
36
+ 'switch',
37
+ size && `switch-${size}`,
38
+ theme && `theme-${theme}`,
39
+ validation && `is-${validation}`,
40
+ className
41
+ )}
42
+ style={
43
+ {
44
+ '--bs-switch-width': width,
45
+ '--bs-switch-indicator-width': indicatorWidth,
46
+ ...style
47
+ } as CSSProperties
48
+ }
49
+ >
50
+ <input
51
+ role="switch"
52
+ {...inputProps}
53
+ type="checkbox"
54
+ aria-invalid={validation ? validation === 'invalid' : inputProps?.['aria-invalid']}
55
+ className={classNames(validation && `is-${validation}`, inputProps?.className)}
56
+ />
57
+ {children}
58
+ </Root>
59
+ )
33
60
  );
34
-
35
61
  FormSwitch.displayName = 'FormSwitch';
@@ -1,5 +1,6 @@
1
1
  export * from './Adorn';
2
2
  export * from './Checkbox';
3
+ export * from './ChipInput';
3
4
  export * from './Chips';
4
5
  export * from './Combobox';
5
6
  export * from './Control';
package/source/Grid.tsx CHANGED
@@ -1,5 +1,7 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
+
4
+ import { polymorphic } from './Polymorphic';
3
5
 
4
6
  export type Breakpoint = 'sm' | 'md' | 'lg' | 'xl' | '2xl';
5
7
  export type ColSize = number | 'auto';
@@ -10,15 +12,18 @@ export interface ContainerProps extends ComponentPropsWithRef<'div'> {
10
12
  breakpoint?: Breakpoint;
11
13
  }
12
14
 
13
- export const Container: FC<ContainerProps> = ({ fluid, breakpoint, className, ...props }) => (
14
- <div
15
- {...props}
16
- className={classNames(
17
- breakpoint ? `${breakpoint}:container` : fluid ? 'container-fluid' : 'container',
18
- className
19
- )}
20
- />
15
+ export const Container = polymorphic<ContainerProps>(
16
+ ({ as: Root = 'div', fluid, breakpoint, className, ...props }) => (
17
+ <Root
18
+ {...props}
19
+ className={classNames(
20
+ breakpoint ? `${breakpoint}:container` : fluid ? 'container-fluid' : 'container',
21
+ className
22
+ )}
23
+ />
24
+ )
21
25
  );
26
+ Container.displayName = 'Container';
22
27
 
23
28
  export type RowCols = number | 'auto';
24
29
 
@@ -31,29 +36,33 @@ export interface RowProps extends ComponentPropsWithRef<'div'> {
31
36
  '2xl'?: RowCols;
32
37
  }
33
38
 
34
- export const Row: FC<RowProps> = ({ cols, sm, md, lg, xl, '2xl': xxl, className, ...props }) => {
35
- const sizes: [Breakpoint, RowCols | undefined][] = [
36
- ['sm', sm],
37
- ['md', md],
38
- ['lg', lg],
39
- ['xl', xl],
40
- ['2xl', xxl]
41
- ];
39
+ export const Row = polymorphic<RowProps>(
40
+ ({ as: Root = 'div', cols, sm, md, lg, xl, '2xl': xxl, className, ...props }) => {
41
+ const sizes: [Breakpoint, RowCols | undefined][] = [
42
+ ['sm', sm],
43
+ ['md', md],
44
+ ['lg', lg],
45
+ ['xl', xl],
46
+ ['2xl', xxl]
47
+ ];
42
48
 
43
- return (
44
- <div
45
- {...props}
46
- className={classNames(
47
- 'row',
48
- cols !== undefined && `row-cols-${cols}`,
49
- sizes.map(
50
- ([breakpoint, size]) => size !== undefined && `${breakpoint}:row-cols-${size}`
51
- ),
52
- className
53
- )}
54
- />
55
- );
56
- };
49
+ return (
50
+ <Root
51
+ {...props}
52
+ className={classNames(
53
+ 'row',
54
+ cols !== undefined && `row-cols-${cols}`,
55
+ sizes.map(
56
+ ([breakpoint, size]) =>
57
+ size !== undefined && `${breakpoint}:row-cols-${size}`
58
+ ),
59
+ className
60
+ )}
61
+ />
62
+ );
63
+ }
64
+ );
65
+ Row.displayName = 'Row';
57
66
 
58
67
  export interface ColProps extends ComponentPropsWithRef<'div'> {
59
68
  span?: ColSize;
@@ -64,29 +73,32 @@ export interface ColProps extends ComponentPropsWithRef<'div'> {
64
73
  '2xl'?: BreakpointColSize;
65
74
  }
66
75
 
67
- export const Col: FC<ColProps> = ({ span, sm, md, lg, xl, '2xl': xxl, className, ...props }) => {
68
- const sizes: [Breakpoint, BreakpointColSize | undefined][] = [
69
- ['sm', sm],
70
- ['md', md],
71
- ['lg', lg],
72
- ['xl', xl],
73
- ['2xl', xxl]
74
- ];
75
- const sized = sizes.some(([, size]) => size !== undefined);
76
+ export const Col = polymorphic<ColProps>(
77
+ ({ as: Root = 'div', span, sm, md, lg, xl, '2xl': xxl, className, ...props }) => {
78
+ const sizes: [Breakpoint, BreakpointColSize | undefined][] = [
79
+ ['sm', sm],
80
+ ['md', md],
81
+ ['lg', lg],
82
+ ['xl', xl],
83
+ ['2xl', xxl]
84
+ ];
85
+ const sized = sizes.some(([, size]) => size !== undefined);
76
86
 
77
- return (
78
- <div
79
- {...props}
80
- className={classNames(
81
- !span && !sized && 'col',
82
- span !== undefined && `col-${span}`,
83
- sizes.map(
84
- ([breakpoint, size]) =>
85
- size !== undefined &&
86
- (size === true ? `${breakpoint}:col` : `${breakpoint}:col-${size}`)
87
- ),
88
- className
89
- )}
90
- />
91
- );
92
- };
87
+ return (
88
+ <Root
89
+ {...props}
90
+ className={classNames(
91
+ !span && !sized && 'col',
92
+ span !== undefined && `col-${span}`,
93
+ sizes.map(
94
+ ([breakpoint, size]) =>
95
+ size !== undefined &&
96
+ (size === true ? `${breakpoint}:col` : `${breakpoint}:col-${size}`)
97
+ ),
98
+ className
99
+ )}
100
+ />
101
+ );
102
+ }
103
+ );
104
+ Col.displayName = 'Col';
package/source/Image.tsx CHANGED
@@ -1,15 +1,19 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
+
4
+ import { polymorphic } from './Polymorphic';
3
5
 
4
6
  export interface ImageProps extends ComponentPropsWithRef<'img'> {
5
7
  fluid?: boolean;
6
8
  thumbnail?: boolean;
7
9
  }
8
10
 
9
- export const Image: FC<ImageProps> = ({ fluid, thumbnail, className, ...props }) => (
10
- <img
11
- {...props}
12
- className={classNames({ 'img-fluid': fluid, 'img-thumbnail': thumbnail }, className)}
13
- />
11
+ export const Image = polymorphic<ImageProps>(
12
+ ({ as: Root = 'img', fluid, thumbnail, className, ...props }) => (
13
+ <Root
14
+ {...props}
15
+ className={classNames({ 'img-fluid': fluid, 'img-thumbnail': thumbnail }, className)}
16
+ />
17
+ )
14
18
  );
15
19
  Image.displayName = 'Image';
@@ -1,27 +1,28 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
3
 
4
4
  import type { ComponentTheme } from './Button';
5
5
  import type { Breakpoint } from './Grid';
6
+ import { polymorphic } from './Polymorphic';
6
7
 
7
8
  export type ListGroupHorizontal = boolean | Breakpoint;
8
9
 
9
- export interface ListGroupProps extends ComponentPropsWithRef<'ul'> {
10
+ interface ListGroupStyleProps {
10
11
  flush?: boolean;
11
12
  numbered?: boolean;
12
13
  horizontal?: ListGroupHorizontal;
13
14
  }
14
15
 
15
- export const ListGroup: FC<ListGroupProps> = ({
16
- flush,
17
- numbered,
18
- horizontal,
19
- className,
20
- ...props
21
- }) => (
22
- <ul
23
- {...props}
24
- className={classNames(
16
+ export type ListGroupProps = ListGroupStyleProps &
17
+ (
18
+ | (ComponentPropsWithRef<'ul'> & { as?: 'ul' })
19
+ | (ComponentPropsWithRef<'ol'> & { as: 'ol' })
20
+ | (ComponentPropsWithRef<'div'> & { as: 'div' })
21
+ );
22
+
23
+ export const ListGroup = polymorphic<ListGroupStyleProps & ComponentPropsWithRef<'ul'>>(
24
+ ({ as: Root = 'ul', flush, numbered, horizontal, className, ...props }) => {
25
+ const classes = classNames(
25
26
  'list-group',
26
27
  { 'list-group-flush': flush, 'list-group-numbered': numbered },
27
28
  horizontal &&
@@ -29,9 +30,12 @@ export const ListGroup: FC<ListGroupProps> = ({
29
30
  ? 'list-group-horizontal'
30
31
  : `${horizontal}:list-group-horizontal`),
31
32
  className
32
- )}
33
- />
33
+ );
34
+
35
+ return <Root {...props} className={classes} />;
36
+ }
34
37
  );
38
+ ListGroup.displayName = 'ListGroup';
35
39
 
36
40
  interface ListGroupItemStyleProps {
37
41
  theme?: ComponentTheme;
@@ -54,50 +58,47 @@ const isLink = (props: ListGroupItemElementAttributes): props is ListGroupItemLi
54
58
  const isButton = (props: ListGroupItemElementAttributes): props is ListGroupItemButtonAttributes =>
55
59
  typeof props.type === 'string';
56
60
 
57
- export const ListGroupItem: FC<ListGroupItemProps> = ({
58
- theme,
59
- action,
60
- active,
61
- disabled,
62
- className,
63
- ...props
64
- }) => {
65
- const classes = classNames(
66
- 'list-group-item',
67
- theme && `theme-${theme}`,
68
- action && 'list-group-item-action',
69
- { active, disabled },
70
- disabled && 'pe-none',
71
- className
72
- );
61
+ export const ListGroupItem = polymorphic<ListGroupItemProps>(
62
+ ({ as: RootAs, theme, action, active, disabled, className, ...props }) => {
63
+ const Root = RootAs ?? 'a';
64
+ const Root1 = RootAs ?? 'button';
65
+ const Root2 = RootAs ?? 'li';
73
66
 
74
- return isLink(props) ? (
75
- <a
76
- {...props}
77
- className={classes}
78
- href={disabled ? undefined : props.href}
79
- aria-current={active ? 'true' : undefined}
80
- aria-disabled={disabled || undefined}
81
- tabIndex={disabled ? -1 : props.tabIndex}
82
- onClick={disabled ? undefined : props.onClick}
83
- />
84
- ) : isButton(props) ? (
85
- <button
86
- {...props}
87
- type={props.type || 'button'}
88
- className={classes}
89
- aria-current={active ? 'true' : undefined}
90
- disabled={disabled}
91
- />
92
- ) : (
93
- <li
94
- {...(props as ListGroupItemAttributes)}
95
- className={classes}
96
- aria-current={active ? 'true' : undefined}
97
- aria-disabled={disabled || undefined}
98
- />
99
- );
100
- };
67
+ const classes = classNames(
68
+ 'list-group-item',
69
+ theme && `theme-${theme}`,
70
+ action && 'list-group-item-action',
71
+ { active, disabled },
72
+ disabled && 'pe-none',
73
+ className
74
+ );
101
75
 
102
- ListGroup.displayName = 'ListGroup';
76
+ return isLink(props) ? (
77
+ <Root
78
+ {...props}
79
+ className={classes}
80
+ href={disabled ? undefined : props.href}
81
+ aria-current={active ? 'true' : undefined}
82
+ aria-disabled={disabled || undefined}
83
+ tabIndex={disabled ? -1 : props.tabIndex}
84
+ onClick={disabled ? undefined : props.onClick}
85
+ />
86
+ ) : isButton(props) ? (
87
+ <Root1
88
+ {...props}
89
+ type={props.type || 'button'}
90
+ className={classes}
91
+ aria-current={active ? 'true' : undefined}
92
+ disabled={disabled}
93
+ />
94
+ ) : (
95
+ <Root2
96
+ {...(props as ListGroupItemAttributes)}
97
+ className={classes}
98
+ aria-current={active ? 'true' : undefined}
99
+ aria-disabled={disabled || undefined}
100
+ />
101
+ );
102
+ }
103
+ );
103
104
  ListGroupItem.displayName = 'ListGroupItem';