Browse Source

fix: Button default event handler (#43666)

* test: test driven

* fix: types

* chore: post script
master
二货爱吃白萝卜 1 year ago
committed by GitHub
parent
commit
4de4367706
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      components/button/__tests__/index.test.tsx
  2. 29
      components/button/button.tsx
  3. 1
      scripts/post-script.js

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

@ -351,7 +351,38 @@ describe('Button', () => {
const button = <Button onClick={onBtnClick} />; const button = <Button onClick={onBtnClick} />;
const anchor = <Button href="https://ant.design" onClick={onAnchorClick} />; const anchor = <Button href="https://ant.design" onClick={onAnchorClick} />;
const defaultBtn = (
<Button
onClick={(e) => {
expect(e).toBeTruthy();
}}
/>
);
const defaultABtn = (
<Button
href="https://ant.design"
onClick={(e) => {
expect(e).toBeTruthy();
}}
/>
);
const btnRef = React.createRef<HTMLButtonElement>();
const refBtn = <Button ref={btnRef} />;
const anchorRef = React.createRef<HTMLAnchorElement>();
const refAnchor = <Button ref={anchorRef} />;
const htmlRef = React.createRef<HTMLElement>();
const refHtml = <Button ref={htmlRef} />;
expect(button).toBeTruthy(); expect(button).toBeTruthy();
expect(anchor).toBeTruthy(); expect(anchor).toBeTruthy();
expect(defaultBtn).toBeTruthy();
expect(defaultABtn).toBeTruthy();
expect(refBtn).toBeTruthy();
expect(refAnchor).toBeTruthy();
expect(refHtml).toBeTruthy();
}); });
}); });

29
components/button/button.tsx

@ -55,22 +55,15 @@ export interface BaseButtonProps {
styles?: { icon: React.CSSProperties }; styles?: { icon: React.CSSProperties };
} }
export type AnchorButtonProps = { type MergedHTMLAttributes = Omit<
href: string; React.HTMLAttributes<HTMLElement> & React.AnchorHTMLAttributes<HTMLElement>,
target?: React.HTMLAttributeAnchorTarget; 'type'
onClick?: React.MouseEventHandler<HTMLAnchorElement>; >;
} & BaseButtonProps &
Omit<React.AnchorHTMLAttributes<HTMLAnchorElement | HTMLButtonElement>, 'type' | 'onClick'>;
export type NativeButtonProps = {
htmlType?: ButtonHTMLType;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
} & BaseButtonProps &
Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type' | 'onClick'>;
export type ButtonProps = AnchorButtonProps | NativeButtonProps;
type InternalButtonProps = Partial<AnchorButtonProps & NativeButtonProps>; export interface ButtonProps extends BaseButtonProps, MergedHTMLAttributes {
href?: string;
htmlType?: ButtonHTMLType;
}
type CompoundedComponent = React.ForwardRefExoticComponent< type CompoundedComponent = React.ForwardRefExoticComponent<
ButtonProps & React.RefAttributes<HTMLElement> ButtonProps & React.RefAttributes<HTMLElement>
@ -125,7 +118,7 @@ const InternalButton: React.ForwardRefRenderFunction<
classNames: customClassNames, classNames: customClassNames,
style: customStyle = {}, style: customStyle = {},
...rest ...rest
} = props as InternalButtonProps; } = props;
const { getPrefixCls, autoInsertSpaceInButton, direction, button } = useContext(ConfigContext); const { getPrefixCls, autoInsertSpaceInButton, direction, button } = useContext(ConfigContext);
const prefixCls = getPrefixCls('btn', customizePrefixCls); const prefixCls = getPrefixCls('btn', customizePrefixCls);
@ -218,7 +211,7 @@ const InternalButton: React.ForwardRefRenderFunction<
const iconType = innerLoading ? 'loading' : icon; const iconType = innerLoading ? 'loading' : icon;
const linkButtonRestProps = omit(rest as InternalButtonProps & { navigate: any }, ['navigate']); const linkButtonRestProps = omit(rest as ButtonProps & { navigate: any }, ['navigate']);
const classes = classNames( const classes = classNames(
prefixCls, prefixCls,
@ -280,7 +273,7 @@ const InternalButton: React.ForwardRefRenderFunction<
let buttonNode = ( let buttonNode = (
<button <button
{...(rest as NativeButtonProps)} {...rest}
type={htmlType} type={htmlType}
className={classes} className={classes}
style={fullStyle} style={fullStyle}

1
scripts/post-script.js

@ -39,6 +39,7 @@ const DEPRECIATED_VERSION = {
], ],
'5.6.2': ['https://github.com/ant-design/ant-design/issues/43113'], '5.6.2': ['https://github.com/ant-design/ant-design/issues/43113'],
'5.6.3': ['https://github.com/ant-design/ant-design/issues/43190'], '5.6.3': ['https://github.com/ant-design/ant-design/issues/43190'],
'5.7.1': ['https://github.com/ant-design/ant-design/issues/43654'],
}; };
function matchDeprecated(version) { function matchDeprecated(version) {

Loading…
Cancel
Save