-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom validation properties panel
Related to #1162
- Loading branch information
Showing
6 changed files
with
194 additions
and
1 deletion.
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
109 changes: 109 additions & 0 deletions
109
packages/form-js-editor/src/features/properties-panel/entries/CustomValidationEntry.js
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,109 @@ | ||
import { get, set } from 'min-dash'; | ||
|
||
import { useService } from '../hooks'; | ||
|
||
import { FeelEntry, isFeelEntryEdited } from '@bpmn-io/properties-panel'; | ||
import { useCallback } from 'preact/hooks'; | ||
|
||
export function CustomValidationEntry(props) { | ||
const { editField, field, idPrefix, index } = props; | ||
|
||
const entries = [ | ||
{ | ||
component: Condition, | ||
editField, | ||
field, | ||
id: idPrefix + '-condition', | ||
idPrefix, | ||
index, | ||
}, | ||
{ | ||
component: Message, | ||
editField, | ||
field, | ||
id: idPrefix + '-message', | ||
idPrefix, | ||
index, | ||
}, | ||
]; | ||
|
||
return entries; | ||
} | ||
|
||
function Condition(props) { | ||
const { editField, field, id, index } = props; | ||
|
||
const debounce = useService('debounce'); | ||
|
||
const setValue = (value, error) => { | ||
if (error) { | ||
return; | ||
} | ||
|
||
const validate = get(field, ['validate']); | ||
const newValidate = set(validate, ['custom', index, 'condition'], value); | ||
|
||
return editField(field, 'validate', newValidate); | ||
}; | ||
|
||
const getValue = () => { | ||
return get(field, ['validate', 'custom', index, 'condition']); | ||
}; | ||
|
||
const conditionEntryValidate = useCallback((value) => { | ||
if (typeof value !== 'string' || value.length === 0) { | ||
return 'Must not be empty.'; | ||
} | ||
}, []); | ||
|
||
return FeelEntry({ | ||
feel: 'required', | ||
isEdited: isFeelEntryEdited, | ||
debounce, | ||
element: field, | ||
getValue, | ||
id, | ||
label: 'Condition', | ||
setValue, | ||
validate: conditionEntryValidate, | ||
}); | ||
} | ||
|
||
function Message(props) { | ||
const { editField, field, id, index } = props; | ||
|
||
const debounce = useService('debounce'); | ||
|
||
const setValue = (value, error) => { | ||
if (error) { | ||
return; | ||
} | ||
|
||
const validate = get(field, ['validate']); | ||
const newValidate = set(validate, ['custom', index, 'message'], value); | ||
|
||
return editField(field, 'validate', newValidate); | ||
}; | ||
|
||
const getValue = () => { | ||
return get(field, ['validate', 'custom', index, 'message']); | ||
}; | ||
|
||
const messageEntryValidate = useCallback((value) => { | ||
if (typeof value !== 'string' || value.length === 0) { | ||
return 'Must not be empty.'; | ||
} | ||
}, []); | ||
|
||
return FeelEntry({ | ||
feel: 'optional', | ||
isEdited: isFeelEntryEdited, | ||
debounce, | ||
element: field, | ||
getValue, | ||
id, | ||
label: 'Message if condition not met', | ||
setValue, | ||
validate: messageEntryValidate, | ||
}); | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/form-js-editor/src/features/properties-panel/groups/CustomValidationsGroup.js
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,80 @@ | ||
import { get, set, without } from 'min-dash'; | ||
|
||
import { arrayAdd } from '../Util'; | ||
|
||
import { ListGroup } from '@bpmn-io/properties-panel'; | ||
|
||
import { VALIDATION_TYPE_OPTIONS } from './ValidationGroup'; | ||
import { CustomValidationEntry } from '../entries/CustomValidationEntry'; | ||
|
||
export function CustomValidationsGroup(field, editField, getService) { | ||
const validate = get(field, ['validate'], {}); | ||
const { validatable, keyed } = getService('formFields').get(field.type).config; | ||
|
||
const isValidatable = validatable !== undefined ? validatable : keyed; | ||
const isCustomValidation = [undefined, VALIDATION_TYPE_OPTIONS.custom.value].includes(validate.validationType); | ||
const shouldRender = isValidatable && isCustomValidation; | ||
|
||
if (!shouldRender) { | ||
return; | ||
} | ||
|
||
return { | ||
id: 'custom-validation', | ||
label: 'Custom validations', | ||
tooltip: "Define custom validation rules for this field. Use 'value' to reference the field value.", | ||
component: ListGroup, | ||
...CustomValidationsEntry({ editField, field, id: 'custom-validation-list' }), | ||
}; | ||
} | ||
|
||
export function CustomValidationsEntry(props) { | ||
const { editField, field, id: idPrefix } = props; | ||
|
||
const addEntry = (e) => { | ||
e.stopPropagation(); | ||
|
||
const customValidations = get(field, ['validate', 'custom'], []); | ||
const newIndex = customValidations.length + 1; | ||
|
||
const newValue = { | ||
condition: '=false', | ||
message: 'Error message.', | ||
}; | ||
|
||
const newArray = arrayAdd(customValidations, newIndex, newValue); | ||
const newValidate = set(field.validate || {}, ['custom'], newArray); | ||
|
||
editField(field, ['validate'], newValidate); | ||
}; | ||
|
||
const removeEntry = (entry) => { | ||
const customValidations = get(field, ['validate', 'custom'], []); | ||
const newArray = without(customValidations, entry); | ||
const newValidate = set(field.validate, ['custom'], newArray); | ||
|
||
editField(field, ['validate'], newValidate); | ||
}; | ||
|
||
const items = get(field, ['validate', 'custom'], []).map((entry, index) => { | ||
const id = idPrefix + '-' + index; | ||
|
||
return { | ||
id, | ||
entries: CustomValidationEntry({ | ||
editField, | ||
field, | ||
idPrefix, | ||
index, | ||
}), | ||
label: 'Rule ' + (index + 1), | ||
remove: () => removeEntry(entry), | ||
}; | ||
}); | ||
|
||
return { | ||
items, | ||
add: addEntry, | ||
shouldSort: false, | ||
}; | ||
} |
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