Skip to content

Commit

Permalink
fix: simplify badge children type
Browse files Browse the repository at this point in the history
  • Loading branch information
shleewhite committed Jan 25, 2022
1 parent c25e29a commit 59e8e78
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 14 deletions.
4 changes: 1 addition & 3 deletions packages/paste-core/components/badge/src/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as React from 'react';
import {useUIDSeed} from '@twilio-paste/uid-library';

import type {BadgeChildren} from './types';

const DEFAULT_ICON_SIZE = 'sizeIcon10';

export const useResizeChildIcons = (children: BadgeChildren): BadgeChildren => {
export const useResizeChildIcons = (children: React.ReactNode): React.ReactNode => {
const seed = useUIDSeed();

if (!Array.isArray(children)) {
Expand Down
9 changes: 2 additions & 7 deletions packages/paste-core/components/badge/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import * as PropTypes from 'prop-types';
import {Box} from '@twilio-paste/box';

import type {BadgeProps, BadgeVariants, BadgeChildren} from './types';
import type {BadgeProps, BadgeVariants} from './types';
import {useResizeChildIcons} from './hooks';
import {badgeFocusableStyles, badgeVariantStyles} from './styles';
import {getBadgeSpanProps, isFocusableElement} from './utils';
Expand Down Expand Up @@ -51,12 +51,7 @@ export const Badge = React.forwardRef<HTMLElement, BadgeProps>(
Badge.displayName = 'Badge';

Badge.propTypes = {
children: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
PropTypes.element,
PropTypes.arrayOf(PropTypes.element),
]).isRequired as PropTypes.Validator<BadgeChildren>,
children: PropTypes.node.isRequired,
element: PropTypes.string,
variant: PropTypes.oneOf(['info', 'default', 'warning', 'error', 'success', 'new'] as BadgeVariants[]).isRequired,
// @ts-expect-error type unions are a little too much for prop types inferred types to handle
Expand Down
4 changes: 1 addition & 3 deletions packages/paste-core/components/badge/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type {ButtonProps} from '@twilio-paste/button';
import type {BoxProps} from '@twilio-paste/box';

export type NamedChild = React.ReactElement<Record<string, any>, React.NamedExoticComponent>;
export type BadgeChildren = NamedChild | React.ReactText | (React.ReactText | NamedChild)[];
export type BadgeVariants = 'info' | 'default' | 'warning' | 'error' | 'success' | 'new';

type BadgeBaseProps = Pick<BoxProps, 'element'> & {
children: BadgeChildren;
children: NonNullable<React.ReactNode>;
variant: BadgeVariants;
};
type BadgeAsSpanProps = React.HTMLAttributes<HTMLSpanElement> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ const BadgeExample = () => (

| Prop | Type | Description | Default |
| -------- | -------------------------------------- | ------------------------------------------------------------------ | ----------- |
| children | `BadgeChildren` | | `null` |
| children | `NonNullable<React.ReactNode>` | | `null` |
| variant | `BadgeVariants` | `default`, `info`, `success`, `warning`, `error`, `new` | `null` |
| as | `BadgeBaseElements` | The HTML tag to use as base. -`span`, `button`, `a` | `null` |
| href? | `string` | A URL to route to. Must use `as="a"` for this prop to work. | `undefined` |
Expand Down

0 comments on commit 59e8e78

Please sign in to comment.