Skip to content

Commit

Permalink
Add disable and visible condition to TypeToolbarAction
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Jan 24, 2020
1 parent e338c9a commit 7683bc8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Expand Up @@ -13,6 +13,7 @@ jest.mock('../../../../stores/ResourceStore', () => jest.fn());

jest.mock('../../../../containers/Form', () => ({
ResourceFormStore: class {
data = {};
resourceStore;
types = {};
typesLoading = true;
Expand All @@ -35,7 +36,7 @@ jest.mock('../../../../views/Form', () => jest.fn(function() {
this.submit = jest.fn();
}));

function createTypeToolbarAction() {
function createTypeToolbarAction(options = {}) {
const resourceStore = new ResourceStore('test');
const resourceFormStore = new ResourceFormStore(resourceStore, 'test');
const router = new Router({});
Expand All @@ -46,7 +47,7 @@ function createTypeToolbarAction() {
router,
});

return new TypeToolbarAction(resourceFormStore, form, router, [], {});
return new TypeToolbarAction(resourceFormStore, form, router, [], options);
}

test('Return item config with correct disabled, loading, options, icon, type and value ', () => {
Expand Down Expand Up @@ -130,3 +131,29 @@ test('Throw error if no types are available in FormStore', () => {

expect(() => typeToolbarAction.getToolbarItemConfig()).toThrow(/actually supporting types/);
});

test('Return empty item config when passed visible condition is not met', () => {
const typeToolbarAction = createTypeToolbarAction({visible_condition: 'url == "/"'});

expect(typeToolbarAction.getToolbarItemConfig()).toBeUndefined();
});

test('Return item config when passed visible condition is met', () => {
const typeToolbarAction = createTypeToolbarAction({visible_condition: 'url == "/"'});
typeToolbarAction.resourceFormStore.data.url = '/';

expect(typeToolbarAction.getToolbarItemConfig()).toBeDefined();
});

test('Return disabled true when passed disable condition is not met', () => {
const typeToolbarAction = createTypeToolbarAction({disable_condition: 'url == "/"'});

expect(typeToolbarAction.getToolbarItemConfig()).toEqual(expect.objectContaining({disabled: false}));
});

test('Return disabled true when passed disable condition is met', () => {
const typeToolbarAction = createTypeToolbarAction({disable_condition: 'url == "/"'});
typeToolbarAction.resourceFormStore.data.url = '/';

expect(typeToolbarAction.getToolbarItemConfig()).toEqual(expect.objectContaining({disabled: true}));
});
@@ -1,4 +1,5 @@
// @flow
import jexl from 'jexl';
import type {ToolbarItemConfig} from '../../../containers/Toolbar/types';
import AbstractFormToolbarAction from './AbstractFormToolbarAction';

Expand All @@ -10,6 +11,20 @@ export default class TypeToolbarAction extends AbstractFormToolbarAction {
throw new Error('The ToolbarAction for types only works with entities actually supporting types!');
}

const {
visible_condition: visibleCondition,
disable_condition: disableCondition,
} = this.options;

if (visibleCondition && !jexl.evalSync(visibleCondition, this.resourceFormStore.data)) {
return;
}

let isDisabled = false;
if (disableCondition && jexl.evalSync(disableCondition, this.resourceFormStore.data)) {
isDisabled = true;
}

return {
type: 'select',
icon: 'su-brush',
Expand All @@ -22,6 +37,7 @@ export default class TypeToolbarAction extends AbstractFormToolbarAction {
},
loading: this.resourceFormStore.typesLoading,
value: this.resourceFormStore.type,
disabled: isDisabled,
options: Object.keys(formTypes).map((key: string) => ({
value: formTypes[key].key,
label: formTypes[key].title,
Expand Down

0 comments on commit 7683bc8

Please sign in to comment.