From 92313da17a92dc9a80ac8afc849372da8e5e8df4 Mon Sep 17 00:00:00 2001 From: dengfuping Date: Fri, 17 Jan 2025 19:53:08 +0800 Subject: [PATCH] improve(design): Set required to true for Form.Item with Switch to hide optional mark --- packages/design/src/form/FormItem.tsx | 9 +- .../__snapshots__/switch.test.tsx.snap | 296 ++++++++++++++++++ .../design/src/form/__tests__/switch.test.tsx | 67 ++++ 3 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 packages/design/src/form/__tests__/__snapshots__/switch.test.tsx.snap create mode 100644 packages/design/src/form/__tests__/switch.test.tsx diff --git a/packages/design/src/form/FormItem.tsx b/packages/design/src/form/FormItem.tsx index 1459e9116..d8742ee7d 100644 --- a/packages/design/src/form/FormItem.tsx +++ b/packages/design/src/form/FormItem.tsx @@ -4,6 +4,7 @@ import type { ReactNode } from 'react'; import React from 'react'; import type { TooltipProps } from '../tooltip'; import { useTooltipTypeList } from '../tooltip/hooks/useTooltipTypeList'; +import { isPlainObject } from 'lodash'; const AntFormItem = AntForm.Item; @@ -34,7 +35,13 @@ const Item: React.FC = ({ children, tooltip, ...restProps }) => { } return ( - + {children} ); diff --git a/packages/design/src/form/__tests__/__snapshots__/switch.test.tsx.snap b/packages/design/src/form/__tests__/__snapshots__/switch.test.tsx.snap new file mode 100644 index 000000000..2adb03940 --- /dev/null +++ b/packages/design/src/form/__tests__/__snapshots__/switch.test.tsx.snap @@ -0,0 +1,296 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`Form.Item with Switch > dynamic Switch 1`] = ` +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+`; + +exports[`Form.Item with Switch > nested Switch 1`] = ` +
+
+
+
+ +
+
+
+
+ + + +
+
+
+
+
+
+`; + +exports[`Form.Item with Switch > normal Switch 1`] = ` +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+`; + +exports[`Form.Item with Switch > renderProps Switch 1`] = ` +
+
+
+
+ +
+
+
+
+
+
+
+
+ +`; + +exports[`Form.Item with Switch > sub component Switch 1`] = ` +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+`; diff --git a/packages/design/src/form/__tests__/switch.test.tsx b/packages/design/src/form/__tests__/switch.test.tsx new file mode 100644 index 000000000..00205fc5a --- /dev/null +++ b/packages/design/src/form/__tests__/switch.test.tsx @@ -0,0 +1,67 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import { ConfigProvider, Form, Switch } from '@oceanbase/design'; +import type { FormItemProps } from '@oceanbase/design'; + +const FormItemTest: React.FC = ({ children }) => ( +
+ + {children} + +
+); + +describe('Form.Item with Switch', () => { + it('normal Switch', () => { + const { container, asFragment } = render( + + + + ); + expect(container.querySelector('.ant-form-item-optional')).toBeFalsy(); + expect(asFragment().firstChild).toMatchSnapshot(); + }); + + it('dynamic Switch', () => { + const { container, asFragment } = render( + {true ? : null} + ); + expect(container.querySelector('.ant-form-item-optional')).toBeFalsy(); + expect(asFragment().firstChild).toMatchSnapshot(); + }); + + it('nested Switch', () => { + const { container, asFragment } = render( + + + + + + ); + expect(container.querySelector('.ant-form-item-optional')).toBeTruthy(); + expect(asFragment().firstChild).toMatchSnapshot(); + }); + + it('renderProps Switch', () => { + const { container, asFragment } = render( + + {() => { + return true ? : null; + }} + + ); + expect(container.querySelector('.ant-form-item-optional')).toBeTruthy(); + expect(asFragment().firstChild).toMatchSnapshot(); + }); + + it('sub component Switch', () => { + const SwitchComponent = () => ; + const { container, asFragment } = render( + + + + ); + expect(container.querySelector('.ant-form-item-optional')).toBeTruthy(); + expect(asFragment().firstChild).toMatchSnapshot(); + }); +});