Skip to content

Commit

Permalink
fix(Step): compatible with legacy direction and labelPlacement, close #…
Browse files Browse the repository at this point in the history
…4813 (#4814)

* fix(Step): compatible with legacy direction and labelPlacement, close #4813

* chore(Step): fix types error and clean eslint warning

* chore(Step): improve by codereview
  • Loading branch information
YSMJ1994 authored and eternalsky committed Jul 16, 2024
1 parent 8c8ead9 commit 98079d1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 22 deletions.
4 changes: 2 additions & 2 deletions components/step/__docs__/adaptor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export default {
propsValue: _propsValue,
adaptor: ({ shape, level, state, location, width, height, data, style, ...others }: any) => {
const list = parseData(data, { parseContent: true }).filter(
({ type }) => type === NodeType.node
);
({ type }: any) => type === NodeType.node
) as any[];
const dataSouce: (ItemProps & { key: number })[] = [];
let current = 0;
list.forEach((item, index) => {
Expand Down
2 changes: 1 addition & 1 deletion components/step/__docs__/demo/read-only/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Step, Button } from '@alifd/next';
import { StepProps } from '@alifd/meet-react/lib/step';
import type { StepProps } from '@alifd/next/lib/step';

const StepItem = Step.Item,
ButtonGroup = Button.Group;
Expand Down
2 changes: 1 addition & 1 deletion components/step/__docs__/theme/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { type ReactNode } from 'react';
import ReactDOM from 'react-dom';
import { Demo, DemoGroup, initDemo, DemoFunctionDefineForObject } from '../../../demo-helper';
import { Demo, DemoGroup, initDemo, type DemoFunctionDefineForObject } from '../../../demo-helper';
import Step from '../../index';
import type { StepDirection, ItemProps, StepShape } from '../../types';
import '../../style';
Expand Down
38 changes: 38 additions & 0 deletions components/step/__tests__/index-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ describe('Step', () => {
cy.get('.next-step-vertical').should('have.length', 0);
});

it('should support legacy directions', () => {
cy.mount(
<Step current={1} direction={'vertical' as 'ver'}>
<StepItem title="步骤1" />
<StepItem title="步骤2" />
<StepItem title="步骤3" />
</Step>
);
cy.get('.next-step-vertical').should('have.length', 1);
cy.mount(
<Step current={1} shape="dot" direction={'horizontal' as 'hoz'}>
<StepItem title="步骤1" />
<StepItem title="步骤2" />
<StepItem title="步骤3" />
</Step>
);
cy.get('.next-step-horizontal').should('have.length', 1);
});

it('should render with labelPlacement', () => {
cy.mount(
<Step current={1} labelPlacement="ver">
Expand All @@ -119,6 +138,25 @@ describe('Step', () => {
cy.get('.next-step-label-horizontal').should('have.length', 1);
});

it('should support legacy labelPlacement', () => {
cy.mount(
<Step current={1} labelPlacement={'vertical' as 'ver'}>
<StepItem title="步骤1" />
<StepItem title="步骤2" />
<StepItem title="步骤3" />
</Step>
);
cy.get('.next-step-label-vertical').should('have.length', 1);
cy.mount(
<Step current={1} shape="dot" labelPlacement={'horizontal' as 'hoz'}>
<StepItem title="步骤1" />
<StepItem title="步骤2" />
<StepItem title="步骤3" />
</Step>
);
cy.get('.next-step-label-horizontal').should('have.length', 1);
});

it('should render when direction and labelPlacement are both hoz', () => {
const steps = ['知道自己不懂', '不知道自己懂', '知道自己懂了'].map((item, index) => (
<Step.Item key={index} title={item} itemRender={() => <Icon type="smile" />} />
Expand Down
32 changes: 20 additions & 12 deletions components/step/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ export default ConfigProvider.config(WithStepItem, {
transform: (props, deprecated) => {
if ('type' in props) {
deprecated('type', 'shape', 'Step');
const { type, direction, labelPlacement, ...others } = props as DeprecatedStepProps;
const resolvedDirection =
direction === 'vertical' ? 'ver' : direction === 'horizontal' ? 'hoz' : direction;
const resolvedLabelPlacement =
labelPlacement === 'vertical'
? 'ver'
: labelPlacement === 'horizontal'
? 'hoz'
: labelPlacement;
const { type, ...others } = props as DeprecatedStepProps;
props = {
shape: type,
direction: resolvedDirection,
labelPlacement: resolvedLabelPlacement,
...others,
};
} as StepProps;
}

const { direction, labelPlacement } = props as DeprecatedStepProps;
if (direction === 'vertical') {
deprecated('direction="vertical"', 'direction="ver"', 'Step');
props = { ...props, direction: 'ver' };
}
if (direction === 'horizontal') {
deprecated('direction="horizontal"', 'direction="hoz"', 'Step');
props = { ...props, direction: 'hoz' };
}
if (labelPlacement === 'vertical') {
deprecated('labelPlacement="vertical"', 'labelPlacement="ver"', 'Step');
props = { ...props, labelPlacement: 'ver' };
}
if (labelPlacement === 'horizontal') {
deprecated('labelPlacement="horizontal"', 'labelPlacement="hoz"', 'Step');
props = { ...props, labelPlacement: 'hoz' };
}

return props;
Expand Down
8 changes: 4 additions & 4 deletions components/step/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { type ReactNode } from 'react';
import { CommonProps } from '../util';
import type React from 'react';
import type { CommonProps } from '../util';

type HTMLAttributesWeak<T> = Omit<
React.HTMLAttributes<T>,
Expand Down Expand Up @@ -167,8 +167,8 @@ export interface StepProps
itemRender?: (
index: number,
status: StepStatus,
title?: ReactNode,
content?: ReactNode
title?: React.ReactNode,
content?: React.ReactNode
) => React.ReactNode;

/**
Expand Down
28 changes: 26 additions & 2 deletions components/step/view/step-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Icon from '../../icon';
import Progress from '../../progress';
import ConfigProvider from '../../config-provider';
import { support, events, dom, obj } from '../../util';
import type { ItemProps, StepStatus } from '../types';
import type { DeprecatedStepProps, ItemProps, StepStatus } from '../types';

const getWidth = (el: HTMLElement) => dom.getStyle(el, 'width') as number;
const getHeight = (el: HTMLElement) => dom.getStyle(el, 'height') as number;
Expand Down Expand Up @@ -366,4 +366,28 @@ class StepItem extends Component<ItemProps> {
return shape === 'arrow' ? arrowElement : otherElement;
}
}
export default ConfigProvider.config(StepItem);
export default ConfigProvider.config(StepItem, {
transform(props, deprecated) {
const { direction, labelPlacement } = props as Pick<
DeprecatedStepProps,
'direction' | 'labelPlacement'
>;
if (direction === 'vertical') {
deprecated('direction="vertical"', 'direction="ver"', 'Step.Item');
props = { ...props, direction: 'ver' };
}
if (direction === 'horizontal') {
deprecated('direction="horizontal"', 'direction="hoz"', 'Step.Item');
props = { ...props, direction: 'hoz' };
}
if (labelPlacement === 'vertical') {
deprecated('labelPlacement="vertical"', 'labelPlacement="ver"', 'Step.Item');
props = { ...props, labelPlacement: 'ver' };
}
if (labelPlacement === 'horizontal') {
deprecated('labelPlacement="horizontal"', 'labelPlacement="hoz"', 'Step.Item');
props = { ...props, labelPlacement: 'hoz' };
}
return props;
},
});

0 comments on commit 98079d1

Please sign in to comment.