forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Metrics UI] Fix EuiTheme type issue (elastic#69735)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> # Conflicts: # x-pack/plugins/infra/public/pages/metrics/inventory_view/components/waffle/waffle_sort_controls.tsx
- Loading branch information
1 parent
f34e428
commit 8516b39
Showing
10 changed files
with
152 additions
and
27 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
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
125 changes: 125 additions & 0 deletions
125
...gins/infra/public/pages/metrics/inventory_view/components/waffle/waffle_sort_controls.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,125 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { useCallback, useMemo, useState, ReactNode } from 'react'; | ||
import { EuiSwitch, EuiContextMenuPanelDescriptor, EuiPopover, EuiContextMenu } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiTheme, withTheme } from '../../../../../../../observability/public'; | ||
import { WaffleSortOption } from '../../hooks/use_waffle_options'; | ||
import { DropdownButton } from '../dropdown_button'; | ||
|
||
interface Props { | ||
sort: WaffleSortOption; | ||
onChange: (sort: WaffleSortOption) => void; | ||
} | ||
|
||
const LABELS = { | ||
name: i18n.translate('xpack.infra.waffle.sortNameLabel', { defaultMessage: 'Name' }), | ||
value: i18n.translate('xpack.infra.waffle.sort.valueLabel', { defaultMessage: 'Metric value' }), | ||
}; | ||
|
||
export const WaffleSortControls = ({ sort, onChange }: Props) => { | ||
const [isOpen, setIsOpen] = useState<boolean>(false); | ||
|
||
const showPopover = useCallback(() => { | ||
setIsOpen(true); | ||
}, [setIsOpen]); | ||
|
||
const closePopover = useCallback(() => { | ||
setIsOpen(false); | ||
}, [setIsOpen]); | ||
|
||
const label = LABELS[sort.by]; | ||
|
||
const button = ( | ||
<DropdownButton | ||
label={i18n.translate('xpack.infra.waffle.sortLabel', { defaultMessage: 'Sort by' })} | ||
onClick={showPopover} | ||
> | ||
{label} | ||
</DropdownButton> | ||
); | ||
|
||
const selectName = useCallback(() => { | ||
onChange({ ...sort, by: 'name' }); | ||
closePopover(); | ||
}, [closePopover, onChange, sort]); | ||
|
||
const selectValue = useCallback(() => { | ||
onChange({ ...sort, by: 'value' }); | ||
closePopover(); | ||
}, [closePopover, onChange, sort]); | ||
|
||
const toggleSort = useCallback(() => { | ||
onChange({ | ||
...sort, | ||
direction: sort.direction === 'asc' ? 'desc' : 'asc', | ||
}); | ||
}, [sort, onChange]); | ||
|
||
const panels = useMemo<EuiContextMenuPanelDescriptor[]>( | ||
() => [ | ||
{ | ||
id: 0, | ||
title: '', | ||
items: [ | ||
{ | ||
name: LABELS.name, | ||
icon: sort.by === 'name' ? 'check' : 'empty', | ||
onClick: selectName, | ||
}, | ||
{ | ||
name: LABELS.value, | ||
icon: sort.by === 'value' ? 'check' : 'empty', | ||
onClick: selectValue, | ||
}, | ||
], | ||
}, | ||
], | ||
[sort.by, selectName, selectValue] | ||
); | ||
|
||
return ( | ||
<EuiPopover | ||
isOpen={isOpen} | ||
id="sortPopover" | ||
button={button} | ||
anchorPosition="downLeft" | ||
panelPaddingSize="none" | ||
closePopover={closePopover} | ||
> | ||
<EuiContextMenu initialPanelId={0} panels={panels} /> | ||
<SwitchContainer> | ||
<EuiSwitch | ||
compressed | ||
label={i18n.translate('xpack.infra.waffle.sortDirectionLabel', { | ||
defaultMessage: 'Reverse direction', | ||
})} | ||
checked={sort.direction === 'desc'} | ||
onChange={toggleSort} | ||
/> | ||
</SwitchContainer> | ||
</EuiPopover> | ||
); | ||
}; | ||
|
||
interface SwitchContainerProps { | ||
theme: EuiTheme | undefined; | ||
children: ReactNode; | ||
} | ||
|
||
const SwitchContainer = withTheme(({ children, theme }: SwitchContainerProps) => { | ||
return ( | ||
<div | ||
style={{ | ||
padding: theme?.eui.paddingSizes.m, | ||
borderTop: `1px solid ${theme?.eui.euiBorderColor}`, | ||
}} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}); |
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