初始化 antd-pro

This commit is contained in:
sin
2019-02-27 11:06:55 +08:00
parent 9f4fdb7f6e
commit b2068ae44b
458 changed files with 28090 additions and 2 deletions

View File

@@ -0,0 +1,24 @@
---
order: 0
title:
zh-CN: 基本
en-US: Basic
---
## zh-CN
简单的倒计时组件使用。
## en-US
The simplest usage.
````jsx
import CountDown from 'ant-design-pro/lib/CountDown';
const targetTime = new Date().getTime() + 3900000;
ReactDOM.render(
<CountDown style={{ fontSize: 20 }} target={targetTime} />
, mountNode);
````

View File

@@ -0,0 +1,9 @@
import * as React from 'react';
export interface ICountDownProps {
format?: (time: number) => void;
target: Date | number;
onEnd?: () => void;
style?: React.CSSProperties;
}
export default class CountDown extends React.Component<ICountDownProps, any> {}

View File

@@ -0,0 +1,15 @@
---
title: CountDown
cols: 1
order: 3
---
Simple CountDown Component.
## API
| Property | Description | Type | Default |
|----------|------------------------------------------|-------------|-------|
| format | Formatter of time | Function(time) | |
| target | Target time | Date | - |
| onEnd | Countdown to the end callback | funtion | -|

View File

@@ -0,0 +1,121 @@
import React, { Component } from 'react';
function fixedZero(val) {
return val * 1 < 10 ? `0${val}` : val;
}
const initTime = props => {
let lastTime = 0;
let targetTime = 0;
try {
if (Object.prototype.toString.call(props.target) === '[object Date]') {
targetTime = props.target.getTime();
} else {
targetTime = new Date(props.target).getTime();
}
} catch (e) {
throw new Error('invalid target prop', e);
}
lastTime = targetTime - new Date().getTime();
return {
lastTime: lastTime < 0 ? 0 : lastTime,
};
};
class CountDown extends Component {
timer = 0;
interval = 1000;
constructor(props) {
super(props);
const { lastTime } = initTime(props);
this.state = {
lastTime,
};
}
static getDerivedStateFromProps(nextProps, preState) {
const { lastTime } = initTime(nextProps);
if (preState.lastTime !== lastTime) {
return {
lastTime,
};
}
return null;
}
componentDidMount() {
this.tick();
}
componentDidUpdate(prevProps) {
const { target } = this.props;
if (target !== prevProps.target) {
clearTimeout(this.timer);
this.tick();
}
}
componentWillUnmount() {
clearTimeout(this.timer);
}
// defaultFormat = time => (
// <span>{moment(time).format('hh:mm:ss')}</span>
// );
defaultFormat = time => {
const hours = 60 * 60 * 1000;
const minutes = 60 * 1000;
const h = Math.floor(time / hours);
const m = Math.floor((time - h * hours) / minutes);
const s = Math.floor((time - h * hours - m * minutes) / 1000);
return (
<span>
{fixedZero(h)}:{fixedZero(m)}:{fixedZero(s)}
</span>
);
};
tick = () => {
const { onEnd } = this.props;
let { lastTime } = this.state;
this.timer = setTimeout(() => {
if (lastTime < this.interval) {
clearTimeout(this.timer);
this.setState(
{
lastTime: 0,
},
() => {
if (onEnd) {
onEnd();
}
}
);
} else {
lastTime -= this.interval;
this.setState(
{
lastTime,
},
() => {
this.tick();
}
);
}
}, this.interval);
};
render() {
const { format = this.defaultFormat, onEnd, ...rest } = this.props;
const { lastTime } = this.state;
const result = format(lastTime);
return <span {...rest}>{result}</span>;
}
}
export default CountDown;

View File

@@ -0,0 +1,16 @@
---
title: CountDown
subtitle: 倒计时
cols: 1
order: 3
---
倒计时组件。
## API
| 参数 | 说明 | 类型 | 默认值 |
|----------|------------------------------------------|-------------|-------|
| format | 时间格式化显示 | Function(time) | |
| target | 目标时间 | Date | - |
| onEnd | 倒计时结束回调 | funtion | -|