+> {
+ // Not `Readonly`: lets JSX infer `T` from a string `tag` (else it widens to `ElementType`).
+ declare props: DropdownProps;
+ static override contextType = InputGroupContext;
+ declare context: ContextType;
+
+ containerRef = createRef();
+ menuRef = createRef();
+ toggleRef = createRef();
+ private menuCtrl: HTMLElement | null = null;
+
+ override componentDidMount() {
+ this.handleProps();
+ }
+
+ override componentDidUpdate(prevProps: DropdownProps) {
+ if (this.props.isOpen !== prevProps.isOpen) this.handleProps();
+ }
+
+ override componentWillUnmount() {
+ this.removeEvents();
+ }
+
+ handleMenuRef = (el: HTMLElement | null) => {
+ this.menuRef.current = el;
+ };
+
+ handleToggleRef = (el: HTMLElement | null) => {
+ this.toggleRef.current = el;
+ };
+
+ handleDocumentClick = (e: Event) => {
+ const which = keyCodeOf(e as KeyboardEvent);
+ if (which === 3 || (e.type === 'keyup' && which !== keyCodes.tab)) return;
+ const container = this.containerRef.current;
+ const menu = this.menuRef.current;
+ const toggle = this.toggleRef.current;
+ const target = e.target as HTMLElement;
+
+ // No toggle component in the dropdown: nothing to toggle against.
+ if (!toggle) return;
+
+ const targetIsToggle = toggle.contains(target);
+ const clickIsInMenu = !!menu && menu.contains(target) && menu !== target;
+
+ // Only for InputGroup with type dropdown
+ const clickIsInInput =
+ !!container &&
+ container.classList.contains('input-group') &&
+ container.classList.contains('dropdown') &&
+ target.tagName === 'INPUT';
+
+ if (
+ ((targetIsToggle && !clickIsInInput) || clickIsInMenu) &&
+ (e.type !== 'keyup' || which === keyCodes.tab)
+ ) {
+ return;
+ }
+
+ this.toggle(e);
+ };
+
+ handleKeyDown = (e: ReactKeyboardEvent) => {
+ const target = e.target as HTMLElement;
+ const which = keyCodeOf(e);
+ const role = target.getAttribute('role');
+ const isTargetMenuItem = role === 'menuitem' || role === 'option';
+ const isTargetMenuCtrl = this.getMenuCtrl() === target;
+ const isTab = keyCodes.tab === which;
+ const { a11y = true, disabled, isOpen } = this.props;
+
+ if (
+ /input|textarea/i.test(target.tagName) ||
+ (isTab && !a11y) ||
+ (isTab && !(isTargetMenuItem || isTargetMenuCtrl))
+ ) {
+ return;
+ }
+
+ if (preventDefaultKeys.includes(which) || isAlphanumeric(which)) {
+ e.preventDefault();
+ }
+
+ if (disabled) return;
+
+ if (isTargetMenuCtrl) {
+ if (openKeys.includes(which)) {
+ // Open the menu (if not open) and focus the first menu item
+ if (!isOpen) this.toggle(e);
+ setTimeout(() => this.getMenuItems()[0]?.focus());
+ } else if (isOpen && isTab) {
+ // Focus the first menu item if tabbing from an open menu. We need this
+ // for cases where the DropdownMenu sets a custom container, which may
+ // not be the natural next item to tab to from the DropdownToggle.
+ e.preventDefault();
+ this.getMenuItems()[0]?.focus();
+ } else if (isOpen && which === keyCodes.esc) {
+ this.toggle(e);
+ }
+ }
+
+ if (isOpen && isTargetMenuItem) {
+ if (which === keyCodes.tab || which === keyCodes.esc) {
+ this.toggle(e);
+ this.getMenuCtrl()?.focus();
+ } else if (which === keyCodes.space || which === keyCodes.enter) {
+ target.click();
+ this.getMenuCtrl()?.focus();
+ } else if (
+ which === keyCodes.down ||
+ which === keyCodes.up ||
+ ((which === keyCodes.n || which === keyCodes.p) && e.ctrlKey)
+ ) {
+ const items = this.getMenuItems();
+ let index = items.indexOf(target);
+ if (keyCodes.up === which || (keyCodes.p === which && e.ctrlKey)) {
+ index = index !== 0 ? index - 1 : items.length - 1;
+ } else {
+ index = index === items.length - 1 ? 0 : index + 1;
+ }
+ items[index].focus();
+ } else if (keyCodes.end === which) {
+ const items = this.getMenuItems();
+ items[items.length - 1].focus();
+ } else if (keyCodes.home === which) {
+ this.getMenuItems()[0].focus();
+ } else if (isAlphanumeric(which)) {
+ const charPressed = String.fromCharCode(which).toLowerCase();
+ this.getMenuItems()
+ .find((item) => item.textContent?.[0]?.toLowerCase() === charPressed)
+ ?.focus();
+ }
+ }
+ };
+
+ handleProps() {
+ if (this.props.isOpen) this.addEvents();
+ else this.removeEvents();
+ }
+
+ getContextValue(): DropdownContextValue {
+ const { direction = 'down', dropup } = this.props;
+ return {
+ toggle: this.toggle,
+ isOpen: this.props.isOpen,
+ direction: direction === 'down' && dropup ? 'up' : direction,
+ inNavbar: this.props.inNavbar,
+ disabled: this.props.disabled,
+ // Called by DropdownMenu with its DOM node
+ onMenuRef: this.handleMenuRef,
+ onToggleRef: this.handleToggleRef,
+ menuRole: this.props.menuRole,
+ };
+ }
+
+ getMenuCtrl() {
+ this.menuCtrl ??= this.toggleRef.current;
+ return this.menuCtrl;
+ }
+
+ getMenuItems(): HTMLElement[] {
+ // The menu should never be null with a child DropdownMenu, but it can be
+ // in tests; fall back to the container.
+ const menuContainer = this.menuRef.current || this.containerRef.current;
+ const itemRole = this.props.menuRole === 'listbox' ? 'option' : 'menuitem';
+ return Array.from(
+ menuContainer?.querySelectorAll(`[role="${itemRole}"]`) ??
+ [],
+ );
+ }
+
+ addEvents() {
+ ['click', 'touchstart', 'keyup'].forEach((event) =>
+ document.addEventListener(event, this.handleDocumentClick, true),
+ );
+ }
+
+ removeEvents() {
+ ['click', 'touchstart', 'keyup'].forEach((event) =>
+ document.removeEventListener(event, this.handleDocumentClick, true),
+ );
+ }
+
+ toggle = (e?: AnyEvent) => {
+ if (this.props.disabled) {
+ e?.preventDefault();
+ return;
+ }
+ this.props.toggle?.(e as never);
+ };
+
+ override render() {
+ const {
+ className,
+ cssModule,
+ direction = 'down',
+ isOpen,
+ group,
+ size,
+ nav = false,
+ setActiveFromChild = false,
+ active = false,
+ tag,
+ menuRole: _menuRole,
+ dropup: _dropup,
+ ...attrs
+ } = omit(this.props as DropdownImplProps, [
+ 'toggle',
+ 'disabled',
+ 'inNavbar',
+ 'a11y',
+ ]);
+
+ const Tag: ElementType = tag || (nav ? 'li' : 'div');
+
+ let subItemIsActive = false;
+ if (setActiveFromChild) {
+ const menu = (this.props.children as ReactElement[])[1];
+ Children.forEach(menu.props.children, (item) => {
+ if (isValidElement<{ active?: boolean }>(item) && item.props.active) {
+ subItemIsActive = true;
+ }
+ });
+ }
+
+ const classes = mapToCssModules(
+ classNames(
+ className,
+ nav && active ? 'active' : false,
+ setActiveFromChild && subItemIsActive ? 'active' : false,
+ {
+ 'btn-group': group,
+ [`btn-group-${size}`]: !!size,
+ dropdown: !group,
+ dropup: direction === 'up',
+ dropstart: direction === 'start' || direction === 'left',
+ dropend: direction === 'end' || direction === 'right',
+ show: isOpen,
+ 'nav-item': nav,
+ },
+ ),
+ cssModule,
+ );
+
+ if (this.context.insideInputGroup) {
+ return (
+
+
+ {Children.map(this.props.children, (child) =>
+ cloneElement(child as ReactElement, {
+ onKeyDown: this.handleKeyDown,
+ }),
+ )}
+
+
+ );
+ }
+
+ return (
+
+
+
+
+
+ );
+ }
+}
+
+export default Dropdown;
diff --git a/src/DropdownContext.js b/src/DropdownContext.js
deleted file mode 100644
index f740f2860..000000000
--- a/src/DropdownContext.js
+++ /dev/null
@@ -1,13 +0,0 @@
-import React from 'react';
-
-/**
- * DropdownContext
- * {
- * toggle: PropTypes.func.isRequired,
- * isOpen: PropTypes.bool.isRequired,
- * direction: PropTypes.oneOf(['up', 'down', 'start', 'end']).isRequired,
- * inNavbar: PropTypes.bool.isRequired,
- * disabled: PropTypes.bool
- * }
- */
-export const DropdownContext = React.createContext({});
diff --git a/src/DropdownContext.ts b/src/DropdownContext.ts
new file mode 100644
index 000000000..7c6b562fc
--- /dev/null
+++ b/src/DropdownContext.ts
@@ -0,0 +1,20 @@
+import { createContext, type SyntheticEvent } from 'react';
+
+export type DropdownDirection =
+ 'up' | 'down' | 'start' | 'end' | 'left' | 'right';
+
+export interface DropdownContextValue {
+ toggle: (e?: SyntheticEvent | Event) => void;
+ isOpen?: boolean;
+ direction: DropdownDirection;
+ inNavbar?: boolean;
+ disabled?: boolean;
+ /** DropdownMenu reports its DOM node, so outside clicks close the dropdown. */
+ onMenuRef?: (el: HTMLElement | null) => void;
+ onToggleRef?: (el: HTMLElement | null) => void;
+ menuRole?: 'listbox' | 'menu' | boolean;
+}
+
+export const DropdownContext = createContext(
+ {} as DropdownContextValue,
+);
diff --git a/src/DropdownItem.js b/src/DropdownItem.js
deleted file mode 100644
index 7e5f004ba..000000000
--- a/src/DropdownItem.js
+++ /dev/null
@@ -1,119 +0,0 @@
-import React from 'react';
-import PropTypes from 'prop-types';
-import classNames from 'classnames';
-import { DropdownContext } from './DropdownContext';
-import { mapToCssModules, omit, tagPropType } from './utils';
-
-const propTypes = {
- children: PropTypes.node,
- active: PropTypes.bool,
- disabled: PropTypes.bool,
- divider: PropTypes.bool,
- tag: tagPropType,
- header: PropTypes.bool,
- onClick: PropTypes.func,
- className: PropTypes.string,
- cssModule: PropTypes.object,
- toggle: PropTypes.bool,
- text: PropTypes.bool,
-};
-
-class DropdownItem extends React.Component {
- constructor(props) {
- super(props);
-
- this.onClick = this.onClick.bind(this);
- this.getTabIndex = this.getTabIndex.bind(this);
- }
-
- onClick(e) {
- const { disabled, header, divider, text } = this.props;
- if (disabled || header || divider || text) {
- e.preventDefault();
- return;
- }
-
- if (this.props.onClick) {
- this.props.onClick(e);
- }
-
- if (this.props.toggle ?? true) {
- this.context.toggle(e);
- }
- }
-
- getRole() {
- if (this.context.menuRole === 'listbox') {
- return 'option';
- }
- return 'menuitem';
- }
-
- getTabIndex() {
- const { disabled, header, divider, text } = this.props;
- if (disabled || header || divider || text) {
- return '-1';
- }
-
- return '0';
- }
-
- render() {
- const tabIndex = this.getTabIndex();
- const role = tabIndex > -1 ? this.getRole() : undefined;
- let {
- className,
- cssModule,
- divider,
- tag: Tag = 'button',
- header,
- active,
- text,
- ...props
- } = omit(this.props, ['toggle']);
-
- const classes = mapToCssModules(
- classNames(className, {
- disabled: props.disabled,
- 'dropdown-item': !divider && !header && !text,
- active: active,
- 'dropdown-header': header,
- 'dropdown-divider': divider,
- 'dropdown-item-text': text,
- }),
- cssModule,
- );
-
- if (Tag === 'button') {
- if (header) {
- Tag = 'h6';
- } else if (divider) {
- Tag = 'div';
- } else if (props.href) {
- Tag = 'a';
- } else if (text) {
- Tag = 'span';
- }
- }
-
- return (
-
- );
- }
-}
-
-DropdownItem.propTypes = propTypes;
-DropdownItem.contextType = DropdownContext;
-
-export default DropdownItem;
diff --git a/src/DropdownItem.tsx b/src/DropdownItem.tsx
new file mode 100644
index 000000000..5a15a4b84
--- /dev/null
+++ b/src/DropdownItem.tsx
@@ -0,0 +1,99 @@
+import {
+ useContext,
+ type ElementType,
+ type MouseEvent,
+ type MouseEventHandler,
+} from 'react';
+import classNames from 'classnames';
+import { DropdownContext } from './DropdownContext';
+import {
+ mapToCssModules,
+ type CSSModule,
+ type PolymorphicExtras,
+} from './utils';
+
+export interface DropdownItemOwnProps {
+ active?: boolean;
+ className?: string;
+ disabled?: boolean;
+ /** Render as `.dropdown-divider`. */
+ divider?: boolean;
+ /** Render as `h6.dropdown-header`. */
+ header?: boolean;
+ cssModule?: CSSModule;
+ /** Renders an `a` when the tag is the default. */
+ href?: string;
+ onClick?: MouseEventHandler;
+ /** Close the dropdown on click. @default true */
+ toggle?: boolean;
+ /** Render as `span.dropdown-item-text`. */
+ text?: boolean;
+}
+
+/** Extra props are typed by `tag` (default `button`; `h6`/`div`/`a`/`span` per `header`/`divider`/`href`/`text`). */
+export type DropdownItemProps =
+ DropdownItemOwnProps & PolymorphicExtras;
+
+function DropdownItem({
+ className,
+ cssModule,
+ divider,
+ tag,
+ header,
+ active,
+ text,
+ toggle,
+ onClick,
+ ...props
+}: DropdownItemProps) {
+ const context = useContext(DropdownContext);
+ const inert = props.disabled || header || divider || text;
+ const tabIndex = inert ? '-1' : '0';
+ const role = inert
+ ? undefined
+ : context.menuRole === 'listbox'
+ ? 'option'
+ : 'menuitem';
+
+ const handleClick = (e: MouseEvent) => {
+ if (inert) {
+ e.preventDefault();
+ return;
+ }
+ onClick?.(e);
+ if (toggle ?? true) context.toggle(e);
+ };
+
+ const classes = mapToCssModules(
+ classNames(className, {
+ disabled: props.disabled,
+ 'dropdown-item': !divider && !header && !text,
+ active,
+ 'dropdown-header': header,
+ 'dropdown-divider': divider,
+ 'dropdown-item-text': text,
+ }),
+ cssModule,
+ );
+
+ let Tag: ElementType = tag ?? 'button';
+ if (Tag === 'button') {
+ if (header) Tag = 'h6';
+ else if (divider) Tag = 'div';
+ else if (props.href) Tag = 'a';
+ else if (text) Tag = 'span';
+ }
+
+ return (
+
+ );
+}
+
+export default DropdownItem;
diff --git a/src/DropdownMenu.js b/src/DropdownMenu.js
deleted file mode 100644
index 6a3b3d704..000000000
--- a/src/DropdownMenu.js
+++ /dev/null
@@ -1,162 +0,0 @@
-import React from 'react';
-import PropTypes from 'prop-types';
-import ReactDOM from 'react-dom';
-import classNames from 'classnames';
-import { Popper } from 'react-popper';
-import { DropdownContext } from './DropdownContext';
-import {
- mapToCssModules,
- tagPropType,
- targetPropType,
- getTarget,
- deprecated,
-} from './utils';
-
-const propTypes = {
- tag: tagPropType,
- children: PropTypes.node.isRequired,
- dark: PropTypes.bool,
- end: PropTypes.bool,
- /** Flips the menu to the opposite side if there is not enough space to fit */
- flip: PropTypes.bool,
- modifiers: PropTypes.array,
- className: PropTypes.string,
- cssModule: PropTypes.object,
- style: PropTypes.object,
- persist: PropTypes.bool,
- strategy: PropTypes.string,
- container: targetPropType,
- /** Update popper layout when a click event comes up. This leverages event bubbling. */
- updateOnSelect: PropTypes.bool,
- right: deprecated(PropTypes.bool, 'Please use "end" instead.'),
-};
-
-const directionPositionMap = {
- up: 'top',
- left: 'left',
- right: 'right',
- start: 'left',
- end: 'right',
- down: 'bottom',
-};
-
-class DropdownMenu extends React.Component {
- getRole() {
- if (this.context.menuRole === 'listbox') {
- return 'listbox';
- }
- return 'menu';
- }
-
- render() {
- const {
- className,
- cssModule,
- dark,
- end,
- right,
- tag = 'div',
- flip = true,
- modifiers = [],
- persist,
- strategy,
- container,
- updateOnSelect,
- ...attrs
- } = this.props;
-
- const classes = mapToCssModules(
- classNames(className, 'dropdown-menu', {
- 'dropdown-menu-dark': dark,
- 'dropdown-menu-end': end || right,
- show: this.context.isOpen,
- }),
- cssModule,
- );
-
- const Tag = tag;
-
- if (persist || (this.context.isOpen && !this.context.inNavbar)) {
- const position1 =
- directionPositionMap[this.context.direction] || 'bottom';
- const position2 = end || right ? 'end' : 'start';
- const poperPlacement = `${position1}-${position2}`;
- const poperModifiers = [
- ...modifiers,
- {
- name: 'flip',
- enabled: !!flip,
- },
- ];
-
- const persistStyles = {};
- if (persist) {
- persistStyles.display = 'block';
- persistStyles.visibility = this.context.isOpen ? 'visible' : 'hidden';
- }
-
- const popper = (
-
- {({ ref, style, placement, update }) => {
- let combinedStyle = {
- ...this.props.style,
- ...persistStyles,
- ...style,
- };
-
- const handleRef = (tagRef) => {
- // Send the ref to `react-popper`
- ref(tagRef);
- // Send the ref to the parent Dropdown so that clicks outside
- // it will cause it to close
- const { onMenuRef } = this.context;
- if (onMenuRef) onMenuRef(tagRef);
- };
-
- return (
- updateOnSelect && update()}
- />
- );
- }}
-
- );
-
- if (container) {
- return ReactDOM.createPortal(popper, getTarget(container));
- }
- return popper;
- }
- const { onMenuRef } = this.context;
-
- return (
-
- );
- }
-}
-
-DropdownMenu.propTypes = propTypes;
-DropdownMenu.contextType = DropdownContext;
-
-export default DropdownMenu;
diff --git a/src/DropdownMenu.tsx b/src/DropdownMenu.tsx
new file mode 100644
index 000000000..df928afab
--- /dev/null
+++ b/src/DropdownMenu.tsx
@@ -0,0 +1,156 @@
+import {
+ Component,
+ type ContextType,
+ type CSSProperties,
+ type ElementType,
+ type HTMLAttributes,
+ type ReactNode,
+} from 'react';
+import { createPortal } from 'react-dom';
+import classNames from 'classnames';
+import type { Placement, PositioningStrategy } from '@popperjs/core';
+import { Popper } from './popper';
+import type { PopperModifier } from './PopperContent';
+import { DropdownContext, type DropdownDirection } from './DropdownContext';
+import {
+ getTarget,
+ mapToCssModules,
+ type CSSModule,
+ type PolymorphicClassExtras,
+ type Target,
+} from './utils';
+
+export interface DropdownMenuOwnProps {
+ children?: ReactNode;
+ className?: string;
+ /** Render as `.dropdown-menu-dark`. */
+ dark?: boolean;
+ /** Align the menu to the end of the toggle. */
+ end?: boolean;
+ /** Flips the menu to the opposite side if there is not enough space to fit */
+ flip?: boolean;
+ modifiers?: PopperModifier[];
+ cssModule?: CSSModule;
+ /** Keep the menu mounted (and positioned) while closed. */
+ persist?: boolean;
+ strategy?: PositioningStrategy;
+ /** Portal the menu into this element. */
+ container?: Target;
+ /** Update popper layout when a click event comes up. This leverages event bubbling. */
+ updateOnSelect?: boolean;
+}
+
+/** Extra props are typed by `tag` (default `div`). */
+export type DropdownMenuProps =
+ DropdownMenuOwnProps & PolymorphicClassExtras;
+
+const directionPositionMap: Record = {
+ up: 'top',
+ left: 'left',
+ right: 'right',
+ start: 'left',
+ end: 'right',
+ down: 'bottom',
+};
+
+class DropdownMenu extends Component<
+ DropdownMenuProps
+> {
+ // Not `Readonly`: lets JSX infer `T` from a string `tag` (else it widens to `ElementType`).
+ declare props: DropdownMenuProps;
+ static override contextType = DropdownContext;
+ declare context: ContextType;
+
+ getRole() {
+ return this.context.menuRole === 'listbox' ? 'listbox' : 'menu';
+ }
+
+ override render() {
+ const {
+ className,
+ cssModule,
+ dark,
+ end,
+ tag: Tag = 'div',
+ flip = true,
+ modifiers = [],
+ persist,
+ strategy,
+ container,
+ updateOnSelect,
+ ...attrs
+ } = this.props as DropdownMenuOwnProps & {
+ tag?: ElementType;
+ placement?: string;
+ } & HTMLAttributes;
+ const { isOpen, inNavbar, direction, onMenuRef } = this.context;
+
+ const classes = mapToCssModules(
+ classNames(className, 'dropdown-menu', {
+ 'dropdown-menu-dark': dark,
+ 'dropdown-menu-end': end,
+ show: isOpen,
+ }),
+ cssModule,
+ );
+
+ if (persist || (isOpen && !inNavbar)) {
+ const position1 = directionPositionMap[direction] || 'bottom';
+ const position2 = end ? 'end' : 'start';
+ const persistStyles: CSSProperties = persist
+ ? { display: 'block', visibility: isOpen ? 'visible' : 'hidden' }
+ : {};
+
+ const popper = (
+
+ {({ ref, style, placement, update }) => {
+ const handleRef = (el: HTMLElement | null) => {
+ // Send the ref to popper
+ ref(el);
+ // Send the ref to the parent Dropdown so that clicks outside
+ // it will cause it to close
+ onMenuRef?.(el);
+ };
+
+ return (
+ updateOnSelect && update()}
+ />
+ );
+ }}
+
+ );
+
+ return container
+ ? createPortal(popper, getTarget(container) as Element)
+ : popper;
+ }
+
+ return (
+
+ );
+ }
+}
+
+export default DropdownMenu;
diff --git a/src/DropdownToggle.js b/src/DropdownToggle.js
deleted file mode 100644
index f61ba7084..000000000
--- a/src/DropdownToggle.js
+++ /dev/null
@@ -1,137 +0,0 @@
-import React from 'react';
-import PropTypes from 'prop-types';
-import classNames from 'classnames';
-import { Reference } from 'react-popper';
-import { DropdownContext } from './DropdownContext';
-import { mapToCssModules, tagPropType } from './utils';
-import Button from './Button';
-
-const propTypes = {
- caret: PropTypes.bool,
- color: PropTypes.string,
- children: PropTypes.node,
- className: PropTypes.string,
- cssModule: PropTypes.object,
- disabled: PropTypes.bool,
- onClick: PropTypes.func,
- 'aria-haspopup': PropTypes.bool,
- split: PropTypes.bool,
- tag: tagPropType,
- nav: PropTypes.bool,
- innerRef: PropTypes.oneOfType([
- PropTypes.object,
- PropTypes.string,
- PropTypes.func,
- ]),
-};
-
-const defaultProps = {
- color: 'secondary',
- 'aria-haspopup': true,
-};
-
-class DropdownToggle extends React.Component {
- constructor(props) {
- super(props);
-
- this.onClick = this.onClick.bind(this);
- }
-
- onClick(e) {
- if (this.props.disabled || this.context.disabled) {
- e.preventDefault();
- return;
- }
-
- if (this.props.nav && !this.props.tag) {
- e.preventDefault();
- }
-
- if (this.props.onClick) {
- this.props.onClick(e);
- }
-
- this.context.toggle(e);
- }
-
- getRole() {
- return this.context.menuRole || this.props['aria-haspopup'];
- }
-
- render() {
- const {
- className,
- color,
- cssModule,
- caret,
- split,
- nav,
- tag,
- innerRef,
- ...props
- } = this.props;
- const ariaLabel = props['aria-label'] || 'Toggle Dropdown';
- const classes = mapToCssModules(
- classNames(className, {
- 'dropdown-toggle': caret || split,
- 'dropdown-toggle-split': split,
- 'nav-link': nav,
- }),
- cssModule,
- );
- const children =
- typeof props.children !== 'undefined' ? (
- props.children
- ) : (
- {ariaLabel}
- );
-
- let Tag;
-
- if (nav && !tag) {
- Tag = 'a';
- props.href = '#';
- } else if (!tag) {
- Tag = Button;
- props.color = color;
- props.cssModule = cssModule;
- } else {
- Tag = tag;
- }
-
- // extracted the rendering of the Tag component
- const returnFunction = ({ ref }) => {
- const handleRef = (tagRef) => {
- ref(tagRef);
- const { onToggleRef } = this.context;
- if (onToggleRef) onToggleRef(tagRef);
- };
-
- return (
-
- );
- };
-
- // No Reference component if the component is in Navbar
- if (this.context.inNavbar) {
- return <>{returnFunction({ ref: this.context.onToggleRef })}>;
- }
-
- // Normal rendering if component not in NavBar
- return {returnFunction};
- }
-}
-
-DropdownToggle.propTypes = propTypes;
-DropdownToggle.defaultProps = defaultProps;
-DropdownToggle.contextType = DropdownContext;
-
-export default DropdownToggle;
diff --git a/src/DropdownToggle.tsx b/src/DropdownToggle.tsx
new file mode 100644
index 000000000..c2d912ef2
--- /dev/null
+++ b/src/DropdownToggle.tsx
@@ -0,0 +1,145 @@
+import {
+ Component,
+ type ContextType,
+ type ElementType,
+ type HTMLAttributes,
+ type MouseEvent,
+ type MouseEventHandler,
+ type Ref,
+} from 'react';
+import classNames from 'classnames';
+import { Reference } from './popper';
+import { DropdownContext } from './DropdownContext';
+import {
+ mapToCssModules,
+ type CSSModule,
+ type PolymorphicClassExtras,
+} from './utils';
+import Button from './Button';
+
+export interface DropdownToggleOwnProps {
+ /** Render as `.dropdown-toggle` (with caret). */
+ caret?: boolean;
+ /** Button color, when rendered as the default `Button`. @default 'secondary' */
+ color?: string;
+ className?: string;
+ cssModule?: CSSModule;
+ disabled?: boolean;
+ /** Forwarded to `Button`. */
+ outline?: boolean;
+ /** Forwarded to `Button`. */
+ size?: string;
+ /** @default true */
+ 'aria-haspopup'?: boolean;
+ /** Render as `.dropdown-toggle-split`. */
+ split?: boolean;
+ /** Render as `a.nav-link`. */
+ nav?: boolean;
+ innerRef?: Ref;
+ onClick?: MouseEventHandler;
+}
+
+/** Default rendered component: `Button` (as a `button`), or `a` when `nav`. */
+type DefaultTag = typeof Button<'button'>;
+
+/** Extra props are typed by `tag` (default `Button`). */
+export type DropdownToggleProps =
+ DropdownToggleOwnProps & PolymorphicClassExtras;
+
+type DropdownToggleImplProps = DropdownToggleOwnProps & {
+ tag?: ElementType;
+} & HTMLAttributes;
+
+class DropdownToggle extends Component<
+ DropdownToggleProps
+> {
+ // Not `Readonly`: lets JSX infer `T` from a string `tag` (else it widens to `ElementType`).
+ declare props: DropdownToggleProps;
+ static override contextType = DropdownContext;
+ declare context: ContextType;
+
+ onClick = (e: MouseEvent) => {
+ if (this.props.disabled || this.context.disabled) {
+ e.preventDefault();
+ return;
+ }
+ if (this.props.nav && !this.props.tag) e.preventDefault();
+ this.props.onClick?.(e);
+ this.context.toggle(e);
+ };
+
+ getRole() {
+ return this.context.menuRole || (this.props['aria-haspopup'] ?? true);
+ }
+
+ override render() {
+ const {
+ className,
+ color = 'secondary',
+ cssModule,
+ caret,
+ split,
+ nav,
+ tag,
+ innerRef,
+ ...props
+ } = this.props as DropdownToggleImplProps;
+ const ariaLabel = props['aria-label'] || 'Toggle Dropdown';
+ const classes = mapToCssModules(
+ classNames(className, {
+ 'dropdown-toggle': caret || split,
+ 'dropdown-toggle-split': split,
+ 'nav-link': nav,
+ }),
+ cssModule,
+ );
+ const children =
+ props.children !== undefined ? (
+ props.children
+ ) : (
+ {ariaLabel}
+ );
+
+ let Tag: ElementType;
+ let extra = {};
+ if (nav && !tag) {
+ Tag = 'a';
+ extra = { href: '#' };
+ } else if (!tag) {
+ Tag = Button;
+ extra = { color, cssModule };
+ } else {
+ Tag = tag;
+ }
+
+ const renderTag = ({ ref }: { ref?: (el: HTMLElement | null) => void }) => {
+ const handleRef = (el: HTMLElement | null) => {
+ ref?.(el);
+ this.context.onToggleRef?.(el);
+ };
+
+ return (
+
+ {children}
+
+ );
+ };
+
+ // No Reference component if the component is in Navbar
+ if (this.context.inNavbar) {
+ return renderTag({ ref: this.context.onToggleRef });
+ }
+
+ return {renderTag};
+ }
+}
+
+export default DropdownToggle;
diff --git a/src/Fade.js b/src/Fade.js
deleted file mode 100644
index e38330c23..000000000
--- a/src/Fade.js
+++ /dev/null
@@ -1,83 +0,0 @@
-import React, { useRef } from 'react';
-import PropTypes from 'prop-types';
-import classNames from 'classnames';
-import { Transition } from 'react-transition-group';
-import {
- addDefaultProps,
- mapToCssModules,
- omit,
- pick,
- tagPropType,
- TransitionPropTypeKeys,
- TransitionTimeouts,
-} from './utils';
-
-const propTypes = {
- ...Transition.propTypes,
- children: PropTypes.oneOfType([
- PropTypes.arrayOf(PropTypes.node),
- PropTypes.node,
- ]),
- tag: tagPropType,
- baseClass: PropTypes.string,
- baseClassActive: PropTypes.string,
- className: PropTypes.string,
- cssModule: PropTypes.object,
- innerRef: PropTypes.oneOfType([
- PropTypes.object,
- PropTypes.string,
- PropTypes.func,
- ]),
-};
-
-const defaultProps = {
- ...Transition.defaultProps,
- timeout: TransitionTimeouts.Fade,
- appear: true,
- enter: true,
- exit: true,
- in: true,
-};
-
-function Fade(props) {
- const ref = useRef(null);
-
- const {
- tag: Tag = 'div',
- baseClass = 'fade',
- baseClassActive = 'show',
- className,
- cssModule,
- children,
- innerRef = ref,
- ...otherProps
- } = addDefaultProps(defaultProps, props);
-
- const transitionProps = pick(
- { defaultProps, ...otherProps },
- TransitionPropTypeKeys,
- );
-
- const childProps = omit(otherProps, TransitionPropTypeKeys);
-
- return (
-
- {(status) => {
- const isActive = status === 'entered';
- const classes = mapToCssModules(
- classNames(className, baseClass, isActive && baseClassActive),
- cssModule,
- );
- return (
-
- {children}
-
- );
- }}
-
- );
-}
-
-Fade.propTypes = propTypes;
-
-export default Fade;
diff --git a/src/Fade.tsx b/src/Fade.tsx
new file mode 100644
index 000000000..e4e8198d1
--- /dev/null
+++ b/src/Fade.tsx
@@ -0,0 +1,78 @@
+import type { ElementType, ReactNode, Ref } from 'react';
+import classNames from 'classnames';
+import { Transition, useNodeRef, type TransitionProps } from './Transition';
+import {
+ mapToCssModules,
+ omit,
+ pick,
+ TransitionPropTypeKeys,
+ TransitionTimeouts,
+ type CSSModule,
+ type PolymorphicExtras,
+} from './utils';
+
+export interface FadeOwnProps extends TransitionProps {
+ children?: ReactNode;
+ baseClass?: string;
+ baseClassActive?: string;
+ className?: string;
+ cssModule?: CSSModule;
+ innerRef?: Ref;
+}
+
+export type FadeProps = FadeOwnProps &
+ PolymorphicExtras;
+
+function Fade({
+ tag,
+ baseClass = 'fade',
+ baseClassActive = 'show',
+ className,
+ cssModule,
+ children,
+ innerRef,
+ ref: refProp,
+ timeout = TransitionTimeouts.Fade,
+ appear = true,
+ enter = true,
+ exit = true,
+ in: isIn = true,
+ ...otherProps
+}: FadeProps) {
+ const Tag: ElementType = tag ?? 'div';
+ const [nodeRef, ref] = useNodeRef(
+ innerRef ?? (refProp as Ref | undefined),
+ );
+
+ const transitionProps = {
+ ...pick(otherProps as TransitionProps, TransitionPropTypeKeys),
+ timeout,
+ appear,
+ enter,
+ exit,
+ in: isIn,
+ };
+ const childProps = omit(otherProps, TransitionPropTypeKeys);
+
+ return (
+
+ {(status) => {
+ const classes = mapToCssModules(
+ classNames(
+ className,
+ baseClass,
+ status === 'entered' && baseClassActive,
+ ),
+ cssModule,
+ );
+ return (
+
+ {children}
+
+ );
+ }}
+
+ );
+}
+
+export default Fade;
diff --git a/src/Form.js b/src/Form.js
deleted file mode 100644
index c2ba3be6e..000000000
--- a/src/Form.js
+++ /dev/null
@@ -1,53 +0,0 @@
-import React, { Component } from 'react';
-import PropTypes from 'prop-types';
-import { mapToCssModules, tagPropType } from './utils';
-
-const propTypes = {
- children: PropTypes.node,
- tag: tagPropType,
- innerRef: PropTypes.oneOfType([
- PropTypes.object,
- PropTypes.func,
- PropTypes.string,
- ]),
- className: PropTypes.string,
- cssModule: PropTypes.object,
-};
-
-class Form extends Component {
- constructor(props) {
- super(props);
- this.getRef = this.getRef.bind(this);
- this.submit = this.submit.bind(this);
- }
-
- getRef(ref) {
- if (this.props.innerRef) {
- this.props.innerRef(ref);
- }
- this.ref = ref;
- }
-
- submit() {
- if (this.ref) {
- this.ref.submit();
- }
- }
-
- render() {
- const {
- className,
- cssModule,
- tag: Tag = 'form',
- innerRef,
- ...attributes
- } = this.props;
-
- const classes = mapToCssModules(className, cssModule);
- return ;
- }
-}
-
-Form.propTypes = propTypes;
-
-export default Form;
diff --git a/src/Form.tsx b/src/Form.tsx
new file mode 100644
index 000000000..37a3315c6
--- /dev/null
+++ b/src/Form.tsx
@@ -0,0 +1,35 @@
+import type { ElementType, Ref } from 'react';
+import {
+ mapToCssModules,
+ type CSSModule,
+ type PolymorphicExtras,
+} from './utils';
+
+export interface FormOwnProps {
+ className?: string;
+ cssModule?: CSSModule;
+ innerRef?: Ref;
+}
+
+export type FormProps = FormOwnProps &
+ PolymorphicExtras;
+
+function Form({
+ className,
+ cssModule,
+ tag,
+ innerRef,
+ ref,
+ ...attributes
+}: FormProps) {
+ const Tag: ElementType = tag ?? 'form';
+ return (
+
+ );
+}
+
+export default Form;
diff --git a/src/FormFeedback.js b/src/FormFeedback.js
deleted file mode 100644
index a1d348285..000000000
--- a/src/FormFeedback.js
+++ /dev/null
@@ -1,40 +0,0 @@
-import React from 'react';
-import PropTypes from 'prop-types';
-import classNames from 'classnames';
-import { mapToCssModules, tagPropType } from './utils';
-
-const propTypes = {
- children: PropTypes.node,
- tag: tagPropType,
- className: PropTypes.string,
- cssModule: PropTypes.object,
- valid: PropTypes.bool,
- tooltip: PropTypes.bool,
-};
-
-function FormFeedback(props) {
- const {
- className,
- cssModule,
- valid = undefined,
- tooltip,
- tag: Tag = 'div',
- ...attributes
- } = props;
-
- const validMode = tooltip ? 'tooltip' : 'feedback';
-
- const classes = mapToCssModules(
- classNames(
- className,
- valid ? `valid-${validMode}` : `invalid-${validMode}`,
- ),
- cssModule,
- );
-
- return ;
-}
-
-FormFeedback.propTypes = propTypes;
-
-export default FormFeedback;
diff --git a/src/FormFeedback.tsx b/src/FormFeedback.tsx
new file mode 100644
index 000000000..6972f976c
--- /dev/null
+++ b/src/FormFeedback.tsx
@@ -0,0 +1,42 @@
+import type { ElementType } from 'react';
+import classNames from 'classnames';
+import {
+ mapToCssModules,
+ type CSSModule,
+ type PolymorphicExtras,
+} from './utils';
+
+export interface FormFeedbackOwnProps {
+ className?: string;
+ cssModule?: CSSModule;
+ /** Render as `valid-*` instead of `invalid-*` */
+ valid?: boolean;
+ /** Render as a tooltip instead of block feedback */
+ tooltip?: boolean;
+}
+
+export type FormFeedbackProps =
+ FormFeedbackOwnProps & PolymorphicExtras;
+
+function FormFeedback({
+ className,
+ cssModule,
+ valid,
+ tooltip,
+ tag,
+ ...attributes
+}: FormFeedbackProps) {
+ const Tag: ElementType = tag ?? 'div';
+ const validMode = tooltip ? 'tooltip' : 'feedback';
+ const classes = mapToCssModules(
+ classNames(
+ className,
+ valid ? `valid-${validMode}` : `invalid-${validMode}`,
+ ),
+ cssModule,
+ );
+
+ return ;
+}
+
+export default FormFeedback;
diff --git a/src/FormGroup.js b/src/FormGroup.js
deleted file mode 100644
index 0fb6a8c9b..000000000
--- a/src/FormGroup.js
+++ /dev/null
@@ -1,60 +0,0 @@
-import React from 'react';
-import PropTypes from 'prop-types';
-import classNames from 'classnames';
-import { mapToCssModules, tagPropType } from './utils';
-
-const propTypes = {
- children: PropTypes.node,
- row: PropTypes.bool,
- check: PropTypes.bool,
- switch: PropTypes.bool,
- inline: PropTypes.bool,
- floating: PropTypes.bool,
- noMargin: PropTypes.bool,
- disabled: PropTypes.bool,
- tag: tagPropType,
- className: PropTypes.string,
- cssModule: PropTypes.object,
-};
-
-function FormGroup(props) {
- const {
- className,
- cssModule,
- row,
- disabled,
- check,
- inline,
- floating,
- noMargin,
- tag: Tag = 'div',
- switch: switchProp,
- ...attributes
- } = props;
-
- const formCheck = check || switchProp;
-
- const classes = mapToCssModules(
- classNames(
- className,
- row ? 'row' : false,
- formCheck ? 'form-check' : false,
- switchProp ? 'form-switch' : false,
- formCheck || noMargin ? false : 'mb-3',
- formCheck && inline ? 'form-check-inline' : false,
- formCheck && disabled ? 'disabled' : false,
- floating && 'form-floating',
- ),
- cssModule,
- );
-
- if (Tag === 'fieldset') {
- attributes.disabled = disabled;
- }
-
- return ;
-}
-
-FormGroup.propTypes = propTypes;
-
-export default FormGroup;
diff --git a/src/FormGroup.tsx b/src/FormGroup.tsx
new file mode 100644
index 000000000..415694f3f
--- /dev/null
+++ b/src/FormGroup.tsx
@@ -0,0 +1,70 @@
+import type { ElementType } from 'react';
+import classNames from 'classnames';
+import {
+ mapToCssModules,
+ type CSSModule,
+ type PolymorphicExtras,
+} from './utils';
+
+export interface FormGroupOwnProps {
+ className?: string;
+ /** Lay out as a grid row */
+ row?: boolean;
+ /** Wrap a checkbox/radio (`.form-check`) */
+ check?: boolean;
+ /** Wrap a switch (`.form-check.form-switch`) */
+ switch?: boolean;
+ /** Inline check/switch */
+ inline?: boolean;
+ /** Floating label wrapper (`.form-floating`) */
+ floating?: boolean;
+ /** Omit the default `mb-3` margin */
+ noMargin?: boolean;
+ /** Adds `.disabled` to check/switch groups; sets `disabled` on a `fieldset` tag */
+ disabled?: boolean;
+ cssModule?: CSSModule;
+}
+
+export type FormGroupProps = FormGroupOwnProps &
+ PolymorphicExtras;
+
+function FormGroup({
+ className,
+ cssModule,
+ row,
+ disabled,
+ check,
+ inline,
+ floating,
+ noMargin,
+ tag,
+ switch: switchProp,
+ ...attributes
+}: FormGroupProps) {
+ const Tag: ElementType = tag ?? 'div';
+ const formCheck = check || switchProp;
+
+ const classes = mapToCssModules(
+ classNames(
+ className,
+ row && 'row',
+ formCheck && 'form-check',
+ switchProp && 'form-switch',
+ !(formCheck || noMargin) && 'mb-3',
+ formCheck && inline && 'form-check-inline',
+ formCheck && disabled && 'disabled',
+ floating && 'form-floating',
+ ),
+ cssModule,
+ );
+
+ return (
+
+ );
+}
+
+export default FormGroup;
diff --git a/src/FormText.js b/src/FormText.js
deleted file mode 100644
index 43ae9cc8e..000000000
--- a/src/FormText.js
+++ /dev/null
@@ -1,39 +0,0 @@
-import React from 'react';
-import PropTypes from 'prop-types';
-import classNames from 'classnames';
-import { mapToCssModules, tagPropType } from './utils';
-
-const propTypes = {
- children: PropTypes.node,
- inline: PropTypes.bool,
- tag: tagPropType,
- color: PropTypes.string,
- className: PropTypes.string,
- cssModule: PropTypes.object,
-};
-
-function FormText(props) {
- const {
- className,
- cssModule,
- inline,
- color = 'muted',
- tag: Tag = 'small',
- ...attributes
- } = props;
-
- const classes = mapToCssModules(
- classNames(
- className,
- !inline ? 'form-text' : false,
- color ? `text-${color}` : false,
- ),
- cssModule,
- );
-
- return ;
-}
-
-FormText.propTypes = propTypes;
-
-export default FormText;
diff --git a/src/FormText.tsx b/src/FormText.tsx
new file mode 100644
index 000000000..50161a136
--- /dev/null
+++ b/src/FormText.tsx
@@ -0,0 +1,38 @@
+import type { ElementType } from 'react';
+import classNames from 'classnames';
+import {
+ mapToCssModules,
+ type CSSModule,
+ type PolymorphicExtras,
+} from './utils';
+
+export interface FormTextOwnProps {
+ className?: string;
+ /** Omit the `.form-text` class */
+ inline?: boolean;
+ /** Text color (`text-${color}`); empty string for none */
+ color?: string;
+ cssModule?: CSSModule;
+}
+
+export type FormTextProps = FormTextOwnProps &
+ PolymorphicExtras;
+
+function FormText({
+ className,
+ cssModule,
+ inline,
+ color = 'muted',
+ tag,
+ ...attributes
+}: FormTextProps) {
+ const Tag: ElementType = tag ?? 'small';
+ const classes = mapToCssModules(
+ classNames(className, !inline && 'form-text', color && `text-${color}`),
+ cssModule,
+ );
+
+ return ;
+}
+
+export default FormText;
diff --git a/src/Input.js b/src/Input.js
deleted file mode 100644
index d15f25e22..000000000
--- a/src/Input.js
+++ /dev/null
@@ -1,142 +0,0 @@
-/* eslint react/prefer-stateless-function: 0 */
-
-import React from 'react';
-import PropTypes from 'prop-types';
-import classNames from 'classnames';
-import { mapToCssModules, warnOnce, tagPropType } from './utils';
-
-const propTypes = {
- children: PropTypes.node,
- type: PropTypes.string,
- size: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
- bsSize: PropTypes.string,
- valid: PropTypes.bool,
- invalid: PropTypes.bool,
- tag: tagPropType,
- innerRef: PropTypes.oneOfType([
- PropTypes.object,
- PropTypes.func,
- PropTypes.string,
- ]),
- plaintext: PropTypes.bool,
- addon: PropTypes.bool,
- className: PropTypes.string,
- cssModule: PropTypes.object,
-};
-
-class Input extends React.Component {
- constructor(props) {
- super(props);
- this.getRef = this.getRef.bind(this);
- this.focus = this.focus.bind(this);
- }
-
- getRef(ref) {
- if (this.props.innerRef) {
- this.props.innerRef(ref);
- }
- this.ref = ref;
- }
-
- focus() {
- if (this.ref) {
- this.ref.focus();
- }
- }
-
- render() {
- let {
- className,
- cssModule,
- type = 'text',
- bsSize,
- valid,
- invalid,
- tag,
- addon,
- plaintext,
- innerRef,
- ...attributes
- } = this.props;
-
- const checkInput = ['switch', 'radio', 'checkbox'].indexOf(type) > -1;
- const isNotaNumber = /\D/g;
-
- const textareaInput = type === 'textarea';
- const selectInput = type === 'select';
- const rangeInput = type === 'range';
- let Tag = tag || (selectInput || textareaInput ? type : 'input');
-
- let formControlClass = 'form-control';
-
- if (plaintext) {
- formControlClass = `${formControlClass}-plaintext`;
- Tag = tag || 'input';
- } else if (rangeInput) {
- formControlClass = 'form-range';
- } else if (selectInput) {
- formControlClass = 'form-select';
- } else if (checkInput) {
- if (addon) {
- formControlClass = null;
- } else {
- formControlClass = 'form-check-input';
- }
- }
-
- if (attributes.size && isNotaNumber.test(attributes.size)) {
- warnOnce(
- 'Please use the prop "bsSize" instead of the "size" to bootstrap\'s input sizing.',
- );
- bsSize = attributes.size;
- delete attributes.size;
- }
-
- const classes = mapToCssModules(
- classNames(
- className,
- invalid && 'is-invalid',
- valid && 'is-valid',
- bsSize
- ? selectInput
- ? `form-select-${bsSize}`
- : `form-control-${bsSize}`
- : false,
- formControlClass,
- ),
- cssModule,
- );
-
- if (Tag === 'input' || (tag && typeof tag === 'function')) {
- attributes.type = type === 'switch' ? 'checkbox' : type;
- }
-
- if (
- attributes.children &&
- !(
- plaintext ||
- type === 'select' ||
- typeof Tag !== 'string' ||
- Tag === 'select'
- )
- ) {
- warnOnce(
- `Input with a type of "${type}" cannot have children. Please use "value"/"defaultValue" instead.`,
- );
- delete attributes.children;
- }
-
- return (
-
- );
- }
-}
-
-Input.propTypes = propTypes;
-
-export default Input;
diff --git a/src/Input.tsx b/src/Input.tsx
new file mode 100644
index 000000000..8ecd20710
--- /dev/null
+++ b/src/Input.tsx
@@ -0,0 +1,182 @@
+import type {
+ ElementType,
+ InputHTMLAttributes,
+ ReactNode,
+ Ref,
+ SelectHTMLAttributes,
+ TextareaHTMLAttributes,
+} from 'react';
+import classNames from 'classnames';
+import {
+ mapToCssModules,
+ warnOnce,
+ type CSSModule,
+ type PolymorphicExtras,
+} from './utils';
+
+export type InputType =
+ | 'text'
+ | 'email'
+ | 'select'
+ | 'file'
+ | 'radio'
+ | 'checkbox'
+ | 'switch'
+ | 'textarea'
+ | 'button'
+ | 'reset'
+ | 'submit'
+ | 'date'
+ | 'datetime-local'
+ | 'hidden'
+ | 'image'
+ | 'month'
+ | 'number'
+ | 'range'
+ | 'search'
+ | 'tel'
+ | 'url'
+ | 'week'
+ | 'password'
+ | 'datetime'
+ | 'time'
+ | 'color';
+
+export interface InputOwnProps {
+ className?: string;
+ children?: ReactNode;
+ /** Bootstrap sizing (`form-control-*` / `form-select-*`) */
+ bsSize?: 'lg' | 'sm';
+ /** Adds `.is-valid` */
+ valid?: boolean;
+ /** Adds `.is-invalid` and `aria-invalid` */
+ invalid?: boolean;
+ innerRef?: Ref;
+ /** Render as read-only plain text (`.form-control-plaintext`) */
+ plaintext?: boolean;
+ /** Checkbox/radio inside an input group: omit `.form-check-input` */
+ addon?: boolean;
+ cssModule?: CSSModule;
+}
+
+/** Element rendered for a given `type` when no `tag` is set. */
+export type InputElement = K extends 'textarea'
+ ? 'textarea'
+ : K extends 'select'
+ ? 'select'
+ : 'input';
+
+type ElementOf = K extends 'textarea'
+ ? HTMLTextAreaElement
+ : K extends 'select'
+ ? HTMLSelectElement
+ : HTMLInputElement;
+
+/** Attributes when `type` isn't one literal (e.g. `isLong ? 'textarea' : 'text'`): all, typed on the element union. */
+type MixedControlAttributes = InputHTMLAttributes &
+ TextareaHTMLAttributes &
+ SelectHTMLAttributes & { ref?: Ref };
+
+type IsUnion = T extends unknown
+ ? [U] extends [T]
+ ? false
+ : true
+ : never;
+
+type InputTypeProps = InputOwnProps & {
+ /** Input type; `select`/`textarea` render those elements, `switch` renders a checkbox */
+ type?: K;
+};
+
+/**
+ * HTML attributes follow the rendered element: `type="textarea"` → `