diff --git a/public/app/features/dashboard/containers/MultiOptionDropdown.tsx b/public/app/features/dashboard/containers/MultiOptionDropdown.tsx new file mode 100644 index 00000000..ba1f87cb --- /dev/null +++ b/public/app/features/dashboard/containers/MultiOptionDropdown.tsx @@ -0,0 +1,126 @@ +import { css, cx } from '@emotion/css'; +import React, { useEffect, useState } from 'react'; + +import { GrafanaTheme2 } from '@grafana/data'; +import { VariableOption } from '@grafana/data/src/types/templateVars'; +import { reportInteraction } from '@grafana/runtime'; +import { Menu, Dropdown, useStyles2, ToolbarButton, Icon } from '@grafana/ui'; +import { useDashboardList } from 'app/features/browse-dashboards/state'; + +import { GitHubButtonStyles } from '../../../../style/GitHubButtonStyles'; + +interface Props { + values: VariableOption[]; + selectedValues: VariableOption[]; + onToggle: (option: VariableOption, clearOthers: boolean) => void; + showOptions: () => void; +} + +const OptionDropdown = ({ values, onToggle, selectedValues, showOptions }: Props) => { + const [isOpen, setIsOpen] = useState(false); + const gitHubButtonStyles = useStyles2(GitHubButtonStyles); + const styles = useStyles2(getStyles); + const dashboardList = useDashboardList(); + const isValid = dashboardList !== undefined; + const subCaterogyName = function (title: string | null) { + switch (title) { + default: + return 'namespace'; + } + }; + + const handleOnToggle = (option: VariableOption) => (event: React.MouseEvent) => { + const clearOthers = event.shiftKey || event.ctrlKey || event.metaKey; + event.preventDefault(); + event.stopPropagation(); + onToggle(option, clearOthers); + }; + + // 1. 현재 대시보드 uid 가져오기 + const currDashboard = + isValid && dashboardList.length + ? dashboardList.filter((v) => v.uid === window.location.pathname.split('/')[2])[0] + : { kind: '', uid: '', title: '', url: '' }; + + const createActions = values.map((value, index) => ({ + id: index, + text: value.text as string, // can't be string[] due to disabled multi-select + icon: 'plus', + url: `/d/${currDashboard.uid}/${currDashboard.title}?var-${subCaterogyName(currDashboard.title)}=${value.text}`, + hideFromTabs: true, + isCreateAction: true, + option: value, + })); + + // 2. 현재 체크된 것 가져오기 + const check = (selectedValues: VariableOption[], title: string) => { + for (const value of selectedValues) { + if (value.text === title) { + return true; + } + } + return false; + }; + + useEffect(() => { + showOptions(); + }, [showOptions]); + + const MenuActions = () => { + return ( + + {createActions.map((createAction, index) => ( + + ))} + + ); + }; + + return ( + + +
+
+ +
+
multi-select
+
+
+
+ ); +}; + +const getStyles = (theme: GrafanaTheme2) => ({ + buttonContent: css({ + alignItems: 'center', + display: 'flex', + }), + buttonText: css({ + [theme.breakpoints.down('md')]: { + display: 'none', + }, + }), + ellipsis: css({ + display: 'flex', + maxWidth: '200px', + whiteSpace: 'nowrap', + overflow: 'hidden', + textOverflow: 'ellipsis', + }), + button: css({ maxWidth: '200px' }), + text: css({ + paddingLeft: '10px', + }), +}); + +export default OptionDropdown; diff --git a/public/app/features/dashboard/containers/OptionDropdown.tsx b/public/app/features/dashboard/containers/OptionDropdown.tsx index 91f1dfa9..d74e28fa 100644 --- a/public/app/features/dashboard/containers/OptionDropdown.tsx +++ b/public/app/features/dashboard/containers/OptionDropdown.tsx @@ -10,11 +10,10 @@ import { useDashboardList } from 'app/features/browse-dashboards/state'; import { GitHubButtonStyles } from '../../../../style/GitHubButtonStyles'; interface Props { - // picker: OptionsPickerState; - options: VariableOption[]; + values: VariableOption[]; } -const OptionDropdown = ({ options }: Props) => { +const OptionDropdown = ({ values }: Props) => { const [isOpen, setIsOpen] = useState(false); const gitHubButtonStyles = useStyles2(GitHubButtonStyles); const styles = useStyles2(getStyles); @@ -34,16 +33,16 @@ const OptionDropdown = ({ options }: Props) => { ? dashboardList.filter((v) => v.uid === window.location.pathname.split('/')[2])[0] : { kind: '', uid: '', title: '', url: '' }; - const createActions = options.map((option, index) => ({ + const createActions = values.map((value, index) => ({ id: index, - text: option.text as string, // can't be string[] due to disabled multi-select + text: value.text as string, // can't be string[] due to disabled multi-select icon: 'plus', - url: `/d/${currDashboard.uid}/${currDashboard.title}?var-${subCaterogyName(currDashboard.title)}=${option.text}`, + url: `/d/${currDashboard.uid}/${currDashboard.title}?var-${subCaterogyName(currDashboard.title)}=${value.text}`, hideFromTabs: true, isCreateAction: true, })); // 2. 현재 체크된 것 가져오기 - const currSubCategory = options.filter((v) => v.selected === true)[0].text; + const currSubCategory = values.filter((v) => v.selected === true)[0].text; const MenuActions = () => { return ( diff --git a/public/app/features/variables/pickers/OptionsPicker/OptionsPicker.tsx b/public/app/features/variables/pickers/OptionsPicker/OptionsPicker.tsx index e64ad5fe..20d51318 100644 --- a/public/app/features/variables/pickers/OptionsPicker/OptionsPicker.tsx +++ b/public/app/features/variables/pickers/OptionsPicker/OptionsPicker.tsx @@ -4,6 +4,7 @@ import { bindActionCreators } from 'redux'; import { LoadingState } from '@grafana/data'; import { ClickOutsideWrapper } from '@grafana/ui'; +import MultiOptionDropdown from 'app/features/dashboard/containers/MultiOptionDropdown'; import OptionDropdown from 'app/features/dashboard/containers/OptionDropdown'; import { StoreState, ThunkDispatch } from 'app/types'; @@ -111,16 +112,30 @@ export const optionPickerFactory = - - {/* {showOptions ? this.renderOptions(picker) : this.renderLink(variable)} */} - + <> +
+ {picker.multi ? ( + + ) : ( + + )} + {/* {showOptions ? this.renderOptions(picker) : this.renderLink(variable)} */} +
+
+ {picker.selectedValues.map((v, i) => ( +
{v.text}
+ ))} +
+ ); }