-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: custom validation properties panel
- Loading branch information
Showing
5 changed files
with
216 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
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
import { | ||
get, | ||
set | ||
} from 'min-dash'; | ||
|
||
import { useService } from '../hooks'; | ||
|
||
import { TextFieldEntry, FeelEntry } 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', | ||
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 TextFieldEntry({ | ||
debounce, | ||
element: field, | ||
getValue, | ||
id, | ||
label: 'Message', | ||
setValue, | ||
validate: messageEntryValidate | ||
}); | ||
} |
86 changes: 86 additions & 0 deletions
86
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,86 @@ | ||
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) { | ||
|
||
const validate = get(field, [ 'validate' ], {}); | ||
const shouldRender = [ undefined, VALIDATION_TYPE_OPTIONS.custom.value ].includes(validate.validationType); | ||
|
||
if (!shouldRender) { | ||
return; | ||
} | ||
|
||
return { | ||
id: 'custom-validation', | ||
label: 'Custom Validations', | ||
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' | ||
}; | ||
|
||
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 | ||
}), | ||
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