hbp-spark v1.1.27
![]()
HBP-Spark 
HBP-Spark is a React components library built on top of MobX and React-Bootstrap.
Its goal is to provide a set of useful react components that let you build a consistent user interface with very little boilerplate code. The main focus of the framework is to provide a simple but powerful entry forms management for React applications.
Installation:
npm i -s hbp-sparkPeer dependencies
In order to use hbp-spark in an application, the following peer dependencies need to be installed:
- mobx >=4.0
- mobx-react >=5.0
- react >=15.4.0
- react-dom" >=15.4.0
- react-bootstrap >=0.32
Getting started
HBP-Spark form mechanism is based on a declarative configuration of the form structure as a Javascript (or JSON) Object, like so:
{
fields:{
username: {
type: "InputText",
label: "Your username"
},
age: {
type: "InputText",
label: "Your age",
inputType: "number"
},
preferedColor: {
type: "InputText",
label: "Your prefered color",
inputType: "color",
value: "#FF0000"
}
}
}Once this object matching your form data structure this object is provided to a FormStore instance provided by this library, you can use this store object and provide it to the <Form/> component. HBP-Spark lets you decide how you want to layout your form, or you can use one of the provided automatic layout (feature coming soon...). Check the example below:
import React from "react";
import { observer } from "mobx-react";
import { Row, Grid, Col } from "react-bootstrap";
import { Form, FormStore, Field } from "hbp-spark";
let peopleFormStructure = {...}; //See example definition above
@observer
export default class PeopleForm extends React.Component {
constructor(props) {
super(props);
this.formStore = new FormStore(peopleFormStructure);
}
render() {
return (
<Form store={this.formStore}>
<Grid>
<h2>People Form</h2>
<Row>
<Col xs={4}>
<Field name="username" />
</Col>
<Col xs={4}>
<Field name="preferedColor" />
</Col>
<Col xs={4}>
<Field name="age" />
</Col>
</Row>
<h2>Result</h2>
<Row>
<Col xs={12}>
<pre>{JSON.stringify(this.formStore.getValues(), null, 4)}</pre>
</Col>
</Row>
</Grid>
</Form>
);
}
}Getting the form data
At any time (e.g. when submitting the form), the getValues() method of the FormStore object returns the processed field values in a structured object matching the definition.
Documentation
To get a more detailed documentation with plenty of examples open a console in the package root and run:
npm installThen change your current directory to ./docs/ and run :
npm install
npm run startAPI Documentation
You can also find the API documentation below:
Table of Contents
- Stores
- Form
- FormFields
- SingleField
- ActionIcon
- GenericList
Stores
Define stores namespace.
FormStore
Mobx store to manage the Form React Component
Parameters
structurejson the underlying form definition
getValues
Get the form field values
Parameters
fieldsapplyMapping(optional, defaulttrue)
Returns object a structured object of the form field values
injectValues
Inject values into form fields, must be input the same format as valuesmethod output
Parameters
valuesobject structured object of the form field valuesmergeboolean whether or not to reset the whole form or merge with the passed in values (optional, defaultfalse)fieldspathstring base path for change
reset
Parameters
fieldsbasePathstring optional, base path to reset from Resets the form to their default values from the base path or completely if no path is provided
update
updates the underlying field definition
Parameters
parentPath
returns the parent path for a field
Parameters
field(string | field) can be either be a field path or a field object
genSiblingPath
Parameters
validate
validates all form fields at once
registerCustomValidationFunction
registers a custom validation functions that can be used in all fields
Parameters
namestring a name to uniquely identify the rulefuncfunction The definition of the validation function. The function parameters are the field value and attribute name. Can be a sync or async function. Expected return value either boolean or promise, indication if validation was successful.errorMessagestring The error message in case the validation fails
registerAxiosInstance
registers a custom axios instance - useful for APIs requiring tokens
Parameters
axiosInstanceobject a valid axios instance
toggleReadMode
toggles or force readMode to display form values as pure text instead of input fields
Parameters
statusboolean optional, a boolean indicating what the readMode state should be. If none is passed then the state is toggled
ClipboardStore
A Store handling a virtual clipboard of text selected inside the current browser window. Importing this module will always return the same instance of that store
reset
Clear the value stored in the virtual clipboard
Form
Form component that wraps the underlying Fields
Properties
storeobject required - An instance of the FormStore class
values
Get the form field values
Returns object a structured object of the form field values
FormFields
Field is a generic react component that supports all kinds of different input types
Properties
namestring required - Name of the field as defined in the definition object passed to the FormStoreonChangefunction Event handler triggered when changes occur to the underlying field value
InputTextField
A simple text input
Options
Parameters
labelstring "" - The field labeltypestring "InputText"valuestring "" - The current value of the fielddefaultValuestring "" - The defaultValue of the fieldinputTypestring "text" - The input type of the field (e.g. text, password, email)placeholderstring "" - A placeholder that is displayed when the field is emptypathstring "" - Field pathuseVirtualClipboardboolean false - Flag if virtual clipboard feature is enabled for this fieldemptyToNullboolean true - Flag that determines if empty values are transformed to null in the value function of the formStoredisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form inputvalidationRulesarray [] - A list of validation rulescustomErrorMessagesobject {} - Definition for custom error messages in the form: {rule: errorMessage}validationOptionsobject {onBlur: true, onChange: false} - Validation options to define when validation is executed
InputTextMultipleField
Allows the input of multiple values
Options
Parameters
labelstring "" - The field labeltypestring "InputTextMultiple"valuearray [] - The current value of the fielddefaultValuearray [] - The defaultValue of the fieldpathstring "" - Field pathmaxnumber Infinity - Maximum values that the field can haveuseVirtualClipboardboolean false - Flag if virtual clipboard feature is enabled for this fieldemptyToNullboolean false - Flag that determines if empty values are transformed to null in the value function of the formStoredisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form input
TextAreaField
Textarea input field. Field options are the same as for the InputTextField
Options
Parameters
labelstring "" - The field labeltypestring "InputText"valuestring "" - The current value of the fielddefaultValuestring "" - The defaultValue of the fieldpathstring "" - Field pathemptyToNullboolean true - Flag that determines if empty values are transformed to null in the value function of the formStoredisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form inputautosizeboolean true - If true, the textarea resizes automaticallyrowsnumber 1 - How many rows are displayed by default. Represents the min valuemaxRowsnumber null - How many rows are displayed at most before the field does not grow anymore (only possible if autosize is enabled)resizableboolean false - If true, the textarea is horizontally resizable by the user
NestedField
Allows the implementation of a nested field structure
NestedRemoveButton
Action button to remove a nested instance, has to be called by the app with <Field.Remove/> component
NestedMoveUpButton
Action button to move up a nested instance in the list, has to be called by the app with <Field.MoveUp/> component
NestedMoveDownButton
Action button to move down a nested instance in the list, has to be called by the app with <Field.MoveDown/> component
NestedDuplicateButton
Action button to duplicate a nested instance in the list, has to be called by the app with <Field.Duplicate/> component
Options
Parameters
labelstring "" - The field labelbuttonLabelstring "Add an item" - The label used for adding an item to the repeatable fieldstypestring "Nested"minnumber 1 - min of nested children the field can havemaxnumber 1 - max of nested children the field can havefieldsobject {} - The nested fields definitionsvaluestring [] - The value of the fielddefaultValuearray [] - The defaultValue of the fieldpathstring "" - Field pathtopAddButtonstring true - Whether or not to display the Add button before the fieldsbottomAddButtonstring true - Whether or not to display the Add button after the fieldsemptyToNullboolean false - Flag that determines if empty values are transformed to null in the value function of the formStoredisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form input
addInstance
add a new instance to a nested field
duplicateInstance
duplicates a nested instance at a given index
Parameters
indexinteger the instance to duplicate index
moveInstance
move a nested instance at a given index to a new given index
Parameters
indexinteger the instance to movenewIndexinteger the index that instance will have
removeInstance
removes a nested instance at a given index
Parameters
indexinteger the instance to remove index
SelectField
A simple select input field
Options
Parameters
labelstring "" - The field labeltypestring "Select"valuestring "" - The current value of the fielddefaultValuestring "" - The defaultValue of the fieldoptionsarray [] - an array of strings or objects with value and label defined by the mappingoptionsUrlstring null - url to fetch the select options fromcacheOptionsUrlstring false - whether to cache optionsUrl fetching responsepathstring "" - Field pathmappingValue(string | array) "value" - The name(s) of the option object field(s) related to the option value, used to match passed in values to actual optionsmappingLabelstring "label" - the name of the option object field related to the option labeldefaultLabelstring "null" - The label to be displayed as a default value when setemptyToNullboolean true - Flag that determines if empty values are transformed to null in the value function of the formStoredisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form input
DropdownSelectField
Form component allowing to select multiple values from a drop-down list with an option to allow custom values entered by the user. The handling of the a custom value is delegated to the application level through the call of the "onAddCustomValue" callback passed in paramter
Options
Parameters
labelstring "" - The field labeltypestring "DropdownSelect"valuearray [] - The current value of the fielddefaultValuearray [] - The defaultValue of the fieldoptionsarray [] - The options of the dropdown, must be an array of objectsoptionsUrlstring null - url to fetch the select options fromcacheOptionsUrlstring false - whether to cache optionsUrl fetching responsepathstring "" - Field pathallowCustomValuesboolean false - if the field should try to accept user inputed valuesmappingValue(string | array) "value" - The name(s) of the option object field(s) related to the option value, used to match passed in values to actual optionsmappingLabelstring "label" - the name of the option object field related to the option labelmappingReturnstring null - the property of the option object used to return the value(s) - null will return the whole objectreturnSingleboolean boolean - wether or not to return the first value or an array of valuesmaxnumber Infinity - Maximum values that the field can haveemptyToNullboolean false - Flag that determines if empty values are transformed to null in the value function of the formStorelistPositionboolean "bottom" - Can be "top" or "bottom", whether to display the dropdown list above or below the fielddisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form input
TreeSelectField
Form component allowing to select multiple values from a tree structure
Options
Parameters
labelstring "" - The field labeltypestring "TreeSelect"valuearray [] - The current value of the fielddefaultValuearray [] - The defaultValue of the fielddataarray {} - The tree structure to select from, must be an object with eventually an array of childrendataUrlarray null - url to fetch the tree structure fromcacheDataUrlstring false - whether to cache dataUrl fetching responsepathstring "" - Field pathmappingValue(string | array) "value" - The name(s) of the node object field(s) related to the node value, used to match passed in values to actual tree nodesmappingLabelstring "label" - the name of the node object field related to the node labelmappingChildrenstring "children" - the name of the node object field related to the node childrenmappingReturnstring null - the property of the option object used to return the value(s) - null will return the whole objectreturnSingleboolean boolean - wether or not to return the first value or an array of valuesmaxnumber Infinity - Maximum values that the field can haveselectOnlyLeafboolean false - If enabled, only leaves can be selected and not the intermediary nodesexpandToSelectedNodesboolean false - If enabled, tree selection modal will recursively expand to all the already selected valuesdefaultExpandedarray [] - an array of arrays describing a path of nodes expanded by default (tested on node labels, path parts are considered as RegExp)showOnlySearchedNodesboolean false - Flag that determines if nodes that doesn't match the text search should be hiddenemptyToNullboolean false - Flag that determines if empty values are transformed to null in the value function of the formStoredisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form input
CheckBoxField
A simple checkbox
Options
Parameters
labelstring "" - The field labeltypestring "CheckBox"valuestring false - The current value of the fielddefaultValuestring false - The defaultValue of the fieldpathstring "" - Field pathdisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form input
GroupSelectField
Form component allowing to select on/multiple values from a group of checkboxes/radioboxes
Options
Parameters
labelstring "" - The field labeltypestring "GroupSelect"valuearray [] - The current value of the fielddefaultValuearray [] - The defaultValue of the fieldoptionsarray [] - The options of the dropdown, must be an array of objectsoptionsUrlstring null - url to fetch the select options fromcacheOptionsUrlstring false - whether to cache optionsUrl fetching responsepathstring "" - Field pathmappingValue(string | array) "value" - The name(s) of the option object field(s) related to the option value, used to match passed in values to actual optionsmappingLabelstring "label" - the name of the option object field related to the option labelmappingReturnstring null - the property of the option object used to return the value(s) - null will return the whole objectreturnSingleboolean boolean - wether or not to return the first value or an array of valuesmaxnumber Infinity - Maximum values that the field can haveemptyToNullboolean false - Flag that determines if empty values are transformed to null in the value function of the formStoredisplayInlineboolean false - Display choices in line, default is display as a listdisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form input
Slider
Slider input field
Options
Parameters
labelstring "" - The field labeltypestring "Slider"value(number | Range) null - The current value. If only a number is provided, a single slider will get rendered. If a range object {min:x, max:y} is provided, two sliders will get rendered.defaultValue(number | Range) null - The defaultValue of the fieldpathstring "" - Field pathdisabledboolean false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues()readOnlyboolean false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues()readModeboolean false - If true, displays the field as label and value without the actual form inputminnumber null (required) - minimum value. You cannot drag your slider under this value.maxnumber null (required) - maximum value. You cannot drag your slider beyond this value.stepnumber 1 - The default increment/decrement is 1. You can change that by setting a different number to this property.
SingleField
SingleField is a generic react component that supports all kinds of different input types without a form component
ActionIcon
ActionIcon component
Properties
iconstring required - glyphicon to display https://getbootstrap.com/docs/3.3/components/#glyphiconsonClickfunction optional - callback for when action is clicked
GenericList
Generic List component that renders a list of items using Bootstrap Panels
Properties
itemsarray required - an array of items to be displayed in the list. Can be an array of primitives or objectsexpandedboolean optional - if the panel is expanded by defaultitemTitleobject optional - react component to render the title for individual items. Gets passed the item to be rendered as a prop. Default value: ({ item }) => itemitemBodyboolean optional - react component to render the body for individual items. Gets passed the item to be rendered as a prop. Only necessary if you want a body to be displayedactionsarray required - an array of actions. An actions can be any react components that get rendered in the top right corner of the panel. For callback, implement the onClick which gets called with the selected item.
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago