Skip to content

Commit

Permalink
refactor: close: ant-design#3490
Browse files Browse the repository at this point in the history
  • Loading branch information
benjycui committed Oct 24, 2016
1 parent 4878258 commit 08e21e0
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 127 deletions.
16 changes: 9 additions & 7 deletions components/radio/group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ export interface RadioGroupProps {

export default class RadioGroup extends React.Component<RadioGroupProps, any> {
static defaultProps = {
prefixCls: 'ant-radio-group',
disabled: false,
onChange() {
},
};
constructor(props) {
super(props);
Expand Down Expand Up @@ -76,11 +73,15 @@ export default class RadioGroup extends React.Component<RadioGroupProps, any> {
value: ev.target.value,
});
}
this.props.onChange(ev);

const onChange = this.props.onChange;
if (onChange) {
onChange(ev);
}
}
render() {
const props = this.props;
const children = React.Children.map(props.children, (radio: any) => {
const children = React.Children.map((props.children || {}), (radio: any) => {
if (radio && (radio.type === Radio || radio.type === RadioButton) && radio.props) {
const keyProps = {};
if (!('key' in radio) && typeof radio.props.value === 'string') {
Expand All @@ -94,9 +95,10 @@ export default class RadioGroup extends React.Component<RadioGroupProps, any> {
}
return radio;
});
const prefixCls = props.prefixCls || 'ant-radio-group';
const classString = classNames({
[props.prefixCls]: true,
[`${props.prefixCls}-${props.size}`]: props.size,
[prefixCls]: true,
[`${prefixCls}-${props.size}`]: props.size,
});
return <div className={classString} style={props.style}>{children}</div>;
}
Expand Down
2 changes: 1 addition & 1 deletion components/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class Radio extends React.Component<RadioProps, any> {
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
render() {
const { prefixCls, children, checked, disabled, className, style } = this.props;
const { prefixCls, children, checked, disabled, className = '', style } = this.props;
const wrapperClassString = classNames({
[`${prefixCls}-wrapper`]: true,
[`${prefixCls}-wrapper-checked`]: checked,
Expand Down
4 changes: 2 additions & 2 deletions components/select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface SelectProps {
defaultValue?: SelectValue;
size?: 'default' | 'large' | 'small';
combobox?: boolean;
notFoundContent?: React.ReactNode | string;
notFoundContent?: React.ReactNode | null;
showSearch?: boolean;
transitionName?: string;
choiceTransitionName?: string;
Expand Down Expand Up @@ -84,7 +84,7 @@ export default class Select extends React.Component<SelectProps, any> {
render() {
const {
prefixCls,
className,
className = '',
size,
combobox,
showSearch,
Expand Down
2 changes: 1 addition & 1 deletion components/switch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class Switch extends React.Component<SwitchProps, any> {
};

render() {
const { prefixCls, size, className } = this.props;
const { prefixCls, size, className = '' } = this.props;
const classes = classNames({
[className]: !!className,
[`${prefixCls}-small`]: size === 'small',
Expand Down
13 changes: 13 additions & 0 deletions components/table/FilterDropdownMenuWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';

export interface FilterDropdownMenuWrapperProps {
onClick?: React.MouseEventHandler<any>;
children?: any;
className?: string;
}
export default class FilterDropdownMenuWrapper extends React.Component<FilterDropdownMenuWrapperProps, any> {
render() {
const { onClick, children, className } = this.props;
return <div className={className} onClick={onClick}>{children}</div>;
}
}
67 changes: 39 additions & 28 deletions components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface TableProps {
pagination?: PaginationProps | boolean;
size?: 'default' | 'small';
dataSource?: Object[];
columns?: TableColumnConfig[];
columns: TableColumnConfig[];
rowKey?: string | ((record: Object, index: number) => string);
rowClassName?: (record: Object, index: number) => string;
expandedRowRender?: any;
Expand All @@ -87,7 +87,7 @@ export interface TableProps {
footer?: (currentPageData: Object[]) => React.ReactNode;
title?: (currentPageData: Object[]) => React.ReactNode;
scroll?: { x?: boolean | number, y?: boolean | number};
childrenColumnName?: 'string';
childrenColumnName?: string;
bodyStyle?: React.CSSProperties;
className?: string;
}
Expand Down Expand Up @@ -117,18 +117,15 @@ export default class Table extends React.Component<TableProps, any> {
static defaultProps = {
dataSource: [],
prefixCls: 'ant-table',
dropdownPrefixCls: 'ant-dropdown',
useFixedHeader: false,
rowSelection: null,
className: '',
size: 'large',
loading: false,
bordered: false,
indentSize: 20,
onChange: noop,
locale: {},
rowKey: 'key',
childrenColumnName: 'children',
};

static contextTypes = {
Expand Down Expand Up @@ -361,7 +358,10 @@ export default class Table extends React.Component<TableProps, any> {
this.setState(newState);
}

this.props.onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, newState)));
const onChange = this.props.onChange;
if (onChange) {
onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, newState)));
}
}

handleFilter = (column, nextFilters) => {
Expand Down Expand Up @@ -409,11 +409,14 @@ export default class Table extends React.Component<TableProps, any> {
}

this.setState(newState, () => {
props.onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
selectionDirty: false,
filters,
pagination,
})));
const onChange = this.props.onChange;
if (onChange) {
onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
selectionDirty: false,
filters,
pagination,
})));
}
});
}

Expand Down Expand Up @@ -463,7 +466,7 @@ export default class Table extends React.Component<TableProps, any> {
.map((item, i) => this.getRecordKey(item, i));

// 记录变化的列
const changeRowKeys = [];
const changeRowKeys: string[] = [];
if (checked) {
changableRowKeys.forEach(key => {
if (selectedRowKeys.indexOf(key) < 0) {
Expand Down Expand Up @@ -511,10 +514,13 @@ export default class Table extends React.Component<TableProps, any> {
}
this.setState(newState);

this.props.onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
selectionDirty: false,
pagination,
})));
const onChange = this.props.onChange;
if (onChange) {
onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
selectionDirty: false,
pagination,
})));
}
}

renderSelectionRadio = (_, record, index) => {
Expand Down Expand Up @@ -558,7 +564,7 @@ export default class Table extends React.Component<TableProps, any> {
);
}

getRecordKey(record, index?) {
getRecordKey(record, index?): string {
const rowKey = this.props.rowKey;
if (typeof rowKey === 'function') {
return rowKey(record, index);
Expand All @@ -584,11 +590,11 @@ export default class Table extends React.Component<TableProps, any> {
}

renderRowSelection() {
const prefixCls = this.props.prefixCls;
const { prefixCls, rowSelection } = this.props;
const columns = this.props.columns.concat();
if (this.props.rowSelection) {
if (rowSelection) {
const data = this.getFlatCurrentPageData().filter((item) => {
if (this.props.rowSelection.getCheckboxProps) {
if (rowSelection.getCheckboxProps) {
return !this.getCheckboxPropsByItem(item).disabled;
}
return true;
Expand Down Expand Up @@ -617,7 +623,7 @@ export default class Table extends React.Component<TableProps, any> {
);
}
let selectionColumn;
if (this.props.rowSelection.type === 'radio') {
if (rowSelection.type === 'radio') {
selectionColumn = {
key: 'selection-column',
render: this.renderSelectionRadio,
Expand Down Expand Up @@ -689,7 +695,7 @@ export default class Table extends React.Component<TableProps, any> {
selectedKeys={colFilters}
confirmFilter={this.handleFilter}
prefixCls={`${prefixCls}-filter`}
dropdownPrefixCls={dropdownPrefixCls}
dropdownPrefixCls={dropdownPrefixCls || 'ant-dropdown'}
/>
);
}
Expand Down Expand Up @@ -736,9 +742,13 @@ export default class Table extends React.Component<TableProps, any> {
pagination.onShowSizeChange(current, pageSize);
const nextPagination = assign({}, pagination, { pageSize, current });
this.setState({ pagination: nextPagination });
this.props.onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
pagination: nextPagination,
})));

const onChange = this.props.onChange;
if (onChange) {
onChange.apply(null, this.prepareParamsArguments(assign({}, this.state, {
pagination: nextPagination,
})));
}
}

renderPagination() {
Expand Down Expand Up @@ -819,7 +829,7 @@ export default class Table extends React.Component<TableProps, any> {
}

recursiveSort(data, sorterFn) {
const { childrenColumnName } = this.props;
const { childrenColumnName = 'children' } = this.props;
return data.sort(sorterFn).map(item => (item[childrenColumnName] ? assign(
{},
item, {
Expand Down Expand Up @@ -849,8 +859,9 @@ export default class Table extends React.Component<TableProps, any> {
if (values.length === 0) {
return;
}
data = col.onFilter ? data.filter(record => {
return values.some(v => col.onFilter(v, record));
const onFilter = col.onFilter;
data = onFilter ? data.filter(record => {
return values.some(v => onFilter(v, record));
}) : data;
});
}
Expand Down
10 changes: 1 addition & 9 deletions components/table/filterDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@ import Dropdown from '../dropdown';
import Icon from '../icon';
import Checkbox from '../checkbox';
import Radio from '../radio';

export interface FilterDropdownMenuWrapperProps {
onClick?: React.MouseEventHandler<any>;
children?: any;
className?: string;
}
const FilterDropdownMenuWrapper: React.StatelessComponent<FilterDropdownMenuWrapperProps> =
({ onClick, children, className }) =>
<div className={className} onClick={onClick}>{children}</div>;
import FilterDropdownMenuWrapper from './FilterDropdownMenuWrapper';

export interface FilterMenuProps {
locale: any;
Expand Down
4 changes: 2 additions & 2 deletions components/table/util.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assign from 'object-assign';
export function flatArray(data = [], childrenName = 'children') {
const result = [];
export function flatArray(data: Object[] = [], childrenName = 'children') {
const result: Object[] = [];
const loop = (array) => {
array.forEach(item => {
const newItem = assign({}, item);
Expand Down
31 changes: 20 additions & 11 deletions components/tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface TabsProps {
hideAdd?: boolean;
onChange?: (activeKey: string) => void;
onTabClick?: Function;
tabBarExtraContent?: React.ReactNode;
tabBarExtraContent?: React.ReactNode | null;
type?: TabsType;
tabPosition?: TabsPosition;
onEdit?: (targetKey: string, action: any) => void;
Expand All @@ -24,6 +24,7 @@ export interface TabsProps {
prefixCls?: string;
className?: string;
animated?: boolean;
children: any;
}

// Tabs
Expand All @@ -38,43 +39,51 @@ export default class Tabs extends React.Component<TabsProps, any> {

static defaultProps = {
prefixCls: 'ant-tabs',
type: 'line', // or 'card' 'editable-card'
onChange() { },
onEdit() { },
hideAdd: false,
animated: true,
};

createNewTab = (targetKey) => {
this.props.onEdit(targetKey, 'add');
const onEdit = this.props.onEdit;
if (onEdit) {
onEdit(targetKey, 'add');
}
}

removeTab = (targetKey, e) => {
e.stopPropagation();
if (!targetKey) {
return;
}
this.props.onEdit(targetKey, 'remove');

const onEdit = this.props.onEdit;
if (onEdit) {
onEdit(targetKey, 'remove');
}
}

handleChange = (activeKey) => {
this.props.onChange(activeKey);
const onChange = this.props.onChange;
if (onChange) {
onChange(activeKey);
}
}

render() {
let {
prefixCls,
className = '',
size,
type,
type = 'line',
tabPosition,
children,
tabBarExtraContent,
hideAdd,
onTabClick,
animated,
} = this.props;
let className = classNames({
[this.props.className]: !!this.props.className,
let cls = classNames({
[className]: !!className,
[`${prefixCls}-mini`]: size === 'small' || size as string === 'mini',
[`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right',
[`${prefixCls}-card`]: type.indexOf('card') >= 0,
Expand Down Expand Up @@ -115,7 +124,7 @@ export default class Tabs extends React.Component<TabsProps, any> {

return (
<RcTabs {...this.props}
className={className}
className={cls}
tabBarPosition={tabPosition}
renderTabBar={() => (
<ScrollableInkTabBar
Expand Down
Loading

0 comments on commit 08e21e0

Please sign in to comment.