Skip to content

Commit a7d9dec

Browse files
authored
fix: Typography.Text delete did not follow state changing (ant-design#53861)
1 parent e73f274 commit a7d9dec

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

components/typography/Base/index.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ function wrapperDecorations(
111111

112112
const ELLIPSIS_STR = '...';
113113

114+
const DECORATION_PROPS = [
115+
'delete',
116+
'mark',
117+
'code',
118+
'underline',
119+
'strong',
120+
'keyboard',
121+
'italic'
122+
] as const;
123+
114124
const Base = React.forwardRef<HTMLElement, BlockProps>((props, ref) => {
115125
const {
116126
prefixCls: customizePrefixCls,
@@ -135,15 +145,7 @@ const Base = React.forwardRef<HTMLElement, BlockProps>((props, ref) => {
135145
// ============================ MISC ============================
136146
const prefixCls = getPrefixCls('typography', customizePrefixCls);
137147

138-
const textProps = omit(restProps, [
139-
'mark',
140-
'code',
141-
'delete',
142-
'underline',
143-
'strong',
144-
'keyboard',
145-
'italic',
146-
]);
148+
const textProps = omit(restProps, DECORATION_PROPS);
147149

148150
// ========================== Editable ==========================
149151
const [enableEdit, editConfig] = useMergedConfig<EditConfig>(editable);
@@ -463,7 +465,15 @@ const Base = React.forwardRef<HTMLElement, BlockProps>((props, ref) => {
463465
width={ellipsisWidth}
464466
onEllipsis={onJsEllipsis}
465467
expanded={expanded}
466-
miscDeps={[copied, expanded, copyLoading, enableEdit, enableCopy, textLocale]}
468+
miscDeps={[
469+
copied,
470+
expanded,
471+
copyLoading,
472+
enableEdit,
473+
enableCopy,
474+
textLocale,
475+
...DECORATION_PROPS.map(key => props[key as keyof BlockProps])
476+
]}
467477
>
468478
{(node, canEllipsis) =>
469479
wrapperDecorations(

components/typography/__tests__/index.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,51 @@ describe('Typography', () => {
497497
userEvent.keyboard('{enter}');
498498
await waitFor(() => expect(onEditStart).toHaveBeenCalledTimes(1));
499499
});
500+
501+
// https://github.com/ant-design/ant-design/issues/53858
502+
describe('decoration props can be changed dynamically', () => {
503+
const decorationProps = [
504+
{ propName: 'delete', tagName: 'del' },
505+
{ propName: 'mark', tagName: 'mark' },
506+
{ propName: 'code', tagName: 'code' },
507+
{ propName: 'underline', tagName: 'u' },
508+
{ propName: 'strong', tagName: 'strong' },
509+
{ propName: 'keyboard', tagName: 'kbd' },
510+
{ propName: 'italic', tagName: 'i' },
511+
];
512+
513+
decorationProps.forEach(({ propName, tagName }) => {
514+
it(`${propName} prop can be changed dynamically`, () => {
515+
const DynamicPropsTestCase = () => {
516+
const [propState, setPropState] = React.useState(false);
517+
const textProps = { [propName]: propState };
518+
return (
519+
<div>
520+
<Text {...textProps}>{`dynamic ${propName} text`}</Text>
521+
<button type="button" onClick={() => setPropState(!propState)} data-testid="toggle">
522+
Toggle
523+
</button>
524+
</div>
525+
);
526+
};
527+
528+
const { container, getByTestId } = render(<DynamicPropsTestCase />);
529+
530+
expect(container.querySelector(tagName)).toBeFalsy();
531+
532+
act(() => {
533+
fireEvent.click(getByTestId('toggle'));
534+
});
535+
536+
expect(container.querySelector(tagName)).toBeTruthy();
537+
expect(container.querySelector(tagName)?.textContent).toBe(`dynamic ${propName} text`);
538+
539+
act(() => {
540+
fireEvent.click(getByTestId('toggle'));
541+
});
542+
543+
expect(container.querySelector(tagName)).toBeFalsy();
544+
});
545+
});
546+
});
500547
});

0 commit comments

Comments
 (0)