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

[Theme/Warning] Optional callback argument to setEuiDevProviderWarning #7820

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
1 change: 1 addition & 0 deletions packages/eui/changelogs/upcoming/7820.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Updated `setEuiDevProviderWarning` to additionally accept a custom callback function, which warning messages will be passed to
12 changes: 9 additions & 3 deletions packages/eui/src-docs/src/views/provider/provider_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { GuideSectionPropsTable } from '../../components/guide_section/guide_sec

import Setup from './provider_setup';
import GlobalStyles from './provider_styles';
import Warnings from './provider_warning';
import Warnings, { CallbackExample } from './provider_warning';
import {
EuiComponentDefaultsProps,
euiProviderComponentDefaultsSnippet,
Expand Down Expand Up @@ -237,8 +237,7 @@ export const ProviderExample = {
<Warnings />

<p>
<EuiCode>setEuiDevProviderWarning</EuiCode>
accepts three levels:
<EuiCode>setEuiDevProviderWarning</EuiCode> accepts three levels:
</p>
<ul>
<li>
Expand All @@ -254,6 +253,13 @@ export const ProviderExample = {
exception
</li>
</ul>

<p>
It also accepts a callback function instead of a default warning
level. The warning message string will be passed to your callback,
where any custom action can be performed on it. Example usage:
</p>
<CallbackExample />
</EuiText>
),
},
Expand Down
13 changes: 13 additions & 0 deletions packages/eui/src-docs/src/views/provider/provider_warning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,16 @@ const AppWithDuplicateProvider = () => (
</>
);
};

export const CallbackExample = () => (
<EuiCodeBlock language="tsx" fontSize="m" isCopyable>
{`import { setEuiDevProviderWarning } from '@elastic/eui';

const customWarningHandler = (message: string) => {
sendWarningToTelemetryService(message);
console.debug(message);
};

setEuiDevProviderWarning(customWarningHandler);`}
</EuiCodeBlock>
);
13 changes: 13 additions & 0 deletions packages/eui/src/services/theme/warning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,18 @@ describe('EUI provider dev warnings', () => {
expect(consoleLogSpy).not.toHaveBeenCalled();
expect(consoleWarnSpy).not.toHaveBeenCalled();
});

it('passes messages to callback functions', () => {
const devCallback = jest.fn();
setEuiDevProviderWarning(devCallback);

emitEuiProviderWarning(providerMessage);

expect(devCallback).toHaveBeenCalledWith('hello world');
expect(devCallback).toHaveBeenCalledTimes(1);

expect(consoleLogSpy).not.toHaveBeenCalled();
expect(consoleWarnSpy).not.toHaveBeenCalled();
});
});
});
12 changes: 9 additions & 3 deletions packages/eui/src/services/theme/warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
*/

type LEVELS = 'log' | 'warn' | 'error';
type ProviderCallback = (message: string | Error) => void;

let providerWarning: LEVELS | undefined = undefined;
let providerWarning: LEVELS | ProviderCallback | undefined = undefined;

export const setEuiDevProviderWarning = (level: LEVELS | undefined) =>
(providerWarning = level);
export const setEuiDevProviderWarning = (warningType: typeof providerWarning) =>
(providerWarning = warningType);

export const getEuiDevProviderWarning = () => providerWarning;

// Not a public top-level EUI export, currently for internal use
export const emitEuiProviderWarning = (providerMessage: string) => {
// Handle callback types
if (typeof providerWarning === 'function') {
return providerWarning(providerMessage);
}
// Handle level types
switch (providerWarning) {
case 'log':
console.log(providerMessage);
Expand Down
Loading