Skip to content

Commit 1f229dc

Browse files
authored
feat(FloatButton): support backTopIcon global config (ant-design#54493)
1 parent c2ec447 commit 1f229dc

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

components/config-provider/context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ export type TransferConfig = ComponentStyleConfig & Pick<TransferProps, 'selecti
182182
export type FormConfig = ComponentStyleConfig &
183183
Pick<FormProps, 'requiredMark' | 'colon' | 'scrollToFirstError' | 'validateMessages' | 'variant'>;
184184

185+
export type FloatButtonConfig = {
186+
backTopIcon?: React.ReactNode;
187+
};
188+
185189
export type FloatButtonGroupConfig = Pick<FloatButtonGroupProps, 'closeIcon'>;
186190

187191
export type PaginationConfig = ComponentStyleConfig & Pick<PaginationProps, 'showSizeChanger'>;
@@ -267,6 +271,7 @@ export interface ConfigComponentProps {
267271
cascader?: CascaderConfig;
268272
treeSelect?: TreeSelectConfig;
269273
collapse?: CollapseConfig;
274+
floatButton?: FloatButtonConfig;
270275
floatButtonGroup?: FloatButtonGroupConfig;
271276
typography?: ComponentStyleConfig;
272277
skeleton?: ComponentStyleConfig;

components/config-provider/index.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const {
129129
| dropdown | Set Dropdown common props | { className?: string, style?: React.CSSProperties } | - | 5.11.0 |
130130
| empty | Set Empty common props | { className?: string, style?: React.CSSProperties, classNames?: [EmptyProps\["classNames"\]](/components/empty#api), styles?: [EmptyProps\["styles"\]](/components/empty#api), image?: ReactNode } | - | 5.7.0, `classNames` and `styles`: 5.23.0, `image`: 5.27.0 |
131131
| flex | Set Flex common props | { className?: string, style?: React.CSSProperties, vertical?: boolean } | - | 5.10.0 |
132+
| floatButton | Set FloatButton common props | { backTopIcon?: React.ReactNode } | - | 5.27.0 |
132133
| floatButtonGroup | Set FloatButton.Group common props | { closeIcon?: React.ReactNode } | - | 5.16.0 |
133134
| form | Set Form common props | { className?: string, style?: React.CSSProperties, validateMessages?: [ValidateMessages](/components/form/#validatemessages), requiredMark?: boolean \| `optional`, scrollToFirstError?: boolean \| [Options](https://github.com/stipsan/scroll-into-view-if-needed/tree/ece40bd9143f48caf4b99503425ecb16b0ad8249#options) } | - | `requiredMark`: 4.8.0; `colon`: 4.18.0; `scrollToFirstError`: 5.2.0; `className` and `style`: 5.7.0 |
134135
| image | Set Image common props | { className?: string, style?: React.CSSProperties, preview?: { closeIcon?: React.ReactNode } } | - | 5.7.0, `closeIcon`: 5.14.0 |

components/config-provider/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type {
2929
DrawerConfig,
3030
EmptyConfig,
3131
FlexConfig,
32+
FloatButtonConfig,
3233
FloatButtonGroupConfig,
3334
FormConfig,
3435
ImageConfig,
@@ -210,6 +211,7 @@ export interface ConfigProviderProps {
210211
slider?: ComponentStyleConfig;
211212
breadcrumb?: ComponentStyleConfig;
212213
menu?: MenuConfig;
214+
floatButton?: FloatButtonConfig;
213215
floatButtonGroup?: FloatButtonGroupConfig;
214216
checkbox?: ComponentStyleConfig;
215217
descriptions?: ComponentStyleConfig;
@@ -400,6 +402,7 @@ const ProviderChildren: React.FC<ProviderChildrenProps> = (props) => {
400402
tooltip,
401403
popover,
402404
popconfirm,
405+
floatButton,
403406
floatButtonGroup,
404407
variant,
405408
inputNumber,
@@ -503,6 +506,7 @@ const ProviderChildren: React.FC<ProviderChildrenProps> = (props) => {
503506
tooltip,
504507
popover,
505508
popconfirm,
509+
floatButton,
506510
floatButtonGroup,
507511
variant,
508512
inputNumber,

components/config-provider/index.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ const {
131131
| dropdown | 设置 Dropdown 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.11.0 |
132132
| empty | 设置 Empty 组件的通用属性 | { className?: string, style?: React.CSSProperties, classNames?:[EmptyProps\["classNames"\]](/components/empty-cn#api), styles?: [EmptyProps\["styles"\]](/components/empty-cn#api), image?: ReactNode } | - | 5.7.0, `classNames``styles`: 5.23.0, `image`: 5.27.0 |
133133
| flex | 设置 Flex 组件的通用属性 | { className?: string, style?: React.CSSProperties, vertical?: boolean } | - | 5.10.0 |
134+
| floatButton | 设置 FloatButton 组件的通用属性 | { backTopIcon?: React.ReactNode } | - | 5.27.0 |
134135
| floatButtonGroup | 设置 FloatButton.Group 组件的通用属性 | { closeIcon?: React.ReactNode } | - | 5.16.0 |
135136
| form | 设置 Form 组件的通用属性 | { className?: string, style?: React.CSSProperties, validateMessages?: [ValidateMessages](/components/form-cn#validatemessages), `requiredMark`?: boolean \| `optional`, colon?: boolean, scrollToFirstError?: boolean \| [Options](https://github.com/stipsan/scroll-into-view-if-needed/tree/ece40bd9143f48caf4b99503425ecb16b0ad8249#options)} | - | `requiredMark`: 4.8.0; `colon`: 4.18.0; `scrollToFirstError`: 5.2.0; `className``style`: 5.7.0 |
136137
| image | 设置 Image 组件的通用属性 | { className?: string, style?: React.CSSProperties, preview?: { closeIcon?: React.ReactNode } } | - | 5.7.0, `closeIcon`: 5.14.0 |

components/float-button/BackTop.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scrollTo from '../_util/scrollTo';
99
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
1010
import type { ConfigConsumerProps } from '../config-provider';
1111
import { ConfigContext } from '../config-provider';
12+
import { useComponentConfig } from '../config-provider/context';
1213
import FloatButtonGroupContext from './context';
1314
import FloatButton, { floatButtonPrefixCls } from './FloatButton';
1415
import type {
@@ -19,20 +20,26 @@ import type {
1920
FloatButtonShape,
2021
} from './interface';
2122

23+
const defaultIcon = <VerticalAlignTopOutlined />;
24+
2225
const BackTop = React.forwardRef<FloatButtonRef, BackTopProps>((props, ref) => {
26+
const { backTopIcon: contextIcon } = useComponentConfig('floatButton');
27+
2328
const {
2429
prefixCls: customizePrefixCls,
2530
className,
2631
type = 'default',
2732
shape = 'circle',
2833
visibilityHeight = 400,
29-
icon = <VerticalAlignTopOutlined />,
34+
icon,
3035
target,
3136
onClick,
3237
duration = 450,
3338
...restProps
3439
} = props;
3540

41+
const mergedIcon = icon ?? contextIcon ?? defaultIcon;
42+
3643
const [visible, setVisible] = useState<boolean>(visibilityHeight === 0);
3744

3845
const internalRef = React.useRef<FloatButtonRef['nativeElement']>(null);
@@ -78,7 +85,7 @@ const BackTop = React.forwardRef<FloatButtonRef, BackTopProps>((props, ref) => {
7885

7986
const contentProps: FloatButtonProps = {
8087
prefixCls,
81-
icon,
88+
icon: mergedIcon,
8289
type,
8390
shape: mergedShape,
8491
...restProps,

components/float-button/__tests__/back-top.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import FloatButton from '..';
44
import mountTest from '../../../tests/shared/mountTest';
55
import rtlTest from '../../../tests/shared/rtlTest';
66
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
7+
import ConfigProvider from '../../config-provider';
78

89
const { BackTop } = FloatButton;
910

@@ -61,4 +62,13 @@ describe('BackTop', () => {
6162
expect(errSpy).not.toHaveBeenCalled();
6263
errSpy.mockRestore();
6364
});
65+
66+
it('supports ConfigProvider backTopIcon', () => {
67+
const wrapper = render(
68+
<ConfigProvider floatButton={{ backTopIcon: <span>666</span> }}>
69+
<BackTop visibilityHeight={0} />
70+
</ConfigProvider>,
71+
);
72+
expect(wrapper.getByText('666')).toBeInTheDocument();
73+
});
6474
});

0 commit comments

Comments
 (0)