ant-design-vue/components/table/index.jsx

118 lines
3.4 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import T from './Table';
2018-03-29 22:08:04 +08:00
2019-01-12 11:33:27 +08:00
import {
getOptionProps,
getKey,
getClass,
getStyle,
getEvents,
getSlotOptions,
camelize,
getSlots,
} from '../_util/props-util';
import Base from '../base';
2018-04-01 16:50:31 +08:00
2018-04-01 12:52:27 +08:00
const Table = {
2018-04-08 21:17:20 +08:00
name: 'ATable',
2018-04-01 12:52:27 +08:00
Column: T.Column,
ColumnGroup: T.ColumnGroup,
props: T.props,
methods: {
2019-01-12 11:33:27 +08:00
normalize(elements = []) {
const columns = [];
2018-04-01 12:52:27 +08:00
elements.forEach(element => {
if (!element.tag) {
2019-01-12 11:33:27 +08:00
return;
2018-04-01 12:52:27 +08:00
}
2019-01-12 11:33:27 +08:00
const key = getKey(element);
const style = getStyle(element);
const cls = getClass(element);
const props = getOptionProps(element);
const events = getEvents(element);
const listeners = {};
2018-04-01 12:52:27 +08:00
Object.keys(events).forEach(e => {
2019-01-12 11:33:27 +08:00
const k = `on-${e}`;
listeners[camelize(k)] = events[e];
});
const { default: children, ...restSlots } = getSlots(element);
const column = { ...restSlots, ...props, style, class: cls, ...listeners };
2018-04-01 12:52:27 +08:00
if (key) {
2019-01-12 11:33:27 +08:00
column.key = key;
2018-04-01 12:52:27 +08:00
}
if (getSlotOptions(element).__ANT_TABLE_COLUMN_GROUP) {
2019-08-07 21:56:30 +08:00
column.children = this.normalize(typeof children === 'function' ? children() : children);
2018-04-01 12:52:27 +08:00
} else {
2019-01-12 11:33:27 +08:00
const customRender =
element.data && element.data.scopedSlots && element.data.scopedSlots.default;
column.customRender = column.customRender || customRender;
2018-04-01 12:52:27 +08:00
}
2019-01-12 11:33:27 +08:00
columns.push(column);
});
return columns;
2018-04-01 12:52:27 +08:00
},
2019-01-12 11:33:27 +08:00
updateColumns(cols = []) {
const columns = [];
const { $slots, $scopedSlots } = this;
2018-04-01 12:52:27 +08:00
cols.forEach(col => {
2019-01-12 11:33:27 +08:00
const { slots = {}, scopedSlots = {}, ...restProps } = col;
2018-04-01 12:52:27 +08:00
const column = {
...restProps,
2019-01-12 11:33:27 +08:00
};
2018-04-02 09:48:03 +08:00
Object.keys(slots).forEach(key => {
2019-01-12 11:33:27 +08:00
const name = slots[key];
2018-04-02 09:48:03 +08:00
if (column[key] === undefined && $slots[name]) {
2019-01-12 11:33:27 +08:00
column[key] = $slots[name].length === 1 ? $slots[name][0] : $slots[name];
2018-04-02 09:48:03 +08:00
}
2019-01-12 11:33:27 +08:00
});
2018-04-02 09:48:03 +08:00
Object.keys(scopedSlots).forEach(key => {
2019-01-12 11:33:27 +08:00
const name = scopedSlots[key];
2018-04-02 09:48:03 +08:00
if (column[key] === undefined && $scopedSlots[name]) {
2019-01-12 11:33:27 +08:00
column[key] = $scopedSlots[name];
2018-04-02 09:48:03 +08:00
}
2019-01-12 11:33:27 +08:00
});
2018-04-02 09:48:03 +08:00
// if (slotScopeName && $scopedSlots[slotScopeName]) {
// column.customRender = column.customRender || $scopedSlots[slotScopeName]
// }
2018-04-01 12:52:27 +08:00
if (col.children) {
2019-01-12 11:33:27 +08:00
column.children = this.updateColumns(column.children);
2018-04-01 12:52:27 +08:00
}
2019-01-12 11:33:27 +08:00
columns.push(column);
});
return columns;
2018-04-01 12:52:27 +08:00
},
},
2019-01-12 11:33:27 +08:00
render() {
const { $listeners, $slots, normalize, $scopedSlots } = this;
const props = getOptionProps(this);
const columns = props.columns ? this.updateColumns(props.columns) : normalize($slots.default);
let { title, footer } = props;
2018-04-02 22:13:11 +08:00
const {
title: slotTitle,
footer: slotFooter,
expandedRowRender = props.expandedRowRender,
2019-01-12 11:33:27 +08:00
} = $scopedSlots;
title = title || slotTitle;
footer = footer || slotFooter;
2018-04-01 12:52:27 +08:00
const tProps = {
props: {
...props,
columns,
2018-04-01 16:50:31 +08:00
title,
footer,
2018-04-02 22:13:11 +08:00
expandedRowRender,
2018-04-01 12:52:27 +08:00
},
on: $listeners,
2019-01-12 11:33:27 +08:00
};
return <T {...tProps} />;
2018-04-01 12:52:27 +08:00
},
2019-01-12 11:33:27 +08:00
};
/* istanbul ignore next */
2019-01-12 11:33:27 +08:00
Table.install = function(Vue) {
Vue.use(Base);
2019-01-12 11:33:27 +08:00
Vue.component(Table.name, Table);
Vue.component(Table.Column.name, Table.Column);
Vue.component(Table.ColumnGroup.name, Table.ColumnGroup);
};
2018-03-29 22:08:04 +08:00
2019-01-12 11:33:27 +08:00
export default Table;