Skip to content

Commit

Permalink
fix: Button ts
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jul 18, 2023
1 parent cf40fb0 commit 70500f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
11 changes: 11 additions & 0 deletions components/button/__tests__/index.test.tsx
Expand Up @@ -343,4 +343,15 @@ describe('Button', () => {
);
expect(window.getComputedStyle(container.querySelector('#link')!).pointerEvents).toBe('none');
});

it('Correct type', () => {
const onBtnClick: React.MouseEventHandler<HTMLButtonElement> = () => {};
const onAnchorClick: React.MouseEventHandler<HTMLAnchorElement> = () => {};

const button = <Button onClick={onBtnClick} />;
const anchor = <Button href="https://ant.design" onClick={onAnchorClick} />;

expect(button).toBeTruthy();
expect(anchor).toBeTruthy();
});
});
12 changes: 8 additions & 4 deletions components/button/button.tsx
Expand Up @@ -27,7 +27,9 @@ import useStyle from './style';

export type LegacyButtonType = ButtonType | 'danger';

export function convertLegacyProps(type?: LegacyButtonType): ButtonProps {
export function convertLegacyProps(
type?: LegacyButtonType,
): Pick<BaseButtonProps, 'danger' | 'type'> {
if (type === 'danger') {
return { danger: true };
}
Expand Down Expand Up @@ -66,7 +68,9 @@ export type NativeButtonProps = {
} & BaseButtonProps &
Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type' | 'onClick'>;

export type ButtonProps = Partial<AnchorButtonProps & NativeButtonProps>;
export type ButtonProps = AnchorButtonProps | NativeButtonProps;

type InternalButtonProps = Partial<AnchorButtonProps & NativeButtonProps>;

type CompoundedComponent = React.ForwardRefExoticComponent<
ButtonProps & React.RefAttributes<HTMLElement>
Expand Down Expand Up @@ -121,7 +125,7 @@ const InternalButton: React.ForwardRefRenderFunction<
classNames: customClassNames,
style: customStyle = {},
...rest
} = props;
} = props as InternalButtonProps;

const { getPrefixCls, autoInsertSpaceInButton, direction, button } = useContext(ConfigContext);
const prefixCls = getPrefixCls('btn', customizePrefixCls);
Expand Down Expand Up @@ -214,7 +218,7 @@ const InternalButton: React.ForwardRefRenderFunction<

const iconType = innerLoading ? 'loading' : icon;

const linkButtonRestProps = omit(rest as ButtonProps & { navigate: any }, ['navigate']);
const linkButtonRestProps = omit(rest as InternalButtonProps & { navigate: any }, ['navigate']);

const classes = classNames(
prefixCls,
Expand Down
6 changes: 5 additions & 1 deletion components/popconfirm/PurePanel.tsx
Expand Up @@ -77,7 +77,11 @@ export const Overlay: React.FC<OverlayProps> = (props) => {
</Button>
)}
<ActionButton
buttonProps={{ size: 'small', ...convertLegacyProps(okType), ...okButtonProps }}
buttonProps={{
size: 'small',
...convertLegacyProps(okType),
...okButtonProps,
}}
actionFn={onConfirm}
close={close}
prefixCls={getPrefixCls('btn')}
Expand Down

0 comments on commit 70500f8

Please sign in to comment.