mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-11-30 11:08:45 +08:00
refactor skeleton Paragraph
This commit is contained in:
parent
b951105b93
commit
afbf9e59bc
@ -1,6 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { polyfill } from 'react-lifecycles-compat';
|
|
||||||
|
|
||||||
type widthUnit = number | string;
|
type widthUnit = number | string;
|
||||||
|
|
||||||
@ -12,56 +11,28 @@ export interface SkeletonParagraphProps {
|
|||||||
rows?: number;
|
rows?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SkeletonParagraphState {
|
class Paragraph extends React.Component<SkeletonParagraphProps, {}> {
|
||||||
prevProps: SkeletonParagraphProps;
|
|
||||||
widthList: Array<widthUnit>;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Paragraph extends React.Component<SkeletonParagraphProps, SkeletonParagraphState> {
|
|
||||||
static defaultProps: Partial<SkeletonParagraphProps> = {
|
static defaultProps: Partial<SkeletonParagraphProps> = {
|
||||||
prefixCls: 'ant-skeleton-paragraph',
|
prefixCls: 'ant-skeleton-paragraph',
|
||||||
};
|
};
|
||||||
|
|
||||||
static getDerivedStateFromProps(
|
getWidth(index: number) {
|
||||||
props: SkeletonParagraphProps,
|
const { width, rows = 2 } = this.props;
|
||||||
state: SkeletonParagraphState,
|
if (Array.isArray(width)) {
|
||||||
): Partial<SkeletonParagraphState> {
|
return width[index];
|
||||||
const { prevProps } = state;
|
|
||||||
const { width, rows = 2 } = props;
|
|
||||||
|
|
||||||
const newState: Partial<SkeletonParagraphState> = {
|
|
||||||
prevProps: props,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (rows !== prevProps.rows || width !== prevProps.width) {
|
|
||||||
// Parse width list
|
|
||||||
let widthList = [];
|
|
||||||
if (width && Array.isArray(width)) {
|
|
||||||
widthList = width;
|
|
||||||
} else if (width && !Array.isArray(width)) {
|
|
||||||
widthList = [];
|
|
||||||
widthList[rows - 1] = width;
|
|
||||||
}
|
|
||||||
|
|
||||||
newState.widthList = widthList;
|
|
||||||
}
|
}
|
||||||
|
// last paragraph
|
||||||
return newState;
|
if (rows - 1 === index) {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
state: SkeletonParagraphState = {
|
|
||||||
prevProps: {},
|
|
||||||
widthList: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { widthList } = this.state;
|
|
||||||
const { prefixCls, className, style, rows } = this.props;
|
const { prefixCls, className, style, rows } = this.props;
|
||||||
|
|
||||||
const rowList = [...Array(rows)].map((_, index) => (
|
const rowList = [...Array(rows)].map((_, index) => (
|
||||||
<li key={index} style={{ width: widthList[index] }} />
|
<li key={index} style={{ width: this.getWidth(index) }} />
|
||||||
));
|
));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ul
|
<ul
|
||||||
className={classNames(prefixCls, className)}
|
className={classNames(prefixCls, className)}
|
||||||
@ -73,6 +44,4 @@ class Paragraph extends React.Component<SkeletonParagraphProps, SkeletonParagrap
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
polyfill(Paragraph);
|
|
||||||
|
|
||||||
export default Paragraph;
|
export default Paragraph;
|
||||||
|
Loading…
Reference in New Issue
Block a user