|
@@ -1,14 +1,17 @@
|
|
|
-import { Input, Table } from "antd";
|
|
|
+import { Form, FormInstance, Input, Table } from "antd";
|
|
|
import { DageEditableRow } from "./EditableRow";
|
|
|
import { DageEditableCell } from "./EditableCell";
|
|
|
import { Editable } from "./style";
|
|
|
-import { DageColumnTypes, DageEditableProps } from "./types";
|
|
|
+import { DageEditableProps } from "./types";
|
|
|
+import { DageEditableContext } from "./context";
|
|
|
+import { ForwardedRef, forwardRef, useImperativeHandle } from "react";
|
|
|
+
|
|
|
+function DageEditableInner<T extends Record<string, any>>(
|
|
|
+ { defaultColumns, handleSave, ...rest }: DageEditableProps<T>,
|
|
|
+ ref: ForwardedRef<FormInstance<T>>
|
|
|
+) {
|
|
|
+ const [form] = Form.useForm<T>();
|
|
|
|
|
|
-export const DageEditable = <T extends Record<string, any>>({
|
|
|
- defaultColumns,
|
|
|
- handleSave,
|
|
|
- ...rest
|
|
|
-}: DageEditableProps<T>) => {
|
|
|
const components = {
|
|
|
body: {
|
|
|
row: DageEditableRow,
|
|
@@ -16,7 +19,7 @@ export const DageEditable = <T extends Record<string, any>>({
|
|
|
},
|
|
|
};
|
|
|
|
|
|
- const columns = defaultColumns.map((col) => {
|
|
|
+ const columns: any = defaultColumns.map((col) => {
|
|
|
if (!col.editable) {
|
|
|
return col;
|
|
|
}
|
|
@@ -33,17 +36,29 @@ export const DageEditable = <T extends Record<string, any>>({
|
|
|
};
|
|
|
});
|
|
|
|
|
|
+ useImperativeHandle(ref, () => form, [form]);
|
|
|
+
|
|
|
return (
|
|
|
- <Editable>
|
|
|
- <Table
|
|
|
- {...rest}
|
|
|
- columns={columns as DageColumnTypes<T>}
|
|
|
- rowClassName={() => "dage-editable-row"}
|
|
|
- components={components}
|
|
|
- />
|
|
|
- </Editable>
|
|
|
+ <Form form={form} component={false}>
|
|
|
+ <DageEditableContext.Provider value={form}>
|
|
|
+ <Editable>
|
|
|
+ <Table<T>
|
|
|
+ {...rest}
|
|
|
+ columns={columns}
|
|
|
+ rowClassName={() => "dage-editable-row"}
|
|
|
+ components={components}
|
|
|
+ />
|
|
|
+ </Editable>
|
|
|
+ </DageEditableContext.Provider>
|
|
|
+ </Form>
|
|
|
);
|
|
|
-};
|
|
|
+}
|
|
|
+
|
|
|
+export const DageEditable = forwardRef(DageEditableInner) as <
|
|
|
+ T extends Record<string, any>
|
|
|
+>(
|
|
|
+ props: DageEditableProps<T> & { ref?: ForwardedRef<FormInstance<T>> }
|
|
|
+) => React.ReactElement;
|
|
|
|
|
|
export * from "./context";
|
|
|
export * from "./types";
|