Skip to content

Commit aafbdbf

Browse files
authored
refactor(types): standardize and cleanup types (react-bootstrap#5672)
1 parent 907c0b1 commit aafbdbf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+697
-937
lines changed

src/AbstractNav.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const propTypes = {
3131
};
3232

3333
// TODO: is this correct?
34-
interface AbstractNavProps {
34+
interface AbstractNavProps extends React.HTMLAttributes<HTMLElement> {
3535
activeKey?: any;
3636
as?: React.ElementType;
3737
getControlledId?: any;
@@ -42,9 +42,10 @@ interface AbstractNavProps {
4242
role?: string;
4343
}
4444

45-
type AbstractNav = BsPrefixRefForwardingComponent<'ul', AbstractNavProps>;
46-
47-
const AbstractNav: AbstractNav = React.forwardRef(
45+
const AbstractNav: BsPrefixRefForwardingComponent<
46+
'ul',
47+
AbstractNavProps
48+
> = React.forwardRef<HTMLElement, AbstractNavProps>(
4849
(
4950
{
5051
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
@@ -54,7 +55,7 @@ const AbstractNav: AbstractNav = React.forwardRef(
5455
role,
5556
onKeyDown,
5657
...props
57-
}: AbstractNavProps,
58+
},
5859
ref,
5960
) => {
6061
// A ref and forceUpdate for refocus, b/c we only want to trigger when needed

src/AbstractNavItem.tsx

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,17 @@ import SelectableContext, { makeEventKey } from './SelectableContext';
99
import { BsPrefixRefForwardingComponent } from './helpers';
1010

1111
// TODO: check this
12-
interface AbstractNavItemProps {
12+
export interface AbstractNavItemProps
13+
extends Omit<React.HTMLAttributes<HTMLElement>, 'onSelect'> {
1314
active?: boolean;
1415
as: React.ElementType;
15-
className?: string;
1616
disabled?: boolean;
1717
eventKey?: any; // TODO: especially fix this
1818
href?: string;
19-
role?: string;
20-
id?: string;
2119
tabIndex?: number;
22-
onClick?: (e: any) => void;
2320
onSelect?: (navKey: string, e: any) => void;
2421
}
2522

26-
type AbstractNavItem = BsPrefixRefForwardingComponent<
27-
'div',
28-
AbstractNavItemProps
29-
>;
30-
3123
const propTypes = {
3224
id: PropTypes.string,
3325
active: PropTypes.bool,
@@ -49,17 +41,12 @@ const defaultProps = {
4941
disabled: false,
5042
};
5143

52-
const AbstractNavItem: AbstractNavItem = React.forwardRef(
44+
const AbstractNavItem: BsPrefixRefForwardingComponent<
45+
'div',
46+
AbstractNavItemProps
47+
> = React.forwardRef<HTMLElement, AbstractNavItemProps>(
5348
(
54-
{
55-
active,
56-
className,
57-
eventKey,
58-
onSelect,
59-
onClick,
60-
as: Component,
61-
...props
62-
}: AbstractNavItemProps,
49+
{ active, className, eventKey, onSelect, onClick, as: Component, ...props },
6350
ref,
6451
) => {
6552
const navKey = makeEventKey(eventKey, props.href);

src/Accordion.tsx

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ export interface AccordionProps
2424
flush?: boolean;
2525
}
2626

27-
type Accordion = BsPrefixRefForwardingComponent<'div', AccordionProps> & {
28-
Button: typeof AccordionButton;
29-
Collapse: typeof AccordionCollapse;
30-
Item: typeof AccordionItem;
31-
Header: typeof AccordionHeader;
32-
Body: typeof AccordionBody;
33-
};
34-
3527
const propTypes = {
3628
/** Set a custom element for this component */
3729
as: PropTypes.elementType,
@@ -45,13 +37,14 @@ const propTypes = {
4537
/** The default active key that is expanded on start */
4638
defaultActiveKey: PropTypes.string,
4739

48-
/**
49-
* Renders accordion edge-to-edge with its parent container
50-
*/
40+
/** Renders accordion edge-to-edge with its parent container */
5141
flush: PropTypes.bool,
5242
};
5343

54-
const Accordion = (React.forwardRef((props: AccordionProps, ref) => {
44+
const Accordion: BsPrefixRefForwardingComponent<
45+
'div',
46+
AccordionProps
47+
> = React.forwardRef<HTMLElement, AccordionProps>((props, ref) => {
5548
const {
5649
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
5750
as: Component = 'div',
@@ -86,14 +79,15 @@ const Accordion = (React.forwardRef((props: AccordionProps, ref) => {
8679
</Component>
8780
</AccordionContext.Provider>
8881
);
89-
}) as unknown) as Accordion;
82+
});
9083

9184
Accordion.displayName = 'Accordion';
9285
Accordion.propTypes = propTypes;
93-
Accordion.Button = AccordionButton;
94-
Accordion.Collapse = AccordionCollapse;
95-
Accordion.Item = AccordionItem;
96-
Accordion.Header = AccordionHeader;
97-
Accordion.Body = AccordionBody;
9886

99-
export default Accordion;
87+
export default Object.assign(Accordion, {
88+
Button: AccordionButton,
89+
Collapse: AccordionCollapse,
90+
Item: AccordionItem,
91+
Header: AccordionHeader,
92+
Body: AccordionBody,
93+
});

src/AccordionBody.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export interface AccordionBodyProps
1313
extends BsPrefixPropsWithChildren,
1414
React.HTMLAttributes<HTMLElement> {}
1515

16-
type AccordionBody = BsPrefixRefForwardingComponent<'div', AccordionBodyProps>;
17-
1816
const propTypes = {
1917
/** Set a custom element for this component */
2018
as: PropTypes.elementType,
@@ -23,15 +21,18 @@ const propTypes = {
2321
bsPrefix: PropTypes.string,
2422
};
2523

26-
const AccordionBody: AccordionBody = React.forwardRef(
24+
const AccordionBody: BsPrefixRefForwardingComponent<
25+
'div',
26+
AccordionBodyProps
27+
> = React.forwardRef<HTMLElement, AccordionBodyProps>(
2728
(
2829
{
2930
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
3031
as: Component = 'div',
3132
bsPrefix,
3233
className,
3334
...props
34-
}: AccordionBodyProps,
35+
},
3536
ref,
3637
) => {
3738
bsPrefix = useBootstrapPrefix(bsPrefix, 'accordion-body');

src/AccordionButton.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ export interface AccordionButtonProps
1515
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
1616
BsPrefixPropsWithChildren {}
1717

18-
type AccordionButton = BsPrefixRefForwardingComponent<
19-
'div',
20-
AccordionButtonProps
21-
>;
22-
2318
const propTypes = {
2419
/** Set a custom element for this component */
2520
as: PropTypes.elementType,
@@ -49,7 +44,10 @@ export function useAccordionButton(
4944
};
5045
}
5146

52-
const AccordionButton: AccordionButton = React.forwardRef(
47+
const AccordionButton: BsPrefixRefForwardingComponent<
48+
'div',
49+
AccordionButtonProps
50+
> = React.forwardRef<HTMLButtonElement, AccordionButtonProps>(
5351
(
5452
{
5553
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
@@ -59,7 +57,7 @@ const AccordionButton: AccordionButton = React.forwardRef(
5957
children,
6058
onClick,
6159
...props
62-
}: AccordionButtonProps,
60+
},
6361
ref,
6462
) => {
6563
bsPrefix = useBootstrapPrefix(bsPrefix, 'accordion-button');

src/AccordionCollapse.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ export interface AccordionCollapseProps
1515
eventKey: string;
1616
}
1717

18-
type AccordionCollapse = BsPrefixRefForwardingComponent<
19-
'div',
20-
AccordionCollapseProps
21-
>;
22-
2318
const propTypes = {
2419
/**
2520
* A key that corresponds to the toggler that triggers this collapse's expand or collapse.
@@ -30,17 +25,11 @@ const propTypes = {
3025
children: PropTypes.element.isRequired,
3126
};
3227

33-
const AccordionCollapse: AccordionCollapse = React.forwardRef<typeof Collapse>(
34-
(
35-
{
36-
bsPrefix,
37-
className,
38-
children,
39-
eventKey,
40-
...props
41-
}: AccordionCollapseProps,
42-
ref,
43-
) => {
28+
const AccordionCollapse: BsPrefixRefForwardingComponent<
29+
'div',
30+
AccordionCollapseProps
31+
> = React.forwardRef<typeof Collapse, AccordionCollapseProps>(
32+
({ bsPrefix, className, children, eventKey, ...props }, ref) => {
4433
const { activeEventKey } = useContext(AccordionContext);
4534
bsPrefix = useBootstrapPrefix(bsPrefix, 'accordion-collapse');
4635

src/AccordionHeader.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ export interface AccordionHeaderProps
1212
extends BsPrefixPropsWithChildren,
1313
React.HTMLAttributes<HTMLElement> {}
1414

15-
type AccordionHeader = BsPrefixRefForwardingComponent<
16-
'h2',
17-
AccordionHeaderProps
18-
>;
19-
2015
const propTypes = {
2116
/** Set a custom element for this component */
2217
as: PropTypes.elementType,
@@ -28,7 +23,10 @@ const propTypes = {
2823
onClick: PropTypes.func,
2924
};
3025

31-
const AccordionHeader: AccordionHeader = React.forwardRef(
26+
const AccordionHeader: BsPrefixRefForwardingComponent<
27+
'h2',
28+
AccordionHeaderProps
29+
> = React.forwardRef<HTMLElement, AccordionHeaderProps>(
3230
(
3331
{
3432
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
@@ -38,7 +36,7 @@ const AccordionHeader: AccordionHeader = React.forwardRef(
3836
children,
3937
onClick,
4038
...props
41-
}: AccordionHeaderProps,
39+
},
4240
ref,
4341
) => {
4442
bsPrefix = useBootstrapPrefix(bsPrefix, 'accordion-header');

src/AccordionItem.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export interface AccordionItemProps
1616
eventKey: string;
1717
}
1818

19-
type AccordionItem = BsPrefixRefForwardingComponent<'div', AccordionItemProps>;
20-
2119
const propTypes = {
2220
/** Set a custom element for this component */
2321
as: PropTypes.elementType,
@@ -32,7 +30,10 @@ const propTypes = {
3230
eventKey: PropTypes.string.isRequired,
3331
};
3432

35-
const AccordionItem: AccordionItem = React.forwardRef(
33+
const AccordionItem: BsPrefixRefForwardingComponent<
34+
'div',
35+
AccordionItemProps
36+
> = React.forwardRef<HTMLElement, AccordionItemProps>(
3637
(
3738
{
3839
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
@@ -41,7 +42,7 @@ const AccordionItem: AccordionItem = React.forwardRef(
4142
className,
4243
eventKey,
4344
...props
44-
}: AccordionItemProps,
45+
},
4546
ref,
4647
) => {
4748
bsPrefix = useBootstrapPrefix(bsPrefix, 'accordion-item');

src/Alert.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import createWithBsPrefix from './createWithBsPrefix';
1313
import SafeAnchor from './SafeAnchor';
1414
import { TransitionType } from './helpers';
1515

16-
export interface AlertProps extends React.HTMLProps<HTMLDivElement> {
16+
export interface AlertProps extends React.HTMLAttributes<HTMLDivElement> {
1717
bsPrefix?: string;
1818
variant?: Variant;
1919
dismissible?: boolean;
@@ -35,11 +35,6 @@ const AlertLink = createWithBsPrefix('alert-link', {
3535
Component: SafeAnchor,
3636
});
3737

38-
type Alert = React.ForwardRefExoticComponent<AlertProps> & {
39-
Link: typeof AlertLink;
40-
Heading: typeof AlertHeading;
41-
};
42-
4338
const propTypes = {
4439
/**
4540
* @default 'alert'
@@ -97,7 +92,7 @@ const defaultProps = {
9792
closeLabel: 'Close alert',
9893
};
9994

100-
const Alert = (React.forwardRef<HTMLDivElement, AlertProps>(
95+
const Alert = React.forwardRef<HTMLDivElement, AlertProps>(
10196
(uncontrolledProps: AlertProps, ref) => {
10297
const {
10398
bsPrefix,
@@ -153,12 +148,13 @@ const Alert = (React.forwardRef<HTMLDivElement, AlertProps>(
153148
</Transition>
154149
);
155150
},
156-
) as unknown) as Alert;
151+
);
157152

158153
Alert.displayName = 'Alert';
159-
Alert.defaultProps = defaultProps as any;
154+
Alert.defaultProps = defaultProps;
160155
Alert.propTypes = propTypes;
161-
Alert.Link = AlertLink;
162-
Alert.Heading = AlertHeading;
163156

164-
export default Alert;
157+
export default Object.assign(Alert, {
158+
Link: AlertLink,
159+
Heading: AlertHeading,
160+
});

0 commit comments

Comments
 (0)