diff --git a/src/components/capsule-tabs/capsule-tabs.tsx b/src/components/capsule-tabs/capsule-tabs.tsx index b5d7b4fedc..c932c4f1c2 100644 --- a/src/components/capsule-tabs/capsule-tabs.tsx +++ b/src/components/capsule-tabs/capsule-tabs.tsx @@ -1,16 +1,15 @@ -import React, { isValidElement, useRef } from 'react' -import type { FC, ReactNode, ReactElement } from 'react' -import classNames from 'classnames' import { animated } from '@react-spring/web' +import classNames from 'classnames' +import type { FC, ReactElement, ReactNode } from 'react' +import React, { isValidElement, useRef } from 'react' import { NativeProps, withNativeProps } from '../../utils/native-props' +import { ShouldRender } from '../../utils/should-render' +import { traverseReactNode } from '../../utils/traverse-react-node' import { usePropsValue } from '../../utils/use-props-value' import { useResizeEffect } from '../../utils/use-resize-effect' import { useTabListScroll } from '../../utils/use-tab-list-scroll' +import { useConfig } from '../config-provider' import ScrollMask from '../scroll-mask' -import { ShouldRender } from '../../utils/should-render' -import { traverseReactNode } from '../../utils/traverse-react-node' - -const classPrefix = `adm-capsule-tabs` export type CapsuleTabProps = { title: ReactNode @@ -27,6 +26,7 @@ export type CapsuleTabsProps = { defaultActiveKey?: string | null onChange?: (key: string) => void children?: ReactNode + prefixCls?: string } & NativeProps export const CapsuleTabs: FC = props => { @@ -34,7 +34,8 @@ export const CapsuleTabs: FC = props => { const rootRef = useRef(null) const keyToIndexRecord: Record = {} let firstActiveKey: string | null = null - + const { getPrefixCls } = useConfig() + const prefixCls = getPrefixCls('capsule-tabs', props.prefixCls) const panes: ReactElement[] = [] traverseReactNode(props.children, (child, index) => { @@ -68,18 +69,18 @@ export const CapsuleTabs: FC = props => { return withNativeProps( props, -
-
+
+
{panes.map(pane => withNativeProps( pane.props, -
+
{ const { key } = pane @@ -89,9 +90,9 @@ export const CapsuleTabs: FC = props => { } setActiveKey(key.toString()) }} - className={classNames(`${classPrefix}-tab`, { - [`${classPrefix}-tab-active`]: pane.key === activeKey, - [`${classPrefix}-tab-disabled`]: pane.props.disabled, + className={classNames(`${prefixCls}-tab`, { + [`${prefixCls}-tab-active`]: pane.key === activeKey, + [`${prefixCls}-tab-disabled`]: pane.props.disabled, })} > {pane.props.title} @@ -114,7 +115,7 @@ export const CapsuleTabs: FC = props => { destroyOnClose={pane.props.destroyOnClose} >
{pane.props.children} diff --git a/src/components/capsule-tabs/tests/__snapshots__/capsule-tabs.test.tsx.snap b/src/components/capsule-tabs/tests/__snapshots__/capsule-tabs.test.tsx.snap new file mode 100644 index 0000000000..ce8659b9bc --- /dev/null +++ b/src/components/capsule-tabs/tests/__snapshots__/capsule-tabs.test.tsx.snap @@ -0,0 +1,69 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`CapsuleTabs should apply prefixCls from ConfigProvider 1`] = ` +
+
+
+
+
+
+
+
+ fruits +
+
+
+
+
+
+`; + +exports[`CapsuleTabs should prioritize component prefixCls over ConfigProvider 1`] = ` +
+
+
+
+
+
+
+
+ fruits +
+
+
+
+
+
+`; diff --git a/src/components/capsule-tabs/tests/capsule-tabs.test.tsx b/src/components/capsule-tabs/tests/capsule-tabs.test.tsx index f7ed27fe8e..b6eb883b94 100644 --- a/src/components/capsule-tabs/tests/capsule-tabs.test.tsx +++ b/src/components/capsule-tabs/tests/capsule-tabs.test.tsx @@ -1,6 +1,7 @@ import React, { useState } from 'react' -import { render, testA11y, fireEvent } from 'testing' +import { fireEvent, render, testA11y } from 'testing' import CapsuleTabs, { CapsuleTabsProps } from '..' +import ConfigProvider from '../../config-provider' const classPrefix = `adm-capsule-tabs` @@ -95,4 +96,29 @@ describe('CapsuleTabs', () => { fireEvent.click(getByText('vegetables')) expect(queryByText('Apple')).not.toBeInTheDocument() }) + + test('should apply prefixCls from ConfigProvider', () => { + const { container } = render( + + + + + + ) + expect(container.querySelector('.config-prefix-capsule-tabs')).toBeTruthy() + expect(container).toMatchSnapshot() + }) + + test('should prioritize component prefixCls over ConfigProvider', () => { + const { container } = render( + + + + + + ) + expect(container.querySelector('.component-prefix')).toBeTruthy() + expect(container.querySelector('.config-prefix-capsule-tabs')).toBeFalsy() + expect(container).toMatchSnapshot() + }) })