Table of Content
// 'contract-crudeditor' package.
import React from 'react';
import createEditor, {
VIEW_SEARCH,
VIEW_CREATE,
VIEW_EDIT,
VIEW_SHOW
} from '@opuscapita/react-crudeditor';
const ContractEditor = createEditor(<Model Definition>);
export default ContractEditor;
// application.
import React from 'react';
import ContractEditor from 'contract-crudeditor';
export default class extends React.Component {
render() {
return (
...
<ContractEditor
?view={?name: <string>, ?state: <object>}
?onTransition={<function>}
?externalOperations={<function>}
?uiConfig={{
?headerLevel: <integer>
}}
/>;
...
)
createEditor
is a function which the only argument is Model Definition object. It returns EditorComponent.
React component with the following props:
Name | Default | Description |
---|---|---|
view | { name: "search", state: {} } |
View Name and full/sliced View State |
onTransition | - | Editor State transition handler |
externalOperations | - | Function returning a set of External Operations handlers |
Name of a custom/standard View. Custom Views are defined in Model Definition's ui.customViews. Standard View is one of:
View Name | Description |
---|---|
search | Search criteria and result |
create | New entity instance creation |
edit | Existing entity instance editing |
show | The same as edit but read-only |
error | Error page |
Full/sliced State describing props.view.name. Its structure is determined by View it describes.
If View State is sliced, not given or {}
, all not-mentioned properties have their default values.
View State must be serializable.
{
?filter: {
<field name>: <serializable, filter value for the field>,
...
},
?sort: <string, sort field name>,
?order: <"asc"|"desc", sort order>,
?max: <natural number, search result limit>,
?offset: <whole number, search result offset>,
?hideSearchForm: <boolean, search form initial visibility>
}
Name | Default |
---|---|
filter | {} |
sort | Result field marked with sortByDefault (first sortable result field if no sortByDefault marker is set, or first result field if there are neither sortByDefault no sortable fields) |
order | "asc" |
max | 30 |
offset | 0 |
hideSearchForm | false |
{
?predefinedFields: <object, an entity instance with predefined field values>
}
Name | Default |
---|---|
predefinedFields | {} |
{
instance: <object, an entity instance with Logical Key fields only>,
?tab: <string, active tab name>
}
Name | Default |
---|---|
instance | - |
tab | First tab name |
{
code: <natural number, error code>,
?payload: <any, structure is defined by error code>
}
or
[{
code: <natural number, error code>,
?payload: <any, structure is defined by error code>
}, ...]
Name | Default |
---|---|
code | - |
payload | - |
A transition handler to be called after Editor State changes to the one with "ready" status. Its only argument is Editor State object. Usually the function reflects Editor State to URL. It may also change Editor State by rendering EditorComponent with new props.
function ({
name: <string, View name>, // See EditorComponent props.view.name
state: <object, Full View State> // See EditorComponent props.view.state
}) {
...
return; // Return value is ignored.
}
A function returning an array of External Operations. Each has a handler which is called when a corresponding External Operation is triggered by CRUD Editor.
No arguments are passed to the function in Create View since it does not have persistent instance.
In case of unsaved changes, Confirmation Dialog is called after dedicated button press and before handler() call => each external operation must have side effects, or set disabled to true, or set show to false - othersise calling Confirmation Dialog is in vain.
function(<object, entity persistent instance> ) {
...
return [{
handler() {
...
return; // Return value is ignored.
},
ui({
name: <string, View name>, // See EditorComponent props.view.name
state: <object, Full View State> // See EditorComponent props.view.state
}) {
return {
title() {
...
return <string, external operation translated title>,
},
?show: <boolean, true by default>,
?disabled: <boolean, false by default>,
/*
* whether the operation has own dedicated button (false)
* or it is to be placed in a dropdown of a previous button (true).
* A previous button is either previous external operation with "dropdown" set to false
* OR previous custom operation with "dropdown" set to false if there is no such external operation
* OR (for Search View) "Edit" button if there is no such external/custom operation.
*/
?dropdown: <boolean, true by default>,
/*
* React Element or string name of an icon to be displayed inside a button, ex. "trash", "edit";
* see full list at
* http://getbootstrap.com/components/#glyphicons
*/
?icon: <string|element>
};
}
}, ...]
}
An array of objects defines bulk operations that could be done with selected instances.
An object consist of two parts: handler function, that accepts an array of selected instances, and UI configuration for dropdown element(title).
...
customBulkOperations={[{
handler(instances) {
...
return ...; // Could return a Promise. Return nothing in case of synchronous function.
},
ui({ instances }) {
return {
title: <string, Button title>,
}
}
}]}
...
An object with optional configurations for UI.
Name | Type | Default | Description |
---|---|---|---|
headerLevel | integer from 1 to 6 | 1 | Header text size in all Views. Specially designed for sub-editors. |
Complete example of the model configuration file: contracts model.
Model Definition is an object describing an entity. It has the following structure:
{
model: {
name: <string, usually singular entity name>,
translationsKeyPrefix: <string>,
/*
* Persistent fields CRUD Editor is interested in.
*/
fields: {
<field name>: {
/*
* At least one field must have "unique" property set to true.
*/
?unique: <boolean, whether the field is a part of Logical Key, false by default>,
?type: <string, field type (see corresponding "Terminology" section)>,
/*
* Constraints for field validation.
* Their allowed set and tuning parameters of each constraint depend on field type.
* Constraints are usually called after field input's "onBlur" event
* and before saving instance modifications.
*/
?constraints: {
?max: <number|date, max length for strings or max value for dates/numbers>,
?min: <number|date, min length for strings or min value for dates/numbers>,
?required: <boolean, whether the field value can be empty>,
?email: <boolean>,
?matches: <regexp>,
?url: <boolean>,
/*
* Custom field-validator returning boolean true in case of successful validation,
* or throwing an array of errors (or single error object) if validation failed.
*/
?validate(<serializable, field value>, <object, entity instance>) {
...
throw [<Field Validation Error>, ...];
...
return true;
}
}
},
...
},
/*
* Custom instance-validator, usually called after "Submit" button press
* but before sending the instance to the server for save/modify.
* Field-validation is done upon all fields just before calling the instance-validator.
* The function returns boolean true in case of successful validation,
* or throws an array of error (or single error object) if validation failed.
* The function may also be asyncronous and return a promise.
*/
?validate({
persistentInstnace: <object, entity instance as saved on server, null for Create View>,
formInstnace: <object, entity instance as displayed in the View>,
viewName: <string, View name>, // See EditorComponent props.view.name
}) {
...
throw [<Instance Validation Error>, ...];
...
return true;
},
translations: <object, i18n translations> // See "i18n tranlations" section.
},
permissions: {
crudOperations: {
/*
* At least one field must be set to 'true' or defined as a function.
*
* Each permission can be defined as either a boolean or a function.
*
* If defined as a boolean, a permission sets editor-wise user permission
* for a specific operation.
*
* An example for booleans:
* {
* create: true,
* delete: false,
* ...
* }
*
* If defined as a function, a permission operates in two modes,
* depending on a number of function arguments:
* - "global" mode - (no arguments) function's return value
* sets editor-wise user permission for a specific operation.
* - "per-instance" mode - (<object, entity instance> as the only argument)
* function's return value sets instance-wise user permission for
* a specific operation.
*
* Editor-wise permission is checked before instance-wise one, therefore if
* "global" premission is 'false' then "per-instance" permission is ignored.
*
* If specified, 'edit' and 'delete' permission defined as a function
* _must_ operate in both "global" and "per-instance" mode.
*
* If specified, 'create' and 'view' permission defined as a function
* _must_ operate in "global" only mode.
*
* An example for functions:
* {
* create: () => {
* ...
* return <boolean>; // editor-wise permission.
* },
* delete: ({ instance } = {}) => {
* if (instance) {
* // The function is called in "per-instance" mode.
* ...
* return <boolean>; // instance-wise permission.
* } else {
* // The function is called in "global" mode.
* ...
* return <boolean>; // editor-wise permission.
* }
* },
* ...
* }
*/
?create: <boolean|function>, // false by default
?edit: <boolean|function>, // false by default
?delete: <boolean|function>, // false by default
?view: <boolean|function>, // false by default
}
},
/*
* Methods for async operations.
* Each method returns a promise. In case of failure it rejects to
* {
* code: <whole number, error code>,
* ?payload: <any, structure is defined by error code>
* }
*/
api: {
/*
* get single entity instance by its Logical Key.
*/
async get: function({
instance: <object, an entity instance with at least Logical Key fields>
}) {
...
return {
<field name>: <serializable, field value>,
...
};
},
/*
* search for entity instances by a criteria.
*/
async search: function({
?filter: {
<field name>: <serializable, filter value for the field>,
...
},
?sort: <string, sort field name>,
?order: <"asc"|"desc", sort order>,
?max: <natural number, search result limit>,
?offset: <whole number, search result offset>
}) {
...
return {
instances: [{
<field name>: <serializable, field value>,
...
}, ...],
totalCount: <whole number, total number of filtered entity instances>
};
},
/*
* Delete entity instances by their Logical Keys.
* In case of a failure deleting one or more instances,
* an optional "errors" property may be specified with an array of error objects.
* If error object format corresponds to Instance Validation Error, appropriately
* translated messages are displayed as Notifications.
* Errors array length may be different from the number of instances failed to be deleted.
*/
async delete: function({
instances: <array[object], entity instances with at least Logical Key fields>
}) {
...
return {
count: <whole number, how many entity instances where actually deleted>,
?errors: [<object>, ...]
};
},
/*
* create new entity instance and return its actial server copy.
*/
async create: function({
instance: {
<field name>: <serializable, field value>,
...
}
}) {
...
return {
<field name>: <serializable, field value>,
...
};
},
/*
* update existing entity instance and return its actial server copy.
*/
async update: function({
instance: {
<field name>: <serializable, field value>,
...
}
}) {
...
return {
<field name>: <serializable, field value>,
...
};
}
},
?ui: {
?Spinner: <function, React component to be displayed instead of built-in spinner>,
?search: function() {
...
return {
/*
* Only Persistent fields from model.fields are allowed.
* By default, all Persistent fields from model.fields
* are used for building search criteria.
*/
?searchableFields: [{
name: <string, persistent field name>,
/*
* There is no default "render" property for a field of custom Field Type
* => "render" property must be explicitly defined in such a case.
*
* Default "render" property for a field of standard Field Type:
* {
* component: <string, id of default FieldInputComponent for displaying the Field Type>,
*
* value: {
* propName: "value",
* type: <string, UI Type peculiar to the default FieldInputComponent>
* }
* }
*/
?render: {
/*
* Either custom FieldInputComponent (see corresponding subheading)
* or id of embedded FieldInputComponent.
*/
component: <FieldInputComponent|string>,
?props: <object, the component props to overwrite defaults>,
?value: {
?propName: <string, a name of component prop with field value>,
/*
* Redundant for an embedded FieldInputComponent,
* because UI Type it works with is already known to CRUD Editor.
*
* When omitted for custom FieldInputComponent, UI Type is considered to be unknown.
* In such a case:
* 1. either define converter,
* 2. or unconverted (i.e. of Field Type) field value is sent to FieldInputComponent and
* FieldInputComponent is presupposed to return a value of Field Type.
*
* Ignored when custom "converter" is defined.
*/
?type: <string, embedded UI Type (see corresponding "Terminology" section)>,
/*
* Custom converter which overwrites default converter, if any.
*
* There is a default converter when Field Type is known to CRUID Editor and
* 1. component is embedded FieldInputComponent, or
* 2. component is custom FieldInputComponent and "type" with UI Type is specified.
*/
?converter: {
/*
* Field Type to UI Typer converter.
*/
format(value) {
...
return <serializable>;
}
/*
* UI Type to Field Type converter.
* An error must be thrown if a value is invalid, i.e. cannot be converted to the Field Type.
*/
parse(value) {
...
return <serializable>;
}
}
}
}
}, ...],
/*
* Both persistent and composite fields are allowed.
* By default, all Persistent fields from model.fields are used in result listing.
* Only one field may have "sortByDefault" set to true.
*/
?resultFields: [{
name: <string, persistent or composite field name>,
?sortable: <boolean, false by default>,
?sortByDefault: <boolean, false by default>,
?textAlignment: <"left"|"center"|"right">,
?component: <FieldRenderComponent> // see "FieldRenderComponent" subheading.
}, ...]
};
},
/*
* Generate label for entity instance description.
* Default is instance._objectLabel
*/
?instanceLabel(<object, entity instance>) {
...
return <string, entity instance description>;
},
?create: {
/*
* Generate and return an entity instance with predefined field values.
* The instance is not persistent.
*/
?defaultNewInstance: function(<object, "search" View State>) {
...
return <object, entity instance>;
},
/*
* tab(), section() and field() may be replaced with false/undefined/null which are ignored.
*
* See "TabFormComponent" and "FieldInputComponent" subheading for React components props.
*
* If formLayout is not specified, create/edit/show View does not have any tabs/sections
* and displays all fields from the model. The following fields are read-only in such case:
* -- all fields in Show view,
* -- Logical Key fields in Edit view.
*/
?formLayout: ({ tab, section, field }) => instance => {
...
return [
?tab(
{
name: <string, tab name>,
?disabled: <boolean, false by default>,
?component: <function, TabFormComponent>,
?columns: <number, 1 by default>
},
?section(
{ name: <string, section name>, ?columns: <number, columns in parent tab by default> },
?field({
name: <string, field name>,
?readOnly: <boolean, false by default>,
?render: { // see "searchableFields" above for detailed explanation.
component: <FieldInputComponent|string>,
?props: <object, the component props to overwrite defaults>,
?value: {
?propName: <string, a name of component prop with field value, "value" by default>,
?type: <string, embedded UI Type (see corresponding "Terminology" section)>,
?converter: { format, parse }
}
},
?validate(<serializable, field value>, <object, entity instance>) { // Field-validator.
...
throw [<Instance Validation Error>, ...];
...
return true;
}
}),
?field({
name: <string, field name>,
?readOnly: <boolean, false by default>,
?render: { // see "searchableFields" above for detailed explanation.
component: <FieldInputComponent|string>,
?props: <object, the component props to overwrite defaults>,
?value: {
?propName: <string, a name of Component prop with field value, "value" by default>,
?type: <string, embedded UI Type (see corresponding "Terminology" section)>,
?converter: { format, parse }
},
?validate(<serializable, field value>, <object, entity instance>) { // Field-validator.
...
throw [<Instance Validation Error>, ...];
...
return true;
}
}
}),
...
),
?field({
name: <string, field name>,
?readOnly: <boolean, false by default>,
?render: { // see "searchableFields" above for detailed explanation.
component: <FieldInputComponent|string>,
?props: <object, the component props to overwrite defaults>,
?value: {
?propName: <string, a name of Component prop with field value, "value" by default>,
?type: <string, embedded UI Type (see corresponding "Terminology" section)>,
?converter: { format, parse }
}
},
?validate(<serializable, field value>, <object, entity instance>) { // Field-validator.
...
throw [<Instance Validation Error>, ...];
...
return true;
}
}),
...
)
?section({ name: <string, section name> },
?field({
name: <string, field name>,
?readOnly: <boolean, false by default>,
?render: { // see "searchableFields" above for detailed explanation.
component: <FieldInputComponent|string>,
?props: <object, the component props to overwrite defaults>,
?value: {
?propName: <string, a name of Component prop with field value, "value" by default>,
?type: <string, embedded UI Type (see corresponding "Terminology" section)>,
?converter: { format, parse }
}
}
?validate(<serializable, field value>, <object, entity instance>) { // Field-validator.
...
throw [<Instance Validation Error>, ...];
...
return true;
}
}),
...
),
?field({
name: <string, field name>,
?readOnly: <boolean, false by default>,
?render: { // see "searchableFields" above for detailed explanation.
component: <FieldInputComponent|string>,
?props: <object, the component props to overwrite defaults>,
?value: {
?propName: <string, a name of Component prop with field value, "value" by default>,
?type: <string, embedded UI Type (see corresponding "Terminology" section)>,
?converter: { format, parse }
}
}
?validate(<serializable, field value>, <object, entity instance>) { // Field-validator.
...
throw [<Instance Validation Error>, ...];
...
return true;
}
}),
...
]
}
},
?edit: {
?formLayout: <function> // see ui.create.formLayout for details
},
?show: {
?formLayout: <function> // see ui.create.formLayout for details
},
/*
* Views in addition to standard ones.
* TODO
*/
?customViews: {
<view name>: <ViewComponent>, // see "ViewComponent" subheading.
...
},
/*
* Custom operations available in CRUD Editor.
* No arguments are passed to the method in Create View
* since it does not have persistent instance.
*/
?customOperations: function(<object, entity persistent instance> ) {
...
return [{
/*
* handler() is called at operation button render, not after button press
* => handler() must be a pure function.
* If handler() returns undefined, the button is displayed as disabled;
* otherwise view's name/state are saved and get redirected to only after the button press.
* When the button gets pressed and there are unsaved changes, Confirmation Dialog is called.
*
* Disabling the button by appropriate ui() return value
* prevents handler from been called at operation button render.
*/
handler() {
...
// return value is either undefined or view name/state.
return {
name: <string, View Name>,
?state: <object, View State, empty object by default>
};
},
ui({
name: <string, View name>, // See EditorComponent props.view.name
state: <object, Full View State> // See EditorComponent props.view.state
}) {
return {
title() {
...
return <string, custom operation translated title>,
},
?show: <boolean, true by default>,
?disabled: <boolean, false by default>,
/*
* whether the operation has own dedicated button (false)
* or it is to be placed in a dropdown of a previous button (true).
* A previous button is either previous custom operation with "dropdown" set to false
* OR (for Search View) "Edit" button if there is no such custom operation.
*/
?dropdown: <boolean, true by default>,
/*
* React Element or string name of an icon to be displayed inside a button, ex. "trash", "edit";
* see full list at
* http://getbootstrap.com/components/#glyphicons
*/
?icon: <string|element>
};
}
}, ...]
}
}
}
Custom React Component for rendering Formatted Instance's field in Search Form or Create/Edit/Show Form.
Props:
Name | Type | Necessity | Default | Description |
---|---|---|---|---|
readOnly | boolean | optional | false | Whether field value can be changed |
value | serializable | mandatory | - | Persistent field value formated to appropriate UI Type |
onChange | function | mandatory | - | Handler called when component's value changes.
|
onBlur | function | optional | - | Handler called when component loses focus.
|
In CRUD Editor here are two embedded FieldInputComponents:
FieldInputComponent | id |
---|---|
BUILTIN_INPUT | "input" |
BUILTIN_RANGE_INPUT | "rangeInput" |
For being treated as embedded, string id must be used. Additionally, the embedded FieldInputComponents can be imported from CRUD Editor package:
import { BUILTIN_INPUT, BUILTIN_RANGE_INPUT } from '@opuscapita/react-crudeditor';
Embedded FieldInputComponents also accept all props defined for FieldInputComponent.
Singular input field.
props.type | Description | UI Type | Auto-convertable field types |
---|---|---|---|
string |
Regular input field which works with strings | UI_TYPE_STRING | FIELD_TYPE_STRING, FIELD_TYPE_BOOLEAN, FIELD_TYPE_DECIMAL, FIELD_TYPE_INTEGER, FIELD_TYPE_STRING_DATE, FIELD_TYPE_STRING_DECIMAL, FIELD_TYPE_STRING_INTEGER |
checkbox |
Checkbox | UI_TYPE_BOOLEAN | FIELD_TYPE_BOOLEAN |
date |
DateInput | UI_TYPE_DATE | FIELD_TYPE_STRING_DATE |
integer |
Input which accepts only numbers and - sign and formats using i18n.formatNumber |
UI_TYPE_INTEGER | FIELD_TYPE_STRING_INTEGER, FIELD_TYPE_INTEGER, FIELD_TYPE_BOOLEAN, FIELD_TYPE_STRING |
decimal |
Input which accepts only numbers and - sign and formats using i18n.formatDecimalNumber |
UI_TYPE_DECIMAL | FIELD_TYPE_STRING_DECIMAL, FIELD_TYPE_DECIMAL, FIELD_TYPE_BOOLEAN, FIELD_TYPE_STRING |
Range input field.
props.type | Description | UI Type | Auto-convertable field types |
---|---|---|---|
string |
Range input which works with strings | UI_TYPE_STRING_RANGE_OBJECT | FIELD_TYPE_DECIMAL_RANGE, FIELD_TYPE_INTEGER_RANGE, FIELD_TYPE_STRING_DATE_RANGE, FIELD_TYPE_STRING_DECIMAL_RANGE, FIELD_TYPE_STRING_INTEGER_RANGE |
date |
DateRangeInput | UI_TYPE_DATE_RANGE_OBJECT | FIELD_TYPE_STRING_DATE_RANGE |
integer |
Range input which accepts only numbers and - sign and formats using i18n.formatNumber |
UI_TYPE_INTEGER_RANGE_OBJECT | FIELD_TYPE_STRING_INTEGER_RANGE, FIELD_TYPE_INTEGER_RANGE |
decimal |
Range input which accepts only numbers and - sign and formats using i18n.formatDecimalNumber |
UI_TYPE_DECIMAL_RANGE_OBJECT | FIELD_TYPE_STRING_DECIMAL_RANGE, FIELD_TYPE_DECIMAL_RANGE |
If you define just a Field Type in Model Definition's model.fields.\<field name>.type (and omit any custom render in searchableFields and formLayout), the following components will be default for the fields:
Field Type | Component | props.type |
---|---|---|
FIELD_TYPE_BOOLEAN | BUILTIN_INPUT | 'checkbox' |
FIELD_TYPE_STRING | BUILTIN_INPUT | 'string' |
FIELD_TYPE_DECIMAL_RANGE | BUILTIN_RANGE_INPUT | 'decimal' |
FIELD_TYPE_INTEGER_RANGE | BUILTIN_RANGE_INPUT | 'integer' |
FIELD_TYPE_STRING_DATE_RANGE | BUILTIN_RANGE_INPUT | 'date' |
FIELD_TYPE_STRING_DECIMAL_RANGE | BUILTIN_RANGE_INPUT | 'string' |
FIELD_TYPE_STRING_INTEGER_RANGE | BUILTIN_RANGE_INPUT | 'string' |
Field Type | Component | props.type |
---|---|---|
FIELD_TYPE_DECIMAL | BUILTIN_INPUT | 'decimal' |
FIELD_TYPE_INTEGER | BUILTIN_INPUT | 'integer' |
FIELD_TYPE_STRING_DATE | BUILTIN_INPUT | 'date' |
FIELD_TYPE_STRING_DECIMAL | BUILTIN_INPUT | 'string' |
FIELD_TYPE_STRING_INTEGER | BUILTIN_INPUT | 'string' |
Field Type | Component | props.type |
---|---|---|
FIELD_TYPE_DECIMAL | BUILTIN_RANGE_INPUT | 'decimal' |
FIELD_TYPE_INTEGER | BUILTIN_RANGE_INPUT | 'integer' |
FIELD_TYPE_STRING_DATE | BUILTIN_RANGE_INPUT | 'date' |
FIELD_TYPE_STRING_DECIMAL | BUILTIN_RANGE_INPUT | 'string' |
FIELD_TYPE_STRING_INTEGER | BUILTIN_RANGE_INPUT | 'string' |
Custom React component for rendering Formated Instance's persistent/composite field value in Search Result listing.
Props:
Name | Type | Necessity | Default | Description |
---|---|---|---|---|
name | string | mandatory | - | Field name from Model Definition's ui.search().resultFields |
instance | object | mandatory | - | Entity instance |
React component for a custom rendering of Tab form in create/edit/show Views.
Props:
Name | Type | Necessity | Default | Description |
---|---|---|---|---|
viewName | string | mandatory | - | View Name |
instance | object | mandatory | - | persistent instance |
doTransition | function | optional | - | Editor State change handler |
React component for a custom View.
Props:
Name | Type | Necessity | Default | Description |
---|---|---|---|---|
viewState | object | mandatory | - | Custom View State |
doTransition | function | optional | - | Editor State change handler |
This handler is called when
function ({
?name: <string, View Name>,
?state: <object, View State>
}) {
...
return; // return value is ignored.
}
Arguments:
Name | Default | Description |
---|---|---|
name | active View Name | To-be-displayed View Name |
state | {} |
Full/sliced to-be-displayed View State |
If View State is sliced, not given or {}
, all not-mentioned properties retain their current values (or default values in case of initial View rendering).
Model Definition's model.translations object has translations for labels/messages defined in the model. Its shape should correspond to preferred format for @opuscapita/i18n library.
Translation keys convention:
Translation Target | Translation Key | Default translation |
---|---|---|
Model name (shown in the header) | "model.name" |
"model.name" |
Model tab label | "model.tab.<tab name>.label" |
titleCase("<tab name>") |
Model section label | "model.section.<section name>.label" |
titleCase("<section name>") |
Model field label | "model.field.<field name>.label" |
titleCase("<field name>") |
Model field hint | "model.field.<field name>.hint" |
- |
Model field tooltip | "model.field.<field name>.tooltip" |
- |
Custom Field Validation Error | "model.field.<field name>.error.<error id>" |
error.message \|\| error.id |
Instance Validation Error | "model.error.<error id>" |
error.message \|\| <built-in error message> |
titleCase() converts its arugment from camelcase to titlecase, ex. titleCase("maxOrderValue") === "Max Order Value"
.
React context must have i18n
property with I18nManager as its value.
Every view must have "ready" status defined in its constants.js file for onTransition call to work properly.
{
common: {
activeViewName: <"search"|"create"|"edit"|"show"|"error">,
},
views: {
search: {
/*
* filter used in Search Result
*/
resultFilter: {
<field name>: <serializable, filter value for the field>,
...
},
/*
* raw filter as displayed in Search Form
* (may be equal to or different from "resultFilter")
*/
formFilter: {
<field name>: <serializable, filter value for the field>,
...
},
/*
* raw filter as communicated to React Components rendering Search fields
*/
formatedFilter: {
<field name>: <serializable, filter value for the field formated to corresponding UI Type>,
...
},
sortParams: {
field: <string, sort field name>,
order: <"asc"|"desc", sort order>,
},
pageParams: {
max: <natural number, search result limit>,
offset: <whole number, search result offset>,
}
resultInstances: [{
<field name>: <serializable, field value>,
...
}, ...],
selectedInstances: [
<ref, reference to an object from "instances" array>,
...
],
totalCount: <whole number, total number of filtered entity instances>,
status: <"uninitialized"|"initializing"|"ready"|"searching"|"deleting"|"redirecting", search view status>,
/*
* Parsing and Internal Errors -- see relevant subheadings
* (all other errors are displayed on "error" view)
*/
errors: {
fields: {
<field name>: [<Parsing Error>, ...],
...
},
general: [<Internal Error>, ...]
}
},
create: {
formInstance: {
<field name>: <serializable, field value>,
...
},
formatedInstance: {
<field name>: <serializable, field value formated to corresponding UI Type>,
...
},
status: <"ready"|"saving", create view status>
/*
* Parsing, Field/Instance Validation and Internal Errors -- see relevant subheadings
* (all other errors are displayed on "error" view)
*/
errors: {
fields: {
<field name>: [<Parsing Error or Field Validation Error>, ...],
...
},
general: [<Instance Validation Error or Internal Error>, ...]
}
},
edit: {
/*
* instance in its "canonical state", i.e. as present on the server
*/
persistentInstance: {
<field name>: <serializable, field value>,
...
},
/*
* row instance as displayed in Edit Form
*/
formInstance: {
<field name>: <serializable, field value>,
...
},
/*
* raw instance as communicated to React Components rendering Edit Form fields
*/
formatedInstance: {
<field name>: <serializable, field value formated to corresponding UI Type>,
...
},
/*
* Either an array of arrays (representing tabs) -- for tabbed layout,
* or an array of arrays (representing sections) and objects (representing fields) -- otherwise.
*/
formLayout: [
/*
* array representing a tab. Its elements are sections/fields. The array also has props:
* -- "tab", string with tab name,
* -- "disabled", boolean.
* -- "component", optional custom React Component, see TabFormComponent subheading.
*/
[
/*
* array representing a section. Its elements are fields. The array also has props:
* -- "section", string with section name.
*/
[
/*
* object representing a field.
*/
{
field: <string, field name>,
readOnly: <boolean>,
component: <function, FieldInputComponent or default React Component for displaying the field>
},
...
],
...
]
...
],
/*
* a ref to array representing active tab - for tabbed form layout,
* undefined - otherwise.
*/
activeTab: <array|undefined>,
instanceLabel: <string, entity instance description>,
status: <"uninitialized"|"initializing"|"ready"|"extracting"|"updating"|"deleting"|"redirecting", edit view status>,
/*
* Parsing, Field/Instance Validation and Internal Errors -- see relevant subheadings
* (all other errors are displayed on "error" view)
*/
errors: {
fields: {
<field name>: [<Parsing Error or Field Validation Error>, ...],
...
},
general: [<Instance Validation Error or Internal Error>, ...]
}
},
show: {
instance: {
<field name>: <serializable, field value>,
...
},
tab: <string, active tab name>,
status: <"uninitialized"|"initializing"|"ready"|"redirecting", show view status>,
},
error: {
errors: [{
code: <natural number, error code>,
?payload: <any, structure is defined by error code>
}],
status: <"uninitialized"|"ready"|"redirecting", error view status>
}
}
}
{
code: 400,
id: <string, error id used by translation service>,
?message: <string, default error message, usually in English - in case a translation is not provided>,
?args: <object, optional parameters for i18n service>
}
Both plain objects and instances of Error may be used. The error must not be an instance of system error constructor, like RangeError, SyntaxError, TypeError, etc.
{
code: 500,
id: <string, error id used by translation service>,
message: <string, default error message in English - in case a translation is not provided>,
?args: <object, optional parameters for i18n service>
}
All internal errors are plain objects, not instances of Error, InternalError, TypeError, etc.
Every view passes model property to external React Components it uses. The property is designed for communication with CRUD Editor and is distinct for different views. It's important to explicitly forward the property to children if they are designed to communicate with the editor. model property must never be modified by React Components.
model property general structure:
{
/*
* Dynamic collection of data from Redux store
* linked with selectors
* and updated every time the store state changes.
*/
data: {...},
/*
* Static collection of event handlers
* triggering async actions and Redux store state changes.
*/
actions: {...}
}
model property structure set by Search View:
{
data: {
entityName: <Model Definition>.model.name,
formFilter: state.formFilter,
formatedFilter: state.formatedFilter,
isLoading: <boolean, whether API async operation is in progress>,
pageParams: {
max: state.pageParams.max,
offset: state.pageParams.offset
},
resultFields: <Model Definition>.ui.search().resultFields || <array, default Result Fields>,
resultFilter: state.resultFilter,
resultInstances: state.resultInstances,
searchableFields: [{
name: <string, persistent field name>,
component: <function, React Component for rendering Formated Instance's field>,
valuePropName: <string, a name of component's prop with field value>,
/*
* Boolean, whether two react components for from-to ranging must be rendered.
* if true, filter field value consists of two keys, "from" and "to",
* and two distinct components are rendered for each of them.
* NOTE: always false for custom component.
*/
isRange: <boolean>
}]
selectedInstances: state.selectedInstances,
sortParams: {
field: state.sortParams.field,
order: state.sortParams.order
},
status: state.status,
totalCount: state.totalCount
},
actions: {
/*
* Switch to Create View
* and populate its form fields with <Model Definition>.ui.create.defaultNewInstance if exists.
*/
createInstance(),
/*
* Delete instances by asynchronously calling the server with
* <Model Definition>.api.delete()
* and <Model Definition>.api.search() to refresh Result Listing.
* Only Logical Key fields are required, all others are ignored.
deleteInstances([{
<field name>: <serializable, field value>,
...
}, ...]),
/*
* Load an instance in Edit View.
* Only Logical Key fields of the instance are required, all others are ignored.
*/
editInstance({
instance: {
<field name>: <serializable, field value>,
...
},
?tab: <string, active tab name>
}),
/*
* Clear all filter fields without Result Listing change.
*/
resetFormFilter(),
/*
* Make <Model Definition>.api.search() call to the server and display response in Result Listing.
*/
searchInstances({
?filter: {
<field name>: <serializable, filter value for the field>,
...
},
?sort: <string, sort field name>,
?order: <"asc"|"desc", sort order>,
?max: <natural number, search result limit>,
?offset: <whole number, search result offset>
}),
toggleSelected({
instance: <object, ref to an element of resultInstances array>
selected: <boolean, new selection state of the instance>,
}),
toggleSelectedAll(<boolean, new selection state of all instances from resultInstances array>),
/*
* Usually called with form field's onChange event. Result Listing is not automatically changed.
*/
updateFormFilter({
name: <string, field name>,
value: <serializable, filter value for the field>
})
}
}
, where state
is <Redux store state>.views.edit
.
model property structure set by Create View:
{
data: {...},
actions: {...}
, where state
is <Redux store state>.views.create
.
model property structure set by Edit View:
{
data: {
activeEntries: state.activeTab || state.formLayout,
activeTab: state.activeTab,
entityName: <Model Definition>.model.name,
formatedInstance: state.formatedInstance,
fieldsErrors: state.errors.fields,
fieldsMeta: <Model Definition>.model.fields,
generalErrors: storeState.errors.general,
instanceLabel: state.instanceLabel,
isLoading: <boolean, whether API async operation is in progress>,
persistentInstance: state.persistentInstance,
/*
* Elements from state.formLayout representing tabs.
* Empy array in case of tabless form layout.
*/
tabs: state.formLayout.filter(({ tab }) => tab),
status: state.status,
viewName: 'edit'
},
actions: {
/*
* Usually called with field's onChange event.
*/
changeInstanceField({
name: <string, field name>,
value: <serializable, new field value>
}),
/*
* Delete an instance by asynchronously calling the server with
* <Model Definition>.api.delete()
* and <Model Definition>.api.search() to exit to Search View and refresh its Result Listing.
* Only Logical Key fields are required, all others are ignored =>
* the action can be called either on formInstance or persistentInstance with the same effect.
*/
deleteInstances({
<field name>: <serializable, field value>,
...
}),
/*
* Exit to Search View.
*/
exitEdit(),
saveInstance(),
saveAndNewInstance(),
saveAndNextInstance(),
selectTab(<string, name of tab to activate>),
/*
* Usually called with field's onBlur event.
*/
validateInstanceField(<string, field name>)
}
}
, where state
is <Redux store state>.views.edit
.
model property structure set by Show View:
{
data: {...},
actions: {...}
, where state
is <Redux store state>.views.show
.
model property structure set by Error View:
{
data: {
errors: state.errors,
isLoading: <boolean, whether API async operation is in progress>,
},
actions: {
/*
* Navigate to CRUD Editor home page, which is Search View with default View State.
*/
goHome()
}
, where state
is <Redux store state>.views.show
.
An action symbolizes not a command but an effect, i.e. a change already happened in the application.
All actions are FSA-compliant.
Action types are in CONSTANT_CASE
and follow <NOUN>_<VERB>
pattern, e.g. `INSTANCE_ADD
. VERB
is in the present tense. Putting NOUN
first makes sorting actions more efficient.
An action creator name follows <verb><Noun>
pattern, e.g. createInstance()
.
Async actions are suffixed with
!!!TBD: "_SUCCESS" and "_FAIL" are not needed when FSA convention is followed.
Action types are saved in a separate file as sorted constants (e.g. var INSTANCE_ADD = 'INSTANCE_ADD';
) and used them from there. This avoids spelling errors, since if the variable doesn't exist, you'll get an error immediately, especially if you're linting.
Inner-view actions are scoped to their view, e.g. 'search/MY_ACTION_TYPE'
.
NOTE: It's entirely possible for a reducer defined in one folder to respond to an action defined in another folder[1].
project-root/
├── common/
│ └── ... # "common" namespace dir content.
├── components/ # Editor-wide React Components.
│ └── ....
├── views/
│ ├── create/
│ │ └── ... # "create" view namespace dir content.
│ ├── edit/
│ │ └── ... # "edit" view namespace dir content.
│ ├── error/
│ │ └── ... # "error" view namespace dir content.
│ ├── search/
│ │ └── ... # "search" view namespace dir content.
│ └── show/
│ ├── components/ # View-specific Presentational Components not aware of Redux.
│ │ └── ....
│ ├── actions.js # action creators (always encapsulated inside a duck).
│ ├── constants.js # actions' types and other constants.
│ ├── index.js # View main Container Component aware of Redux and subscribing to Redux state.
│ ├── reducer.js
│ ├── sagas.js
│ ├── selectors.js
│ └── tests.js
├── index.js # Editor main React Component.
├── rootReducer.js
└── rootSaga.js
Every view dir and common dir represents a ducks-complient namespace. All namespaces have similar dir structure (see show view for an example).
Not implemented: