chore(form): code style optimization (#43793)

* chore(form): code style optimization

* fix: add type CSSMotionProps

* Update ErrorList.tsx

* Update FormItemLabel.tsx

* Update FormItemLabel.tsx
This commit is contained in:
thinkasany 2023-07-25 19:47:05 +08:00 committed by GitHub
parent 8f7630273c
commit f78f7bfef9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 12 deletions

View File

@ -1,4 +1,5 @@
import classNames from 'classnames'; import classNames from 'classnames';
import type { CSSMotionProps } from 'rc-motion';
import CSSMotion, { CSSMotionList } from 'rc-motion'; import CSSMotion, { CSSMotionList } from 'rc-motion';
import * as React from 'react'; import * as React from 'react';
import { useMemo } from 'react'; import { useMemo } from 'react';
@ -40,7 +41,7 @@ export interface ErrorListProps {
onVisibleChanged?: (visible: boolean) => void; onVisibleChanged?: (visible: boolean) => void;
} }
export default function ErrorList({ const ErrorList: React.FC<ErrorListProps> = ({
help, help,
helpStatus, helpStatus,
errors = EMPTY_LIST, errors = EMPTY_LIST,
@ -48,14 +49,14 @@ export default function ErrorList({
className: rootClassName, className: rootClassName,
fieldId, fieldId,
onVisibleChanged, onVisibleChanged,
}: ErrorListProps) { }) => {
const { prefixCls } = React.useContext(FormItemPrefixContext); const { prefixCls } = React.useContext(FormItemPrefixContext);
const baseClassName = `${prefixCls}-item-explain`; const baseClassName = `${prefixCls}-item-explain`;
const [, hashId] = useStyle(prefixCls); const [, hashId] = useStyle(prefixCls);
const collapseMotion = useMemo(() => initCollapseMotion(prefixCls), [prefixCls]); const collapseMotion: CSSMotionProps = useMemo(() => initCollapseMotion(prefixCls), [prefixCls]);
// We have to debounce here again since somewhere use ErrorList directly still need no shaking // We have to debounce here again since somewhere use ErrorList directly still need no shaking
// ref: https://github.com/ant-design/ant-design/issues/36336 // ref: https://github.com/ant-design/ant-design/issues/36336
@ -131,4 +132,6 @@ export default function ErrorList({
}} }}
</CSSMotion> </CSSMotion>
); );
} };
export default ErrorList;

View File

@ -63,13 +63,13 @@ const FormItemInput: React.FC<FormItemInputProps & FormItemInputMiscProps> = (pr
delete subFormContext.labelCol; delete subFormContext.labelCol;
delete subFormContext.wrapperCol; delete subFormContext.wrapperCol;
const inputDom = ( const inputDom: React.ReactNode = (
<div className={`${baseClassName}-control-input`}> <div className={`${baseClassName}-control-input`}>
<div className={`${baseClassName}-control-input-content`}>{children}</div> <div className={`${baseClassName}-control-input-content`}>{children}</div>
</div> </div>
); );
const formItemContext = React.useMemo(() => ({ prefixCls, status }), [prefixCls, status]); const formItemContext = React.useMemo(() => ({ prefixCls, status }), [prefixCls, status]);
const errorListDom = const errorListDom: React.ReactNode =
marginBottom !== null || errors.length || warnings.length ? ( marginBottom !== null || errors.length || warnings.length ? (
<div style={{ display: 'flex', flexWrap: 'nowrap' }}> <div style={{ display: 'flex', flexWrap: 'nowrap' }}>
<FormItemPrefixContext.Provider value={formItemContext}> <FormItemPrefixContext.Provider value={formItemContext}>
@ -95,13 +95,13 @@ const FormItemInput: React.FC<FormItemInputProps & FormItemInputMiscProps> = (pr
// If extra = 0, && will goes wrong // If extra = 0, && will goes wrong
// 0&&error -> 0 // 0&&error -> 0
const extraDom = extra ? ( const extraDom: React.ReactNode = extra ? (
<div {...extraProps} className={`${baseClassName}-extra`}> <div {...extraProps} className={`${baseClassName}-extra`}>
{extra} {extra}
</div> </div>
) : null; ) : null;
const dom = const dom: React.ReactNode =
formItemRender && formItemRender.mark === 'pro_table_render' && formItemRender.render ? ( formItemRender && formItemRender.mark === 'pro_table_render' && formItemRender.render ? (
formItemRender.render(props, { input: inputDom, errorList: errorListDom, extra: extraDom }) formItemRender.render(props, { input: inputDom, errorList: errorListDom, extra: extraDom })
) : ( ) : (

View File

@ -84,7 +84,7 @@ const FormItemLabel: React.FC<FormItemLabelProps & { required?: boolean; prefixC
}, },
); );
let labelChildren = label; let labelChildren: React.ReactNode = label;
// Keep label is original where there should have no colon // Keep label is original where there should have no colon
const computedColon = colon === true || (contextColon !== false && colon !== false); const computedColon = colon === true || (contextColon !== false && colon !== false);
@ -100,7 +100,7 @@ const FormItemLabel: React.FC<FormItemLabelProps & { required?: boolean; prefixC
if (tooltipProps) { if (tooltipProps) {
const { icon = <QuestionCircleOutlined />, ...restTooltipProps } = tooltipProps; const { icon = <QuestionCircleOutlined />, ...restTooltipProps } = tooltipProps;
const tooltipNode = ( const tooltipNode: React.ReactNode = (
<Tooltip {...restTooltipProps}> <Tooltip {...restTooltipProps}>
{React.cloneElement(icon, { className: `${prefixCls}-item-tooltip`, title: '' })} {React.cloneElement(icon, { className: `${prefixCls}-item-tooltip`, title: '' })}
</Tooltip> </Tooltip>

View File

@ -2,7 +2,7 @@ import { FormProvider as RcFormProvider } from 'rc-field-form';
import type { FormProviderProps as RcFormProviderProps } from 'rc-field-form/lib/FormContext'; import type { FormProviderProps as RcFormProviderProps } from 'rc-field-form/lib/FormContext';
import type { Meta } from 'rc-field-form/lib/interface'; import type { Meta } from 'rc-field-form/lib/interface';
import omit from 'rc-util/lib/omit'; import omit from 'rc-util/lib/omit';
import type { FC, PropsWithChildren, ReactNode } from 'react'; import type { PropsWithChildren, ReactNode } from 'react';
import * as React from 'react'; import * as React from 'react';
import { useContext, useMemo } from 'react'; import { useContext, useMemo } from 'react';
import type { ColProps } from '../grid/col'; import type { ColProps } from '../grid/col';
@ -70,7 +70,7 @@ export type NoFormStyleProps = PropsWithChildren<{
override?: boolean; override?: boolean;
}>; }>;
export const NoFormStyle: FC<NoFormStyleProps> = ({ children, status, override }) => { export const NoFormStyle: React.FC<NoFormStyleProps> = ({ children, status, override }) => {
const formItemInputContext = useContext(FormItemInputContext); const formItemInputContext = useContext(FormItemInputContext);
const newFormItemInputContext = useMemo(() => { const newFormItemInputContext = useMemo(() => {