Skip to content

Commit 7157b5c

Browse files
authored
Merge pull request marmelab#10670 from marmelab/RichTextField-emptyText
Fix `<RichTextField>` should render `emptyText` when value is an empty string
2 parents 771e8b4 + 2432ea7 commit 7157b5c

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

packages/ra-ui-materialui/src/field/RichTextField.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('<RichTextField />', () => {
110110
expect(container.children[0].classList.contains('foo')).toBe(true);
111111
});
112112

113-
it.each([null, undefined])(
113+
it.each([null, undefined, ''])(
114114
'should render the emptyText when value is %s and stripTags is set to false',
115115
body => {
116116
const { queryByText } = render(
@@ -124,7 +124,7 @@ describe('<RichTextField />', () => {
124124
}
125125
);
126126

127-
it.each([null, undefined])(
127+
it.each([null, undefined, ''])(
128128
'should render the emptyText when value is %s and stripTags is set to true',
129129
body => {
130130
const { queryByText } = render(

packages/ra-ui-materialui/src/field/RichTextField.stories.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,29 @@ It is regarded as one of Tolstoy's finest literary achievements and remains a cl
133133
<RichTextField source="body" purifyOptions={{ ADD_ATTR: ['target'] }} />
134134
</RecordContextProvider>
135135
);
136+
137+
export const Empty = ({ emptyText, body }) => (
138+
<RecordContextProvider value={{ id: 1, body }}>
139+
<RichTextField source="body" emptyText={emptyText} />
140+
</RecordContextProvider>
141+
);
142+
Empty.args = {
143+
emptyText: 'empty',
144+
body: '',
145+
};
146+
Empty.argTypes = {
147+
emptyText: {
148+
options: [undefined, 'empty'],
149+
control: { type: 'inline-radio' },
150+
},
151+
body: {
152+
options: [undefined, null, 'empty string', 'foo'],
153+
mapping: {
154+
undefined: undefined,
155+
null: null,
156+
'empty string': '',
157+
'foo': 'foo',
158+
},
159+
control: { type: 'inline-radio' },
160+
},
161+
};

packages/ra-ui-materialui/src/field/RichTextField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const RichTextFieldImpl = <
4444
component="span"
4545
{...sanitizeFieldRestProps(rest)}
4646
>
47-
{value == null && emptyText ? (
47+
{(value == null || value === '') && emptyText ? (
4848
translate(emptyText, { _: emptyText })
4949
) : stripTags ? (
5050
removeTags(value)

0 commit comments

Comments
 (0)