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

Add conditional rendering #403

Merged
merged 15 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PropertiesPanelHeaderProvider } from './PropertiesPanelHeaderProvider';
import { PropertiesPanelPlaceholderProvider } from './PropertiesPanelPlaceholderProvider';

import {
ConditionGroup,
CustomValuesGroup,
GeneralGroup,
ValidationGroup,
Expand All @@ -26,6 +27,7 @@ function getGroups(field, editField) {

const groups = [
GeneralGroup(field, editField),
ConditionGroup(field, editField),
...ValuesGroups(field, editField),
ValidationGroup(field, editField),
CustomValuesGroup(field, editField)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useMemo } from 'preact/hooks';
import { FeelEntry, isFeelEntryEdited } from '@bpmn-io/properties-panel';
import { get } from 'min-dash';

import {
useService,
useVariables
} from '../hooks';


export function ConditionEntry(props) {
const {
editField,
field
} = props;

return [
{
id: 'conditional-hide',
component: Condition,
editField: editField,
field: field,
isEdited: isFeelEntryEdited
}
];
}


function Condition(props) {
const {
editField,
field,
id
} = props;

const debounce = useService('debounce');

const variablesList = useVariables();

// keep stable reference until https://github.com/bpmn-io/properties-panel/issues/196 is fixed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be resolved now via 3a51c37

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! Let's check this in action.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK done

// eslint-disable-next-line react-hooks/exhaustive-deps
const variables = useMemo(() => variablesList.map(name => ({ name })), variablesList);

const path = [ 'conditional', 'hide' ];

const getValue = () => {
return get(field, path, '');
};

const setValue = (value) => {
if (!value) {
return editField(field, 'conditional', undefined);
}

return editField(field, 'conditional', { hide: value });
};

return FeelEntry({
debounce,
description: 'Condition under which the field is hidden',
element: field,
feel: 'required',
getValue,
id,
label: 'Hide if',
setValue,
variables
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export { default as ValueEntry } from './ValueEntry';
export { default as CustomValueEntry } from './CustomValueEntry';
export { default as ValuesSourceSelectEntry } from './ValuesSourceSelectEntry';
export { default as InputKeyValuesSourceEntry } from './InputKeyValuesSourceEntry';
export { default as StaticValuesSourceEntry } from './StaticValuesSourceEntry';
export { default as StaticValuesSourceEntry } from './StaticValuesSourceEntry';
export { ConditionEntry } from './ConditionEntry';
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
ConditionEntry
} from '../entries';


export function ConditionGroup(field, editField) {

const { type } = field;

if (type === 'default') {
return null;
}

const entries = [
...ConditionEntry({ field, editField })
];

return {
id: 'condition',
label: 'Condition',
entries
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as GeneralGroup } from './GeneralGroup';
export { default as ValidationGroup } from './ValidationGroup';
export { default as ValuesGroups } from './ValuesGroups';
export { default as CustomValuesGroup } from './CustomValuesGroup';
export { default as CustomValuesGroup } from './CustomValuesGroup';
export { ConditionGroup } from './ConditionGroup';
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as useService } from './usePropertiesPanelService';
export { default as useService } from './usePropertiesPanelService';
export { useVariables } from './useVariables';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getSchemaVariables } from '@bpmn-io/form-js-viewer';
import useService from './usePropertiesPanelService';

/**
* Retrieve list of variables from the form schema.
*
* @returns { string[] } list of variables used in form schema
*/
export function useVariables() {
const form = useService('formEditor');
const schema = form.getSchema();

return getSchemaVariables(schema);
}
2 changes: 1 addition & 1 deletion packages/form-js-editor/src/render/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as useService } from './useService';
export { default as usePrevious } from './usePrevious';
export { default as useDebounce } from './useDebounce';
export { default as useDebounce } from './useDebounce';
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('properties panel', function() {
it('should render (no field)', async function() {

// given
const result = createPropertiesPanel({ container });
const result = createPropertiesPanel({ container, schema: null });

// then
const placeholder = result.container.querySelector('.bio-properties-panel-placeholder');
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('properties panel', function() {
expect(result.container.querySelector('.fjs-properties-panel-placeholder')).not.to.exist;

expect(result.container.querySelector('.bio-properties-panel-header-type')).to.exist;
expect(result.container.querySelectorAll('.bio-properties-panel-group')).to.have.length(3);
expect(result.container.querySelector('.bio-properties-panel-group')).to.exist;
});


Expand Down Expand Up @@ -2072,7 +2072,7 @@ function createPropertiesPanel(options = {}) {

if (!formEditor) {
formEditor = new formEditorMock({
state: { schema: (options.schema !== 'undefined' ? options.schema : schema) }
state: { schema: options.schema !== undefined ? options.schema : schema }
});
}

Expand Down
Loading