forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Form.Item
noStyle
support validation status (ant-design#44576)
* fix: FormItem.useStatus can not get status * fix: noStyle not patch style * fix: noStyle inhreit logic * docs: update docs * test: add test case * refactor: nostyle block status * fix: coverage
- Loading branch information
Showing
8 changed files
with
256 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import * as React from 'react'; | ||
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled'; | ||
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled'; | ||
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled'; | ||
import LoadingOutlined from '@ant-design/icons/LoadingOutlined'; | ||
import classNames from 'classnames'; | ||
import type { Meta } from 'rc-field-form/lib/interface'; | ||
|
||
import type { ValidateStatus } from '.'; | ||
import { FormItemInputContext, type FormItemStatusContextProps } from '../context'; | ||
import { getStatus } from '../util'; | ||
|
||
const iconMap = { | ||
success: CheckCircleFilled, | ||
warning: ExclamationCircleFilled, | ||
error: CloseCircleFilled, | ||
validating: LoadingOutlined, | ||
}; | ||
|
||
export interface StatusProviderProps { | ||
children?: React.ReactNode; | ||
validateStatus?: ValidateStatus; | ||
prefixCls: string; | ||
meta: Meta; | ||
errors: React.ReactNode[]; | ||
warnings: React.ReactNode[]; | ||
hasFeedback?: boolean; | ||
noStyle?: boolean; | ||
} | ||
|
||
export default function StatusProvider({ | ||
children, | ||
errors, | ||
warnings, | ||
hasFeedback, | ||
validateStatus, | ||
prefixCls, | ||
meta, | ||
noStyle, | ||
}: StatusProviderProps) { | ||
const itemPrefixCls = `${prefixCls}-item`; | ||
|
||
const mergedValidateStatus = getStatus(errors, warnings, meta, null, hasFeedback, validateStatus); | ||
|
||
const { isFormItemInput: parentIsFormItemInput, status: parentStatus } = | ||
React.useContext(FormItemInputContext); | ||
|
||
// ====================== Context ======================= | ||
const formItemStatusContext = React.useMemo<FormItemStatusContextProps>(() => { | ||
let feedbackIcon: React.ReactNode; | ||
if (hasFeedback) { | ||
const IconNode = mergedValidateStatus && iconMap[mergedValidateStatus]; | ||
feedbackIcon = IconNode ? ( | ||
<span | ||
className={classNames( | ||
`${itemPrefixCls}-feedback-icon`, | ||
`${itemPrefixCls}-feedback-icon-${mergedValidateStatus}`, | ||
)} | ||
> | ||
<IconNode /> | ||
</span> | ||
) : null; | ||
} | ||
|
||
let isFormItemInput: boolean | undefined = true; | ||
let status: ValidateStatus = mergedValidateStatus || ''; | ||
|
||
// No style will follow parent context | ||
if (noStyle) { | ||
isFormItemInput = parentIsFormItemInput; | ||
status = (mergedValidateStatus ?? parentStatus) || ''; | ||
} | ||
|
||
return { | ||
status, | ||
errors, | ||
warnings, | ||
hasFeedback, | ||
feedbackIcon, | ||
isFormItemInput, | ||
}; | ||
}, [mergedValidateStatus, hasFeedback, noStyle, parentIsFormItemInput, parentStatus]); | ||
|
||
// ======================= Render ======================= | ||
return ( | ||
<FormItemInputContext.Provider value={formItemStatusContext}> | ||
{children} | ||
</FormItemInputContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.