actionDef加上了一些渲染需要的属性

This commit is contained in:
Xu Chang 2023-03-10 16:48:47 +08:00
parent bcbb26837e
commit 5774e3401f
6 changed files with 33 additions and 16 deletions

3
lib/types/Page.d.ts vendored
View File

@ -42,6 +42,9 @@ export declare type ActionDef<ED extends EntityDict & BaseEntityDict, T extends
action: ED[T]['Action'];
filter?: ED[T]['Selection']['filter'];
data?: Partial<ED[T]['CreateSingle']['data']>;
label?: string;
color?: string;
key?: string;
} | ED[T]['Action'];
export declare type RowWithActions<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = Partial<ED[T]['Schema']> & {
'#oakLegalActions': ActionDef<ED, T>[];

View File

@ -1,7 +1,7 @@
import { EntityDict } from 'oak-domain/lib/types/Entity';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { DataUpsertTransformer } from '../../types/AbstractComponent';
import { makeDataUpsertTransformer } from '../../utils/usefulFn';
import { analyzeDataUpsertTransformer } from '../../utils/usefulFn';
export default OakComponent({
isList: false,
@ -35,7 +35,7 @@ export default OakComponent({
const { attributes, entity } = this.props;
const schema = this.features.cache.getSchema();
const transformer = makeDataUpsertTransformer(schema, entity!, attributes!, (k, params) => this.t(k, params));
const transformer = analyzeDataUpsertTransformer(schema, entity!, attributes!, (k, params) => this.t(k, params));
this.setState({
transformer,
});

View File

@ -20,7 +20,8 @@ import dayjs from 'dayjs';
import { AttrRender, AttrUpsertRender, OakAbsRefAttrPickerRender, OakNativeAttrUpsertRender } from '../../types/AbstractComponent';
import { WebComponentProps } from '../../types/Page';
function makeAttrInput(attrRender: AttrUpsertRender<EntityDict & BaseEntityDict>, onValueChange: (value: any) => void) {
type ED = EntityDict & BaseEntityDict;
function makeAttrInput(attrRender: AttrUpsertRender<ED>, onValueChange: (value: any) => void) {
const { value, type, params, label, defaultValue, required } = attrRender as OakNativeAttrUpsertRender;
switch (type) {
case 'string':
@ -182,6 +183,9 @@ function makeAttrInput(attrRender: AttrUpsertRender<EntityDict & BaseEntityDict>
</Radio.Group>
);
}
case 'ref': {
const { mode, } = attrRender as OakAbsRefAttrPickerRender<ED, keyof ED>;
}
default: {
throw new Error(`【Abstract Update】无法支持的数据类别${type}的渲染`);
}
@ -189,11 +193,11 @@ function makeAttrInput(attrRender: AttrUpsertRender<EntityDict & BaseEntityDict>
}
export default function render(props: WebComponentProps<
EntityDict & BaseEntityDict,
ED,
keyof EntityDict,
false,
{
renderData: AttrUpsertRender<EntityDict & BaseEntityDict>[];
renderData: AttrUpsertRender<ED>[];
children: any;
},
{
@ -225,7 +229,7 @@ export default function render(props: WebComponentProps<
{
makeAttrInput(ele, (value) => {
if (ele.type === 'ref') {
const { attr, entity } = ele as OakAbsRefAttrPickerRender<EntityDict & BaseEntityDict, keyof (EntityDict & BaseEntityDict)>;
const { attr, entity } = ele as OakAbsRefAttrPickerRender<ED, keyof (ED)>;
if (attr) {
update({
[attr]: value,

View File

@ -49,15 +49,13 @@ export interface OakAbsRefAttrPickerDef<ED extends EntityDict & BaseEntityDict,
allowNull?: boolean;
};
export type OakAbsRefAttrPickerRender<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = Pick<
OakAbsRefAttrPickerDef<ED, T>,
'mode' | 'projection' | 'entity' | 'projection' | 'title' | 'filter' | 'count'
> & {
export type OakAbsRefAttrPickerRender<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = {
type: 'ref';
attr?: string; // 为undefined意味着是entity/entityId的指针
attr: string;
label: string;
value: any;
required?: boolean;
mode: OakAbsRefAttrPickerDef<ED, T>['mode'],
}
export type OakAbsGeoAttrsDef = {

View File

@ -89,6 +89,9 @@ export type ActionDef<
action: ED[T]['Action'],
filter?: ED[T]['Selection']['filter'];
data?: Partial<ED[T]['CreateSingle']['data']>;
label?: string;
color?: string;
key?: string;
} | ED[T]['Action'];
export type RowWithActions<

View File

@ -16,6 +16,7 @@ import {
DataUpsertTransformer,
AttrUpsertRender,
OakAbsDerivedAttrDef,
OakAbsRefAttrPickerDef,
} from '../types/AbstractComponent';
import { Attributes } from 'oak-domain/lib/types';
import { get } from 'oak-domain/lib/utils/lodash';
@ -230,11 +231,14 @@ export function makeDataTransformer<ED extends EntityDict & BaseEntityDict>(data
});
}
export function makeDataUpsertTransformer<ED extends EntityDict & BaseEntityDict>(
export function analyzeDataUpsertTransformer<ED extends EntityDict & BaseEntityDict>(
dataSchema: StorageSchema<ED>,
entity: string,
attrUpsertDefs: OakAbsAttrUpsertDef<ED>[],
t: (k: string, params?: object) => string): DataUpsertTransformer<ED> {
const otmPickerDict: Record<string, OakAbsRefAttrPickerDef<ED, keyof ED>> = {};
const transformerFixedPart = attrUpsertDefs.map(
(ele) => {
if (typeof ele === 'string') {
@ -260,14 +264,19 @@ export function makeDataUpsertTransformer<ED extends EntityDict & BaseEntityDict
const rel = judgeRelation(dataSchema, entity, attr);
assert(rel === 2 || rel === ele.entity);
const refEntity = typeof rel === 'string' ? rel : attr;
return {
type: 'ref',
attr: typeof rel === 'string' && `${attr}Id`,
assert(!otmPickerDict[attr]);
otmPickerDict[attr] = {
mode,
attr,
entity: refEntity as keyof ED,
projection,
title,
filter,
count,
title,
};
return {
type: 'ref',
attr: typeof rel === 'string' ? `${attr}Id` : 'entityId',
mode,
get: (data: Record<string, any>) => title(data[attr]),
label: label || t(`${refEntity}:name`),