[Blocks Editor] Link popover save button disabled state (#18332)

* link save button disabled attribute added

* updated Link text field placeholder

* removed empty url check from submit

* minor changes
This commit is contained in:
Madhuri Sandbhor 2023-10-06 17:59:15 +05:30 committed by GitHub
parent 628d1d69a0
commit e76bef0e65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 20 deletions

View File

@ -293,6 +293,33 @@ describe('useBlocksStore', () => {
expect(screen.queryByLabelText(/Edit/i, { selector: 'button' })).toBeInTheDocument(); expect(screen.queryByLabelText(/Edit/i, { selector: 'button' })).toBeInTheDocument();
}); });
it('renders link fields to edit when user clicks the edit option and check save button disabled state', async () => {
const { result } = renderHook(useBlocksStore, { wrapper: Wrapper });
render(
result.current.link.renderElement({
children: 'Some link',
element: { url: 'https://example.com', children: [{ text: 'Some' }, { text: ' link' }] },
attributes: {},
}),
{
wrapper: Wrapper,
}
);
const link = screen.getByRole('link', 'Some link');
await user.click(link);
const editButton = screen.queryByLabelText(/Edit/i, { selector: 'button' });
await user.click(editButton);
const linkTextInput = screen.getByPlaceholderText('Enter link text');
const SaveButton = screen.getAllByRole('button', { type: 'submit' });
expect(SaveButton[1]).not.toBeDisabled(); // SaveButton[1] is a popover save button
// Remove link text and check if save button is disabled
userEvent.clear(linkTextInput);
expect(SaveButton[1]).toBeDisabled();
});
it('renders a code block properly', () => { it('renders a code block properly', () => {
const { result } = renderHook(useBlocksStore, { wrapper: Wrapper }); const { result } = renderHook(useBlocksStore, { wrapper: Wrapper });

View File

@ -272,8 +272,9 @@ const Link = React.forwardRef(({ element, children, ...attributes }, forwardedRe
); );
const [isEditing, setIsEditing] = React.useState(element.url === ''); const [isEditing, setIsEditing] = React.useState(element.url === '');
const linkRef = React.useRef(null); const linkRef = React.useRef(null);
const elementText = element.children.map((child) => child.text).join(''); const elementText = element.children.map((child) => child.text).join('');
const [linkText, setLinkText] = React.useState(elementText);
const [linkUrl, setLinkUrl] = React.useState(element.url);
const handleOpenEditPopover = (e) => { const handleOpenEditPopover = (e) => {
e.preventDefault(); e.preventDefault();
@ -282,22 +283,15 @@ const Link = React.forwardRef(({ element, children, ...attributes }, forwardedRe
const handleSave = (e) => { const handleSave = (e) => {
e.stopPropagation(); e.stopPropagation();
const data = new FormData(e.target);
if (data.get('url') === '') { // If the selection is collapsed, we select the parent node because we want all the link to be replaced
removeLink(editor); if (Range.isCollapsed(editor.selection)) {
setIsEditing(false); const [, parentPath] = Editor.parent(editor, editor.selection.focus?.path);
setPopoverOpen(false); Transforms.select(editor, parentPath);
} else {
// If the selection is collapsed, we select the parent node because we want all the link to be replaced
if (Range.isCollapsed(editor.selection)) {
const [, parentPath] = Editor.parent(editor, editor.selection.focus?.path);
Transforms.select(editor, parentPath);
}
editLink(editor, { url: data.get('url'), text: data.get('text') });
setIsEditing(false);
} }
editLink(editor, { url: linkUrl, text: linkText });
setIsEditing(false);
}; };
const handleCancel = () => { const handleCancel = () => {
@ -346,9 +340,10 @@ const Link = React.forwardRef(({ element, children, ...attributes }, forwardedRe
name="text" name="text"
placeholder={formatMessage({ placeholder={formatMessage({
id: 'components.Blocks.popover.text.placeholder', id: 'components.Blocks.popover.text.placeholder',
defaultMessage: 'This text is the text of the link', defaultMessage: 'Enter link text',
})} })}
defaultValue={elementText} value={linkText}
onChange={(e) => setLinkText(e.target.value)}
/> />
</Field> </Field>
<Field width="300px"> <Field width="300px">
@ -358,7 +353,12 @@ const Link = React.forwardRef(({ element, children, ...attributes }, forwardedRe
defaultMessage: 'Link', defaultMessage: 'Link',
})} })}
</FieldLabel> </FieldLabel>
<FieldInput name="url" placeholder="https://strapi.io" defaultValue={element.url} /> <FieldInput
name="url"
placeholder="https://strapi.io"
value={linkUrl}
onChange={(e) => setLinkUrl(e.target.value)}
/>
</Field> </Field>
<Flex justifyContent="end" width="100%" gap={2}> <Flex justifyContent="end" width="100%" gap={2}>
<Button variant="tertiary" onClick={handleCancel}> <Button variant="tertiary" onClick={handleCancel}>
@ -367,7 +367,7 @@ const Link = React.forwardRef(({ element, children, ...attributes }, forwardedRe
defaultMessage: 'Cancel', defaultMessage: 'Cancel',
})} })}
</Button> </Button>
<Button type="submit"> <Button type="submit" disabled={!linkText || !linkUrl}>
{formatMessage({ {formatMessage({
id: 'components.Blocks.popover.save', id: 'components.Blocks.popover.save',
defaultMessage: 'Save', defaultMessage: 'Save',

View File

@ -628,7 +628,7 @@
"components.Blocks.modifiers.code": "Code", "components.Blocks.modifiers.code": "Code",
"components.Blocks.link": "Link", "components.Blocks.link": "Link",
"components.Blocks.popover.text": "Text", "components.Blocks.popover.text": "Text",
"components.Blocks.popover.text.placeholder": "This text is the text of the link", "components.Blocks.popover.text.placeholder": "Enter link text",
"components.Blocks.popover.link": "Link", "components.Blocks.popover.link": "Link",
"components.Blocks.popover.save": "Save", "components.Blocks.popover.save": "Save",
"components.Blocks.popover.cancel": "Cancel", "components.Blocks.popover.cancel": "Cancel",