Skip to content

Commit

Permalink
chore: Form.Item should not support requiredMark (ant-design#41725)
Browse files Browse the repository at this point in the history
* test: add case

* chore: Form.Item does not support requiredMark
  • Loading branch information
Wxh16144 authored and RedJue committed Apr 25, 2023
1 parent 5f0a8b7 commit 350b599
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components/form/FormItem/ItemHolder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ export default function ItemHolder(props: ItemHolderProps) {
{/* Label */}
<FormItemLabel
htmlFor={fieldId}
requiredMark={requiredMark}
{...props}
requiredMark={requiredMark}
required={required ?? isRequired}
prefixCls={prefixCls}
/>
Expand Down
2 changes: 1 addition & 1 deletion components/form/FormItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const MemoInput = React.memo(
);

export interface FormItemProps<Values = any>
extends FormItemLabelProps,
extends Omit<FormItemLabelProps, 'requiredMark'>,
FormItemInputProps,
RcFieldProps<Values> {
prefixCls?: string;
Expand Down
3 changes: 3 additions & 0 deletions components/form/FormItemLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export interface FormItemLabelProps {
label?: React.ReactNode;
labelAlign?: FormLabelAlign;
labelCol?: ColProps;
/**
* @internal Used for pass `requiredMark` from `<Form />`
*/
requiredMark?: RequiredMark;
tooltip?: LabelTooltipType;
}
Expand Down
25 changes: 25 additions & 0 deletions components/form/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1797,4 +1797,29 @@ describe('Form', () => {
expect(onChange).toHaveBeenNthCalledWith(idx++, 'validating');
expect(onChange).toHaveBeenNthCalledWith(idx++, 'success');
});

// https://user-images.githubusercontent.com/32004925/230819163-464fe90d-422d-4a6d-9e35-44a25d4c64f1.png
it('should not render `requiredMark` when Form.Item has no required prop', () => {
// Escaping TypeScript error
const genProps = (value: any) => ({ ...value });

const { container } = render(
<Form name="basic" requiredMark="optional">
<Form.Item
label="First Name"
name="firstName"
required
{...genProps({ requiredMark: false })}
>
<Input />
</Form.Item>
<Form.Item label="Last Name" name="lastName" required {...genProps({ requiredMark: true })}>
<Input />
</Form.Item>
</Form>,
);

expect(container.querySelectorAll('.ant-form-item-required')).toHaveLength(2);
expect(container.querySelectorAll('.ant-form-item-required-mark-optional')).toHaveLength(2);
});
});

0 comments on commit 350b599

Please sign in to comment.