Skip to content

Commit

Permalink
fix: fix incorrect form status with noStyle (ant-design#36054)
Browse files Browse the repository at this point in the history
* fix: form status

* test: update test case
  • Loading branch information
MadCcc authored and denghuacc committed Jun 23, 2022
1 parent 42afde3 commit 2b4ed4c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 45 deletions.
31 changes: 6 additions & 25 deletions components/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,9 @@ function FormItem<Values = any>(props: FormItemProps<Values>): React.ReactElemen
const getItemRef = useItemRef();

// ======================== Status ========================
const { status: contextStatus, hasFeedback: contextHasFeedback } =
useContext(FormItemInputContext);

let mergedValidateStatus: ValidateStatus | undefined;
let mergedValidateStatus: ValidateStatus = '';
if (validateStatus !== undefined) {
mergedValidateStatus = validateStatus;
} else if (contextStatus !== undefined) {
mergedValidateStatus = contextStatus;
} else if (meta?.validating) {
mergedValidateStatus = 'validating';
} else if (debounceErrors.length) {
Expand All @@ -235,11 +230,9 @@ function FormItem<Values = any>(props: FormItemProps<Values>): React.ReactElemen
mergedValidateStatus = 'success';
}

const mergedHasFeedback = hasFeedback || contextHasFeedback;

const formItemStatusContext = useMemo<FormItemStatusContextProps>(() => {
let feedbackIcon: ReactNode;
if (mergedHasFeedback) {
if (hasFeedback) {
const IconNode = mergedValidateStatus && iconMap[mergedValidateStatus];
feedbackIcon = IconNode ? (
<span
Expand All @@ -255,19 +248,11 @@ function FormItem<Values = any>(props: FormItemProps<Values>): React.ReactElemen

return {
status: mergedValidateStatus,
hasFeedback: mergedHasFeedback,
hasFeedback,
feedbackIcon,
isFormItemInput: true,
};
}, [mergedValidateStatus, mergedHasFeedback]);

const noOverrideFormItemContext = useMemo<FormItemStatusContextProps>(
() => ({
...formItemStatusContext,
isFormItemInput: false,
}),
[formItemStatusContext],
);
}, [mergedValidateStatus, hasFeedback]);

// ======================== Render ========================
function renderLayout(
Expand All @@ -276,11 +261,7 @@ function FormItem<Values = any>(props: FormItemProps<Values>): React.ReactElemen
isRequired?: boolean,
): React.ReactNode {
if (noStyle && !hidden) {
return (
<FormItemInputContext.Provider value={noOverrideFormItemContext}>
{baseChildren}
</FormItemInputContext.Provider>
);
return baseChildren;
}

const itemClassName = {
Expand All @@ -290,7 +271,7 @@ function FormItem<Values = any>(props: FormItemProps<Values>): React.ReactElemen
[`${className}`]: !!className,

// Status
[`${prefixCls}-item-has-feedback`]: mergedValidateStatus && mergedHasFeedback,
[`${prefixCls}-item-has-feedback`]: mergedValidateStatus && hasFeedback,
[`${prefixCls}-item-has-success`]: mergedValidateStatus === 'success',
[`${prefixCls}-item-has-warning`]: mergedValidateStatus === 'warning',
[`${prefixCls}-item-has-error`]: mergedValidateStatus === 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6836,7 +6836,7 @@ exports[`renders ./components/form/demo/normal-login.md extend context correctly
class="ant-form-item-control-input-content"
>
<label
class="ant-checkbox-wrapper ant-checkbox-wrapper-checked"
class="ant-checkbox-wrapper ant-checkbox-wrapper-checked ant-checkbox-wrapper-in-form-item"
>
<span
class="ant-checkbox ant-checkbox-checked"
Expand Down Expand Up @@ -18031,7 +18031,7 @@ exports[`renders ./components/form/demo/validate-other.md extend context correct
class="ant-form-item-control-input-content"
>
<div
class="ant-input-number"
class="ant-input-number ant-input-number-in-form-item"
>
<div
class="ant-input-number-handler-wrap"
Expand Down
4 changes: 2 additions & 2 deletions components/form/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4451,7 +4451,7 @@ exports[`renders ./components/form/demo/normal-login.md correctly 1`] = `
class="ant-form-item-control-input-content"
>
<label
class="ant-checkbox-wrapper ant-checkbox-wrapper-checked"
class="ant-checkbox-wrapper ant-checkbox-wrapper-checked ant-checkbox-wrapper-in-form-item"
>
<span
class="ant-checkbox ant-checkbox-checked"
Expand Down Expand Up @@ -7257,7 +7257,7 @@ exports[`renders ./components/form/demo/validate-other.md correctly 1`] = `
class="ant-form-item-control-input-content"
>
<div
class="ant-input-number"
class="ant-input-number ant-input-number-in-form-item"
>
<div
class="ant-input-number-handler-wrap"
Expand Down
28 changes: 12 additions & 16 deletions components/form/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1207,20 +1207,6 @@ describe('Form', () => {
expect(subFormInstance).toBe(formInstance);
});

it('noStyle should not be affected by parent', () => {
const Demo = () => (
<Form>
<Form.Item>
<Form.Item noStyle>
<Select className="custom-select" />
</Form.Item>
</Form.Item>
</Form>
);
const { container } = render(<Demo />);
expect(container.querySelector('.custom-select')?.className).not.toContain('in-form-item');
});

it('noStyle should not affect status', () => {
const Demo = () => (
<Form>
Expand All @@ -1237,12 +1223,22 @@ describe('Form', () => {
<Select className="custom-select-c" />
</Form.Item>
</Form.Item>
<Form.Item noStyle>
<Form.Item validateStatus="warning">
<Select className="custom-select-d" />
</Form.Item>
</Form.Item>
</Form>
);
const { container } = render(<Demo />);
expect(container.querySelector('.custom-select')?.className).toContain('status-error');
expect(container.querySelector('.custom-select')?.className).not.toContain('status-error');
expect(container.querySelector('.custom-select')?.className).not.toContain('in-form-item');
expect(container.querySelector('.custom-select-b')?.className).toContain('status-error');
expect(container.querySelector('.custom-select-c')?.className).toContain('status-warning');
expect(container.querySelector('.custom-select-b')?.className).toContain('in-form-item');
expect(container.querySelector('.custom-select-c')?.className).toContain('status-error');
expect(container.querySelector('.custom-select-c')?.className).toContain('in-form-item');
expect(container.querySelector('.custom-select-d')?.className).toContain('status-warning');
expect(container.querySelector('.custom-select-d')?.className).toContain('in-form-item');
});

it('should not affect Popup children style', () => {
Expand Down

0 comments on commit 2b4ed4c

Please sign in to comment.