oak-frontend-base/es/components/refAttr/index.js

86 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { assert } from 'oak-domain/lib/utils/assert';
export default OakComponent({
isList: false,
/* entity() {
return this.props.pickerDef!.entity as keyof ED;
}, */
properties: {
placeholder: undefined,
multiple: false,
entityId: '',
entityIds: [],
pickerRender: {},
onChange: (() => undefined),
},
formData() {
const { multiple, entityIds, pickerRender } = this.props;
const { entity, projection, title } = pickerRender;
const rows = entityIds &&
entityIds.length &&
this.features.cache.get(entity, {
data: typeof projection === 'function'
? projection()
: projection,
filter: {
id: {
$in: entityIds,
},
},
});
const renderValue = rows && rows.length ? rows.map((row) => title(row)).join(',') : '';
const schema = this.features.cache.getSchema();
return {
renderValue,
schema,
};
},
data: {
data: undefined,
},
listeners: {
entityId() {
this.reRender();
},
entityIds() {
this.reRender();
},
},
lifetimes: {
async ready() {
this.refreshData();
},
},
methods: {
async refreshData() {
const { pickerRender, multiple } = this.props;
const { mode, entity, projection, filter, count, title } = pickerRender;
if (mode === 'radio') {
// radio的要先取数据出来
assert(typeof count === 'number' && count <= 5, 'radio类型的外键选择总数必须小于5');
}
else if (mode === 'select') {
// select也先取可以点击再取但这样初始状态不好渲染
assert(typeof count === 'number' && count <= 20, 'select类型的外键选择总数必须小于20');
}
else {
return;
}
const proj = typeof projection === 'function' ? projection() : projection;
const filter2 = typeof filter === 'function' ? filter() : filter;
const { data } = await this.features.cache.refresh(entity, {
data: proj,
filter: filter2,
indexFrom: 0,
count,
});
const data2 = data.map((ele) => ({
id: ele.id,
title: title(ele),
}));
this.setState({
data: data2,
});
},
},
});