Skip to content

Commit

Permalink
fix(types): fix types for event key
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatCanadianGuy committed Apr 15, 2021
1 parent 4e86493 commit 700e944
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/AbstractNavItem.tsx
Expand Up @@ -7,14 +7,15 @@ import warning from 'warning';
import NavContext from './NavContext';
import SelectableContext, { makeEventKey } from './SelectableContext';
import { BsPrefixRefForwardingComponent } from './helpers';
import { EventKey } from './types';

// TODO: check this
interface AbstractNavItemProps {
active?: boolean;
as: React.ElementType;
className?: string;
disabled?: boolean;
eventKey?: any; // TODO: especially fix this
eventKey?: EventKey;
href?: string;
role?: string;
id?: string;
Expand Down
9 changes: 5 additions & 4 deletions src/DropdownItem.tsx
Expand Up @@ -12,11 +12,12 @@ import {
BsPrefixRefForwardingComponent,
SelectCallback,
} from './helpers';
import { EventKey } from './types';

export interface DropdownItemProps extends BsPrefixPropsWithChildren {
active?: boolean;
disabled?: boolean;
eventKey?: string;
eventKey?: EventKey;
href?: string;
onClick?: React.MouseEventHandler<this>;
onSelect?: SelectCallback;
Expand Down Expand Up @@ -44,7 +45,7 @@ const propTypes = {
/**
* Value passed to the `onSelect` handler, useful for identifying the selected menu item.
*/
eventKey: PropTypes.any,
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/**
* HTML `href` attribute corresponding to `a.href`.
Expand Down Expand Up @@ -95,8 +96,8 @@ const DropdownItem: DropdownItem = React.forwardRef(
const navContext = useContext(NavContext);

const { activeKey } = navContext || {};
// TODO: Restrict eventKey to string in v5?
const key = makeEventKey(eventKey as any, href);

const key = makeEventKey(eventKey, href);

const active =
propActive == null && key != null
Expand Down
1 change: 0 additions & 1 deletion src/DropdownToggle.tsx
Expand Up @@ -17,7 +17,6 @@ export interface DropdownToggleProps
ButtonProps {
split?: boolean;
childBsPrefix?: string;
eventKey?: any; // TODO: fix this type
}

type DropdownToggle = BsPrefixRefForwardingComponent<
Expand Down
5 changes: 3 additions & 2 deletions src/ListGroup.tsx
Expand Up @@ -13,12 +13,13 @@ import {
BsPrefixRefForwardingComponent,
SelectCallback,
} from './helpers';
import { EventKey } from './types';

export interface ListGroupProps extends BsPrefixProps {
variant?: 'flush';
horizontal?: boolean | 'sm' | 'md' | 'lg' | 'xl';
activeKey?: unknown;
defaultActiveKey?: unknown;
activeKey?: EventKey;
defaultActiveKey?: EventKey;
onSelect?: SelectCallback;
}

Expand Down
10 changes: 3 additions & 7 deletions src/ListGroupItem.tsx
Expand Up @@ -3,18 +3,17 @@ import React, { useCallback } from 'react';
import PropTypes from 'prop-types';

import AbstractNavItem from './AbstractNavItem';
import { makeEventKey } from './SelectableContext';
import { useBootstrapPrefix } from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
import { Variant } from './types';
import { EventKey, Variant } from './types';

export interface ListGroupItemProps
extends Omit<React.HTMLAttributes<HTMLElement>, 'onSelect'>,
BsPrefixProps {
action?: boolean;
active?: boolean;
disabled?: boolean;
eventKey?: string;
eventKey?: EventKey;
href?: string;
onClick?: React.MouseEventHandler;
variant?: Variant;
Expand Down Expand Up @@ -48,7 +47,7 @@ const propTypes = {
*/
disabled: PropTypes.bool,

eventKey: PropTypes.string,
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

onClick: PropTypes.func,

Expand Down Expand Up @@ -79,7 +78,6 @@ const ListGroupItem: ListGroupItem = React.forwardRef(
variant,
action,
as,
eventKey,
onClick,
...props
},
Expand Down Expand Up @@ -109,8 +107,6 @@ const ListGroupItem: ListGroupItem = React.forwardRef(
<AbstractNavItem
ref={ref}
{...props}
// TODO: Restrict eventKey to string in v5?
eventKey={makeEventKey(eventKey as any, props.href)}
// eslint-disable-next-line no-nested-ternary
as={as || (action ? (props.href ? 'a' : 'button') : 'div')}
onClick={handleClick}
Expand Down
7 changes: 4 additions & 3 deletions src/Nav.tsx
Expand Up @@ -16,13 +16,14 @@ import {
BsPrefixRefForwardingComponent,
SelectCallback,
} from './helpers';
import { EventKey } from './types';

export interface NavProps extends BsPrefixPropsWithChildren {
navbarBsPrefix?: string;
cardHeaderBsPrefix?: string;
variant?: 'tabs' | 'pills';
activeKey?: unknown;
defaultActiveKey?: unknown;
activeKey?: EventKey;
defaultActiveKey?: EventKey;
fill?: boolean;
justify?: boolean;
onSelect?: SelectCallback;
Expand Down Expand Up @@ -59,7 +60,7 @@ const propTypes = {
*
* @type {string}
*/
activeKey: PropTypes.any,
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/**
* Have all `NavItem`s proportionately fill all available width.
Expand Down
5 changes: 3 additions & 2 deletions src/NavLink.tsx
Expand Up @@ -11,14 +11,15 @@ import {
BsPrefixRefForwardingComponent,
SelectCallback,
} from './helpers';
import { EventKey } from './types';

export interface NavLinkProps extends BsPrefixPropsWithChildren {
active?: boolean;
disabled?: boolean;
role?: string;
href?: string;
onSelect?: SelectCallback;
eventKey?: unknown;
eventKey?: EventKey;
}

type NavLink = BsPrefixRefForwardingComponent<'a', NavLinkProps>;
Expand Down Expand Up @@ -60,7 +61,7 @@ const propTypes = {
* Uniquely idenifies the `NavItem` amongst its siblings,
* used to determine and control the active state of the parent `Nav`
*/
eventKey: PropTypes.any,
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/** @default 'a' */
as: PropTypes.elementType,
Expand Down
2 changes: 1 addition & 1 deletion src/SelectableContext.tsx
Expand Up @@ -8,7 +8,7 @@ const SelectableContext = React.createContext<SelectableContextType | null>(
);

export const makeEventKey = (
eventKey: string | null,
eventKey?: string | number | null,
href: string | null = null,
): string | null => {
if (eventKey != null) return String(eventKey);
Expand Down
4 changes: 3 additions & 1 deletion src/Tab.tsx
Expand Up @@ -4,9 +4,10 @@ import React from 'react';
import TabContainer from './TabContainer';
import TabContent from './TabContent';
import TabPane from './TabPane';
import { EventKey } from './types';

export interface TabProps extends React.ComponentPropsWithRef<typeof TabPane> {
eventKey?: string;
eventKey?: EventKey;
title: React.ReactNode;
disabled?: boolean;
tabClassName?: string;
Expand All @@ -16,6 +17,7 @@ export interface TabProps extends React.ComponentPropsWithRef<typeof TabPane> {
class Tab extends React.Component<TabProps> {
static propTypes = {
title: PropTypes.node.isRequired,
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};

public static Container = TabContainer;
Expand Down
15 changes: 8 additions & 7 deletions src/TabContainer.tsx
Expand Up @@ -5,16 +5,17 @@ import { useUncontrolled } from 'uncontrollable';
import TabContext, { TabContextType } from './TabContext';
import SelectableContext from './SelectableContext';
import { SelectCallback, TransitionType } from './helpers';
import { EventKey } from './types';

export interface TabContainerProps extends React.PropsWithChildren<unknown> {
id?: string;
transition?: TransitionType;
mountOnEnter?: boolean;
unmountOnExit?: boolean;
generateChildId?: (eventKey: unknown, type: 'tab' | 'pane') => string;
generateChildId?: (eventKey: EventKey, type: 'tab' | 'pane') => string;
onSelect?: SelectCallback;
activeKey?: unknown;
defaultActiveKey?: unknown;
activeKey?: EventKey;
defaultActiveKey?: EventKey;
}

const validateId: Validator<string> = (props, ...args) => {
Expand Down Expand Up @@ -92,7 +93,7 @@ const propTypes = {
*
* @controllable onSelect
*/
activeKey: PropTypes.any,
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};

const TabContainer = (props: TabContainerProps) => {
Expand All @@ -110,7 +111,7 @@ const TabContainer = (props: TabContainerProps) => {
const generateChildId = useMemo(
() =>
generateCustomChildId ||
((key, type) => (id ? `${id}-${type}-${key}` : null)),
((key: EventKey, type: string) => (id ? `${id}-${type}-${key}` : null)),
[id, generateCustomChildId],
);

Expand All @@ -121,8 +122,8 @@ const TabContainer = (props: TabContainerProps) => {
transition,
mountOnEnter: mountOnEnter || false,
unmountOnExit: unmountOnExit || false,
getControlledId: (key) => generateChildId(key, 'tabpane'),
getControllerId: (key) => generateChildId(key, 'tab'),
getControlledId: (key: EventKey) => generateChildId(key, 'tabpane'),
getControllerId: (key: EventKey) => generateChildId(key, 'tab'),
}),
[
onSelect,
Expand Down
5 changes: 3 additions & 2 deletions src/TabPane.tsx
Expand Up @@ -12,9 +12,10 @@ import {
TransitionCallbacks,
TransitionType,
} from './helpers';
import { EventKey } from './types';

export interface TabPaneProps extends TransitionCallbacks, BsPrefixProps {
eventKey?: any;
eventKey?: EventKey;
active?: boolean;
transition?: TransitionType;
mountOnEnter?: boolean;
Expand All @@ -34,7 +35,7 @@ const propTypes = {
/**
* A key that associates the `TabPane` with it's controlling `NavLink`.
*/
eventKey: PropTypes.any,
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/**
* Toggles the active state of the TabPane, this is generally controlled by a
Expand Down
9 changes: 5 additions & 4 deletions src/Tabs.tsx
Expand Up @@ -13,10 +13,11 @@ import TabPane from './TabPane';

import { forEach, map } from './ElementChildren';
import { SelectCallback, TransitionType } from './helpers';
import { EventKey } from './types';

export interface TabsProps extends React.PropsWithChildren<unknown> {
activeKey?: unknown;
defaultActiveKey?: unknown;
activeKey?: EventKey;
defaultActiveKey?: EventKey;
onSelect?: SelectCallback;
variant?: 'tabs' | 'pills';
transition?: TransitionType;
Expand All @@ -31,9 +32,9 @@ const propTypes = {
*
* @controllable onSelect
*/
activeKey: PropTypes.any,
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/** The default active key that is selected on start */
defaultActiveKey: PropTypes.any,
defaultActiveKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/**
* Navigation style
Expand Down
2 changes: 2 additions & 0 deletions src/types.tsx
Expand Up @@ -30,3 +30,5 @@ export type Color =
| 'light'
| 'white'
| 'muted';

export type EventKey = string | number;

0 comments on commit 700e944

Please sign in to comment.