Skip to content

Commit

Permalink
fix(form): InlineErrorFormItemPopover support radio and checkbox
Browse files Browse the repository at this point in the history
close #4997
  • Loading branch information
chenshuai2144 committed Jul 12, 2023
1 parent 854ce2b commit 8c6ede6
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 63 deletions.
18 changes: 17 additions & 1 deletion packages/field/src/components/Checkbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useStyle } from '@ant-design/pro-utils';
import { Checkbox, ConfigProvider, Space, Spin } from 'antd';
import { Checkbox, ConfigProvider, Form, Space, Spin } from 'antd';
import type { CheckboxGroupProps } from 'antd/lib/checkbox';
import classNames from 'classnames';
import React, { useContext, useImperativeHandle, useRef } from 'react';
Expand All @@ -26,11 +26,23 @@ const FieldCheckbox: ProFieldFC<GroupProps> = (
) => {
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const layoutClassName = getPrefixCls('pro-field-checkbox');
const status = Form.Item?.useStatus?.();
const [loading, options, fetchData] = useFieldFetchData(rest);

// css
const { wrapSSR, hashId } = useStyle('Checkbox', (token) => {
return {
[`.${layoutClassName}`]: {
'&-error': {
span: {
color: token.colorError,
},
},
'&-warning': {
span: {
color: token.colorWarning,
},
},
'&-vertical': {
//ant design 5
[`&${token.antCls}-checkbox-group`]: {
Expand Down Expand Up @@ -92,6 +104,10 @@ const FieldCheckbox: ProFieldFC<GroupProps> = (
rest.fieldProps?.className,
hashId,
`${layoutClassName}-${layout}`,
{
[`${layoutClassName}-error`]: status?.status === 'error',
[`${layoutClassName}-warning`]: status?.status === 'warning',
},
)}
options={options}
/>,
Expand Down
17 changes: 16 additions & 1 deletion packages/field/src/components/Radio/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useStyle } from '@ant-design/pro-utils';
import type { RadioGroupProps } from 'antd';
import { ConfigProvider, Radio, Spin } from 'antd';
import { ConfigProvider, Form, Radio, Spin } from 'antd';
import classNames from 'classnames';
import React, { useContext, useImperativeHandle, useRef } from 'react';
import type { ProFieldFC } from '../../index';
Expand Down Expand Up @@ -30,6 +30,7 @@ const FieldRadio: ProFieldFC<GroupProps> = (
const layoutClassName = getPrefixCls('pro-field-radio');
const [loading, options, fetchData] = useFieldFetchData(rest);
const radioRef = useRef();
const status = Form.Item?.useStatus?.();

useImperativeHandle(
ref,
Expand All @@ -43,6 +44,16 @@ const FieldRadio: ProFieldFC<GroupProps> = (
// css
const { wrapSSR, hashId } = useStyle('FieldRadioRadio', (token) => {
return {
[`.${layoutClassName}-error`]: {
span: {
color: token.colorError,
},
},
[`.${layoutClassName}-warning`]: {
span: {
color: token.colorWarning,
},
},
[`.${layoutClassName}-vertical`]: {
[`${token.antCls}-radio-wrapper`]: {
display: 'flex',
Expand Down Expand Up @@ -85,6 +96,10 @@ const FieldRadio: ProFieldFC<GroupProps> = (
{...rest.fieldProps}
className={classNames(
rest.fieldProps?.className,
{
[`${layoutClassName}-error`]: status?.status === 'error',
[`${layoutClassName}-warning`]: status?.status === 'warning',
},
hashId,
`${layoutClassName}-${rest.fieldProps.layout || 'horizontal'}`,
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"> 1%"
],
"dependencies": {
"@ant-design/cssinjs": "^1.11.0",
"@ant-design/cssinjs": "^1.11.1",
"@babel/runtime": "^7.18.0",
"@ctrl/tinycolor": "^3.4.0",
"rc-util": "^5.0.1",
Expand Down
16 changes: 11 additions & 5 deletions packages/provider/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ const ConfigProviderContainer: React.FC<{
},
);

const hashed = useMemo(() => {
if (props.hashed === false) {
return false;
}
if (proProvide.hashed === false) return false;
return true;
}, [proProvide.hashed, props.hashed]);

const hashId = useMemo(() => {
if (props.hashed === false) {
return '';
Expand All @@ -302,11 +310,9 @@ const ConfigProviderContainer: React.FC<{
const themeConfig = {
...restConfig.theme,
hashId: hashId,
hashed:
props.hashed !== false &&
proProvide.hashed !== false &&
isNeedOpenHash(),
hashed: hashed && isNeedOpenHash(),
};

return (
<AntdConfigProvider {...restConfig} theme={{ ...themeConfig }}>
<ProConfigContext.Provider
Expand All @@ -315,7 +321,7 @@ const ConfigProviderContainer: React.FC<{
valueTypeMap: valueTypeMap || proProvideValue?.valueTypeMap,
token,
theme: tokenContext.theme as unknown as Theme<any, any>,
hashed: props.hashed,
hashed,
hashId,
}}
>
Expand Down
32 changes: 25 additions & 7 deletions packages/utils/src/components/InlineErrorFormItem/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LoadingOutlined } from '@ant-design/icons';
import { useToken } from '@ant-design/pro-provider';
import type { FormItemProps, PopoverProps } from 'antd';
import { ConfigProvider, Form, Popover } from 'antd';
import type { NamePath } from 'rc-field-form/lib/interface';
Expand Down Expand Up @@ -38,8 +39,8 @@ const InlineErrorFormItemPopover: React.FC<{
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls();

const token = useToken();
const { wrapSSR, hashId } = useStyle(`${prefixCls}-form-item-with-help`);

useEffect(() => {
if (inputProps.validateStatus !== 'validating') {
setErrorList(inputProps.errors);
Expand All @@ -59,20 +60,37 @@ const InlineErrorFormItemPopover: React.FC<{
return (
<Popover
key="popover"
trigger={popoverProps?.trigger || 'focus'}
placement={popoverProps?.placement || 'topRight'}
trigger={popoverProps?.trigger || ['click']}
placement={popoverProps?.placement || 'topLeft'}
{...popoverOpenProps}
getPopupContainer={popoverProps?.getPopupContainer}
getTooltipContainer={popoverProps?.getTooltipContainer}
content={wrapSSR(
<div className={`${prefixCls}-form-item-with-help ${hashId}`.trim()}>
{loading ? <LoadingOutlined /> : null}
{errorList}
<div
className={`${prefixCls}-form-item ${hashId} ${token.hashId}`.trim()}
style={{
margin: 0,
padding: 0,
}}
>
<div
className={`${prefixCls}-form-item-with-help ${hashId} ${token.hashId}`.trim()}
>
{loading ? <LoadingOutlined /> : null}
{errorList}
</div>
</div>,
)}
{...popoverProps}
>
<div>
<div
style={{
width: 'max-content',
maxWidth: '100%',
padding: 4,
}}
tabIndex={-1}
>
{input}
{extra}
</div>
Expand Down
38 changes: 10 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8c6ede6

Please sign in to comment.