Skip to content

Commit

Permalink
refactor to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Jun 22, 2016
1 parent 2be285d commit 9a0d751
Show file tree
Hide file tree
Showing 162 changed files with 318 additions and 264 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ dist
elasticsearch-*
config/base.yaml
typings
components/**/*.js
components/**/*.jsx
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions components/_util/splitObject.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function splitObject(obj, parts) {
let left = {};
let right = {};
Object.keys(obj).forEach((k)=> {
if (parts.indexOf(k) !== -1) {
left[k] = obj[k];
} else {
right = obj[k];
}
});
return [left, right];
}
16 changes: 13 additions & 3 deletions components/affix/index.jsx → components/affix/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import classNames from 'classnames';
import warning from 'warning';
Expand Down Expand Up @@ -33,7 +33,17 @@ function getOffset(element) {
};
}

export default class Affix extends React.Component {
// Affix
export interface AffixProps {
/**
* 距离窗口顶部达到指定偏移量后触发
*/
offsetTop?:number,
offsetBottom?:number,
style?:React.CSSProperties
}

export default class Affix extends React.Component<AffixProps, any> {
static propTypes = {
offsetTop: React.PropTypes.number,
offsetBottom: React.PropTypes.number,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { createElement } from 'react';
import { findDOMNode } from 'react-dom';
import React, {createElement} from 'react';
import {findDOMNode} from 'react-dom';
import isCssAnimationSupported from '../_util/isCssAnimationSupported';
import assign from 'object-assign';

function getNumberArray(num) {
return num ?
Expand All @@ -15,7 +16,8 @@ export default class ScrollNumber extends React.Component {
prefixCls: 'ant-scroll-number',
count: null,
component: 'sup',
onAnimated() {},
onAnimated() {
},
height: 18,
}

Expand Down Expand Up @@ -122,10 +124,9 @@ export default class ScrollNumber extends React.Component {
}

render() {
const props = {
...this.props,
const props = assign({}, this.props, {
className: `${this.props.prefixCls} ${this.props.className}`,
};
});
return createElement(
this.props.component,
props,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import splitObject from '../_util/splitObject';

export default class BreadcrumbItem extends React.Component {
static defaultProps = {
Expand All @@ -16,7 +17,7 @@ export default class BreadcrumbItem extends React.Component {
}

render() {
const { prefixCls, separator, children, ...restProps } = this.props;
const [{prefixCls, separator, children},restProps] = splitObject(this.props, ['prefixCls', 'separator', 'children']);
let link;
if ('href' in this.props) {
link = <a className={`${prefixCls}-link`} {...restProps}>{children}</a>;
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import classNames from 'classnames';
import splitObject from '../_util/splitObject';

const prefix = 'ant-btn-group-';

export default function ButtonGroup(props) {
const { size, className, ...others } = props;
const [{size, className},others] = splitObject(props, ['size', 'className']);

// large => lg
// small => sm
Expand Down
5 changes: 3 additions & 2 deletions components/button/button.jsx → components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import classNames from 'classnames';
import { findDOMNode } from 'react-dom';
import Icon from '../icon';

import splitObject from '../_util/splitObject';
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
function isString(str) {
Expand Down Expand Up @@ -76,7 +76,8 @@ export default class Button extends React.Component {

render() {
const props = this.props;
const { type, shape, size, className, htmlType, children, icon, loading, prefixCls, ...others } = props;
const [{type, shape, size, className, htmlType, children, icon, loading, prefixCls}, others] = splitObject(props,
['type', 'shape','size', 'className','htmlType', 'children','icon','loading','prefixCls']);

// large => lg
// small => sm
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import defaultLocale from './locale/zh_CN';
import FullCalendar from 'rc-calendar/lib/FullCalendar';
import { PREFIX_CLS } from './Constants';
import Header from './Header';
import assign from 'object-assign';

function noop() { return null; }

Expand Down Expand Up @@ -69,8 +70,8 @@ export default class Calendar extends React.Component {
locale = context.antLocale.Calendar;
}
// 统一合并为完整的 Locale
const result = { ...locale, ...props.locale };
result.lang = { ...locale.lang, ...props.locale.lang };
const result = assign({}, locale, props.locale);
result.lang = assign({}, locale.lang,props.locale.lang);
return result;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 8 additions & 4 deletions components/card/index.jsx → components/card/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React from 'react';
import classNames from 'classnames';

import splitObject from '../_util/splitObject';
export default props => {
let { prefixCls = 'ant-card', className, children, extra, bodyStyle,
title, loading, bordered = true, ...other } = props;
const [{
prefixCls = 'ant-card', className, extra, bodyStyle,
title, loading, bordered = true
}, others] = splitObject(props,
['prefixCls', 'className', 'children', 'extra', 'bodyStyle', 'title', 'loading', 'bordered']);
let children = props.children;
const classString = classNames({
[prefixCls]: true,
[className]: !!className,
Expand All @@ -30,7 +34,7 @@ export default props => {
) : null;

return (
<div {...other} className={classString}>
<div {...others} className={classString}>
{head}
{extra ? <div className={`${prefixCls}-extra`}>{extra}</div> : null}
<div className={`${prefixCls}-body`} style={bodyStyle}>{children}</div>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
import assign from 'object-assign';
if (typeof window !== 'undefined') {
const matchMediaPolyfill = function matchMediaPolyfill() {
return {
Expand All @@ -23,7 +24,7 @@ export default class Carousel extends React.Component {
}

render() {
let props = { ...this.props };
let props = assign({}, this.props);

if (props.effect === 'fade') {
props.fade = true;
Expand Down
File renamed without changes.
11 changes: 7 additions & 4 deletions components/cascader/index.jsx → components/cascader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Input from '../input';
import Icon from '../icon';
import arrayTreeFilter from 'array-tree-filter';
import classNames from 'classnames';
import splitObject from '../_util/splitObject';

export default class Cascader extends React.Component {
static defaultProps = {
Expand Down Expand Up @@ -71,8 +72,10 @@ export default class Cascader extends React.Component {

render() {
const props = this.props;
const { prefixCls, children, placeholder, size, disabled,
className, style, allowClear, ...otherProps } = props;
const [{prefixCls, children, placeholder, size, disabled,
className, style, allowClear}, others] = splitObject(props,
['prefixCls', 'children','placeholder', 'size','disabled', 'className','style','allowClear']);

const sizeCls = classNames({
'ant-input-lg': size === 'large',
'ant-input-sm': size === 'small',
Expand All @@ -93,7 +96,7 @@ export default class Cascader extends React.Component {
});

// Fix bug of https://github.com/facebook/react/pull/5004
delete otherProps.onChange;
delete others.onChange;

return (
<RcCascader {...props}
Expand All @@ -107,7 +110,7 @@ export default class Cascader extends React.Component {
style={style}
className={pickerCls}
>
<Input {...otherProps}
<Input {...others}
placeholder={this.state.value && this.state.value.length > 0 ? null : placeholder}
className={`${prefixCls}-input ant-input ${sizeCls}`}
value={null}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import Checkbox from './index';
import PureRenderMixin from 'react-addons-pure-render-mixin';

import assign from 'object-assign';
export default class CheckboxGroup extends React.Component {
static defaultProps = {
options: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import CheckboxGroup from './Group';
import classNames from 'classnames';
import PureRenderMixin from 'react-addons-pure-render-mixin';

import splitObject from '../_util/splitObject';
export default class Checkbox extends React.Component {
static Group = CheckboxGroup;
static defaultProps = {
Expand All @@ -13,7 +13,7 @@ export default class Checkbox extends React.Component {
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
render() {
const { prefixCls, style, children, className, ...restProps } = this.props;
const [{ prefixCls, style, children, className },restProps] = splitObject(this.props, ['prefixCls', 'style', 'children', 'className']);
const classString = classNames({
[className]: !!className,
[`${prefixCls}-wrapper`]: true,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@ import MonthCalendar from 'rc-calendar/lib/MonthCalendar';
import RcDatePicker from 'rc-calendar/lib/Picker';
import GregorianCalendar from 'gregorian-calendar';
import classNames from 'classnames';
import assign from 'object-assign';

export default function createPicker(TheCalendar) {
return class CalenderWrapper extends React.Component {
constructor(props) {
super(props);
// use class typescript error
const CalenderWrapper = React.createClass({

this.state = {
getInitialState() {
return {
value: this.props.parseDateFromValue(this.props.value || this.props.defaultValue),
};
}
},

componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({
value: nextProps.parseDateFromValue(nextProps.value),
});
}
}
},

handleChange = (value) => {
handleChange(value) {
const props = this.props;
if (!('value' in props)) {
this.setState({ value });
this.setState({value});
}
const timeValue = value ? new Date(value.getTime()) : null;
props.onChange(timeValue, value ? props.getFormatter().format(value) : '');
}
},

render() {
const props = this.props;
Expand Down Expand Up @@ -71,7 +72,7 @@ export default function createPicker(TheCalendar) {
}

return (
<span className={props.pickerClass} style={{ ...pickerStyle, ...props.style }}>
<span className={props.pickerClass} style={assign({}, pickerStyle, props.style)}>
<RcDatePicker
transitionName={props.transitionName}
disabled={props.disabled}
Expand All @@ -89,22 +90,24 @@ export default function createPicker(TheCalendar) {
{
({ value }) => {
return (
<span>
<span>
<input
disabled={props.disabled}
readOnly
value={value ? props.getFormatter().format(value) : ''}
placeholder={placeholder}
className={props.pickerInputClass}
/>
<span className="ant-calendar-picker-icon" />
<span className="ant-calendar-picker-icon"/>
</span>
);
);
}
}
}
</RcDatePicker>
</span>
);
}
};
},
});

return CalenderWrapper;
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import GregorianCalendarLocale from 'gregorian-calendar/lib/locale/en_US';
import CalendarLocale from 'rc-calendar/lib/locale/en_US';
import TimePickerLocale from '../../time-picker/locale/en_US';

import assign from 'object-assign';
// 统一合并为完整的 Locale
const locale = { ...GregorianCalendarLocale };
locale.lang = {
const locale = assign({}, GregorianCalendarLocale);
locale.lang = assign({
placeholder: 'Select date',
rangePlaceholder: ['Start date', 'End date'],
...CalendarLocale,
};
},CalendarLocale);

locale.timePickerLocale = { ...TimePickerLocale };
locale.timePickerLocale = assign({}, TimePickerLocale);

// All settings at:
// https://github.com/ant-design/ant-design/issues/424
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import GregorianCalendarLocale from 'gregorian-calendar/lib/locale/ru_RU';
import CalendarLocale from 'rc-calendar/lib/locale/ru_RU';
import TimePickerLocale from '../../time-picker/locale/ru_RU';

const locale = { ...GregorianCalendarLocale };
locale.lang = {
import assign from 'object-assign';
const locale = assign({}, GregorianCalendarLocale);
locale.lang = assign({
placeholder: 'Выберите дату',
rangePlaceholder: ['Начальная дата', 'Конечная дата'],
...CalendarLocale,
};
},CalendarLocale);

locale.timePickerLocale = { ...TimePickerLocale };
locale.timePickerLocale = assign({}, TimePickerLocale);

// All settings at:
// https://github.com/ant-design/ant-design/issues/424
Expand Down
Loading

0 comments on commit 9a0d751

Please sign in to comment.