Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable edit tag "save changes" button if no changes have been made #138500

Merged
merged 4 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ export const CreateOrEditModal: FC<CreateOrEditModalProps> = ({
const ifMounted = useIfMounted();
const [submitting, setSubmitting] = useState<boolean>(false);

// we don't want this value to change when the user edit the name.
// we don't want this value to change when the user edits the tag
// eslint-disable-next-line react-hooks/exhaustive-deps
const initialName = useMemo(() => tag.name, []);

const initialTag = useMemo(() => tag, []);
const tagHasBeenModified = useMemo(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed a shame to import lodash isEqual here to compare 3 properties of an object so kept it simple

() =>
tag.name !== initialTag.name ||
tag.color !== initialTag.color ||
tag.description !== initialTag.description,
[initialTag, tag]
);
const setName = useMemo(() => setField('name'), [setField]);
const setColor = useMemo(() => setField('color'), [setField]);
const setDescription = useMemo(() => setField('description'), [setField]);
Expand Down Expand Up @@ -94,7 +100,7 @@ export const CreateOrEditModal: FC<CreateOrEditModalProps> = ({
id="xpack.savedObjectsTagging.management.editModal.title"
defaultMessage="Edit '{name}' tag"
values={{
name: initialName,
name: initialTag.name,
}}
/>
) : (
Expand Down Expand Up @@ -232,7 +238,7 @@ export const CreateOrEditModal: FC<CreateOrEditModalProps> = ({
fill
data-test-subj="createModalConfirmButton"
onClick={onFormSubmit}
isDisabled={submitting}
isDisabled={submitting || (isEdit && !tagHasBeenModified)}
>
{isEdit ? (
<FormattedMessage
Expand Down
35 changes: 35 additions & 0 deletions x-pack/test/saved_object_tagging/functional/tests/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,40 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
expect(uneditedTag).not.to.be(undefined);
expect(newTag).to.be(undefined);
});

describe('Disabling save button', () => {
const tag3Unmodified = {
name: 'tag-3',
description: 'Last but not least',
color: '#000000',
};
it('should disable save button if no property is changed', async () => {
await tagModal.openEdit('tag-3');

await tagModal.fillForm(tag3Unmodified, { submit: false });
expect(await tagModal.isConfirmDisabled()).to.be(true);
});
it('should enable save button if name is changed', async () => {
await tagModal.openEdit('tag-3');

await tagModal.fillForm({ ...tag3Unmodified, name: 'changed name' }, { submit: false });
expect(await tagModal.isConfirmDisabled()).to.be(false);
});
it('should enable save button if description is changed', async () => {
await tagModal.openEdit('tag-3');

await tagModal.fillForm(
{ ...tag3Unmodified, description: 'changed description' },
{ submit: false }
);
expect(await tagModal.isConfirmDisabled()).to.be(false);
});
it('should enable save button if color is changed', async () => {
await tagModal.openEdit('tag-3');

await tagModal.fillForm({ ...tag3Unmodified, color: '#FF0000' }, { submit: false });
expect(await tagModal.isConfirmDisabled()).to.be(false);
});
});
});
}