Skip to content

Commit

Permalink
Added maxVisibleValues option in minimize view (#147)
Browse files Browse the repository at this point in the history
* maxVisibleValues for minimize added

* updated description

* Move maxSelectValues to values section and update options picker to make consistent with grafana variable picker

* Fix test warnings

* Formatting

---------

Co-authored-by: asimonok <[email protected]>
Co-authored-by: Mikhail Volkov <[email protected]>
  • Loading branch information
3 people authored Apr 17, 2024
1 parent 482101d commit b1b7456
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Update selecting and deselecting multi-levels (#139)
- Update Auto/Static Minimize label width (#144)
- Add show and hide Minimize label (#144)
- Added maxVisibleValues option in minimize view (#147)

## 2.5.0 (2024-04-01)

Expand Down
58 changes: 33 additions & 25 deletions src/__mocks__/@grafana/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,40 @@ const actual = jest.requireActual('@grafana/ui');
/**
* Mock Select component
*/
const Select = jest.fn(({ options, onChange, value, isMulti, allowCustomValue, ...restProps }) => (
<select
onChange={(event: any) => {
if (onChange) {
if (isMulti) {
onChange(options.filter((option: any) => event.target.values.includes(option.value)));
} else {
onChange(options.find((option: any) => option.value === event.target.value));
const Select = jest.fn(({ options, onChange, value, isMulti, ...restProps }) => {
const selectProps: any = {};

if (restProps['aria-label']) {
selectProps['aria-label'] = restProps['aria-label'];
}

return (
<select
onChange={(event: any) => {
if (onChange) {
if (isMulti) {
onChange(options.filter((option: any) => event.target.values.includes(option.value)));
} else {
onChange(options.find((option: any) => option.value === event.target.value));
}
}
}
}}
/**
* Fix jest warnings because null value.
* For Select component in @grafana/ui should be used null to reset value.
*/
value={value === null ? '' : value}
multiple={isMulti}
{...restProps}
>
{options.map(({ label, value }: any) => (
<option key={value} value={value}>
{label}
</option>
))}
</select>
));
}}
/**
* Fix jest warnings because null value.
* For Select component in @grafana/ui should be used null to reset value.
*/
value={value === null ? '' : value}
multiple={isMulti}
{...selectProps}
>
{options.map(({ label, value }: any) => (
<option key={value} value={value}>
{label}
</option>
))}
</select>
);
});

/**
* Mock Button Row Toolbar
Expand Down
2 changes: 2 additions & 0 deletions src/components/MinimizeView/MinimizeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const MinimizeView: React.FC<Props> = ({
customValue = false,
showLabel = false,
labelWidth,
maxVisibleValues,
} = {},
eventBus,
panelEventBus,
Expand Down Expand Up @@ -94,6 +95,7 @@ export const MinimizeView: React.FC<Props> = ({
persistent={persistent}
customValue={customValue}
panelEventBus={panelEventBus}
maxVisibleValues={maxVisibleValues}
/>
)}
{variable.type === VariableType.TEXTBOX && <TextVariable variable={variable} panelEventBus={panelEventBus} />}
Expand Down
20 changes: 19 additions & 1 deletion src/components/OptionsVariable/OptionsVariable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,27 @@ interface Props {
* @type {EventBus}
*/
panelEventBus: EventBus;

/**
* Maximum visible values
*
* @type {number | undefined}
*/
maxVisibleValues: number | undefined;
}

/**
* Options Variable
* @param props
*/
export const OptionsVariable: React.FC<Props> = ({ variable, emptyValue, persistent, customValue, panelEventBus }) => {
export const OptionsVariable: React.FC<Props> = ({
variable,
emptyValue,
persistent,
customValue,
panelEventBus,
maxVisibleValues,
}) => {
/**
* Persistent storage
*/
Expand Down Expand Up @@ -128,6 +142,10 @@ export const OptionsVariable: React.FC<Props> = ({ variable, emptyValue, persist
isMulti={variable.multi}
value={variable.multi ? values : values[0] || null}
allowCustomValue={customValue}
maxVisibleValues={maxVisibleValues}
hideSelectedOptions={false}
closeMenuOnSelect={!variable.multi}
showAllSelectedWhenOpen={false}
/>
);
};
10 changes: 10 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ export const plugin = new PanelPlugin<PanelOptions>(VariablePanel)
},
category: ['Values'],
showIf: (config) => showForMinimizeView(config),
})
.addNumberInput({
path: 'maxVisibleValues',
name: 'Maximum visible values',
description: 'Supports for multi-choice variable.',
settings: {
placeholder: 'auto',
},
category: ['Values'],
showIf: (config) => showForMinimizeView(config),
});

/**
Expand Down
7 changes: 7 additions & 0 deletions src/types/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ export interface PanelOptions extends TableViewOptions {
* @type {number}
*/
labelWidth: number;

/**
* Maximum visible values
*
* @type {number}
*/
maxVisibleValues: number;
}

/**
Expand Down

0 comments on commit b1b7456

Please sign in to comment.