From 4cf3ee8788c88905455aa66e003b1225bac69f32 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 7 Nov 2024 01:40:09 +1100 Subject: [PATCH] [8.x] feat: SavedObjectSaveModal - add postfix if the title is duplicated (#198777) (#199127) # Backport This will backport the following commits from `main` to `8.x`: - [feat: SavedObjectSaveModal - add postfix if the title is duplicated (#198777)](https://github.com/elastic/kibana/pull/198777) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Paulina Shakirova --- .../saved_object_save_modal.test.tsx | 50 +++++++++++++++++++ .../save_modal/saved_object_save_modal.tsx | 21 ++++++++ 2 files changed, 71 insertions(+) diff --git a/src/plugins/saved_objects/public/save_modal/saved_object_save_modal.test.tsx b/src/plugins/saved_objects/public/save_modal/saved_object_save_modal.test.tsx index 82e8051b897c7..273a79c307001 100644 --- a/src/plugins/saved_objects/public/save_modal/saved_object_save_modal.test.tsx +++ b/src/plugins/saved_objects/public/save_modal/saved_object_save_modal.test.tsx @@ -121,4 +121,54 @@ describe('SavedObjectSaveModal', () => { expect(onSave.mock.calls[0][0].newCopyOnSave).toBe(true); }); }); + + describe('handle title duplication logic', () => { + it('should append "[1]" to title if no number is present', async () => { + const onSave = jest.fn(); + + render( + + {}} + title="Saved Object" + objectType="visualization" + showDescription={true} + showCopyOnSave={true} + /> + + ); + + const switchElement = screen.getByTestId('saveAsNewCheckbox'); + await userEvent.click(switchElement); + + await waitFor(() => { + expect(screen.getByTestId('savedObjectTitle')).toHaveValue('Saved Object [1]'); + }); + }); + + it('should increment the number by one when a number is already present', async () => { + const onSave = jest.fn(); + + render( + + {}} + title="Saved Object [1]" + objectType="visualization" + showDescription={true} + showCopyOnSave={true} + /> + + ); + + const switchElement = screen.getByTestId('saveAsNewCheckbox'); + await userEvent.click(switchElement); + + await waitFor(() => { + expect(screen.getByTestId('savedObjectTitle')).toHaveValue('Saved Object [2]'); + }); + }); + }); }); diff --git a/src/plugins/saved_objects/public/save_modal/saved_object_save_modal.tsx b/src/plugins/saved_objects/public/save_modal/saved_object_save_modal.tsx index aa82feafb60aa..a3e6d1cc22b2a 100644 --- a/src/plugins/saved_objects/public/save_modal/saved_object_save_modal.tsx +++ b/src/plugins/saved_objects/public/save_modal/saved_object_save_modal.tsx @@ -276,7 +276,28 @@ export class SavedObjectSaveModal extends React.Component }); }; + private handleTitleDuplication = () => { + const regex = /\s*\[(\d+)\]$/; + const match = this.state.title.match(regex); + + if (match) { + const newNumber = Number(match[1]) + 1; + + this.setState({ + title: this.state.title.replace(regex, ` [${newNumber}]`), + }); + } else { + this.setState({ + title: this.state.title + ' [1]', + }); + } + }; + private onCopyOnSaveChange = (event: EuiSwitchEvent) => { + if (this.props.title === this.state.title && event.target.checked) { + this.handleTitleDuplication(); + } + this.setState({ copyOnSave: event.target.checked, });