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,48 +1,159 @@
1
+ 'use client';
2
+
3
+ import type OtpInput from 'bootstrap/js/src/otp-input.js';
1
4
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
3
-
4
- export type OTPInputProps = Omit<ComponentPropsWithRef<'input'>, 'type' | 'maxLength'> & {
5
- length: number;
6
- type?: 'numeric' | 'alphanumeric' | 'alpha';
7
- mask?: boolean;
8
- groups?: number[];
9
- separator?: string;
10
- connected?: boolean;
11
- size?: 'sm' | 'lg';
12
- };
13
-
14
- export const OTPInput: FC<OTPInputProps> = ({
15
- className,
16
- length,
17
- type = 'numeric',
18
- mask,
19
- groups,
20
- separator,
21
- connected,
22
- size,
23
- ...props
24
- }) => (
25
- <div
26
- className={classNames('otp', connected && 'otp-connected', size && `otp-${size}`)}
27
- data-bs-otp=""
28
- data-bs-length={length}
29
- data-bs-type={type}
30
- data-bs-mask={mask || undefined}
31
- data-bs-groups={groups ? JSON.stringify(groups) : undefined}
32
- data-bs-separator={separator}
33
- >
34
- <input
35
- {...props}
36
- type="text"
37
- maxLength={length}
38
- autoComplete={props.autoComplete || 'one-time-code'}
39
- inputMode={type === 'numeric' ? 'numeric' : 'text'}
40
- pattern={
41
- type === 'numeric' ? '[0-9]*' : type === 'alpha' ? '[a-zA-Z]*' : '[a-zA-Z0-9]*'
42
- }
43
- className={classNames('otp-input', className)}
44
- />
45
- </div>
46
- );
5
+ import { ComponentPropsWithoutRef, forwardRef, Ref } from 'react';
6
+
7
+ import { AsProps, polymorphic } from '../Polymorphic';
8
+ import { FormPlugin } from './Plugin';
9
+ import { FormValidationProps } from './Primitives';
10
+
11
+ export type OTPInputProps = Omit<ComponentPropsWithoutRef<'input'>, 'type' | 'maxLength' | 'size'> &
12
+ FormValidationProps & {
13
+ length: number;
14
+ type?: 'numeric' | 'alphanumeric' | 'alpha';
15
+ mask?: boolean;
16
+ groups?: number[];
17
+ separator?: string;
18
+ connected?: boolean;
19
+ size?: 'sm' | 'lg';
20
+ wrapperClassName?: string;
21
+ onComplete?: (value: string) => unknown;
22
+ onValueChange?: (value: string) => unknown;
23
+ };
24
+
25
+ interface OTPInputComponentProps extends OTPInputProps, AsProps {
26
+ forwardedRef?: Ref<HTMLElement>;
27
+ }
28
+
29
+ class OTPInputComponent extends FormPlugin<OTPInputComponentProps, OtpInput> {
30
+ protected configurationChanged = (previous: OTPInputComponentProps) =>
31
+ previous.length !== this.props.length ||
32
+ previous.type !== this.props.type ||
33
+ previous.mask !== this.props.mask ||
34
+ previous.separator !== this.props.separator ||
35
+ JSON.stringify(previous.groups) !== JSON.stringify(this.props.groups);
36
+
37
+ protected load = async () => {
38
+ const { default: Plugin } = await import('bootstrap/js/src/otp-input.js');
39
+ const {
40
+ length,
41
+ type = 'numeric',
42
+ mask = false,
43
+ groups = null,
44
+ separator = '·'
45
+ } = this.props;
46
+
47
+ return (element: HTMLDivElement) => {
48
+ element.addEventListener('complete.bs.otpInput', this.handleComplete);
49
+ element.addEventListener('input.bs.otpInput', this.handleValueChange);
50
+
51
+ return Plugin.getOrCreateInstance(element, { length, type, mask, groups, separator });
52
+ };
53
+ };
54
+
55
+ protected updatePlugin() {
56
+ const { value } = this.props;
57
+ const slots = this.element.current?.querySelector('.otp-slots');
58
+
59
+ slots?.classList.add('min-w-0', 'max-w-100');
60
+
61
+ for (const slot of slots?.querySelectorAll('.otp-slot') ?? [])
62
+ slot.classList.add('min-w-0');
47
63
 
64
+ if (value !== undefined && this.instance?.getValue() !== String(value))
65
+ this.instance?.setValue(String(value));
66
+ }
67
+
68
+ protected dispose() {
69
+ this.element.current?.removeEventListener('complete.bs.otpInput', this.handleComplete);
70
+ this.element.current?.removeEventListener('input.bs.otpInput', this.handleValueChange);
71
+ super.dispose();
72
+ }
73
+
74
+ private handleComplete = (event: Event) =>
75
+ this.props.onComplete?.((event as Event & { value: string }).value);
76
+
77
+ private handleValueChange = (event: Event) =>
78
+ this.props.onValueChange?.((event as Event & { value: string }).value);
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
+
93
+ render() {
94
+ const {
95
+ as: Root = 'div',
96
+ className,
97
+ wrapperClassName,
98
+ length,
99
+ type = 'numeric',
100
+ mask,
101
+ groups,
102
+ separator,
103
+ connected,
104
+ size,
105
+ validation,
106
+ onComplete: _onComplete,
107
+ onValueChange: _onValueChange,
108
+ forwardedRef: _forwardedRef,
109
+ autoComplete = 'one-time-code',
110
+ ...props
111
+ } = this.props;
112
+
113
+ return (
114
+ <Root
115
+ {...(this.props.as ? props : {})}
116
+ ref={this.setRootRef}
117
+ className={classNames(
118
+ 'otp min-w-0 max-w-100',
119
+ connected && 'otp-connected',
120
+ size && `otp-${size}`,
121
+ validation && `is-${validation}`,
122
+ wrapperClassName
123
+ )}
124
+ data-bs-otp=""
125
+ data-bs-length={length}
126
+ data-bs-type={type}
127
+ data-bs-mask={mask || undefined}
128
+ data-bs-groups={groups ? JSON.stringify(groups) : undefined}
129
+ data-bs-separator={separator}
130
+ >
131
+ <input
132
+ {...(this.props.as ? {} : props)}
133
+ ref={this.props.as ? undefined : this.forwardRef}
134
+ name={props.name}
135
+ type="text"
136
+ maxLength={length}
137
+ autoComplete={autoComplete}
138
+ inputMode={type === 'numeric' ? 'numeric' : 'text'}
139
+ aria-invalid={validation ? validation === 'invalid' : props['aria-invalid']}
140
+ pattern={
141
+ type === 'numeric'
142
+ ? '[0-9]*'
143
+ : type === 'alpha'
144
+ ? '[A-Za-z]*'
145
+ : '[A-Za-z0-9]*'
146
+ }
147
+ className={classNames('otp-input', className)}
148
+ />
149
+ </Root>
150
+ );
151
+ }
152
+ }
153
+
154
+ export const OTPInput = polymorphic<OTPInputProps & { ref?: Ref<HTMLInputElement> }>(
155
+ forwardRef<HTMLInputElement, OTPInputProps & AsProps>((props, forwardedRef) => (
156
+ <OTPInputComponent {...props} forwardedRef={forwardedRef} />
157
+ ))
158
+ );
48
159
  OTPInput.displayName = 'OTPInput';
@@ -13,6 +13,8 @@ export interface PasswordStrengthProps extends ComponentPropsWithoutRef<'div'> {
13
13
  weights?: Record<string, number>;
14
14
  thresholds?: number[];
15
15
  scorer?: (password: string) => number;
16
+ strength?: PasswordStrengthLevel;
17
+ showText?: boolean;
16
18
  }
17
19
 
18
20
  interface PasswordStrengthComponentProps extends PasswordStrengthProps {
@@ -32,6 +34,7 @@ class PasswordStrengthComponent extends Component<PasswordStrengthComponentProps
32
34
  private instanceElement?: HTMLDivElement;
33
35
  private passwordInput?: HTMLInputElement;
34
36
  private mounted = false;
37
+ private revision = 0;
35
38
 
36
39
  componentDidMount() {
37
40
  this.mounted = true;
@@ -46,11 +49,13 @@ class PasswordStrengthComponent extends Component<PasswordStrengthComponentProps
46
49
  previousProps.weights !== this.props.weights ||
47
50
  previousProps.thresholds !== this.props.thresholds ||
48
51
  previousProps.scorer !== this.props.scorer ||
49
- previousProps.variant !== this.props.variant
52
+ previousProps.variant !== this.props.variant ||
53
+ previousProps.strength !== this.props.strength ||
54
+ previousProps.showText !== this.props.showText
50
55
  ) {
51
56
  this.dispose();
52
57
  this.initialize();
53
- }
58
+ } else if (this.props.strength === undefined) this.handleInput();
54
59
  }
55
60
 
56
61
  componentWillUnmount() {
@@ -59,8 +64,9 @@ class PasswordStrengthComponent extends Component<PasswordStrengthComponentProps
59
64
  }
60
65
 
61
66
  private initialize = async () => {
67
+ const revision = ++this.revision;
62
68
  const element = this.meter;
63
- if (!element) return;
69
+ if (!element || this.props.strength !== undefined) return;
64
70
 
65
71
  const { default: Strength } = await import('bootstrap/js/src/strength.js');
66
72
  const { input, minLength, messages, weights, thresholds, scorer } = this.props;
@@ -74,7 +80,7 @@ class PasswordStrengthComponent extends Component<PasswordStrengthComponentProps
74
80
  ...(thresholds !== undefined && { thresholds }),
75
81
  ...(scorer !== undefined && { scorer })
76
82
  };
77
- if (!this.mounted || this.meter !== element) return;
83
+ if (!this.mounted || this.meter !== element || revision !== this.revision) return;
78
84
 
79
85
  this.instanceElement = element;
80
86
  this.instance = Strength.getOrCreateInstance(element, config);
@@ -101,6 +107,7 @@ class PasswordStrengthComponent extends Component<PasswordStrengthComponentProps
101
107
  };
102
108
 
103
109
  private dispose = () => {
110
+ this.revision++;
104
111
  this.passwordInput?.removeEventListener('input', this.handleInput);
105
112
  this.passwordInput?.removeEventListener('change', this.handleInput);
106
113
  this.instance?.dispose();
@@ -128,24 +135,43 @@ class PasswordStrengthComponent extends Component<PasswordStrengthComponentProps
128
135
  scorer: _scorer,
129
136
  forwardedRef: _forwardedRef,
130
137
  variant = 'segments',
138
+ strength,
139
+ showText = true,
131
140
  ...props
132
141
  } = this.props;
133
142
  const isBar = variant === 'bar';
143
+ const levelIndex = strength ? levels.indexOf(strength) + 1 : 0;
134
144
 
135
145
  return (
136
- <div
137
- {...props}
138
- ref={this.setMeterRef}
139
- className={classNames(isBar ? 'strength-bar' : 'strength', className)}
140
- role="meter"
141
- aria-valuemin={0}
142
- aria-valuemax={levels.length}
143
- aria-valuenow={0}
144
- aria-valuetext="Empty"
145
- >
146
- {!isBar && levels.map(level => <div key={level} className="strength-segment" />)}
147
- {!isBar && <span className="strength-text" />}
148
- </div>
146
+ <>
147
+ <div
148
+ {...props}
149
+ ref={this.setMeterRef}
150
+ className={classNames(isBar ? 'strength-bar' : 'strength', className)}
151
+ data-bs-strength={strength}
152
+ role="meter"
153
+ aria-valuemin={0}
154
+ aria-valuemax={levels.length}
155
+ aria-valuenow={levelIndex}
156
+ aria-valuetext={strength || 'Empty'}
157
+ >
158
+ {!isBar &&
159
+ levels.map((level, index) => (
160
+ <div
161
+ key={level}
162
+ className={classNames(
163
+ 'strength-segment',
164
+ index < levelIndex && 'active'
165
+ )}
166
+ />
167
+ ))}
168
+ </div>
169
+ {showText && (
170
+ <span className="strength-text" data-bs-strength={strength}>
171
+ {strength && (this.props.messages?.[strength] ?? strength)}
172
+ </span>
173
+ )}
174
+ </>
149
175
  );
150
176
  }
151
177
  }
@@ -0,0 +1,53 @@
1
+ 'use client';
2
+
3
+ import { Component, createRef } from 'react';
4
+
5
+ interface Disposable {
6
+ dispose(): void;
7
+ }
8
+
9
+ export abstract class FormPlugin<
10
+ P,
11
+ I extends Disposable,
12
+ E extends HTMLElement = HTMLDivElement
13
+ > extends Component<P> {
14
+ protected element = createRef<E>();
15
+ protected instance?: I;
16
+ private revision = 0;
17
+
18
+ protected abstract load(): Promise<(element: E) => I>;
19
+ protected abstract configurationChanged(previous: P): boolean;
20
+
21
+ protected updatePlugin() {}
22
+
23
+ componentDidMount() {
24
+ this.initialize();
25
+ }
26
+
27
+ componentDidUpdate(previous: P) {
28
+ if (this.configurationChanged(previous)) {
29
+ this.dispose();
30
+ this.initialize();
31
+ } else this.updatePlugin();
32
+ }
33
+
34
+ componentWillUnmount() {
35
+ this.dispose();
36
+ }
37
+
38
+ private initialize = async () => {
39
+ const revision = ++this.revision;
40
+ const create = await this.load();
41
+
42
+ if (revision !== this.revision || !this.element.current) return;
43
+
44
+ this.instance = create(this.element.current);
45
+ this.updatePlugin();
46
+ };
47
+
48
+ protected dispose() {
49
+ this.revision++;
50
+ this.instance?.dispose();
51
+ this.instance = undefined;
52
+ }
53
+ }
@@ -1,46 +1,96 @@
1
1
  import classNames from 'classnames';
2
- import { ComponentPropsWithRef, FC } from 'react';
2
+ import { ComponentPropsWithRef } from 'react';
3
3
 
4
- export type FormProps = ComponentPropsWithRef<'form'>;
4
+ import { polymorphic } from '../Polymorphic';
5
5
 
6
- export const Form: FC<FormProps> = props => <form {...props} />;
6
+ export type FormProps = ComponentPropsWithRef<'form'> & {
7
+ validated?: boolean;
8
+ };
7
9
 
10
+ export const Form = polymorphic<FormProps>(
11
+ ({ as: Root = 'form', className, validated, ...props }) => (
12
+ <Root {...props} className={classNames(validated && 'was-validated', className)} />
13
+ )
14
+ );
8
15
  Form.displayName = 'Form';
9
16
 
10
17
  export type FormFieldsetProps = ComponentPropsWithRef<'fieldset'>;
11
18
 
12
- export const FormFieldset: FC<FormFieldsetProps> = props => <fieldset {...props} />;
19
+ export const FormFieldset = polymorphic<FormFieldsetProps>(
20
+ ({ as: Root = 'fieldset', ...props }) => <Root {...props} />
21
+ );
13
22
 
14
23
  FormFieldset.displayName = 'FormFieldset';
15
24
 
16
- export type FormFieldProps = ComponentPropsWithRef<'div'>;
17
-
18
- export const FormField: FC<FormFieldProps> = ({ className, ...props }) => (
19
- <div {...props} className={classNames('form-field', className)} />
25
+ export type FormFieldProps = ComponentPropsWithRef<'div'> & {
26
+ card?: boolean;
27
+ };
28
+
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
+ )
20
36
  );
21
-
22
37
  FormField.displayName = 'FormField';
23
38
 
24
39
  export type FormGroupProps = ComponentPropsWithRef<'div'>;
25
40
 
26
- export const FormGroup: FC<FormGroupProps> = ({ className, ...props }) => (
27
- <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
+ )
28
45
  );
29
-
30
46
  FormGroup.displayName = 'FormGroup';
31
47
 
32
48
  export type FormFieldContentProps = ComponentPropsWithRef<'div'>;
33
49
 
34
- export const FormFieldContent: FC<FormFieldContentProps> = ({ className, ...props }) => (
35
- <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
+ )
36
54
  );
37
-
38
55
  FormFieldContent.displayName = 'FormFieldContent';
39
56
 
40
- export type FormLabelProps = ComponentPropsWithRef<'label'>;
41
-
42
- export const FormLabel: FC<FormLabelProps> = ({ className, ...props }) => (
43
- <label {...props} className={classNames('form-label', className)} />
57
+ export interface FormValidationProps {
58
+ validation?: 'valid' | 'invalid';
59
+ }
60
+
61
+ export type FormLabelProps = ComponentPropsWithRef<'label'> & {
62
+ plain?: boolean;
63
+ column?: boolean;
64
+ controlSize?: 'sm' | 'lg';
65
+ };
66
+
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
+ )
44
78
  );
45
-
46
79
  FormLabel.displayName = 'FormLabel';
80
+
81
+ export type FormLegendProps = ComponentPropsWithRef<'legend'> &
82
+ Pick<FormLabelProps, 'column' | 'controlSize'>;
83
+
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
+ )
95
+ );
96
+ FormLegend.displayName = 'FormLegend';
@@ -1,51 +1,46 @@
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';
6
+ import { Checkbox, CheckboxProps } from './Checkbox';
7
+ import { FormValidationProps } from './Primitives';
5
8
 
6
- export type RadioProps = Omit<ComponentPropsWithRef<'input'>, 'type' | 'size'> & {
7
- size?: 'sm' | 'lg';
8
- theme?: ComponentTheme;
9
- };
9
+ export type RadioProps = Omit<ComponentPropsWithRef<'input'>, 'type' | 'size'> &
10
+ FormValidationProps & {
11
+ size?: 'sm' | 'lg';
12
+ theme?: ComponentTheme;
13
+ };
10
14
 
11
- export const Radio: FC<RadioProps> = ({ className, size, theme, ...props }) => (
12
- <input
13
- {...props}
14
- type="radio"
15
- className={classNames(
16
- 'radio',
17
- size && `radio-${size}`,
18
- theme && `theme-${theme}`,
19
- className
20
- )}
21
- />
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
+ )
22
30
  );
23
-
24
31
  Radio.displayName = 'Radio';
25
32
 
26
- export type FormCheckProps = Omit<ComponentPropsWithRef<'input'>, 'type' | 'size'> & {
27
- type?: 'checkbox' | 'radio';
28
- size?: 'sm' | 'lg';
29
- theme?: ComponentTheme;
30
- };
33
+ export type FormCheckProps = CheckboxProps &
34
+ Pick<ComponentPropsWithRef<'input'>, 'ref'> & {
35
+ type?: 'checkbox' | 'radio';
36
+ };
31
37
 
32
- export const FormCheck: FC<FormCheckProps> = ({
33
- className,
34
- type = 'checkbox',
35
- size,
36
- theme,
37
- ...props
38
- }) => (
39
- <input
40
- {...props}
41
- type={type}
42
- className={classNames(
43
- type === 'checkbox' ? 'check' : 'radio',
44
- size && `${type === 'checkbox' ? 'check' : 'radio'}-${size}`,
45
- theme && `theme-${theme}`,
46
- className
47
- )}
48
- />
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
+ )
49
45
  );
50
-
51
46
  FormCheck.displayName = 'FormCheck';