Browse Source

fix: Button ts (#43629)

pull/43630/head
二货爱吃白萝卜 1 year ago
committed by GitHub
parent
commit
a9bceec602
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      components/button/__tests__/index.test.tsx
  2. 12
      components/button/button.tsx
  3. 6
      components/popconfirm/PurePanel.tsx

11
components/button/__tests__/index.test.tsx

@ -343,4 +343,15 @@ describe('Button', () => {
); );
expect(window.getComputedStyle(container.querySelector('#link')!).pointerEvents).toBe('none'); 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
components/button/button.tsx

@ -27,7 +27,9 @@ import useStyle from './style';
export type LegacyButtonType = ButtonType | 'danger'; export type LegacyButtonType = ButtonType | 'danger';
export function convertLegacyProps(type?: LegacyButtonType): ButtonProps { export function convertLegacyProps(
type?: LegacyButtonType,
): Pick<BaseButtonProps, 'danger' | 'type'> {
if (type === 'danger') { if (type === 'danger') {
return { danger: true }; return { danger: true };
} }
@ -66,7 +68,9 @@ export type NativeButtonProps = {
} & BaseButtonProps & } & BaseButtonProps &
Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type' | 'onClick'>; 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< type CompoundedComponent = React.ForwardRefExoticComponent<
ButtonProps & React.RefAttributes<HTMLElement> ButtonProps & React.RefAttributes<HTMLElement>
@ -121,7 +125,7 @@ const InternalButton: React.ForwardRefRenderFunction<
classNames: customClassNames, classNames: customClassNames,
style: customStyle = {}, style: customStyle = {},
...rest ...rest
} = props; } = props as InternalButtonProps;
const { getPrefixCls, autoInsertSpaceInButton, direction, button } = useContext(ConfigContext); const { getPrefixCls, autoInsertSpaceInButton, direction, button } = useContext(ConfigContext);
const prefixCls = getPrefixCls('btn', customizePrefixCls); const prefixCls = getPrefixCls('btn', customizePrefixCls);
@ -214,7 +218,7 @@ const InternalButton: React.ForwardRefRenderFunction<
const iconType = innerLoading ? 'loading' : icon; 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( const classes = classNames(
prefixCls, prefixCls,

6
components/popconfirm/PurePanel.tsx

@ -77,7 +77,11 @@ export const Overlay: React.FC<OverlayProps> = (props) => {
</Button> </Button>
)} )}
<ActionButton <ActionButton
buttonProps={{ size: 'small', ...convertLegacyProps(okType), ...okButtonProps }} buttonProps={{
size: 'small',
...convertLegacyProps(okType),
...okButtonProps,
}}
actionFn={onConfirm} actionFn={onConfirm}
close={close} close={close}
prefixCls={getPrefixCls('btn')} prefixCls={getPrefixCls('btn')}

Loading…
Cancel
Save