Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
315 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
id: sharedUX/Page/TabbedModal | ||
slug: /shared-ux/modal/tabbed | ||
title: Kibana Tabbed Modal | ||
description: A component for rendering tabs within a modal. | ||
tags: ['shared-ux', 'component'] | ||
date: 2024-04-28 | ||
--- | ||
|
||
## API | ||
|
||
| Export | Description | | ||
|---|---| | | ||
| `TabbedModal` | component that renders tab content within a modal in kibana. | ||
| `IModalTabDeclaration` | type definition for defining the content of a modal tab, also provides support to make the tab content controlled and have it's state if needed be managed by the modal. | ||
|
||
## EUI Promotion Status | ||
|
||
This component is not currently considered for promotion to EUI. |
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,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/shared-ux/modal/tabbed'], | ||
}; |
110 changes: 110 additions & 0 deletions
110
packages/shared-ux/modal/tabbed/src/context/index.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,110 @@ | ||
/* | ||
* 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 React, { type ComponentProps, type ComponentType } from 'react'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useModalContext, ModalContextProvider } from '.'; | ||
|
||
type ModalContextProviderProps = ComponentProps<typeof ModalContextProvider>; | ||
|
||
function createModalContextWrapper<T extends ComponentType>(props: ModalContextProviderProps) { | ||
return function CreatedWrapper({ children }) { | ||
return <ModalContextProvider {...props}>{children}</ModalContextProvider>; | ||
}; | ||
} | ||
|
||
describe('tabbed modal provider', () => { | ||
it('creates a default internal state of specific shape', () => { | ||
const props: ModalContextProviderProps = { | ||
tabs: [ | ||
{ | ||
id: 'test', | ||
name: 'Test', | ||
}, | ||
], | ||
defaultSelectedTabId: 'test', | ||
}; | ||
|
||
const { result } = renderHook(useModalContext, { | ||
wrapper: createModalContextWrapper(props), | ||
}); | ||
|
||
expect(result.current).toHaveProperty( | ||
'tabs', | ||
([] as ModalContextProviderProps['tabs']).concat(props.tabs).map((tab) => { | ||
if (tab.initialState) { | ||
delete tab.initialState; | ||
} | ||
|
||
return tab; | ||
}) | ||
); | ||
|
||
expect(result.current).toHaveProperty( | ||
'state', | ||
expect.objectContaining({ | ||
meta: expect.objectContaining({ selectedTabId: props.defaultSelectedTabId }), | ||
test: {}, | ||
}) | ||
); | ||
|
||
expect(result.current).toHaveProperty('dispatch', expect.any(Function)); | ||
}); | ||
|
||
it('invocating the context dispatch function causes state changes for the selected tab state', () => { | ||
const SUT_DISPATCH_ACTION = { | ||
type: 'TEST_ACTION', | ||
payload: 'state_update', | ||
}; | ||
|
||
const props: ModalContextProviderProps = { | ||
tabs: [ | ||
{ | ||
id: 'test', | ||
name: 'Test', | ||
reducer: (state = {}, action) => { | ||
switch (action.type) { | ||
case SUT_DISPATCH_ACTION.type: | ||
return { | ||
...state, | ||
sut: action.payload, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}, | ||
}, | ||
], | ||
defaultSelectedTabId: 'test', | ||
}; | ||
|
||
const { result } = renderHook(useModalContext, { | ||
wrapper: createModalContextWrapper(props), | ||
}); | ||
|
||
expect(result.current).toHaveProperty( | ||
'state', | ||
expect.objectContaining({ | ||
test: {}, | ||
}) | ||
); | ||
|
||
act(() => { | ||
result.current.dispatch(SUT_DISPATCH_ACTION); | ||
}); | ||
|
||
expect(result.current).toHaveProperty( | ||
'state', | ||
expect.objectContaining({ | ||
test: { | ||
sut: SUT_DISPATCH_ACTION.payload, | ||
}, | ||
}) | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.