Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add stepIcon renderer #118

Merged
merged 20 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/nextStep.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
}
.my-step-container {
width: 100%;
}
}
32 changes: 32 additions & 0 deletions examples/stepIcon.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import '../assets/index.less';
import '../assets/iconfont.less';
import React from 'react';
import Steps, { Step } from '../src';

function stepIcon({ status, node }) {
const isProcessing = status === 'process';
return isProcessing ? <div style={{ backgroundColor: 'blue' }}>{node}</div> : node;
}

export default () => {
const [current, setCurrent] = React.useState(0);
return (
<>
<button
type="button"
onClick={() => {
setCurrent((current + 1) % 5);
}}
>
loop
</button>
<Steps stepIcon={stepIcon} current={current}>
<Step title="已完成" />
<Step title="进行中" />
<Step title="待运行" />
<Step title="待运行" />
<Step title="待运行" />
</Steps>
</>
);
};
26 changes: 15 additions & 11 deletions src/Step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React from 'react';
import classNames from 'classnames';
import { Status, Icons } from './interface';
import { StepIconRender, ProgressDotRender } from './Steps';

function isString(str: any): str is string {
return typeof str === 'string';
Expand All @@ -26,21 +27,13 @@ export interface StepProps {
icons?: Icons;
onClick?: React.MouseEventHandler<HTMLDivElement>;
onStepClick?: (index: number) => void;
progressDot?: (
iconDot,
info: {
index: number;
status: Status;
title: React.ReactNode;
description: React.ReactNode;
},
) => React.ReactNode;
progressDot?: ProgressDotRender | boolean;
stepIcon?: StepIconRender;
}

export default class Step extends React.Component<StepProps> {
onClick: React.MouseEventHandler<HTMLDivElement> = (...args) => {
const { onClick, onStepClick, stepIndex } = this.props;

if (onClick) {
onClick(...args);
}
Expand All @@ -52,6 +45,7 @@ export default class Step extends React.Component<StepProps> {
const {
prefixCls,
progressDot,
stepIcon,
stepNumber,
status,
title,
Expand Down Expand Up @@ -97,6 +91,16 @@ export default class Step extends React.Component<StepProps> {
iconNode = <span className={`${prefixCls}-icon`}>{stepNumber}</span>;
}

if (stepIcon) {
iconNode = stepIcon({
index: stepNumber - 1,
status,
title,
description,
node: iconNode,
});
}

return iconNode;
}

Expand All @@ -115,7 +119,7 @@ export default class Step extends React.Component<StepProps> {
description,
title,
subTitle,
progressDot,
stepIcon,
tailContent,
icons,
stepIndex,
Expand Down
35 changes: 29 additions & 6 deletions src/Steps.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
/* eslint react/no-did-mount-set-state: 0, react/prop-types: 0 */
import React, { cloneElement } from 'react';
import toArray from 'rc-util/lib/Children/toArray';
import devWarning from 'rc-util/lib/warning';
import classNames from 'classnames';
import { Status, Icons } from './interface';
import Step from './Step';

export type StepIconRender = (info: {
index: number;
status: Status;
title: React.ReactNode;
description: React.ReactNode;
node: React.ReactNode;
}) => React.ReactNode;

export type ProgressDotRender = (
iconDot,
info: {
index: number;
status: Status;
title: React.ReactNode;
description: React.ReactNode;
},
) => React.ReactNode;

export interface StepsProps {
prefixCls?: string;
style?: React.CSSProperties;
Expand All @@ -17,7 +36,8 @@ export interface StepsProps {
status?: Status;
size?: 'default' | 'small';
current?: number;
progressDot?: boolean;
progressDot?: ProgressDotRender | boolean;
stepIcon?: StepIconRender;
initial?: number;
icons?: Icons;
onChange?: (current: number) => void;
Expand Down Expand Up @@ -60,13 +80,13 @@ export default class Steps extends React.Component<StepsProps> {
size,
current,
progressDot,
stepIcon,
initial,
icons,
onChange,
...restProps
} = this.props;
const isNav = type === 'navigation';
const filteredChildren = React.Children.toArray(children).filter(c => !!c);
const adjustedLabelPlacement = progressDot ? 'vertical' : labelPlacement;
const classString = classNames(prefixCls, `${prefixCls}-${direction}`, className, {
[`${prefixCls}-${size}`]: size,
Expand All @@ -75,12 +95,14 @@ export default class Steps extends React.Component<StepsProps> {
[`${prefixCls}-navigation`]: isNav,
});

devWarning(
!progressDot,
'`progressDot` is deprecated. Please use `stepRender` to custom icon.',
);

return (
<div className={classString} style={style} {...restProps}>
{toArray(filteredChildren).map((child, index) => {
if (!child) {
return null;
}
afc163 marked this conversation as resolved.
Show resolved Hide resolved
{toArray(children).map((child, index) => {
const stepNumber = initial + index;
const childProps = {
stepNumber: `${stepNumber + 1}`,
Expand All @@ -90,6 +112,7 @@ export default class Steps extends React.Component<StepsProps> {
iconPrefix,
wrapperStyle: style,
progressDot,
stepIcon,
icons,
onStepClick: onChange && this.onStepClick,
...child.props,
Expand Down
Loading