idea-react 3.0.0-beta.1 → 3.0.0-beta.3

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 (89) hide show
  1. package/AGENTS.md +12 -3
  2. package/ReadMe.md +34 -6
  3. package/dist/index.d.mts +313 -230
  4. package/dist/index.d.ts +313 -230
  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 +13 -13
  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 +73 -48
  62. package/source/Form/inputProps.ts +184 -0
  63. package/source/Grid.tsx +67 -59
  64. package/source/Image.tsx +10 -6
  65. package/source/ListGroup.tsx +57 -69
  66. package/source/Menu.tsx +128 -66
  67. package/source/NativeDialog/index.tsx +51 -29
  68. package/source/Nav.tsx +46 -46
  69. package/source/NavOverflow/index.tsx +26 -10
  70. package/source/Navbar.tsx +60 -55
  71. package/source/Pagination.tsx +63 -69
  72. package/source/Placeholder.tsx +47 -43
  73. package/source/Polymorphic.ts +57 -0
  74. package/source/Popover.tsx +34 -31
  75. package/source/Progress/index.tsx +59 -54
  76. package/source/ScrollSpy/index.tsx +48 -33
  77. package/source/Spinner.tsx +27 -23
  78. package/source/Stack.tsx +25 -0
  79. package/source/Stepper.tsx +46 -40
  80. package/source/Tab.tsx +40 -27
  81. package/source/Table.tsx +53 -42
  82. package/source/Toast.tsx +62 -47
  83. package/source/ToggleButton.tsx +65 -54
  84. package/source/Toggler.tsx +17 -8
  85. package/source/Tooltip.tsx +31 -29
  86. package/source/index.ts +2 -0
  87. package/tsconfig.json +2 -2
  88. package/source/Extension/CodeBlock/index.tsx +0 -58
  89. package/source/Extension/CodeBlock/style.module.css +0 -3
@@ -3,9 +3,17 @@
3
3
  import classNames from 'classnames';
4
4
  import { action, observable } from 'mobx';
5
5
  import { observer } from 'mobx-react';
6
- import { Component, ComponentPropsWithoutRef, createRef, HTMLAttributes, MouseEvent } from 'react';
7
-
8
- import { Button, ButtonProps } from './Button';
6
+ import {
7
+ Component,
8
+ ComponentPropsWithoutRef,
9
+ createRef,
10
+ ElementType,
11
+ HTMLAttributes,
12
+ MouseEvent
13
+ } from 'react';
14
+
15
+ import { ButtonProps } from './Button';
16
+ import { AsProps, PolymorphicClassProps } from './Polymorphic';
9
17
 
10
18
  const TOGGLE = 'idea-react:collapse-toggle';
11
19
  const CHANGE = 'idea-react:collapse-change';
@@ -20,15 +28,23 @@ export interface CollapseProps extends ComponentPropsWithoutRef<'div'> {
20
28
  }
21
29
 
22
30
  @observer
23
- export class Collapse extends Component<CollapseProps> {
31
+ export class Collapse<T extends ElementType | undefined = 'div'> extends Component<
32
+ PolymorphicClassProps<T, CollapseProps>
33
+ > {
34
+ declare readonly props: PolymorphicClassProps<T, CollapseProps>;
35
+
36
+ protected get componentProps(): Readonly<CollapseProps & AsProps> {
37
+ return this.props;
38
+ }
39
+
24
40
  static displayName = 'Collapse';
25
41
 
26
- @observable accessor expanded = this.props.defaultOpen ?? false;
42
+ @observable accessor expanded = this.componentProps.defaultOpen ?? false;
27
43
 
28
44
  root = createRef<HTMLDivElement>();
29
45
 
30
46
  get isOpen() {
31
- return this.props.open ?? this.expanded;
47
+ return this.componentProps.open ?? this.expanded;
32
48
  }
33
49
 
34
50
  componentDidMount() {
@@ -39,7 +55,7 @@ export class Collapse extends Component<CollapseProps> {
39
55
  }
40
56
 
41
57
  componentDidUpdate(previous: CollapseProps) {
42
- if (previous.open !== this.props.open && this.isOpen) this.notifyOpen();
58
+ if (previous.open !== this.componentProps.open && this.isOpen) this.notifyOpen();
43
59
 
44
60
  this.notify();
45
61
  }
@@ -56,9 +72,9 @@ export class Collapse extends Component<CollapseProps> {
56
72
  setOpen = action((open: boolean) => {
57
73
  if (open === this.isOpen) return;
58
74
 
59
- this.props.onOpenChange?.(open);
75
+ this.componentProps.onOpenChange?.(open);
60
76
 
61
- if (this.props.open === undefined) {
77
+ if (this.componentProps.open === undefined) {
62
78
  this.expanded = open;
63
79
  if (open) this.notifyOpen();
64
80
  }
@@ -69,7 +85,7 @@ export class Collapse extends Component<CollapseProps> {
69
85
  };
70
86
 
71
87
  handlePeerOpen = ({ target }: Event) => {
72
- const { parent } = this.props;
88
+ const { parent } = this.componentProps;
73
89
  const root = this.root.current;
74
90
  const group = parent ? root?.closest(parent) : null;
75
91
 
@@ -84,7 +100,10 @@ export class Collapse extends Component<CollapseProps> {
84
100
  };
85
101
 
86
102
  render() {
103
+ const Root: ElementType = this.componentProps.as ?? 'div';
104
+
87
105
  const {
106
+ as: _as,
88
107
  open: _open,
89
108
  defaultOpen: _defaultOpen,
90
109
  parent: _parent,
@@ -92,10 +111,10 @@ export class Collapse extends Component<CollapseProps> {
92
111
  horizontal,
93
112
  className,
94
113
  ...props
95
- } = this.props;
114
+ } = this.componentProps;
96
115
 
97
116
  return (
98
- <div
117
+ <Root
99
118
  {...props}
100
119
  ref={this.root}
101
120
  className={classNames(
@@ -118,14 +137,22 @@ export interface CollapseToggleProps
118
137
  }
119
138
 
120
139
  @observer
121
- export class CollapseToggle extends Component<CollapseToggleProps> {
140
+ export class CollapseToggle<T extends ElementType | undefined = 'button'> extends Component<
141
+ PolymorphicClassProps<T, CollapseToggleProps>
142
+ > {
143
+ declare readonly props: PolymorphicClassProps<T, CollapseToggleProps>;
144
+
145
+ protected get componentProps(): Readonly<PolymorphicClassProps<T, CollapseToggleProps>> {
146
+ return this.props;
147
+ }
148
+
122
149
  static displayName = 'CollapseToggle';
123
150
 
124
151
  @observable accessor expanded = false;
125
152
  @observable accessor controls = '';
126
153
 
127
154
  get targets() {
128
- return [...document.querySelectorAll<HTMLElement>(this.props.target)];
155
+ return [...document.querySelectorAll<HTMLElement>(this.componentProps.target)];
129
156
  }
130
157
 
131
158
  componentDidMount() {
@@ -134,7 +161,7 @@ export class CollapseToggle extends Component<CollapseToggleProps> {
134
161
  }
135
162
 
136
163
  componentDidUpdate(previous: CollapseToggleProps) {
137
- if (previous.target !== this.props.target) this.refresh();
164
+ if (previous.target !== this.componentProps.target) this.refresh();
138
165
  }
139
166
 
140
167
  componentWillUnmount() {
@@ -152,23 +179,51 @@ export class CollapseToggle extends Component<CollapseToggleProps> {
152
179
  });
153
180
 
154
181
  handleClick = (event: MouseEvent<HTMLElement>) => {
155
- this.props.onClick?.(event);
182
+ this.componentProps.onClick?.(event);
156
183
 
157
184
  if (event.defaultPrevented) return;
158
185
 
159
186
  event.preventDefault();
187
+ if (this.componentProps.disabled) return;
160
188
 
161
189
  for (const target of this.targets) target.dispatchEvent(new Event(TOGGLE));
162
190
  };
163
191
 
164
192
  render() {
165
- const { target: _target, href, ...props } = this.props;
193
+ const {
194
+ as: RootAs,
195
+ target: _target,
196
+ href,
197
+ type = 'button',
198
+ variant,
199
+ theme,
200
+ size,
201
+ disabled,
202
+ icon,
203
+ styled,
204
+ className,
205
+ ...props
206
+ } = this.componentProps;
207
+ const Root: ElementType = RootAs ?? (href ? 'a' : 'button');
208
+ const nativeButton = Root === 'button';
209
+ const rootClassName = classNames(
210
+ variant === false ? undefined : variant ? `btn-${variant}` : icon ? undefined : 'btn',
211
+ theme && `theme-${theme}`,
212
+ size && `btn-${size}`,
213
+ { 'btn-icon': icon, 'btn-styled': styled, disabled },
214
+ className
215
+ );
166
216
 
167
217
  return (
168
- <Button
218
+ <Root
169
219
  {...props}
220
+ type={nativeButton ? type : undefined}
221
+ href={disabled ? undefined : href}
222
+ disabled={nativeButton || typeof Root !== 'string' ? disabled : undefined}
223
+ aria-disabled={(!nativeButton && disabled) || undefined}
224
+ tabIndex={!nativeButton && disabled ? -1 : props.tabIndex}
225
+ className={rootClassName}
170
226
  role={props.role ?? 'button'}
171
- href={href}
172
227
  aria-controls={this.controls || undefined}
173
228
  aria-expanded={this.expanded}
174
229
  onClick={this.handleClick}
package/source/Drawer.tsx CHANGED
@@ -3,29 +3,42 @@
3
3
  import classNames from 'classnames';
4
4
  import { action, observable } from 'mobx';
5
5
  import { observer } from 'mobx-react';
6
- import { Component, ComponentPropsWithRef, createRef, FC, ReactNode } from 'react';
6
+ import { Component, ComponentPropsWithRef, createRef, ElementType, ReactNode } from 'react';
7
7
 
8
8
  import { CloseButton } from './CloseButton';
9
9
  import { Breakpoint } from './Grid';
10
10
  import { NativeDialog, NativeDialogProps } from './NativeDialog';
11
+ import { AsProps, polymorphic, PolymorphicClassProps } from './Polymorphic';
11
12
 
12
13
  export type DrawerPlacement = 'start' | 'end' | 'top' | 'bottom';
13
14
 
14
- export const DrawerHeader: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
15
- <div {...props} className={classNames('drawer-header', className)} />
15
+ export const DrawerHeader = polymorphic<ComponentPropsWithRef<'div'>>(
16
+ ({ as: Root = 'div', className, ...props }) => (
17
+ <Root {...props} className={classNames('drawer-header', className)} />
18
+ )
16
19
  );
20
+ DrawerHeader.displayName = 'DrawerHeader';
17
21
 
18
- export const DrawerTitle: FC<ComponentPropsWithRef<'h5'>> = ({ className, ...props }) => (
19
- <h5 {...props} className={classNames('drawer-title', className)} />
22
+ export const DrawerTitle = polymorphic<ComponentPropsWithRef<'h5'>>(
23
+ ({ as: Root = 'h5', className, ...props }) => (
24
+ <Root {...props} className={classNames('drawer-title', className)} />
25
+ )
20
26
  );
27
+ DrawerTitle.displayName = 'DrawerTitle';
21
28
 
22
- export const DrawerBody: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
23
- <div {...props} className={classNames('drawer-body', className)} />
29
+ export const DrawerBody = polymorphic<ComponentPropsWithRef<'div'>>(
30
+ ({ as: Root = 'div', className, ...props }) => (
31
+ <Root {...props} className={classNames('drawer-body', className)} />
32
+ )
24
33
  );
34
+ DrawerBody.displayName = 'DrawerBody';
25
35
 
26
- export const DrawerFooter: FC<ComponentPropsWithRef<'div'>> = ({ className, ...props }) => (
27
- <div {...props} className={classNames('drawer-footer', className)} />
36
+ export const DrawerFooter = polymorphic<ComponentPropsWithRef<'div'>>(
37
+ ({ as: Root = 'div', className, ...props }) => (
38
+ <Root {...props} className={classNames('drawer-footer', className)} />
39
+ )
28
40
  );
41
+ DrawerFooter.displayName = 'DrawerFooter';
29
42
 
30
43
  export interface DrawerProps extends Omit<
31
44
  NativeDialogProps,
@@ -52,12 +65,20 @@ const BREAKPOINT_WIDTHS: Record<Breakpoint, number> = {
52
65
  };
53
66
 
54
67
  @observer
55
- export class Drawer extends Component<DrawerProps> {
68
+ export class Drawer<T extends ElementType | undefined = 'dialog'> extends Component<
69
+ PolymorphicClassProps<T, DrawerProps>
70
+ > {
71
+ declare readonly props: PolymorphicClassProps<T, DrawerProps>;
72
+
73
+ protected get componentProps(): Readonly<DrawerProps & AsProps> {
74
+ return this.props;
75
+ }
76
+
56
77
  static displayName = 'Drawer';
57
78
 
58
79
  @observable accessor expanded = false;
59
80
 
60
- dialog = createRef<NativeDialog>();
81
+ dialog = createRef<NativeDialog<ElementType>>();
61
82
  media?: MediaQueryList;
62
83
  dismissed = false;
63
84
 
@@ -66,9 +87,9 @@ export class Drawer extends Component<DrawerProps> {
66
87
  }
67
88
 
68
89
  componentDidUpdate({ responsive, open }: DrawerProps) {
69
- if (responsive !== this.props.responsive) this.observeBreakpoint();
90
+ if (responsive !== this.componentProps.responsive) this.observeBreakpoint();
70
91
 
71
- if (open !== this.props.open && this.props.open) this.dismissed = false;
92
+ if (open !== this.componentProps.open && this.componentProps.open) this.dismissed = false;
72
93
  }
73
94
 
74
95
  componentWillUnmount() {
@@ -78,7 +99,7 @@ export class Drawer extends Component<DrawerProps> {
78
99
  observeBreakpoint = () => {
79
100
  this.media?.removeEventListener('change', this.handleBreakpoint);
80
101
 
81
- const { responsive } = this.props;
102
+ const { responsive } = this.componentProps;
82
103
  this.media = responsive
83
104
  ? window.matchMedia(`(min-width: ${BREAKPOINT_WIDTHS[responsive]}px)`)
84
105
  : undefined;
@@ -90,20 +111,21 @@ export class Drawer extends Component<DrawerProps> {
90
111
  handleBreakpoint = () => (this.expanded = this.media?.matches ?? false);
91
112
 
92
113
  handleDismiss = () => {
93
- this.props.onDismiss?.();
114
+ this.componentProps.onDismiss?.();
94
115
 
95
116
  this.dismissed = true;
96
117
  this.dialog.current?.close();
97
118
  };
98
119
 
99
120
  handleClose = () => {
100
- if (!this.dismissed && !this.expanded) this.props.onDismiss?.();
121
+ if (!this.dismissed && !this.expanded) this.componentProps.onDismiss?.();
101
122
 
102
123
  this.dismissed = false;
103
124
  };
104
125
 
105
126
  render() {
106
127
  const {
128
+ as: _as,
107
129
  title,
108
130
  placement = 'end',
109
131
  footer,
@@ -121,10 +143,11 @@ export class Drawer extends Component<DrawerProps> {
121
143
  className,
122
144
  children,
123
145
  ...props
124
- } = this.props;
146
+ } = this.componentProps;
125
147
 
126
148
  return (
127
149
  <NativeDialog
150
+ as={this.componentProps.as}
128
151
  {...props}
129
152
  ref={this.dialog}
130
153
  open={this.expanded || open}
@@ -3,11 +3,14 @@
3
3
  import {
4
4
  createRef,
5
5
  DetailedHTMLProps,
6
+ ElementType,
6
7
  HTMLAttributes,
7
8
  PropsWithChildren,
8
9
  PureComponent
9
10
  } from 'react';
10
11
 
12
+ import { AsProps, PolymorphicClassProps } from '../Polymorphic';
13
+
11
14
  export type ClickBoundaryProps = PropsWithChildren<
12
15
  {
13
16
  onInnerClick?: (event: MouseEvent) => unknown;
@@ -15,11 +18,19 @@ export type ClickBoundaryProps = PropsWithChildren<
15
18
  } & DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
16
19
  >;
17
20
 
18
- export class ClickBoundary extends PureComponent<ClickBoundaryProps> {
21
+ export class ClickBoundary<T extends ElementType | undefined = 'div'> extends PureComponent<
22
+ PolymorphicClassProps<T, ClickBoundaryProps>
23
+ > {
24
+ declare readonly props: PolymorphicClassProps<T, ClickBoundaryProps>;
25
+
26
+ protected get componentProps(): Readonly<ClickBoundaryProps & AsProps> {
27
+ return this.props;
28
+ }
29
+
19
30
  root = createRef<HTMLDivElement>();
20
31
 
21
32
  switchClick = (event: MouseEvent) => {
22
- const { onInnerClick, onOuterClick } = this.props;
33
+ const { onInnerClick, onOuterClick } = this.componentProps;
23
34
 
24
35
  if (this.root.current?.contains(event.target as Node)) onInnerClick?.(event);
25
36
  else onOuterClick?.(event);
@@ -34,12 +45,20 @@ export class ClickBoundary extends PureComponent<ClickBoundaryProps> {
34
45
  }
35
46
 
36
47
  render() {
37
- const { children, onInnerClick: _, onOuterClick: __, ...props } = this.props;
48
+ const Root: ElementType = this.componentProps.as ?? 'div';
49
+
50
+ const {
51
+ as: _as,
52
+ children,
53
+ onInnerClick: _,
54
+ onOuterClick: __,
55
+ ...props
56
+ } = this.componentProps;
38
57
 
39
58
  return (
40
- <div ref={this.root} {...props}>
59
+ <Root ref={this.root} {...props}>
41
60
  {children}
42
- </div>
61
+ </Root>
43
62
  );
44
63
  }
45
64
  }
@@ -0,0 +1,64 @@
1
+ import css from '@shikijs/langs/css';
2
+ import html from '@shikijs/langs/html';
3
+ import js from '@shikijs/langs/javascript';
4
+ import json from '@shikijs/langs/json';
5
+ import jsx from '@shikijs/langs/jsx';
6
+ import sh from '@shikijs/langs/sh';
7
+ import tsx from '@shikijs/langs/tsx';
8
+ import ts from '@shikijs/langs/typescript';
9
+ import githubDark from '@shikijs/themes/github-dark';
10
+ import githubLight from '@shikijs/themes/github-light';
11
+ import { HTMLAttributes, isValidElement, ReactNode } from 'react';
12
+ import reactElementToJSXString from 'react-element-to-jsx-string';
13
+ import { createHighlighterCoreSync, ShikiTransformer } from 'shiki/core';
14
+ import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
15
+
16
+ import { polymorphic } from '../Polymorphic';
17
+
18
+ const highlighter = createHighlighterCoreSync({
19
+ themes: [githubLight, githubDark],
20
+ langs: [css, html, js, json, jsx, sh, ts, tsx],
21
+ engine: createJavaScriptRegexEngine()
22
+ });
23
+
24
+ export interface CodeBlockProps extends HTMLAttributes<HTMLPreElement> {
25
+ language: string;
26
+ header?: ReactNode;
27
+ }
28
+
29
+ export const CodeBlock = polymorphic<CodeBlockProps>(
30
+ ({ as: Root = 'pre', className = '', language, header, children, ...props }) => {
31
+ const sourceCode =
32
+ typeof children === 'string'
33
+ ? children
34
+ : reactElementToJSXString(isValidElement(children) ? children : <>{children}</>);
35
+
36
+ const span: ShikiTransformer['span'] = ({ properties }) => {
37
+ properties.style = `${properties.style};color:light-dark(var(--shiki-light),var(--shiki-dark))`;
38
+ };
39
+ const __html = highlighter.codeToHtml(sourceCode, {
40
+ lang: language,
41
+ themes: { light: 'github-light', dark: 'github-dark' },
42
+ defaultColor: false,
43
+ structure: 'inline',
44
+ transformers: [{ span }]
45
+ });
46
+ const code = (
47
+ <Root className={`m-0 shiki language-${language} ${className}`} {...props}>
48
+ <code dangerouslySetInnerHTML={{ __html }} className={`language-${language}`} />
49
+ </Root>
50
+ );
51
+
52
+ return header ? (
53
+ <figure className="m-0">
54
+ <figcaption className="px-3 pb-6 mb-6 bg-body-tertiary border-bottom">
55
+ {header}
56
+ </figcaption>
57
+ {code}
58
+ </figure>
59
+ ) : (
60
+ code
61
+ );
62
+ }
63
+ );
64
+ CodeBlock.displayName = 'CodeBlock';
@@ -3,9 +3,11 @@
3
3
  import { computed, observable } from 'mobx';
4
4
  import { observer } from 'mobx-react';
5
5
  import { ObservedComponent, reaction } from 'mobx-react-helper';
6
- import { HTMLAttributes } from 'react';
6
+ import { ElementType, HTMLAttributes } from 'react';
7
7
  import { Second, TimeData } from 'web-utility';
8
8
 
9
+ import { AsProps, PolymorphicClassProps } from '../Polymorphic';
10
+
9
11
  export interface TimeUnit {
10
12
  scale: number;
11
13
  label: string;
@@ -27,7 +29,15 @@ export interface CountdownProps extends HTMLAttributes<HTMLOListElement> {
27
29
  * Migrate from https://bootstrap.web-cell.dev/#Calendar/CountDown
28
30
  */
29
31
  @observer
30
- export class Countdown extends ObservedComponent<CountdownProps> {
32
+ export class Countdown<T extends ElementType | undefined = 'ol'> extends ObservedComponent<
33
+ PolymorphicClassProps<T, CountdownProps>
34
+ > {
35
+ declare readonly props: PolymorphicClassProps<T, CountdownProps>;
36
+
37
+ protected get componentProps(): CountdownProps & AsProps {
38
+ return this.observedProps;
39
+ }
40
+
31
41
  static displayName = 'Countdown';
32
42
 
33
43
  @observable
@@ -36,12 +46,12 @@ export class Countdown extends ObservedComponent<CountdownProps> {
36
46
  private timer = 0;
37
47
 
38
48
  get endTimestamp() {
39
- return +new Date(this.props.endTime || Date.now());
49
+ return +new Date(this.componentProps.endTime || Date.now());
40
50
  }
41
51
 
42
52
  @computed
43
53
  get timeSections(): TimeSection[] {
44
- const { units } = this.observedProps;
54
+ const { units } = this.componentProps;
45
55
  let { rest } = this;
46
56
 
47
57
  return units.reduce((list, { label }, index) => {
@@ -59,7 +69,7 @@ export class Countdown extends ObservedComponent<CountdownProps> {
59
69
  }
60
70
 
61
71
  tick = () => {
62
- const { onEnd, endTime } = this.props,
72
+ const { onEnd, endTime } = this.componentProps,
63
73
  rest = this.endTimestamp - Date.now();
64
74
 
65
75
  if (rest > 0) {
@@ -83,7 +93,7 @@ export class Countdown extends ObservedComponent<CountdownProps> {
83
93
  this.initTimer();
84
94
  }
85
95
 
86
- @reaction(_this => _this.observedProps.endTime)
96
+ @reaction(_this => _this.componentProps.endTime)
87
97
  initTimer() {
88
98
  this.stop();
89
99
  this.tick();
@@ -97,24 +107,26 @@ export class Countdown extends ObservedComponent<CountdownProps> {
97
107
 
98
108
  render() {
99
109
  const {
110
+ as,
100
111
  className = '',
101
112
  itemClassName = 'border rounded px-4 py-3 d-flex flex-column justify-content-center align-items-center',
102
113
  units: _u,
103
114
  endTime: _e,
104
115
  onEnd: _o,
105
116
  ...props
106
- } = this.props;
117
+ } = this.componentProps;
118
+ const Root: ElementType = as ?? 'ol';
107
119
  const { timeSections } = this;
108
120
 
109
121
  return (
110
- <ol className={`list-unstyled m-0 ${className}`} {...props}>
122
+ <Root className={`list-unstyled m-0 ${className}`} {...props}>
111
123
  {timeSections.map(({ value, label }) => (
112
124
  <li key={label} className={itemClassName}>
113
125
  <strong>{(value + '').padStart(2, '0')}</strong>
114
126
  <span>{label}</span>
115
127
  </li>
116
128
  ))}
117
- </ol>
129
+ </Root>
118
130
  );
119
131
  }
120
132
  }
@@ -1,43 +1,78 @@
1
1
  'use client';
2
2
 
3
3
  import { EditorCore, WrapperProps } from '@react-editor-js/core';
4
- import { observable } from 'mobx';
4
+ import classNames from 'classnames';
5
+ import { action, observable } from 'mobx';
5
6
  import { observer } from 'mobx-react';
6
- import { Component, InputHTMLAttributes, PropsWithoutRef } from 'react';
7
+ import { Component, ComponentPropsWithRef, ElementType, FocusEvent } from 'react';
7
8
  import { createReactEditorJS } from 'react-editor-js';
8
9
 
10
+ import { AsProps, PolymorphicClassProps } from '../Polymorphic';
11
+
9
12
  const ReactEditorJS = createReactEditorJS();
10
13
 
11
- export type EditorProps = PropsWithoutRef<
12
- WrapperProps & Pick<InputHTMLAttributes<HTMLInputElement>, 'name' | 'required'>
13
- >;
14
+ export type EditorProps = WrapperProps &
15
+ Pick<ComponentPropsWithRef<'input'>, 'name' | 'required'> &
16
+ Pick<ComponentPropsWithRef<'div'>, 'className' | 'style' | 'tabIndex' | 'onBlur'>;
14
17
 
15
18
  @observer
16
- export class Editor extends Component<EditorProps> {
19
+ export class Editor<T extends ElementType | undefined = 'div'> extends Component<
20
+ PolymorphicClassProps<T, EditorProps>
21
+ > {
22
+ declare readonly props: PolymorphicClassProps<T, EditorProps>;
23
+
24
+ protected get componentProps(): Readonly<EditorProps & AsProps> {
25
+ return this.props;
26
+ }
27
+
17
28
  static displayName = 'Editor';
18
29
 
19
30
  private core?: EditorCore;
20
31
 
21
32
  @observable
22
- accessor innerValue = this.props.defaultValue;
33
+ accessor innerValue = this.componentProps.defaultValue;
34
+
35
+ setInnerValue = action((value: typeof this.innerValue) => (this.innerValue = value));
23
36
 
24
- save = async () => this.core && (this.innerValue = await this.core.save());
37
+ handleBlur = async (event: FocusEvent<HTMLDivElement>) => {
38
+ this.componentProps.onBlur?.(event);
39
+
40
+ if (this.core) this.setInnerValue(await this.core.save());
41
+ };
42
+
43
+ initialize = (core: EditorCore) => {
44
+ this.componentProps.onInitialize?.(core);
45
+
46
+ this.core = core;
47
+ };
25
48
 
26
49
  render() {
27
- const { name, required, children: _children, ...editorProps } = this.props,
50
+ const Root: ElementType = this.componentProps.as ?? 'div';
51
+
52
+ const {
53
+ as: _as,
54
+ name,
55
+ required,
56
+ className,
57
+ style,
58
+ tabIndex = -1,
59
+ onBlur: _onBlur,
60
+ children: _children,
61
+ ...editorProps
62
+ } = this.componentProps,
28
63
  { innerValue } = this;
29
64
 
30
65
  return (
31
- <div className="form-control" tabIndex={-1} onBlur={this.save}>
66
+ <Root
67
+ {...(this.componentProps.as ? editorProps : {})}
68
+ className={classNames('form-control', className)}
69
+ style={style}
70
+ tabIndex={tabIndex}
71
+ onBlur={this.handleBlur}
72
+ >
32
73
  <input hidden {...{ name, required }} value={JSON.stringify(innerValue)} />
33
- <ReactEditorJS
34
- {...editorProps}
35
- onInitialize={core => {
36
- this.core = core;
37
- editorProps.onInitialize?.(core);
38
- }}
39
- />
40
- </div>
74
+ <ReactEditorJS {...editorProps} onInitialize={this.initialize} />
75
+ </Root>
41
76
  );
42
77
  }
43
78
  }
@@ -1,6 +1,8 @@
1
1
  import { OutputData } from '@editorjs/editorjs';
2
2
  import createParser from 'editorjs-html';
3
- import { FC, HTMLAttributes } from 'react';
3
+ import { HTMLAttributes } from 'react';
4
+
5
+ import { polymorphic } from '../Polymorphic';
4
6
 
5
7
  export const parser = createParser({
6
8
  link: ({ data: { link } }) => `<a href="${link}">${link}</a>`
@@ -10,7 +12,9 @@ export interface EditorHTMLProps extends HTMLAttributes<HTMLDivElement> {
10
12
  data: OutputData;
11
13
  }
12
14
 
13
- export const EditorHTML: FC<EditorHTMLProps> = ({ data, ...props }) => (
14
- <article {...props} dangerouslySetInnerHTML={{ __html: parser.parse(data) }} />
15
+ export const EditorHTML = polymorphic<EditorHTMLProps>(
16
+ ({ as: Root = 'article', data, ...props }) => (
17
+ <Root {...props} dangerouslySetInnerHTML={{ __html: parser.parse(data) }} />
18
+ )
15
19
  );
16
20
  EditorHTML.displayName = 'EditorHTML';