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 { import {
RecordProviderV2, RecordProviderV2,
SchemaComponent, SchemaComponent,
withSchemaComponentProps,
UseDataBlockProps, UseDataBlockProps,
useRecordV2,
useRecordDataV2, useRecordDataV2,
useRecordV2,
withSchemaComponentProps,
} from '@nocobase/client'; } from '@nocobase/client';
import { createApp } from './createApp';
import { Button, Form, Input, InputNumber } from 'antd'; import { Button, Form, Input, InputNumber } from 'antd';
import { FormProps } from 'antd/lib'; import { FormProps } from 'antd/lib';
import React, { FC, useEffect } from 'react';
import { createApp } from './createApp';
interface DemoFormFieldType { interface DemoFormFieldType {
id: number; id: number;

View File

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

View File

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

View File

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

View File

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