From ac5a82fde3bb878b85cd286aec86923dd1dc6236 Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Tue, 26 Mar 2024 17:15:34 +0800 Subject: [PATCH] fix(Table): fix invalid pagination (#3821) * fix(Table): fix invalid pagination * test: add test * fix: unit test bug --------- Co-authored-by: dream2023 <1098626505@qq.com> --- .../hoc/withDynamicSchemaProps.test.tsx | 29 +++++++++++++++---- .../hoc/withDynamicSchemaProps.tsx | 4 +-- .../schema-component/antd/table-v2/Table.tsx | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/packages/core/client/src/application/__tests__/hoc/withDynamicSchemaProps.test.tsx b/packages/core/client/src/application/__tests__/hoc/withDynamicSchemaProps.test.tsx index 6f4f538eed..374beae633 100644 --- a/packages/core/client/src/application/__tests__/hoc/withDynamicSchemaProps.test.tsx +++ b/packages/core/client/src/application/__tests__/hoc/withDynamicSchemaProps.test.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { SchemaComponent, SchemaComponentProvider } from '../../../schema-component'; -import { render, screen, sleep, userEvent, waitFor } from '@nocobase/test/client'; +import { render } from '@nocobase/test/client'; import { withDynamicSchemaProps } from '../../hoc'; const HelloComponent = withDynamicSchemaProps((props: any) => ( @@ -66,7 +66,7 @@ describe('withDynamicSchemaProps', () => { const Demo = withTestDemo(schema, scopes); const { getByTestId } = render(); - expect(getByTestId('component')).toHaveTextContent(JSON.stringify({ a: 'a', b: 'b' })); + expect(getByTestId('component')).toHaveTextContent(JSON.stringify({ b: 'b', a: 'a' })); }); test('x-use-decorator-props', () => { @@ -101,7 +101,7 @@ describe('withDynamicSchemaProps', () => { const Demo = withTestDemo(schema, scopes); const { getByTestId } = render(); - expect(getByTestId('decorator')).toHaveTextContent(JSON.stringify({ a: 'a', b: 'b' })); + expect(getByTestId('decorator')).toHaveTextContent(JSON.stringify({ b: 'b', a: 'a' })); }); test('x-use-component-props and x-use-decorator-props exist simultaneously', () => { @@ -130,8 +130,8 @@ describe('withDynamicSchemaProps', () => { const Demo = withTestDemo(schema, scopes); const { getByTestId } = render(); - expect(getByTestId('decorator')).toHaveTextContent(JSON.stringify({ a: 'a', b: 'b' })); - expect(getByTestId('component')).toHaveTextContent(JSON.stringify({ c: 'c', d: 'd' })); + expect(getByTestId('decorator')).toHaveTextContent(JSON.stringify({ b: 'b', a: 'a' })); + expect(getByTestId('component')).toHaveTextContent(JSON.stringify({ d: 'd', c: 'c' })); }); test('no register scope', () => { @@ -142,4 +142,23 @@ describe('withDynamicSchemaProps', () => { const { getByTestId } = render(); expect(getByTestId('component')).toHaveTextContent(JSON.stringify({})); }); + + test('x-use-component-props should override x-component-props', () => { + function useComponentProps() { + return { + a: 'a', + }; + } + const schema = { + 'x-use-component-props': 'useComponentProps', + 'x-component-props': { + a: 'b', + }, + }; + const scopes = { useComponentProps }; + + const Demo = withTestDemo(schema, scopes); + const { getByTestId } = render(); + expect(getByTestId('component')).toHaveTextContent(JSON.stringify({ a: 'a' })); + }); }); diff --git a/packages/core/client/src/application/hoc/withDynamicSchemaProps.tsx b/packages/core/client/src/application/hoc/withDynamicSchemaProps.tsx index 1fabb2dde9..4d5daf178d 100644 --- a/packages/core/client/src/application/hoc/withDynamicSchemaProps.tsx +++ b/packages/core/client/src/application/hoc/withDynamicSchemaProps.tsx @@ -9,7 +9,7 @@ interface WithSchemaHookOptions { displayName?: string; } -export function withDynamicSchemaProps(Component: ComponentType, options: WithSchemaHookOptions = {}) { +export function withDynamicSchemaProps(Component: any, options: WithSchemaHookOptions = {}) { const displayName = options.displayName || Component.displayName || Component.name; const ComponentWithProps: ComponentType = (props) => { const { dn, findComponent } = useDesignable(); @@ -41,7 +41,7 @@ export function withDynamicSchemaProps(Component: ComponentType, opt const schemaProps = useSchemaProps(props); const memoProps = useMemo(() => { - return merge(omit(schemaProps, 'children'), omit(props, 'children')); + return merge(omit(props, 'children'), omit(schemaProps, 'children')); }, [schemaProps, props]); return {props.children}; diff --git a/packages/core/client/src/schema-component/antd/table-v2/Table.tsx b/packages/core/client/src/schema-component/antd/table-v2/Table.tsx index 6069dc657f..2b54c8fd26 100644 --- a/packages/core/client/src/schema-component/antd/table-v2/Table.tsx +++ b/packages/core/client/src/schema-component/antd/table-v2/Table.tsx @@ -230,7 +230,7 @@ export const Table: any = withDynamicSchemaProps( isSubTable?: boolean; }) => { const { token } = useToken(); - const { pagination: pagination1, useProps, onChange, ...others1 } = props; + const { pagination: pagination1, useProps, ...others1 } = props; // 新版 UISchema(1.0 之后)中已经废弃了 useProps,这里之所以继续保留是为了兼容旧版的 UISchema const { pagination: pagination2, ...others2 } = useProps?.() || {};