-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into expandable-flyout-package-storybook
- Loading branch information
Showing
103 changed files
with
1,258 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# @kbn/unsaved-changes-prompt | ||
|
||
The useUnsavedChangesPrompt function is a custom React hook that prompts users with | ||
a confirmation dialog when they try to leave a page with unsaved changes. It blocks | ||
navigation and shows a dialog using the provided openConfirm function. If the user | ||
confirms, it navigates away; otherwise, it cancels the navigation, ensuring unsaved | ||
changes are not lost. | ||
|
||
|
||
```typescript | ||
import { useUnsavedChangesPrompt } from '@kbn/unsaved-changes-prompt'; | ||
|
||
export const SampleForm = ({ servicesForUnsavedChangesPrompt }) => { | ||
const { form } = useForm(); | ||
const isFormDirty = useFormIsModified({ form }); | ||
|
||
useUnsavedChangesPrompt({ | ||
hasUnsavedChanges: isFormDirty, | ||
...servicesForUnsavedChangesPrompt, | ||
}); | ||
|
||
return ( | ||
<> | ||
<Form form={form}> | ||
.... | ||
</Form> | ||
</> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { useUnsavedChangesPrompt } from './src/unsaved_changes_prompt'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-unsaved-changes-prompt'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/unsaved-changes-prompt", | ||
"owner": "@elastic/kibana-management" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@kbn/unsaved-changes-prompt", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0" | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { useUnsavedChangesPrompt } from './unsaved_changes_prompt'; |
65 changes: 65 additions & 0 deletions
65
...ges/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createMemoryHistory } from 'history'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
|
||
import { coreMock } from '@kbn/core/public/mocks'; | ||
import { CoreScopedHistory } from '@kbn/core/public'; | ||
|
||
import { useUnsavedChangesPrompt } from './unsaved_changes_prompt'; | ||
|
||
const basePath = '/mock'; | ||
const memoryHistory = createMemoryHistory({ initialEntries: [basePath] }); | ||
const history = new CoreScopedHistory(memoryHistory, basePath); | ||
const coreStart = coreMock.createStart(); | ||
const navigateToUrl = jest.fn().mockImplementation(async (url) => { | ||
history.push(url); | ||
}); | ||
|
||
describe('useUnsavedChangesPrompt', () => { | ||
it('should not block if not edited', () => { | ||
renderHook(() => | ||
useUnsavedChangesPrompt({ | ||
hasUnsavedChanges: false, | ||
http: coreStart.http, | ||
openConfirm: coreStart.overlays.openConfirm, | ||
history, | ||
navigateToUrl, | ||
}) | ||
); | ||
|
||
act(() => history.push('/test')); | ||
|
||
expect(history.location.pathname).toBe('/test'); | ||
expect(history.location.search).toBe(''); | ||
expect(coreStart.overlays.openConfirm).not.toBeCalled(); | ||
}); | ||
|
||
it('should block if edited', async () => { | ||
coreStart.overlays.openConfirm.mockResolvedValue(true); | ||
|
||
renderHook(() => | ||
useUnsavedChangesPrompt({ | ||
hasUnsavedChanges: true, | ||
http: coreStart.http, | ||
openConfirm: coreStart.overlays.openConfirm, | ||
history, | ||
navigateToUrl, | ||
}) | ||
); | ||
|
||
act(() => history.push('/test')); | ||
|
||
// needed because we have an async useEffect | ||
await act(() => new Promise((resolve) => resolve())); | ||
|
||
expect(navigateToUrl).toBeCalledWith('/mock/test', expect.anything()); | ||
expect(coreStart.overlays.openConfirm).toBeCalled(); | ||
}); | ||
}); |
Oops, something went wrong.