Browse Source

refactor: remove splitObject

pull/4244/merge
Benjy Cui 8 years ago
parent
commit
1351b5da5d
  1. 12
      components/_util/splitObject.tsx
  2. 2
      components/badge/ScrollNumber.tsx
  3. 20
      components/badge/index.tsx
  4. 17
      components/breadcrumb/BreadcrumbItem.tsx
  5. 4
      components/button/button-group.tsx
  6. 5
      components/button/button.tsx
  7. 9
      components/card/index.tsx
  8. 14
      components/cascader/index.tsx
  9. 13
      components/checkbox/index.tsx
  10. 21
      components/dropdown/dropdown-button.tsx
  11. 2
      components/dropdown/dropdown.tsx
  12. 4
      components/grid/col.tsx
  13. 5
      components/grid/row.tsx
  14. 4
      components/input-number/index.tsx
  15. 5
      components/input/Search.tsx
  16. 4
      components/popconfirm/index.tsx
  17. 19
      components/progress/progress.tsx
  18. 8
      components/spin/index.tsx
  19. 8
      components/table/Table.tsx
  20. 9
      components/tag/CheckableTag.tsx
  21. 10
      components/tag/index.tsx
  22. 10
      components/timeline/Timeline.tsx
  23. 11
      components/timeline/TimelineItem.tsx
  24. 2
      components/tooltip/index.tsx

12
components/_util/splitObject.tsx

@ -1,12 +0,0 @@
export default function splitObject(obj, parts): Array<any> {
const left = {};
const right = {};
Object.keys(obj).forEach((k) => {
if (parts.indexOf(k) !== -1) {
left[k] = obj[k];
} else {
right[k] = obj[k];
}
});
return [left, right];
}

2
components/badge/ScrollNumber.tsx

@ -20,7 +20,7 @@ export interface ScrollNumberProps {
component?: string;
onAnimated?: Function;
height?: number;
style: React.CSSProperties;
style?: React.CSSProperties;
}
export default class ScrollNumber extends Component<ScrollNumberProps, any> {

20
components/badge/index.tsx

@ -3,7 +3,6 @@ import Animate from 'rc-animate';
import ScrollNumber from './ScrollNumber';
import classNames from 'classnames';
import warning from '../_util/warning';
import splitObject from '../_util/splitObject';
export interface BadgeProps {
/** Number to show in badge */
@ -37,23 +36,16 @@ export default class Badge extends React.Component<BadgeProps, any> {
};
render() {
let [{
count, prefixCls, overflowCount, className, style, children, dot, status, text,
}, restProps] = splitObject(
this.props,
['count', 'prefixCls', 'overflowCount', 'className', 'style', 'children', 'dot', 'status', 'text']
);
const { count, prefixCls, overflowCount, className, style, children, dot, status, text, ...restProps } = this.props;
const isDot = dot || status;
const realCount = count;
count = count > overflowCount ? `${overflowCount}+` : count;
let displayCount = count > overflowCount ? `${overflowCount}+` : count;
// dot mode don't need count
if (isDot) {
count = '';
displayCount = '';
}
// null undefined "" "0" 0
const hidden = (!count || count === '0') && !isDot;
const hidden = (!displayCount || displayCount === '0') && !isDot;
const scrollNumberCls = classNames({
[`${prefixCls}-dot`]: isDot,
[`${prefixCls}-count`]: !isDot,
@ -85,7 +77,7 @@ export default class Badge extends React.Component<BadgeProps, any> {
<ScrollNumber
data-show={!hidden}
className={scrollNumberCls}
count={count}
count={displayCount}
style={style}
/>
);
@ -95,7 +87,7 @@ export default class Badge extends React.Component<BadgeProps, any> {
);
return (
<span {...restProps} className={badgeCls} title={realCount}>
<span {...restProps} className={badgeCls} title={count as string}>
{children}
<Animate
component=""

17
components/breadcrumb/BreadcrumbItem.tsx

@ -1,7 +1,8 @@
import React from 'react';
import splitObject from '../_util/splitObject';
import { PropTypes } from 'react';
export interface BreadcrumbItemProps {
prefixCls?: string;
separator?: React.ReactNode;
href?: string;
}
@ -13,18 +14,16 @@ export default class BreadcrumbItem extends React.Component<BreadcrumbItemProps,
};
static propTypes = {
prefixCls: React.PropTypes.string,
separator: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.element,
prefixCls: PropTypes.string,
separator: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
]),
href: React.PropTypes.string,
href: PropTypes.string,
};
render() {
const [{ prefixCls, separator, children }, restProps] = splitObject(
this.props, ['prefixCls', 'separator', 'children']
);
const { prefixCls, separator, children, ...restProps } = this.props;
let link;
if ('href' in this.props) {
link = <a className={`${prefixCls}-link`} {...restProps}>{children}</a>;

4
components/button/button-group.tsx

@ -1,6 +1,5 @@
import React from 'react';
import classNames from 'classnames';
import splitObject from '../_util/splitObject';
export type ButtonSize = 'small' | 'large'
@ -12,8 +11,7 @@ export interface ButtonGroupProps {
}
export default function ButtonGroup(props: ButtonGroupProps) {
const [{ prefixCls = 'ant-btn-group', size, className }, others] =
splitObject(props, ['prefixCls', 'size', 'className']);
const { prefixCls = 'ant-btn-group', size = '', className, ...others } = props;
// large => lg
// small => sm

5
components/button/button.tsx

@ -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) {
@ -102,8 +102,7 @@ export default class Button extends React.Component<ButtonProps, any> {
render() {
const props = this.props;
const [{ type, shape, size, className, htmlType, children, icon, loading, prefixCls }, others] = splitObject(props,
['type', 'shape', 'size', 'className', 'htmlType', 'children', 'icon', 'loading', 'prefixCls']);
const { type, shape, size = '', className, htmlType, children, icon, loading, prefixCls, ...others } = props;
// large => lg
// small => sm

9
components/card/index.tsx

@ -1,8 +1,8 @@
import React from 'react';
import classNames from 'classnames';
import splitObject from '../_util/splitObject';
export interface CardProps {
prefixCls?: string;
title?: React.ReactNode;
extra?: React.ReactNode;
bordered?: boolean;
@ -15,11 +15,10 @@ export interface CardProps {
}
export default (props: CardProps) => {
const [{
const {
prefixCls = 'ant-card', className, extra, bodyStyle,
title, loading, bordered = true,
}, others] = splitObject(props,
['prefixCls', 'className', 'children', 'extra', 'bodyStyle', 'title', 'loading', 'bordered']);
title, loading, bordered = true, ...others,
} = props;
let children = props.children;
const classString = classNames(prefixCls, className, {
[`${prefixCls}-loading`]: loading,

14
components/cascader/index.tsx

@ -5,7 +5,6 @@ import classNames from 'classnames';
import omit from 'omit.js';
import Input from '../input';
import Icon from '../icon';
import splitObject from '../_util/splitObject';
export interface CascaderOptionType {
value: string;
@ -101,7 +100,6 @@ export default class Cascader extends React.Component<CascaderProps, any> {
options: [],
disabled: false,
allowClear: true,
showSearch: false,
notFoundContent: 'Not Found',
};
@ -247,10 +245,10 @@ export default class Cascader extends React.Component<CascaderProps, any> {
render() {
const { props, state } = this;
const [{ prefixCls, inputPrefixCls, children, placeholder, size, disabled,
className, style, allowClear, showSearch }, otherProps] = splitObject(props,
['prefixCls', 'inputPrefixCls', 'children', 'placeholder', 'size', 'disabled',
'className', 'style', 'allowClear', 'showSearch']);
const {
prefixCls, inputPrefixCls, children, placeholder, size, disabled,
className, style, allowClear, showSearch = false, ...otherProps,
} = props;
const value = state.value;
const sizeCls = classNames({
@ -311,7 +309,7 @@ export default class Cascader extends React.Component<CascaderProps, any> {
dropdownMenuColumnStyle.height = 'auto'; // Height of one row.
}
// The default value of `matchInputWidth` is `true`
const resultListMatchInputWidth = showSearch.matchInputWidth === false ? false : true;
const resultListMatchInputWidth = (showSearch as ShowSearchType).matchInputWidth === false ? false : true;
if (resultListMatchInputWidth && state.inputValue && this.refs.input) {
dropdownMenuColumnStyle.width = this.refs.input.refs.input.offsetWidth;
}
@ -324,7 +322,7 @@ export default class Cascader extends React.Component<CascaderProps, any> {
<Input
{...inputProps}
ref="input"
placeholder={value && value.length > 0 ? null : placeholder}
placeholder={value && value.length > 0 ? undefined : placeholder}
className={`${prefixCls}-input ${sizeCls}`}
value={state.inputValue}
disabled={disabled}

13
components/checkbox/index.tsx

@ -3,9 +3,9 @@ import React from 'react';
import CheckboxGroup from './Group';
import classNames from 'classnames';
import PureRenderMixin from 'rc-util/lib/PureRenderMixin';
import splitObject from '../_util/splitObject';
export interface CheckboxProps {
prefixCls?: string;
/** 指定当前是否选中 */
checked?: boolean;
/** 初始是否选中 */
@ -14,6 +14,8 @@ export interface CheckboxProps {
indeterminate?: boolean;
/** 变化时回调函数 */
onChange?: React.FormEventHandler<any>;
onMouseEnter?: React.MouseEventHandler<any>;
onMouseLeave?: React.MouseEventHandler<any>;
style?: React.CSSProperties;
disabled?: boolean;
className?: string;
@ -29,11 +31,10 @@ export default class Checkbox extends React.Component<CheckboxProps, any> {
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
render() {
const [{ prefixCls, style, children, className, indeterminate, onMouseEnter,
onMouseLeave }, restProps] = splitObject(
this.props, ['prefixCls', 'style', 'children', 'className', 'indeterminate',
'onMouseEnter', 'onMouseLeave']
);
const {
prefixCls, style, children, className, indeterminate,
onMouseEnter, onMouseLeave, ...restProps,
} = this.props;
const classString = classNames(className, {
[`${prefixCls}-wrapper`]: true,
});

21
components/dropdown/dropdown-button.tsx

@ -4,19 +4,20 @@ import Icon from '../icon';
import Dropdown from './dropdown';
const ButtonGroup = Button.Group;
import classNames from 'classnames';
import splitObject from '../_util/splitObject';
export interface DropdownButtonProps {
type?: 'primary' | 'ghost' | 'dash';
onClick?: React.FormEventHandler<any>;
trigger?: 'click' | 'hover';
prefixCls?: string;
className?: string;
type?: 'primary' | 'ghost' | 'dashed';
onClick?: React.MouseEventHandler<any>;
trigger?: ('click' | 'hover')[];
align?: any;
overlay: React.ReactNode;
visible?: boolean;
disabled?: boolean;
onVisibleChange?: (visible: boolean) => void;
style?: React.CSSProperties;
prefixCls?: string;
children: any;
children?: any;
}
export default class DropdownButton extends React.Component<DropdownButtonProps, any> {
@ -35,10 +36,10 @@ export default class DropdownButton extends React.Component<DropdownButtonProps,
};
render() {
const [{ type, overlay, trigger, align, children, className, onClick, prefixCls,
disabled, visible, onVisibleChange }, restProps] = splitObject(this.props,
['type', 'overlay', 'trigger', 'align', 'children', 'className', 'onClick',
'prefixCls', 'disabled', 'visible', 'onVisibleChange']);
const {
type, overlay, trigger, align, children, className, onClick, prefixCls,
disabled, visible, onVisibleChange, ...restProps,
} = this.props;
const cls = classNames(prefixCls, className);
const dropdownProps = {

2
components/dropdown/dropdown.tsx

@ -3,7 +3,7 @@ import RcDropdown from 'rc-dropdown';
import classNames from 'classnames';
export interface DropDownProps {
trigger?: Array<'click' | 'hover'>;
trigger?: ('click' | 'hover')[];
overlay: React.ReactNode;
style?: React.CSSProperties;
onVisibleChange?: (visible?: boolean) => void;

4
components/grid/col.tsx

@ -2,7 +2,6 @@ import React from 'react';
import { PropTypes } from 'react';
import classNames from 'classnames';
import assign from 'object-assign';
import splitObject from '../_util/splitObject';
const stringOrNumber = PropTypes.oneOfType([PropTypes.string, PropTypes.number]);
const objectOrNumber = PropTypes.oneOfType([PropTypes.object, PropTypes.number]);
@ -47,8 +46,7 @@ export default class Col extends React.Component<ColProps, any> {
render() {
const props = this.props;
const [{ span, order, offset, push, pull, className, children, prefixCls = 'ant-col' }, others] = splitObject(props,
['span', 'order', 'offset', 'push', 'pull', 'className', 'children', 'prefixCls']);
const { span, order, offset, push, pull, className, children, prefixCls = 'ant-col', ...others } = props;
let sizeClassObj = {};
['xs', 'sm', 'md', 'lg'].forEach(size => {
let sizeProps: ColSize = {};

5
components/grid/row.tsx

@ -2,7 +2,6 @@ import React from 'react';
import { Children, cloneElement } from 'react';
import classNames from 'classnames';
import assign from 'object-assign';
import splitObject from '../_util/splitObject';
export interface RowProps {
className?: string;
@ -17,7 +16,6 @@ export interface RowProps {
export default class Row extends React.Component<RowProps, any> {
static defaultProps = {
gutter: 0,
prefixCls: 'ant-row',
};
static propTypes = {
type: React.PropTypes.string,
@ -29,8 +27,7 @@ export default class Row extends React.Component<RowProps, any> {
prefixCls: React.PropTypes.string,
};
render() {
const [{ type, justify, align, className, gutter, style, children, prefixCls }, others] = splitObject(this.props,
['type', 'justify', 'align', 'className', 'gutter', 'style', 'children', 'prefixCls']);
const { type, justify, align, className, gutter, style, children, prefixCls = 'ant-row', ...others } = this.props;
const classes = classNames({
[prefixCls]: !type,
[`${prefixCls}-${type}`]: type,

4
components/input-number/index.tsx

@ -1,7 +1,6 @@
import React from 'react';
import classNames from 'classnames';
import RcInputNumber from 'rc-input-number';
import splitObject from '../_util/splitObject';
export interface InputNumberProps {
prefixCls?: string;
@ -25,8 +24,7 @@ export default class InputNumber extends React.Component<InputNumberProps, any>
};
render() {
const [{ className, size }, others] = splitObject(this.props,
['size', 'className']);
const { className, size, ...others } = this.props;
const inputNumberClass = classNames({
[`${this.props.prefixCls}-lg`]: size === 'large',
[`${this.props.prefixCls}-sm`]: size === 'small',

5
components/input/Search.tsx

@ -2,7 +2,6 @@ import React from 'react';
import classNames from 'classnames';
import Input from './Input';
import Icon from '../icon';
import splitObject from '../_util/splitObject';
export interface SearchProps {
className?: string;
@ -32,9 +31,7 @@ export default class Search extends React.Component<SearchProps, any> {
this.input.refs.input.focus();
}
render() {
const [{ className, prefixCls, style }, others] = splitObject(
this.props, ['className', 'prefixCls', 'style']
);
const { className, prefixCls, style, ...others } = this.props;
delete others.onSearch;
const wrapperCls = classNames({
[`${prefixCls}-wrapper`]: true,

4
components/popconfirm/index.tsx

@ -3,7 +3,6 @@ import Tooltip from '../tooltip';
import { AbstractTooltipProps } from '../tooltip';
import Icon from '../icon';
import Button from '../button';
import splitObject from '../_util/splitObject';
export interface PopconfirmProps extends AbstractTooltipProps {
title: React.ReactNode;
@ -83,8 +82,7 @@ export default class Popconfirm extends React.Component<PopconfirmProps, any> {
render() {
const { props, context } = this;
const [{ prefixCls, title, placement }, restProps] =
splitObject(props, ['prefixCls', 'title', 'placement']);
const { prefixCls, title, placement, ...restProps } = props;
let { okText, cancelText } = props;
const popconfirmLocale = context.antLocale && context.antLocale.Popconfirm;

19
components/progress/progress.tsx

@ -3,7 +3,6 @@ import React from 'react';
import Icon from '../icon';
import { Circle } from 'rc-progress';
import classNames from 'classnames';
import splitObject from '../_util/splitObject';
const statusColorMap = {
normal: '#108ee9',
exception: '#ff5500',
@ -11,12 +10,15 @@ const statusColorMap = {
};
export interface ProgressProps {
prefixCls?: string;
className?: string;
type?: 'line' | 'circle';
percent?: number;
format?: (percent: number) => string;
status?: 'success' | 'active' | 'exception';
showInfo?: boolean;
strokeWidth?: number;
trailColor?: string;
width?: number;
style?: React.CSSProperties;
}
@ -45,14 +47,13 @@ export default class Progress extends React.Component<ProgressProps, any> {
};
render() {
const [{
prefixCls, status, format, percent, trailColor,
type, strokeWidth, width, className, showInfo,
}, restProps] = splitObject(this.props,
['prefixCls', 'status', 'format', 'percent', 'trailColor', 'type', 'strokeWidth', 'width',
'className', 'showInfo']);
const progressStatus = (parseInt(percent, 10) >= 100 && !('status' in this.props))
? 'success' : (status || 'normal');
const props = this.props;
const {
prefixCls, className, percent = 0, status, format, trailColor,
type, strokeWidth, width, showInfo, ...restProps,
} = props;
const progressStatus = parseInt(percent.toString(), 10) >= 100 && !('status' in props) ?
'success' : (status || 'normal');
let progressInfo;
let progress;
const textFormatter = format || (percentNumber => `${percentNumber}%`);

8
components/spin/index.tsx

@ -4,7 +4,6 @@ import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import Animate from 'rc-animate';
import isCssAnimationSupported from '../_util/isCssAnimationSupported';
import splitObject from '../_util/splitObject';
import omit from 'omit.js';
export interface SpinProps {
@ -69,17 +68,14 @@ export default class Spin extends React.Component<SpinProps, any> {
}
}
render() {
const [{
className, size, prefixCls, tip,
}, restProps] = splitObject(this.props,
['className', 'size', 'prefixCls', 'tip']);
const { className, size, prefixCls, tip, ...restProps } = this.props;
const { spinning } = this.state;
const spinClassName = classNames(prefixCls, {
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-spinning`]: spinning,
[`${prefixCls}-show-text`]: !!this.props.tip,
[`${prefixCls}-show-text`]: !!tip,
}, className);
// fix https://fb.me/react-unknown-prop

8
components/table/Table.tsx

@ -7,7 +7,6 @@ import Spin from '../spin';
import classNames from 'classnames';
import { flatArray, treeMap, normalizeColumns } from './util';
import assign from 'object-assign';
import splitObject from '../_util/splitObject';
import warning from '../_util/warning';
import createStore, { Store } from './createStore';
import SelectionBox from './SelectionBox';
@ -75,6 +74,7 @@ export interface TableProps<T> {
childrenColumnName?: string;
bodyStyle?: React.CSSProperties;
className?: string;
style?: React.CSSProperties;
}
export interface TableContext {
@ -823,9 +823,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
}
render() {
const [{
style, className, prefixCls,
}, restProps] = splitObject(this.props, ['style', 'className', 'prefixCls']);
const { style, className, prefixCls, ...restProps } = this.props;
const data = this.getCurrentPageData();
let columns = this.renderRowSelection();
const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
@ -846,7 +844,7 @@ export default class Table<T> extends React.Component<TableProps<T>, any> {
let expandIconColumnIndex = (columns[0] && columns[0].key === 'selection-column') ? 1 : 0;
if ('expandIconColumnIndex' in restProps) {
expandIconColumnIndex = restProps.expandIconColumnIndex;
expandIconColumnIndex = restProps.expandIconColumnIndex as number;
}
let table = (

9
components/tag/CheckableTag.tsx

@ -1,6 +1,5 @@
import React from 'react';
import classNames from 'classnames';
import splitObject from '../_util/splitObject';
export interface CheckableTagProps {
prefixCls?: string;
@ -17,15 +16,13 @@ export default class CheckableTag extends React.Component<CheckableTagProps, any
}
}
render() {
const [{ prefixCls = 'ant-tag', className = '', checked }, restProps ] = splitObject(
this.props, ['prefixCls', 'className', 'checked']
);
const { prefixCls = 'ant-tag', className, checked, ...restProps } = this.props;
const cls = classNames(prefixCls, {
[`${prefixCls}-checkable`]: true,
[`${prefixCls}-checkable-checked`]: checked,
}, className);
delete restProps.onChange;
return <div {...restProps} className={cls} onClick={this.handleClick} />;
delete restProps.onChange; // TypeScript cannot check delete now.
return <div {...restProps as any} className={cls} onClick={this.handleClick} />;
}
}

10
components/tag/index.tsx

@ -6,10 +6,11 @@ import omit from 'omit.js';
import assign from 'object-assign';
import Icon from '../icon';
import warning from '../_util/warning';
import splitObject from '../_util/splitObject';
import CheckableTag from './CheckableTag';
export interface TagProps {
prefixCls?: string;
className?: string;
color?: string;
/** 标签是否可以关闭 */
closable?: boolean;
@ -73,12 +74,7 @@ export default class Tag extends React.Component<TagProps, any> {
}
render() {
const [{
prefixCls, closable, color, className, children, style,
}, otherProps] = splitObject(
this.props,
['prefixCls', 'closable', 'color', 'className', 'children', 'style']
);
const { prefixCls, closable, color = '', className, children, style, ...otherProps } = this.props;
const closeIcon = closable ? <Icon type="cross" onClick={this.close} /> : '';
const classString = classNames(prefixCls, {
[`${prefixCls}-${color}`]: !!color,

10
components/timeline/Timeline.tsx

@ -1,9 +1,10 @@
import React from 'react';
import classNames from 'classnames';
import TimelineItem from './TimelineItem';
import splitObject from '../_util/splitObject';
export interface TimelineProps {
prefixCls?: string;
className?: string;
/** 指定最后一个幽灵节点是否存在或内容 */
pending?: boolean | React.ReactNode;
style?: React.CSSProperties;
@ -16,17 +17,14 @@ export default class Timeline extends React.Component<TimelineProps, any> {
};
render() {
const [{
prefixCls, children, pending, className,
}, restProps] = splitObject(this.props,
['prefixCls', 'children', 'pending', 'className']);
const { prefixCls, children, pending, className, ...restProps } = this.props;
const pendingNode = typeof pending === 'boolean' ? null : pending;
const classString = classNames(prefixCls, {
[`${prefixCls}-pending`]: !!pending,
}, className);
const items = React.Children.map(children, (ele: React.ReactElement<any>, idx) =>
React.cloneElement(ele, {
last: idx === children.length - 1,
last: idx === (children as { length: number }).length - 1,
})
);
const pendingItem = (!!pending) ? (

11
components/timeline/TimelineItem.tsx

@ -1,13 +1,13 @@
import React from 'react';
import classNames from 'classnames';
import splitObject from '../_util/splitObject';
// Timeline
export interface TimeLineItemProps {
/** 指定圆圈颜色 */
prefixCls?: string;
className?: string;
color?: string;
dot?: React.ReactNode;
pending?: boolean;
last?: boolean;
style?: React.CSSProperties;
}
@ -20,10 +20,7 @@ export default class TimelineItem extends React.Component<TimeLineItemProps, any
};
render() {
const [{
prefixCls, color, last, children, pending, className, dot,
}, restProps] = splitObject(this.props,
['prefixCls', 'color', 'last', 'children', 'pending', 'className', 'dot']);
const { prefixCls, className, color = '', last, children, pending, dot, ...restProps } = this.props;
const itemClassName = classNames({
[`${prefixCls}-item`]: true,

2
components/tooltip/index.tsx

@ -27,7 +27,7 @@ export interface AbstractTooltipProps {
}
export interface TooltipProps extends AbstractTooltipProps {
title: React.ReactNode;
title?: React.ReactNode;
overlay?: React.ReactNode;
}

Loading…
Cancel
Save