feat: FloatButton support button htmlType (#50892)

This commit is contained in:
lijianan 2024-09-17 17:15:26 +08:00 committed by GitHub
parent 72c0377ab8
commit 5b467654aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 24 additions and 10 deletions

View File

@ -67,7 +67,7 @@ Different button styles can be generated by setting Button properties. The recom
| disabled | Disabled state of button | boolean | false | | | disabled | Disabled state of button | boolean | false | |
| ghost | Make background transparent and invert text and border colors | boolean | false | | | ghost | Make background transparent and invert text and border colors | boolean | false | |
| href | Redirect url of link button | string | - | | | href | Redirect url of link button | string | - | |
| htmlType | Set the original html `type` of `button`, see: [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type) | string | `button` | | | htmlType | Set the original html `type` of `button`, see: [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#type) | `submit` \| `reset` \| `button` | `button` | |
| icon | Set the icon component of button | ReactNode | - | | | icon | Set the icon component of button | ReactNode | - | |
| iconPosition | Set the icon position of button | `start` \| `end` | `start` | 5.17.0 | | iconPosition | Set the icon position of button | `start` \| `end` | `start` | 5.17.0 |
| loading | Set the loading status of button | boolean \| { delay: number } | false | | | loading | Set the loading status of button | boolean \| { delay: number } | false | |

View File

@ -73,7 +73,7 @@ group:
| disabled | 设置按钮失效状态 | boolean | false | | | disabled | 设置按钮失效状态 | boolean | false | |
| ghost | 幽灵属性,使按钮背景透明 | boolean | false | | | ghost | 幽灵属性,使按钮背景透明 | boolean | false | |
| href | 点击跳转的地址,指定此属性 button 的行为和 a 链接一致 | string | - | | | href | 点击跳转的地址,指定此属性 button 的行为和 a 链接一致 | string | - | |
| htmlType | 设置 `button` 原生的 `type` 值,可选值请参考 [HTML 标准](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type) | string | `button` | | | htmlType | 设置 `button` 原生的 `type` 值,可选值请参考 [HTML 标准](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/button#type) | `submit` \| `reset` \| `button` | `button` | |
| icon | 设置按钮的图标组件 | ReactNode | - | | | icon | 设置按钮的图标组件 | ReactNode | - | |
| iconPosition | 设置按钮图标组件的位置 | `start` \| `end` | `start` | 5.17.0 | | iconPosition | 设置按钮图标组件的位置 | `start` \| `end` | `start` | 5.17.0 |
| loading | 设置按钮载入状态 | boolean \| { delay: number } | false | | | loading | 设置按钮载入状态 | boolean \| { delay: number } | false | |

View File

@ -1,3 +1,4 @@
/* eslint-disable react/button-has-type */
import React, { useContext, useMemo } from 'react'; import React, { useContext, useMemo } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import omit from 'rc-util/lib/omit'; import omit from 'rc-util/lib/omit';
@ -35,6 +36,7 @@ const InternalFloatButton = React.forwardRef<FloatButtonElement, FloatButtonProp
icon, icon,
description, description,
tooltip, tooltip,
htmlType = 'button',
badge = {}, badge = {},
...restProps ...restProps
} = props; } = props;
@ -107,7 +109,7 @@ const InternalFloatButton = React.forwardRef<FloatButtonElement, FloatButtonProp
{buttonNode} {buttonNode}
</a> </a>
) : ( ) : (
<button ref={ref} {...restProps} className={classString} style={mergedStyle} type="button"> <button ref={ref} {...restProps} className={classString} style={mergedStyle} type={htmlType}>
{buttonNode} {buttonNode}
</button> </button>
), ),

View File

@ -92,4 +92,11 @@ describe('FloatButton', () => {
const badgeElement = container?.querySelector<HTMLSpanElement>('.ant-float-btn .ant-badge'); const badgeElement = container?.querySelector<HTMLSpanElement>('.ant-float-btn .ant-badge');
expect(badgeElement?.querySelector<HTMLElement>('.ant-badge-dot')).toBeTruthy(); expect(badgeElement?.querySelector<HTMLElement>('.ant-badge-dot')).toBeTruthy();
}); });
it('support button htmlType', () => {
const type = 'submit';
const { container } = render(<FloatButton htmlType={type} />);
const element = container?.querySelector<HTMLButtonElement>('.ant-float-btn');
expect(element?.type).toBe(type);
});
}); });

View File

@ -50,6 +50,7 @@ Common props ref[Common props](/docs/react/common-props)
| onClick | Set the handler to handle `click` event | (event) => void | - | | | onClick | Set the handler to handle `click` event | (event) => void | - | |
| href | The target of hyperlink | string | - | | | href | The target of hyperlink | string | - | |
| target | Specifies where to display the linked URL | string | - | | | target | Specifies where to display the linked URL | string | - | |
| htmlType | Set the original html `type` of `button`, see: [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#type) | `submit` \| `reset` \| `button` | `button` | 5.21.0 |
| badge | Attach Badge to FloatButton. `status` and other props related are not supported. | [BadgeProps](/components/badge#api) | - | 5.4.0 | | badge | Attach Badge to FloatButton. `status` and other props related are not supported. | [BadgeProps](/components/badge#api) | - | 5.4.0 |
### FloatButton.Group ### FloatButton.Group

View File

@ -51,6 +51,7 @@ tag: 5.0.0
| onClick | 点击按钮时的回调 | (event) => void | - | | | onClick | 点击按钮时的回调 | (event) => void | - | |
| href | 点击跳转的地址,指定此属性 button 的行为和 a 链接一致 | string | - | | | href | 点击跳转的地址,指定此属性 button 的行为和 a 链接一致 | string | - | |
| target | 相当于 a 标签的 target 属性href 存在时生效 | string | - | | | target | 相当于 a 标签的 target 属性href 存在时生效 | string | - | |
| htmlType | 设置 `button` 原生的 `type` 值,可选值请参考 [HTML 标准](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/button#type) | `submit` \| `reset` \| `button` | `button` | 5.21.0 |
| badge | 带徽标数字的悬浮按钮(不支持 `status` 以及相关属性) | [BadgeProps](/components/badge-cn#api) | - | 5.4.0 | | badge | 带徽标数字的悬浮按钮(不支持 `status` 以及相关属性) | [BadgeProps](/components/badge-cn#api) | - | 5.4.0 |
### FloatButton.Group ### FloatButton.Group

View File

@ -1,6 +1,7 @@
import type React from 'react'; import type React from 'react';
import type { BadgeProps } from '../badge'; import type { BadgeProps } from '../badge';
import type { ButtonHTMLType } from '../button';
import type { TooltipProps } from '../tooltip'; import type { TooltipProps } from '../tooltip';
export type FloatButtonElement = HTMLAnchorElement & HTMLButtonElement; export type FloatButtonElement = HTMLAnchorElement & HTMLButtonElement;
@ -30,6 +31,11 @@ export interface FloatButtonProps extends React.DOMAttributes<FloatButtonElement
href?: string; href?: string;
target?: React.HTMLAttributeAnchorTarget; target?: React.HTMLAttributeAnchorTarget;
badge?: FloatButtonBadgeProps; badge?: FloatButtonBadgeProps;
/**
* @since 5.21.0
* @default button
*/
htmlType?: ButtonHTMLType;
'aria-label'?: React.HtmlHTMLAttributes<HTMLElement>['aria-label']; 'aria-label'?: React.HtmlHTMLAttributes<HTMLElement>['aria-label'];
} }

View File

@ -31,14 +31,11 @@ interface LineProps extends ProgressProps {
* } * }
*/ */
export const sortGradient = (gradients: StringGradients) => { export const sortGradient = (gradients: StringGradients) => {
let tempArr: any[] = []; let tempArr: { key: number; value?: string }[] = [];
Object.keys(gradients).forEach((key) => { Object.keys(gradients).forEach((key) => {
const formattedKey = parseFloat(key.replace(/%/g, '')); const formattedKey = parseFloat(key.replace(/%/g, ''));
if (!isNaN(formattedKey)) { if (!isNaN(formattedKey)) {
tempArr.push({ tempArr.push({ key: formattedKey, value: gradients[key] });
key: formattedKey,
value: gradients[key],
});
} }
}); });
tempArr = tempArr.sort((a, b) => a.key - b.key); tempArr = tempArr.sort((a, b) => a.key - b.key);
@ -126,12 +123,12 @@ const Line: React.FC<LineProps> = (props) => {
const successPercent = getSuccessPercent(props); const successPercent = getSuccessPercent(props);
const successPercentStyle = { const successPercentStyle: React.CSSProperties = {
width: `${validProgress(successPercent)}%`, width: `${validProgress(successPercent)}%`,
height, height,
borderRadius, borderRadius,
backgroundColor: success?.strokeColor, backgroundColor: success?.strokeColor,
} as React.CSSProperties; };
const outerStyle: React.CSSProperties = { const outerStyle: React.CSSProperties = {
width: width < 0 ? '100%' : width, width: width < 0 ? '100%' : width,