idea-react 3.0.0-beta.1 → 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 (87) hide show
  1. package/AGENTS.md +9 -1
  2. package/ReadMe.md +34 -6
  3. package/dist/index.d.mts +270 -186
  4. package/dist/index.d.ts +270 -186
  5. package/dist/index.js +16 -16
  6. package/dist/index.mjs +15 -15
  7. package/dist/style.css +0 -3
  8. package/package.json +1 -1
  9. package/source/Accordion.tsx +61 -37
  10. package/source/Alert.tsx +30 -15
  11. package/source/Avatar.tsx +55 -50
  12. package/source/Badge.tsx +16 -16
  13. package/source/Breadcrumb.tsx +36 -22
  14. package/source/Button.tsx +63 -69
  15. package/source/ButtonGroup.tsx +21 -25
  16. package/source/Card.tsx +67 -59
  17. package/source/Carousel/index.tsx +22 -21
  18. package/source/CloseButton.tsx +20 -15
  19. package/source/Collapse.tsx +74 -19
  20. package/source/Drawer.tsx +40 -17
  21. package/source/Extension/ClickBoundary.tsx +24 -5
  22. package/source/Extension/CodeBlock.tsx +64 -0
  23. package/source/Extension/Countdown.tsx +21 -9
  24. package/source/Extension/Editor.tsx +53 -18
  25. package/source/Extension/EditorHTML.tsx +7 -3
  26. package/source/Extension/HorizontalMarquee/index.tsx +69 -45
  27. package/source/Extension/Icon.tsx +15 -14
  28. package/source/Extension/Loading.tsx +20 -17
  29. package/source/Extension/MonthCalendar.tsx +26 -10
  30. package/source/Extension/OverlayBox.tsx +28 -15
  31. package/source/Extension/PageNav.tsx +16 -7
  32. package/source/Extension/ScrollNav/Scroller.tsx +21 -5
  33. package/source/Extension/ScrollNav/index.tsx +32 -7
  34. package/source/Extension/Select.tsx +12 -11
  35. package/source/Extension/SpinnerButton.tsx +19 -24
  36. package/source/Extension/TableSpinner.tsx +10 -14
  37. package/source/Extension/TextTruncate/index.tsx +17 -13
  38. package/source/Extension/Time.tsx +7 -5
  39. package/source/Extension/TimeDistance.tsx +22 -18
  40. package/source/Extension/Timeline/index.tsx +37 -34
  41. package/source/Extension/TypeEcho/Content.tsx +79 -14
  42. package/source/Extension/TypeEcho/index.tsx +13 -5
  43. package/source/Extension/UserRank/Address.tsx +17 -15
  44. package/source/Extension/UserRank/index.tsx +79 -84
  45. package/source/Extension/VerticalMarquee/index.tsx +14 -16
  46. package/source/Extension/ZodiacBar.tsx +40 -32
  47. package/source/Form/Adorn.tsx +30 -32
  48. package/source/Form/Checkbox.tsx +9 -5
  49. package/source/Form/ChipInput.tsx +9 -5
  50. package/source/Form/Chips.tsx +41 -39
  51. package/source/Form/Combobox.tsx +80 -61
  52. package/source/Form/Control.tsx +53 -58
  53. package/source/Form/DatePicker.tsx +74 -65
  54. package/source/Form/Feedback.tsx +18 -19
  55. package/source/Form/FloatingLabel.tsx +16 -19
  56. package/source/Form/InputGroup.tsx +27 -26
  57. package/source/Form/OTP.tsx +30 -12
  58. package/source/Form/Primitives.tsx +47 -39
  59. package/source/Form/Radio.tsx +25 -22
  60. package/source/Form/Range.tsx +30 -12
  61. package/source/Form/Switch.tsx +43 -40
  62. package/source/Grid.tsx +67 -59
  63. package/source/Image.tsx +10 -6
  64. package/source/ListGroup.tsx +57 -69
  65. package/source/Menu.tsx +128 -66
  66. package/source/NativeDialog/index.tsx +51 -29
  67. package/source/Nav.tsx +46 -46
  68. package/source/NavOverflow/index.tsx +26 -10
  69. package/source/Navbar.tsx +60 -55
  70. package/source/Pagination.tsx +63 -69
  71. package/source/Placeholder.tsx +47 -43
  72. package/source/Polymorphic.ts +57 -0
  73. package/source/Popover.tsx +34 -31
  74. package/source/Progress/index.tsx +59 -54
  75. package/source/ScrollSpy/index.tsx +48 -33
  76. package/source/Spinner.tsx +27 -23
  77. package/source/Stack.tsx +25 -0
  78. package/source/Stepper.tsx +46 -40
  79. package/source/Tab.tsx +40 -27
  80. package/source/Table.tsx +53 -42
  81. package/source/Toast.tsx +62 -47
  82. package/source/ToggleButton.tsx +40 -37
  83. package/source/Toggler.tsx +17 -8
  84. package/source/Tooltip.tsx +31 -29
  85. package/source/index.ts +2 -0
  86. package/source/Extension/CodeBlock/index.tsx +0 -58
  87. package/source/Extension/CodeBlock/style.module.css +0 -3
@@ -1,6 +1,7 @@
1
1
  import classNames from 'classnames';
2
- import { Children, ComponentPropsWithRef, FC, isValidElement, ReactNode } from 'react';
2
+ import { Children, ComponentPropsWithRef, isValidElement, ReactNode } from 'react';
3
3
 
4
+ import { polymorphic } from '../Polymorphic';
4
5
  import { FormLabel } from './Primitives';
5
6
 
6
7
  export type FormFloatingLabelProps = ComponentPropsWithRef<'div'> & {
@@ -10,23 +11,19 @@ export type FormFloatingLabelProps = ComponentPropsWithRef<'div'> & {
10
11
  const isLabel = (child: ReactNode) =>
11
12
  isValidElement(child) && (child.type === 'label' || child.type === FormLabel);
12
13
 
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
- };
14
+ export const FormFloatingLabel = polymorphic<FormFloatingLabelProps>(
15
+ ({ as: Root = 'div', className, always, children, ...props }) => {
16
+ const items = Children.toArray(children);
31
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
+ );
32
29
  FormFloatingLabel.displayName = 'FormFloatingLabel';
@@ -5,6 +5,7 @@ import { observer } from 'mobx-react';
5
5
  import {
6
6
  cloneElement,
7
7
  ComponentPropsWithRef,
8
+ createElement,
8
9
  FC,
9
10
  FocusEvent,
10
11
  KeyboardEventHandler,
@@ -13,57 +14,57 @@ import {
13
14
 
14
15
  import { Button, ButtonProps } from '../Button';
15
16
  import { Menu, MenuProps } from '../Menu';
17
+ import { polymorphic } from '../Polymorphic';
16
18
 
17
19
  export type InputGroupProps = ComponentPropsWithRef<'div'> & {
18
20
  size?: 'sm' | 'lg';
19
21
  };
20
22
 
21
- export const InputGroup: FC<InputGroupProps> = ({ className, size, ...props }) => (
22
- <div
23
- {...props}
24
- className={classNames('input-group', size && `input-group-${size}`, className)}
25
- />
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
+ )
26
30
  );
27
-
28
31
  InputGroup.displayName = 'InputGroup';
29
32
 
30
33
  export type InputGroupTextProps =
31
34
  | (ComponentPropsWithRef<'span'> & { htmlFor?: never })
32
35
  | (ComponentPropsWithRef<'label'> & { htmlFor: string });
33
36
 
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
- };
37
+ export const InputGroupText = polymorphic<InputGroupTextProps>(
38
+ ({ as: RootAs, className, ...props }) => {
39
+ const Root = RootAs ?? (typeof props.htmlFor === 'string' ? 'label' : 'span');
48
40
 
41
+ return <Root {...props} className={classNames('input-group-text', className)} />;
42
+ }
43
+ );
49
44
  InputGroupText.displayName = 'InputGroupText';
50
45
 
51
46
  export type InputGroupButtonProps = ButtonProps;
52
47
 
53
- export const InputGroupButton: FC<InputGroupButtonProps> = ({ className, ...props }) => (
54
- <Button {...props} className={classNames('input-group-btn', className)} />
48
+ export const InputGroupButton = polymorphic<InputGroupButtonProps>(
49
+ ({ as: RootAs, className, ...props }) => (
50
+ <Button as={RootAs} {...props} className={classNames('input-group-btn', className)} />
51
+ )
55
52
  );
56
-
57
53
  InputGroupButton.displayName = 'InputGroupButton';
58
54
 
59
55
  export interface InputGroupIgnoreProps {
60
56
  children: ReactElement<{ className?: string }>;
61
57
  }
62
58
 
63
- export const InputGroupIgnore: FC<InputGroupIgnoreProps> = ({ children }) =>
64
- cloneElement(children, {
59
+ export const InputGroupIgnore = polymorphic<InputGroupIgnoreProps>(({ as, children, ...props }) => {
60
+ const attributes = {
61
+ ...children.props,
62
+ ...props,
65
63
  className: classNames('input-group-ignore', children.props.className)
66
- });
64
+ };
65
+
66
+ return as ? createElement(as, attributes) : cloneElement(children, attributes);
67
+ });
67
68
 
68
69
  InputGroupIgnore.displayName = 'InputGroupIgnore';
69
70
 
@@ -4,6 +4,7 @@ import type OtpInput from 'bootstrap/js/src/otp-input.js';
4
4
  import classNames from 'classnames';
5
5
  import { ComponentPropsWithoutRef, forwardRef, Ref } from 'react';
6
6
 
7
+ import { AsProps, polymorphic } from '../Polymorphic';
7
8
  import { FormPlugin } from './Plugin';
8
9
  import { FormValidationProps } from './Primitives';
9
10
 
@@ -21,8 +22,8 @@ export type OTPInputProps = Omit<ComponentPropsWithoutRef<'input'>, 'type' | 'ma
21
22
  onValueChange?: (value: string) => unknown;
22
23
  };
23
24
 
24
- interface OTPInputComponentProps extends OTPInputProps {
25
- forwardedRef?: Ref<HTMLInputElement>;
25
+ interface OTPInputComponentProps extends OTPInputProps, AsProps {
26
+ forwardedRef?: Ref<HTMLElement>;
26
27
  }
27
28
 
28
29
  class OTPInputComponent extends FormPlugin<OTPInputComponentProps, OtpInput> {
@@ -76,8 +77,22 @@ class OTPInputComponent extends FormPlugin<OTPInputComponentProps, OtpInput> {
76
77
  private handleValueChange = (event: Event) =>
77
78
  this.props.onValueChange?.((event as Event & { value: string }).value);
78
79
 
80
+ private forwardRef = (element: HTMLElement | null) => {
81
+ const { forwardedRef } = this.props;
82
+
83
+ if (typeof forwardedRef === 'function') forwardedRef(element);
84
+ else if (forwardedRef) forwardedRef.current = element;
85
+ };
86
+
87
+ private setRootRef = (element: HTMLDivElement | null) => {
88
+ this.element.current = element;
89
+
90
+ if (this.props.as) this.forwardRef(element);
91
+ };
92
+
79
93
  render() {
80
94
  const {
95
+ as: Root = 'div',
81
96
  className,
82
97
  wrapperClassName,
83
98
  length,
@@ -90,14 +105,15 @@ class OTPInputComponent extends FormPlugin<OTPInputComponentProps, OtpInput> {
90
105
  validation,
91
106
  onComplete: _onComplete,
92
107
  onValueChange: _onValueChange,
93
- forwardedRef,
108
+ forwardedRef: _forwardedRef,
94
109
  autoComplete = 'one-time-code',
95
110
  ...props
96
111
  } = this.props;
97
112
 
98
113
  return (
99
- <div
100
- ref={this.element}
114
+ <Root
115
+ {...(this.props.as ? props : {})}
116
+ ref={this.setRootRef}
101
117
  className={classNames(
102
118
  'otp min-w-0 max-w-100',
103
119
  connected && 'otp-connected',
@@ -113,8 +129,9 @@ class OTPInputComponent extends FormPlugin<OTPInputComponentProps, OtpInput> {
113
129
  data-bs-separator={separator}
114
130
  >
115
131
  <input
116
- {...props}
117
- ref={forwardedRef}
132
+ {...(this.props.as ? {} : props)}
133
+ ref={this.props.as ? undefined : this.forwardRef}
134
+ name={props.name}
118
135
  type="text"
119
136
  maxLength={length}
120
137
  autoComplete={autoComplete}
@@ -129,13 +146,14 @@ class OTPInputComponent extends FormPlugin<OTPInputComponentProps, OtpInput> {
129
146
  }
130
147
  className={classNames('otp-input', className)}
131
148
  />
132
- </div>
149
+ </Root>
133
150
  );
134
151
  }
135
152
  }
136
153
 
137
- export const OTPInput = forwardRef<HTMLInputElement, OTPInputProps>((props, forwardedRef) => (
138
- <OTPInputComponent {...props} forwardedRef={forwardedRef} />
139
- ));
140
-
154
+ export const OTPInput = polymorphic<OTPInputProps & { ref?: Ref<HTMLInputElement> }>(
155
+ forwardRef<HTMLInputElement, OTPInputProps & AsProps>((props, forwardedRef) => (
156
+ <OTPInputComponent {...props} forwardedRef={forwardedRef} />
157
+ ))
158
+ );
141
159
  OTPInput.displayName = 'OTPInput';
@@ -1,19 +1,24 @@
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 FormProps = ComponentPropsWithRef<'form'> & {
5
7
  validated?: boolean;
6
8
  };
7
9
 
8
- export const Form: FC<FormProps> = ({ className, validated, ...props }) => (
9
- <form {...props} className={classNames(validated && 'was-validated', className)} />
10
+ export const Form = polymorphic<FormProps>(
11
+ ({ as: Root = 'form', className, validated, ...props }) => (
12
+ <Root {...props} className={classNames(validated && 'was-validated', className)} />
13
+ )
10
14
  );
11
-
12
15
  Form.displayName = 'Form';
13
16
 
14
17
  export type FormFieldsetProps = ComponentPropsWithRef<'fieldset'>;
15
18
 
16
- export const FormFieldset: FC<FormFieldsetProps> = props => <fieldset {...props} />;
19
+ export const FormFieldset = polymorphic<FormFieldsetProps>(
20
+ ({ as: Root = 'fieldset', ...props }) => <Root {...props} />
21
+ );
17
22
 
18
23
  FormFieldset.displayName = 'FormFieldset';
19
24
 
@@ -21,26 +26,32 @@ export type FormFieldProps = ComponentPropsWithRef<'div'> & {
21
26
  card?: boolean;
22
27
  };
23
28
 
24
- export const FormField: FC<FormFieldProps> = ({ className, card, ...props }) => (
25
- <div {...props} className={classNames('form-field', card && 'form-field-card', className)} />
29
+ export const FormField = polymorphic<FormFieldProps>(
30
+ ({ as: Root = 'div', className, card, ...props }) => (
31
+ <Root
32
+ {...props}
33
+ className={classNames('form-field', card && 'form-field-card', className)}
34
+ />
35
+ )
26
36
  );
27
-
28
37
  FormField.displayName = 'FormField';
29
38
 
30
39
  export type FormGroupProps = ComponentPropsWithRef<'div'>;
31
40
 
32
- export const FormGroup: FC<FormGroupProps> = ({ className, ...props }) => (
33
- <div {...props} className={classNames('form-group', className)} />
41
+ export const FormGroup = polymorphic<FormGroupProps>(
42
+ ({ as: Root = 'div', className, ...props }) => (
43
+ <Root {...props} className={classNames('form-group', className)} />
44
+ )
34
45
  );
35
-
36
46
  FormGroup.displayName = 'FormGroup';
37
47
 
38
48
  export type FormFieldContentProps = ComponentPropsWithRef<'div'>;
39
49
 
40
- export const FormFieldContent: FC<FormFieldContentProps> = ({ className, ...props }) => (
41
- <div {...props} className={classNames('form-field-content', className)} />
50
+ export const FormFieldContent = polymorphic<FormFieldContentProps>(
51
+ ({ as: Root = 'div', className, ...props }) => (
52
+ <Root {...props} className={classNames('form-field-content', className)} />
53
+ )
42
54
  );
43
-
44
55
  FormFieldContent.displayName = 'FormFieldContent';
45
56
 
46
57
  export interface FormValidationProps {
@@ -53,36 +64,33 @@ export type FormLabelProps = ComponentPropsWithRef<'label'> & {
53
64
  controlSize?: 'sm' | 'lg';
54
65
  };
55
66
 
56
- export const FormLabel: FC<FormLabelProps> = ({
57
- className,
58
- plain,
59
- column,
60
- controlSize,
61
- ...props
62
- }) => (
63
- <label
64
- {...props}
65
- className={classNames(
66
- column ? 'col-form-label' : !plain && 'form-label',
67
- column && controlSize && `col-form-label-${controlSize}`,
68
- className
69
- )}
70
- />
67
+ export const FormLabel = polymorphic<FormLabelProps>(
68
+ ({ as: Root = 'label', className, plain, column, controlSize, ...props }) => (
69
+ <Root
70
+ {...props}
71
+ className={classNames(
72
+ column ? 'col-form-label' : !plain && 'form-label',
73
+ column && controlSize && `col-form-label-${controlSize}`,
74
+ className
75
+ )}
76
+ />
77
+ )
71
78
  );
72
-
73
79
  FormLabel.displayName = 'FormLabel';
74
80
 
75
81
  export type FormLegendProps = ComponentPropsWithRef<'legend'> &
76
82
  Pick<FormLabelProps, 'column' | 'controlSize'>;
77
83
 
78
- export const FormLegend: FC<FormLegendProps> = ({ className, column, controlSize, ...props }) => (
79
- <legend
80
- {...props}
81
- className={classNames(
82
- column && 'col-form-label',
83
- column && controlSize && `col-form-label-${controlSize}`,
84
- className
85
- )}
86
- />
84
+ export const FormLegend = polymorphic<FormLegendProps>(
85
+ ({ as: Root = 'legend', className, column, controlSize, ...props }) => (
86
+ <Root
87
+ {...props}
88
+ className={classNames(
89
+ column && 'col-form-label',
90
+ column && controlSize && `col-form-label-${controlSize}`,
91
+ className
92
+ )}
93
+ />
94
+ )
87
95
  );
88
96
  FormLegend.displayName = 'FormLegend';
@@ -1,7 +1,8 @@
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
+ import { polymorphic } from '../Polymorphic';
5
6
  import { Checkbox, CheckboxProps } from './Checkbox';
6
7
  import { FormValidationProps } from './Primitives';
7
8
 
@@ -11,21 +12,22 @@ export type RadioProps = Omit<ComponentPropsWithRef<'input'>, 'type' | 'size'> &
11
12
  theme?: ComponentTheme;
12
13
  };
13
14
 
14
- export const Radio: FC<RadioProps> = ({ className, size, theme, validation, ...props }) => (
15
- <input
16
- {...props}
17
- type="radio"
18
- aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
19
- className={classNames(
20
- 'radio',
21
- size && `radio-${size}`,
22
- theme && `theme-${theme}`,
23
- validation && `is-${validation}`,
24
- className
25
- )}
26
- />
15
+ export const Radio = polymorphic<RadioProps>(
16
+ ({ as: Root = 'input', className, size, theme, validation, ...props }) => (
17
+ <Root
18
+ {...props}
19
+ type="radio"
20
+ aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
21
+ className={classNames(
22
+ 'radio',
23
+ size && `radio-${size}`,
24
+ theme && `theme-${theme}`,
25
+ validation && `is-${validation}`,
26
+ className
27
+ )}
28
+ />
29
+ )
27
30
  );
28
-
29
31
  Radio.displayName = 'Radio';
30
32
 
31
33
  export type FormCheckProps = CheckboxProps &
@@ -33,11 +35,12 @@ export type FormCheckProps = CheckboxProps &
33
35
  type?: 'checkbox' | 'radio';
34
36
  };
35
37
 
36
- export const FormCheck: FC<FormCheckProps> = ({ type = 'checkbox', indeterminate, ...props }) =>
37
- type === 'checkbox' ? (
38
- <Checkbox {...props} indeterminate={indeterminate} />
39
- ) : (
40
- <Radio {...props} />
41
- );
42
-
38
+ export const FormCheck = polymorphic<FormCheckProps>(
39
+ ({ as: RootAs, type = 'checkbox', indeterminate, ...props }) =>
40
+ type === 'checkbox' ? (
41
+ <Checkbox as={RootAs} {...props} indeterminate={indeterminate} />
42
+ ) : (
43
+ <Radio as={RootAs} {...props} />
44
+ )
45
+ );
43
46
  FormCheck.displayName = 'FormCheck';
@@ -5,6 +5,7 @@ import classNames from 'classnames';
5
5
  import { ComponentPropsWithoutRef, CSSProperties, forwardRef, Ref } from 'react';
6
6
 
7
7
  import { ComponentTheme } from '../Button';
8
+ import { AsProps, polymorphic } from '../Polymorphic';
8
9
  import { FormPlugin } from './Plugin';
9
10
  import { FormValidationProps } from './Primitives';
10
11
 
@@ -17,8 +18,8 @@ export type FormRangeProps = Omit<ComponentPropsWithoutRef<'input'>, 'type'> &
17
18
  onValueChange?: (value: number) => unknown;
18
19
  };
19
20
 
20
- interface FormRangeComponentProps extends FormRangeProps {
21
- forwardedRef?: Ref<HTMLInputElement>;
21
+ interface FormRangeComponentProps extends FormRangeProps, AsProps {
22
+ forwardedRef?: Ref<HTMLElement>;
22
23
  }
23
24
 
24
25
  class FormRangeComponent extends FormPlugin<FormRangeComponentProps, Range> {
@@ -50,8 +51,22 @@ class FormRangeComponent extends FormPlugin<FormRangeComponentProps, Range> {
50
51
  private handleValueChange = (event: Event) =>
51
52
  this.props.onValueChange?.((event as Event & { value: number }).value);
52
53
 
54
+ private forwardRef = (element: HTMLElement | null) => {
55
+ const { forwardedRef } = this.props;
56
+
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;
63
+
64
+ if (this.props.as) this.forwardRef(element);
65
+ };
66
+
53
67
  render() {
54
68
  const {
69
+ as: Root = 'div',
55
70
  className,
56
71
  wrapperClassName,
57
72
  min = 0,
@@ -62,7 +77,7 @@ class FormRangeComponent extends FormPlugin<FormRangeComponentProps, Range> {
62
77
  theme,
63
78
  formatter: _formatter,
64
79
  onValueChange: _onValueChange,
65
- forwardedRef,
80
+ forwardedRef: _forwardedRef,
66
81
  validation,
67
82
  style,
68
83
  ...props
@@ -77,8 +92,9 @@ class FormRangeComponent extends FormPlugin<FormRangeComponentProps, Range> {
77
92
  );
78
93
 
79
94
  return (
80
- <div
81
- ref={this.element}
95
+ <Root
96
+ {...(this.props.as ? props : {})}
97
+ ref={this.setRootRef}
82
98
  className={classNames(
83
99
  'form-range',
84
100
  theme && `theme-${theme}`,
@@ -89,8 +105,9 @@ class FormRangeComponent extends FormPlugin<FormRangeComponentProps, Range> {
89
105
  style={{ '--bs-range-fill': fill } as CSSProperties}
90
106
  >
91
107
  <input
92
- {...props}
93
- ref={forwardedRef}
108
+ {...(this.props.as ? {} : props)}
109
+ ref={this.props.as ? undefined : this.forwardRef}
110
+ name={props.name}
94
111
  type="range"
95
112
  min={min}
96
113
  max={max}
@@ -104,13 +121,14 @@ class FormRangeComponent extends FormPlugin<FormRangeComponentProps, Range> {
104
121
  )}
105
122
  style={style}
106
123
  />
107
- </div>
124
+ </Root>
108
125
  );
109
126
  }
110
127
  }
111
128
 
112
- export const FormRange = forwardRef<HTMLInputElement, FormRangeProps>((props, forwardedRef) => (
113
- <FormRangeComponent {...props} forwardedRef={forwardedRef} />
114
- ));
115
-
129
+ export const FormRange = polymorphic<FormRangeProps & { ref?: Ref<HTMLInputElement> }>(
130
+ forwardRef<HTMLInputElement, FormRangeProps & AsProps>((props, forwardedRef) => (
131
+ <FormRangeComponent {...props} forwardedRef={forwardedRef} />
132
+ ))
133
+ );
116
134
  FormRange.displayName = 'FormRange';
@@ -1,7 +1,8 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, CSSProperties, 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';
5
6
  import { FormValidationProps } from './Primitives';
6
7
 
7
8
  export type FormSwitchProps = Omit<ComponentPropsWithRef<'label'>, 'children'> &
@@ -15,44 +16,46 @@ export type FormSwitchProps = Omit<ComponentPropsWithRef<'label'>, 'children'> &
15
16
  }
16
17
  >;
17
18
 
18
- export const FormSwitch: FC<FormSwitchProps> = ({
19
- className,
20
- inputProps,
21
- size,
22
- theme,
23
- validation,
24
- width,
25
- indicatorWidth,
26
- style,
27
- children,
28
- ...props
29
- }) => (
30
- <label
31
- {...props}
32
- className={classNames(
33
- 'switch',
34
- size && `switch-${size}`,
35
- theme && `theme-${theme}`,
36
- validation && `is-${validation}`,
37
- className
38
- )}
39
- style={
40
- {
41
- '--bs-switch-width': width,
42
- '--bs-switch-indicator-width': indicatorWidth,
43
- ...style
44
- } as CSSProperties
45
- }
46
- >
47
- <input
48
- role="switch"
49
- {...inputProps}
50
- type="checkbox"
51
- aria-invalid={validation ? validation === 'invalid' : inputProps?.['aria-invalid']}
52
- className={classNames(validation && `is-${validation}`, inputProps?.className)}
53
- />
54
- {children}
55
- </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
+ )
56
60
  );
57
-
58
61
  FormSwitch.displayName = 'FormSwitch';