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

View File

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

View File

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

View File

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

View File

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

View File

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