feat: add collectionName for Record

This commit is contained in:
Zeke Zhang 2024-01-21 15:27:22 +08:00
parent c0e5dd2453
commit 89828d099c
5 changed files with 39 additions and 11 deletions

View File

@ -1,15 +1,15 @@
import React, { FC, useEffect } from 'react';
import {
RecordProviderV2,
SchemaComponent,
withSchemaComponentProps,
UseDataBlockProps,
useRecordV2,
useRecordDataV2,
useRecordV2,
withSchemaComponentProps,
} from '@nocobase/client';
import { createApp } from './createApp';
import { Button, Form, Input, InputNumber } from 'antd';
import { FormProps } from 'antd/lib';
import React, { FC, useEffect } from 'react';
import { createApp } from './createApp';
interface DemoFormFieldType {
id: number;

View File

@ -11,6 +11,10 @@ interface RecordProviderProps<DataType = {}, ParentDataType = {}> {
isNew?: boolean;
record?: RecordV2<DataType, ParentDataType> | DataType;
parentRecord?: RecordV2<ParentDataType> | DataType;
/**
* 当前记录所属的 collection name
*/
collectionName?: string;
}
```

View File

@ -9,12 +9,17 @@ interface RecordOptions<DataType = {}, ParentDataType = {}> {
isNew?: boolean;
data?: DataType;
parentRecord?: RecordV2<ParentDataType>;
/**
* 当前记录所属的 collection name
*/
collectionName?: string;
}
class RecordV2<DataType = {}, ParentDataType = {}> {
public isNew?: boolean;
public data?: DataType;
public parentRecord?: RecordV2<ParentDataType>;
public collectionName?: string;
constructor(options: RecordOptions<DataType, ParentDataType>) {}
setData(data: DataType) {

View File

@ -2,17 +2,26 @@ export interface RecordOptions<DataType = {}, ParentDataType = {}> {
isNew?: boolean;
data?: DataType;
parentRecord?: RecordV2<ParentDataType>;
/**
* collection name
*/
collectionName?: string;
}
export class RecordV2<DataType = {}, ParentDataType = {}> {
public data?: DataType;
public parentRecord?: RecordV2<ParentDataType>;
public isNew?: boolean;
/**
* collection name
*/
public collectionName?: string;
constructor(options: RecordOptions<DataType, ParentDataType>) {
const { data, parentRecord, isNew } = options;
const { data, parentRecord, isNew, collectionName } = options;
this.isNew = isNew;
this.data = data;
this.parentRecord = parentRecord;
this.collectionName = collectionName;
}
setData(data: DataType) {

View File

@ -9,16 +9,26 @@ export interface RecordProviderProps<DataType = {}, ParentDataType = {}> {
isNew?: boolean;
record?: RecordV2<DataType, ParentDataType> | DataType;
parentRecord?: RecordV2<ParentDataType> | DataType;
/**
* collection name
*/
collectionName?: string;
}
export const RecordProviderV2: FC<RecordProviderProps> = ({ isNew, record, parentRecord, children }) => {
export const RecordProviderV2: FC<RecordProviderProps> = ({
isNew,
record,
parentRecord,
children,
collectionName,
}) => {
const parentRecordValue = useMemo(() => {
if (parentRecord) {
if (parentRecord instanceof RecordV2) return parentRecord;
return new RecordV2({ data: parentRecord });
return new RecordV2({ data: parentRecord, collectionName });
}
if (record instanceof RecordV2) return record.parentRecord;
}, [parentRecord, record]);
}, [collectionName, parentRecord, record]);
const currentRecordValue = useMemo(() => {
let res: RecordV2;
@ -27,14 +37,14 @@ export const RecordProviderV2: FC<RecordProviderProps> = ({ isNew, record, paren
res = record;
res.isNew = record.isNew || isNew;
} else {
res = new RecordV2({ data: record, isNew });
res = new RecordV2({ data: record, isNew, collectionName });
}
} else {
res = new RecordV2({ isNew });
res = new RecordV2({ isNew, collectionName });
}
res.setParentRecord(parentRecordValue);
return res;
}, [parentRecordValue, record, isNew]);
}, [record, parentRecordValue, isNew, collectionName]);
return <RecordContextV2.Provider value={currentRecordValue}>{children}</RecordContextV2.Provider>;
};