antd-mini
Version:
antd-mini 是支付宝小程序 UI 组件库,遵循 Ant Design 规范。
176 lines (175 loc) • 8.17 kB
JavaScript
import { __assign, __awaiter, __generator, __spreadArray } from "tslib";
import equal from 'fast-deep-equal';
import filter from 'lodash.filter';
import get from 'lodash.get';
import reduce from 'lodash.reduce';
import { getSystemInfo } from '../_util/jsapi/get-system-info';
import { Component, getValueFromProps } from '../_util/simply';
import { TableDefaultProps } from './props';
var rpx2px = function (rpx, windowWidth) {
if (windowWidth === void 0) { windowWidth = 375; }
var numRpx = rpx;
if (typeof rpx === 'string' && rpx.slice(-3) === 'rpx') {
numRpx = parseFloat(rpx.slice(0, -3));
}
var px = (numRpx / 750) * windowWidth;
return px;
};
var defaultWidth = 150;
Component({
props: TableDefaultProps,
data: {
widthPx: 0,
list: [],
showFixedShadow: false,
},
sysInfo: Promise.resolve({}),
methods: {
init: function () {
return __awaiter(this, void 0, void 0, function () {
var _a, columns, dataSource, windowWidth, width, columnsData, rowsData;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = getValueFromProps(this, [
'columns',
'dataSource',
]), columns = _a[0], dataSource = _a[1];
return [4 /*yield*/, this.getSysInfo()];
case 1:
windowWidth = (_b.sent()).windowWidth;
width = reduce(columns, function (sum, cur) {
return sum + cur.width;
}, 0);
columnsData = this.renderColumns(columns, windowWidth);
rowsData = this.renderRows(dataSource, windowWidth);
this.setData({
widthPx: rpx2px(width, windowWidth),
list: __spreadArray(__spreadArray([], columnsData, true), rowsData, true),
});
return [2 /*return*/];
}
});
});
},
renderColumns: function (columns, windowWidth, renderRule) {
if (renderRule === void 0) { renderRule = {}; }
var key = renderRule.key, sorterStatus = renderRule.sorterStatus;
return [
{
type: 'columns',
key: 'columns_0',
value: columns.map(function (v) { return (__assign(__assign({}, v), { sorterStatus: key === v.key ? sorterStatus : 'normal', widthPx: rpx2px(v.width || defaultWidth, windowWidth) })); }),
},
];
},
renderRows: function (dataSource, windowWidth, renderRule) {
if (renderRule === void 0) { renderRule = {}; }
var columns = getValueFromProps(this, 'columns');
var rowsData = dataSource.map(function (v, i) {
return {
type: 'rows',
key: "rows_".concat(i),
index: i,
value: columns.map(function (val, idx) { return (__assign(__assign({}, val), { key: val.key, index: idx, dataIndex: val.dataIndex, value: v[val.dataIndex], textAlignRight: v.textAlignRight || val.textAlignRight, rowsData: v, widthPx: rpx2px(val.width || defaultWidth, windowWidth), fixed: val.fixed, ellipsisRow: val.ellipsisRow })); }),
};
});
var key = renderRule.key, sorter = renderRule.sorter, sorterStatus = renderRule.sorterStatus;
// 排序返回
if (sorter && sorterStatus !== 'normal') {
return rowsData.sort(function (a, b) {
var aValue = filter(a.value, { key: key })[0].value;
var bValue = filter(b.value, { key: key })[0].value;
if (sorterStatus === 'forward') {
return aValue - bValue;
}
return bValue - aValue;
});
}
// 默认返回
return rowsData;
},
handleSort: function (e) {
return __awaiter(this, void 0, void 0, function () {
var _a, columns, dataSource, _b, key, sorter, sorterStatus, filterData, onSort, windowWidth, type2status, renderRule, columnsData, rowsData, newDataSource;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = getValueFromProps(this, [
'columns',
'dataSource',
]), columns = _a[0], dataSource = _a[1];
_b = get(e, 'currentTarget.dataset.item'), key = _b.key, sorter = _b.sorter, sorterStatus = _b.sorterStatus;
filterData = filter(columns, { key: key });
onSort = filterData && filterData[0] && filterData[0].onSort;
return [4 /*yield*/, this.getSysInfo()];
case 1:
windowWidth = (_c.sent()).windowWidth;
type2status = {
// 默认时点击就正序
normal: 'forward',
// 正序时点击就倒序
forward: 'reverse',
// 倒序时点击就默认
reverse: 'normal',
};
renderRule = {
key: key,
sorter: sorter,
sorterStatus: type2status[sorterStatus],
};
columnsData = this.renderColumns(columns, windowWidth, renderRule);
rowsData = [];
if (!onSort) return [3 /*break*/, 3];
return [4 /*yield*/, onSort(renderRule.sorterStatus)];
case 2:
newDataSource = _c.sent();
rowsData = this.renderRows(newDataSource, windowWidth);
return [3 /*break*/, 4];
case 3:
rowsData = this.renderRows(dataSource, windowWidth, renderRule);
_c.label = 4;
case 4:
this.setData({
list: __spreadArray(__spreadArray([], columnsData, true), rowsData, true),
});
return [2 /*return*/];
}
});
});
},
handleTouchMove: function () {
this.setData({
showFixedShadow: true,
});
},
handleScrollToUpper: function () {
this.setData({
showFixedShadow: false,
});
},
getSysInfo: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this.sysInfo)
return [2 /*return*/, this.sysInfo];
this.sysInfo = getSystemInfo();
return [2 /*return*/, this.sysInfo];
});
});
},
},
onInit: function () {
this.init();
},
didUpdate: function (prevProps) {
var prevDataSource = prevProps.dataSource, prevColumns = prevProps.columns;
var _a = getValueFromProps(this, [
'columns',
'dataSource',
]), columns = _a[0], dataSource = _a[1];
if (!equal(prevDataSource, dataSource) || !equal(prevColumns, columns)) {
this.init();
}
},
});