Skip to content

Commit

Permalink
chore: CRUD 调整方便外围覆盖
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Dec 23, 2024
1 parent d34d619 commit 7e46eb8
Show file tree
Hide file tree
Showing 23 changed files with 1,671 additions and 461 deletions.
573 changes: 554 additions & 19 deletions docs/zh-CN/components/crud.md

Large diffs are not rendered by default.

239 changes: 212 additions & 27 deletions docs/zh-CN/components/table.md

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions packages/amis-core/src/RootRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ export class RootRenderer extends React.Component<RootRendererProps> {
);
});
} else if (action.actionType === 'toast') {
action.toast?.items?.forEach((item: any) => {
action.toast?.items?.forEach(({level, body, title, ...item}: any) => {
env.notify(
item.level || 'info',
item.body
? render('body', item.body, {
level || 'info',
body
? render('body', body, {
...this.props,
data: ctx,
context: store.context
Expand All @@ -247,8 +247,8 @@ export class RootRenderer extends React.Component<RootRendererProps> {
{
...action.toast,
...item,
title: item.title
? render('title', item.title, {
title: title
? render('title', title, {
...this.props,
data: ctx,
context: store.context
Expand Down
47 changes: 32 additions & 15 deletions packages/amis-core/src/store/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
applyFilters,
isEmpty,
qsstringify,
findTreeIndex,
getVariable
} from '../utils/helper';
import {Api, Payload, fetchOptions, ActionObject, ApiObject} from '../types';
Expand Down Expand Up @@ -94,22 +95,16 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
// 包两层,主要是为了处理以下 case
// 里面放了个 form,form 提交过来的时候不希望把 items 这些发送过来。
// 因为会把数据呈现在地址栏上。
return createObject(
createObject(self.data, {
items: self.items.concat(),
selectedItems: self.selectedItems.concat(),
unSelectedItems: self.unSelectedItems.concat()
}),
{...self.query}
);
return createObject(createObject(self.data, this.eventContext), {
...self.query
});
},

get mergedData() {
return extendObject(self.data, {
...self.query,
...self.data,
selectedItems: self.selectedItems,
unSelectedItems: self.unSelectedItems
...this.eventContext,
...self.data
});
},

Expand All @@ -121,6 +116,10 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
return self.selectedItems.concat();
},

get itemsAsArray() {
return self.items.concat();
},

fetchCtxOf(
data: any,
options: {
Expand All @@ -134,6 +133,23 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
[options.perPageField || 'perPage']: self.perPage,
...data
});
},

get eventContext() {
const context = {
items: self.items.concat(),
selectedItems: self.selectedItems.concat(),
unSelectedItems: self.unSelectedItems.concat(),
selectedIndexes: self.selectedItems.map(
item =>
findTreeIndex(
self.items,
i => (item.__pristine || item) === (i.__pristine || i)
)?.join('.') || '-1'
)
};

return context;
}
}))
.actions(self => {
Expand Down Expand Up @@ -727,9 +743,7 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
total: self.total,
page: self.page,
perPage: self.perPage,
items: self.items.concat(),
selectedItems: self.selectedItems.concat(),
unSelectedItems: self.unSelectedItems.concat()
...self.eventContext
});
};

Expand Down Expand Up @@ -776,7 +790,10 @@ export const CRUDStore = ServiceStore.named('CRUDStore')
exportAsCSV,
updateColumns,
updateTotal,
resetSelection
resetSelection,
replaceItems(items: Array<any>) {
self.items.replace(items);
}
};
});

Expand Down
67 changes: 58 additions & 9 deletions packages/amis-core/src/store/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export const Item = types
index: self.index,

// 只有table时,也可以获取选中行
selectedItems: listStore.selectedItems.map(item => item.data),
unSelectedItems: listStore.unSelectedItems.map(item => item.data)
...listStore.eventContext
}),
self.data
);
Expand Down Expand Up @@ -101,6 +100,13 @@ export const ListStore = iRendererStore
.named('ListStore')
.props({
items: types.array(Item),

// 记录原始列表和原始选中的列表
// 因为如果是前端分页,上层 crud 或者 input-table 下发到这层的
// 是某个页区间的数据,这个时候 items 和 selectedItems 会少很多条
fullItems: types.optional(types.array(types.frozen()), []),
fullSelectedItems: types.optional(types.array(types.frozen()), []),

selectedItems: types.array(types.reference(Item)),
primaryField: 'id',
orderBy: '',
Expand Down Expand Up @@ -171,6 +177,45 @@ export const ListStore = iRendererStore

get movedItems() {
return getMovedItems();
},

/**
* 构建事件的上下文数据
* @param buildChain
* @returns
*/
get eventContext() {
const context = {
selectedItems: self.selectedItems.map(item => item.data),
selectedIndexes: self.selectedItems.map(item => item.index),
items: self.items.map(item => item.data),
unSelectedItems: this.unSelectedItems.map(item => item.data)
};

// 如果是前端分页情况,需要根据全量数据计算
// 如果不是前端分页,数据都没有返回,那种就没办法支持全量数据信息了
if (self.fullItems.length > self.items.length) {
// todo 这里的选择顺序会一直变,这个有影响吗?
const selectedItems = self.fullSelectedItems
.filter(
item =>
!self.items.find(
row => row.pristine === (item.__pristine || item)
)
)
.concat(context.selectedItems);

context.selectedItems = selectedItems;
context.items = self.fullItems.concat();
context.unSelectedItems = self.fullItems.filter(
item => !selectedItems.includes(item)
);
context.selectedIndexes = selectedItems.map(item =>
self.fullItems.indexOf(item.__pristine || item)
);
}

return context;
}
};
})
Expand All @@ -196,7 +241,11 @@ export const ListStore = iRendererStore
(self.itemDraggableOn = config.itemDraggableOn);
}

function initItems(items: Array<object>) {
function initItems(
items: Array<object>,
fullItems?: Array<any>,
fullSelectedItems?: Array<any>
) {
let arr = items.map((item, key) => {
item = isObject(item)
? item
Expand All @@ -209,14 +258,16 @@ export const ListStore = iRendererStore
id: guid(),
index: key,
newIndex: key,
pristine: item,
data: item,
modified: false
pristine: (item as any).__pristine || item,
data: item
};
});
self.selectedItems.clear();
self.items.replace(arr as Array<IItem>);
self.dragging = false;
Array.isArray(fullItems) && self.fullItems.replace(fullItems);
Array.isArray(fullSelectedItems) &&
self.fullSelectedItems.replace(fullSelectedItems);
}

function updateSelected(selected: Array<any>, valueField?: string) {
Expand Down Expand Up @@ -319,9 +370,7 @@ export const ListStore = iRendererStore

function getData(superData: any): any {
return createObject(superData, {
items: self.items.map(item => item.data),
selectedItems: self.selectedItems.map(item => item.data),
unSelectedItems: self.unSelectedItems.map(item => item.data)
...self.eventContext
});
}

Expand Down
Loading

0 comments on commit 7e46eb8

Please sign in to comment.