react-kekkai v1.0.16
react-kekkai
It's a component library for React, and named 'Kekkai'(Japanese). Let's see Kekkai via the agenda as follows:
What is Kekkai
Kekkai provided 5 components to build the common data layout(such as Form / Grid / Card). You could use the same way to build the layout through these Kekkai components, and change layout via easy setting.
The pattern about using 5 components is...
<KekkaiContainer panel={LayoutOpts.List} view={data => (
<KekkaiDataview key={data.$uid} dataModel={data}>
<KekkaiField label="Name" name="fullName">
<KekkaiDisplay># {data.fullName}</KekkaiDisplay>
<KekkaiEditor required={true} validation={value =>
value.trim().length === 0 ? 'Name is required.' : true
}>
<input type="text" />
</KekkaiEditor>
</KekkaiField>
</KekkaiDataview>
)}/>In Japanese, 'Kekkai' means magic circle, and the pattern just looks like a magic circle, so I named it as 'Kekkai' ^^.
About Install
Download from NPM
npm i react moment numeral react-kekkai -s
Import Kekkai
Except the kekkai components, also import the CSS.
import {
// Components
KekkaiContainer, KekkaiDataview, KekkaiField, KekkaiDisplay, KekkaiEditor,
// Option Enums
LayoutOpts, TriggerOpts, EditingOpts,
// Operations Building Helper
Todo, TodoScripts
} from 'react-kekkai';
import 'react-kekkai/dist/index.css';5 Components
Using this pattern concept to accomplish building the different layout via the only one way. It will reduce our work for HTML, and make sure all the layout could follow the same business rule. Now, let's check the purposes of these 5 components:
<KekkaiContainer />(API Documentation) 1<KekkaiContainer />means 1 data source, all the data will work under the container.<KekkaiContainer />is responsible for privding a basic Toolbar / Pagination, and allocating HTML Layout Panel.<KekkaiDataview />(API Documentation) 1<KekkaiDataview />means 1 data, so there will be many<KekkaiDataview />in 1<KekkaiContainer />. We use<KekkaiDataview />to pack the data columns(field), and it provided Data-Row Selection / Menu.<KekkaiField />(API Documentation)<KekkaiField />is used to pack Display and Editor, and responsible for switching them by data status on the right time. So, we don't need to use any skill to make up the switching control. It could also define the column layout under the different panel.<KekkaiDisplay />(API Documentation)<KekkaiField />has provided a default<KekkaiDisplay />to show value. If you wanna use different ways or format to show the value, you could override it by your own. This is responsibility of<KekkaiDisplay />.<KekkaiEditor />(API Documentation) When data need to be edited, we could use<KekkaiEditor />to pack the form items. We could also bind the events to append rule to control editable status / handle data changed / do validation and tip, and make every form item be unified into one solution by these 3 events. By the way, the form item which is packed by<KekkaiEditor />must have supported properties of 'value' and 'onChange'.
API Documentation
After the introduction of Kekkai, let's see the API to know how to use Kekkai. (PS. '*' means required.)
Components
<KekkaiContainer />
Options(props)
*
ref:stringDefine a ref, we could call the methods through ref.panel: LayoutOpts Specify the HTML layout, default isLayoutOpts.List.toolBgColor:stringDefine background-color of tool, default is#007bff.toolTxColor:stringDefine text-color of tool, default iswhite.pageSize:numberDefine data count in 1 page, and the allowed number is10/25/50/100(means show all by infinite-scroll), default is10.todos: (Todo | TodoScripts)[] Inject the data manipulations which are defined by yourself or Kekkai provided. If there is not anytodos, users could only read the data in .
Events
*
getSearchResponse:async ({ sort, filters, page}) => { data, total }This is the most important event, all the data that needs are returned by it. You can also make up parameters of request and send request here. Check more as follows:Parameters
sort:[{ name, dir }]-name:string- means the field name of data.dir:string- It will only beascordesc.
filters:[{ name, operator, value }]-name:string- means the field name of data.operator:string- means condition, and it's defined by<KekkaiField />'s property:filter.value:any- This is input by user filtering.
page:{ page, pageSize, skip, start }-page:number- The target page index of loading.pageSize:number- Data count of 1 page.skip:number- Skip count.start:number- Target start data index in the page.
Return:
{ data, total }data:Object[]- When you get the data JSON array from response with await, please put the data JSON into this property.total:number- Set the total count of data to make pagination work.
*
view: (data) =><KekkaiDataview />This event will be fired when Kekkai get the response data and convert JSON to KekkaiModel. KekkaiModel will be inputted in this event, so we could build<KekkaiDataview />by it.Parameters
data: KekkaiModel - The converted data from response.
Return:
<KekkaiDataview />Defined layout of data.
onCommit:async (todoRef, { target, modifieds, removes }) => { success, msg }If there is any request-process intodos, this event will be fired when user submit modifieds. We could get the target modified data to make up the parameters and send request here. Check more as follows:Parameters
todoRef:string- We could check what request should be sent by this parameter, and it's defined intodo. Here are 2 cases that we should know:Specific Case: Most manipulations and their own request process are explicitly and single, so we could get the only one
todoRefthat defined by ourself.Commit Case: Kekkai provided inline-editing, and user could edit multiple data via this feature. No metter data is updated or created, we'll only get
todoRefasKekkaiContainer.CommitAll, and it means this commit is for all modified data.
target: KekkaiModel[] - In specific case, we could get the request target data from this parameter.modifieds: KekkaiModel[] - This parameter will be inputted when thetodoRefisKekkaiContainer.CommitAll, and it contains the updated and created data.removes: KekkaiModel[] - This parameter will be inputted when thetodoRefisKekkaiContainer.CommitAll. It means the temporarily removed data.
Return:
{ success, msg }success:boolean- The response result, set asfalsewill make Kekkai popup a Error Message Box, and the content ismsg.msg:string- Error message.
Readonly Properties
panel: LayoutOpts Get current layout type.pager: KekkaiPager Get pagination manager. If you wanna change page size or index, you could use this property.data: KekkaiModel[] Get all data in current page.selecteds: KekkaiModel[] Get the selected data in current page.editings: KekkaiModel[] Get the editable data in current page.modifieds: KekkaiModel[] Get the dirty data in current page.
Methods
setAlert(content, { type, title, icon, callbackFn }): voidTo show a message tip.Parameters
*
content:string|React Element- The message content.type:string- Define the background-color of message box, and the allowed values areinfo/success/warning/danger. Default isinfo.title:string- The message title.icon:string- Set as a icon class name, such as Font-Awesome. By the way, Kekkai use Font Awesome 4.7 as default icon.callbackFn:() => void- This function will be called after the message box closed.
setConfirm(content, { type, title, icon, callbackFn }): voidTo show a confirmed message.Parameters
*
content:string|React Element- The message content.type:string- Define the background-color of message box, and the allowed values areinfo/success/warning/danger. Default isinfo.title:string- The message title.icon:string- Set as a icon class name, such as Font-Awesome. By the way, Kekkai use Font Awesome 4.7 as default icon.*
callbackFn:(isConfirmed: boolean) => void- This function will be called after the message box closed.
add(newData, { index }): voidAppend new JSON data to<KekkaiContainer />, and you could also specify the index of data(default is 0).edit(turnOn, { editData }): voidTurn on/off the editable status of data. If you haven't specified the target data, the default targets are all data.Parameters
*
turnOn:boolean- Set astrueto turn on editable, andfalseto turn off.editData: KekkaiModel | KekkaiModel[] - Specify one or more target data.
setExecuting({ todo, data, popup }): voidIf there is a executingTodowhich has to storage temporarily, call this method. TheTodoin temporary storage will be the top priority when you calldoCommit.Parameters
todo:Todo- Target temporary storageTodo.data: KekkaiModel - Target processed data, only support single data.popup:boolean- Iftodo'seditingModeis set asEditingOpts.POPUP, please set this option astrueto let<KekkaiContainer />know it has to popup a editing modal.
async doSearch(filters, specifyPage): voidTo load data. Event - getSearchResponse will be fired when called this method.async doRollback(showConfirm): voidTo rollback all the modifieds of data, and you could also rollback without confirm message box.- Parameters
showConfirm:boolean- Default istrue, and setting asfalsecould ignore confirming.
- Parameters
async doCommit(): voidTo commit all the modifieds of data. Kekkai will make up the target process data according to manipulation situational from user, and puttodoRefand data into Event - onCommit.
<KekkaiDataview />
Options(props)
*
key:stringIn React, the element array must be set a key, so our suggest is setting as KekkaiModel.$uid.*
dataModel: KekkaiModel Binding data to this<KekkaiDataview />to make sure it could work.selectable:boolean|(data) => booleanThis option will be actived when the panel isLayoutOpts.ListorLayoutOpts.Card. To define this<KekkaiDataview />could be selected by user, and default is false. You could also set as a function to build business rules.- Parameters
data: KekkaiModel - Kekkai will put the data into this function.
- Parameters
reorderable:booleanThis option will be actived when the panel isLayoutOpts.List. To define all the columns in<KekkaiDataview />could be reorder by drag and drop.labelSize:numberThis option will be actived when the panel isLayoutOpts.Form. To define all the<label />'s width, and the allowed values are between 1 ~ 12.viewSize: RWD Options This option will be actived when the panel isLayoutOpts.Card. To define<KekkaiDataview />'s width, and default is{ def: 12 }.
Events
onSelected:(isChecked, data) => voidThis event will be fired when user select the data by Data-Row Selection.Parameters
isChecked:boolean-truemeans data is selected, andfalseis deselected.data: KekkaiModel - The target data.
<KekkaiField />
Options(props)
*
name:stringBinding data field name.key:stringIf you wanna build a pseudo column, you must have to set this property. Just give it a unique name.*
label:stringDefine description for field. When panel isLayoutOpts.List, this label will show on the header. In other panel, it will become<label />.align:stringSet the text-align in<KekkaiDisplay />, default isleft.sortable:boolean|{ seq, dir }This option will make Kekkai generate sort parameters. Set astruemeans this column will be sortable.seq:number- Means priority order.dir:string- The allowed values areascordesc.
form: RWD Options When layout panel isLayoutOpts.Formor in Kekkai Popup Modal, the column width is set by this option. Default is{ def: 12 }.card: RWD Options When layout panel isLayoutOpts.Card, the column width is set by this option. Default is{ def: 12 }.list:numberWhen layout panel isLayoutOpts.List, the column width is set by this option. The number meanspx, and default is120.locked:booleanOnly forLayoutOpts.List. Set column as locked, default isfalse.lockable:booleanOnly forLayoutOpts.List. Set column could be switched locked status by user, default isfalse.hidden:booleanOnly forLayoutOpts.List. Set column as hidden, default isfalse.hideable:booleanOnly forLayoutOpts.List. Set column could be switched hidden by user, default isfalse.resizable:booleanOnly forLayoutOpts.List. Set column could be resized, default isfalse.filter:anyOnly forLayoutOpts.List. Kekkai will build a filter feature on the header which is like Excel. This option could define filter condition, such as'eq'/'like'. See your request parameter format to decide it.
<KekkaiDisplay />
There isn't not any options for this component. It's just used to pack the value format.
<KekkaiEditor />
Options(props)
required:booleanSet value as be required when user edits, default isfalse.editable:boolean|(value, data) => booleanThis option will be used before Kekkai switch to edit mode. If this option value isfalseor returnfalse, that means this column wouldn't be switched to edit mode.Parameters
value:any- The current field value.data: KekkaiModel - The data record.
Return:
booleanReturntrueorfalseto tell Kekkai this column is able to be swtiched to edit mode.
Events
onChange:(name, value, data) => voidThis event is fired when field value changed.Parameters
name:string- The field name of data.value:any- The new value.data: KekkaiModel - The data record.
validation:(name, value, data) => true | stringThis event is fired afteronChange, and we could check the data validation in this event. If data is valid, please returntrue, or return the error message tip. Kekkai will popup a message tip when it get the error message.Parameters
name:string- The field name of data.value:any- The new value.data: KekkaiModel - The data record.
Return:
true|stringtruemeans valid, andstringis error message.
Options
LayoutOpts
This option will be used in <KekkaiContainer />. There are 3 kinds layout, see as follows:
Card:
LayoutOpts.CardCard layout is built byLayout-Grid&Components-Cardfrom Bootstrap 4.0. We could use it to show multiple data, and it's RWD design. Under this panel, there isn't any Data-Row Selection, all the manipulations about Selection and Row Double Click will become to be trigger by Data-Row Menu. PS. I wanna append data sort feature on the label in the future.Form:
LayoutOpts.FormForm layout is built byLayout-Gridfrom Bootstrap 4.0, and it's also RWD design. In Kekkai, if setpanelasLayoutOpts.Form, thepageSizewill become to1. Yes, it means show only one data in Kekkai, so all the manipulations about data could only be trigger by Toolbar. By the way, when Kekkai wanna generate a popup modal of single data, the content layout in modal are form the options of form.List:
LayoutOpts.ListShow data by list. List layout has 2 blocks: one is locked on the left, and one is scrollable on the right. Under this panel, user could adjust the columns layout, such as width / hidden / order / locked / data sort, and also could use data filter feature on the header. It distributes the manipulations into 4 kinds: Toolbar / Selection / Row Menu / Row Double Click.
TriggerOpts
This option will be bound in Todo or TodoScripts. Kekkai distributes the manipulations about data into 4 kinds, and all the trigger ways are different. As follows:
TOOLBAR:
TriggerOpts.TOOLBARIt means a initially state. It will be default shown on the Toolbar, but if user selects any data, the toolbar buttons will be hidden(become to Selection-Mode).SELECTION:
TriggerOpts.SELECTIONThis trigger is different from TOOLBAR, and it means Selection-Mode. When user selects data through Data-Row Selection, the selection button will be visible on the Toolbar.ROW_MENU:
TriggerOpts.ROW_MENUIt means single-selection, so Kekkai will build a menu to show them on the row. This menu will only be visible when there isn't any data which is selected or on editing.ROW_DBCLICK:
TriggerOpts.ROW_DBCLICKThis trigger also means single-selection, but there could be only one Todo set as ROW_DBCLICK at most.
EditingOpts
If you want to set data as editable when Todo is executing, you need to bind this option to Todo.
INLINE:
EditingOpts.INLINEMeans inline editing, edit data with popup modal. It's for multiple case.POPUP:
EditingOpts.POPUPMeans editing by popup modal, and it's for single case.
RWD Options
This option is base on Bootstrap 4.0, and it was designed from the default 5 sizes from Boostrap.
Default type is
{ def, sm, md, lg, xl }def:false|numberAllowed numbers are between 1 ~ 12, andfalsemeans hidden. Default is12.sm:false|numberAllowed numbers are between 1 ~ 12, andfalsemeans hidden.md:false|numberAllowed numbers are between 1 ~ 12, andfalsemeans hidden.lg:false|numberAllowed numbers are between 1 ~ 12, andfalsemeans hidden.xl:false|numberAllowed numbers are between 1 ~ 12, andfalsemeans hidden.
Base Types
KekkaiModel
This type is built in <KekkaiContainer /> when it get the data JSON array, so we don't need to construct it by ourself.
Readonly Properties
[data field name]:anyIf you wanna get / set the field value, you could use like as follows.- getter:
console.log(data.fullName); - setter:
data.fullName = 'Tom';
- getter:
$uid:stringGet the unique key of data.$json:ObjectGet the values JSON from data.$isNew:booleanCheck data is the new created.$isValid:booleanCheck data is valid.$editable:booleanTo know data is editing or not.$checked:booleanTo know data is selected or not.$hidden:booleanTo know data is hidden or not.$isDirty:booleanTo know data is modified or not.
Methods
$setHidden(hidden): booleanSet data as hidden on the view.Parameters
hidden:boolean-truemeans hidden, andfalsemeans visible. Default isfalse.
Return:
booleanGet the last hidden result.
$setEditable(turnOn): booleanSet data as editing.Parameters
turnOn:boolean-truemeans show<KekkaiEditor />, andfalsemeans display. Default isfalse.
Return:
booleanGet the last editable result.
$setChecked(checked): booleanSet data as selected.Parameters
checked:boolean-truemeans selected, andfalsemeans deselected. Default isfalse.
Return:
booleanGet the last selected result.
$getValid(name): boolean | stringGet the specify field's validation result.Parameters
name:string- Target field name.
Return:
boolean|stringtruemeans data is valid, andstringis the error message.
$undo(): voidRemove all the modifieds to restore data to initially, but couldn't remove new created data.
KekkaiPager
This type is built in <KekkaiContainer />, so we never and ever need to construct it by ourself. We could get this pager from <KekkaiContainer />.pager, and then override its options.
Readonly Properties
pageSize-numberGet data count of one page, and this property is allowed to set value. If you wanna change pageSize, you could use like this:pager.pageSize = 25;.skip-numberGet how many data is skipped.start-numberGet the start data index in current page.end-numberGet the end data index in current page.total-numberGet the total data count from current searching.page-numberGet current page index, and this property is allowed to set value. If you wanna change it, you could use like this:pager.page = 3;.maxPage-numberGet the last page index.
Methods
toNext(): voidGo to next page.toPrev(): voidGo to previous page.toFirst(): voidGo to the first page.toLast(): voidGo to the last page.
Todo
Kekkai provided this type to let us build data manipulations more quickly. Though constructing Todo, we don't need to put <button /> on HTML, and also think how to accomplish the control rules. All the controls are packed in Kekkai, we just set options and inject checking/executing function at most. Let's see how to build the Todo:
new Todo({
ref: string,
concat: string,
text: string,
icon: string,
trigger: TriggerOpts,
editingMode: EditingOpts,
executable: () => boolean,
execute: () => void,
onSuccess: () => void,
confirmMsg: { type: string, icon: string, title: string, content: string },
responseMsg: { type: string, icon: string, title: string, content: string }
})Constructor Parameters
*
ref:stringGive theTodoa reference key. This key wouldn't be unique, it's just used to decide the committed data how to process in<KekkaiContainer />'s Event - onCommit.concat:stringIf there are the same 2 concatsTodoin one<KekkaiContainer />, Kekkai will help us to build them as a dropdown menu in Toolbar.*
text:stringGive theTodoa display name.icon:stringSet as a icon class name, such as Font-Awesome. By the way, Kekkai use Font Awesome 4.7 as default icon.*
trigger: TriggerOpts Kekkai will generate a trigger way via this option, see TriggerOpts.editingMode: EditingOpts See EditingOpts and<KekkaiContainer />'s Method - setExecuting.executable:({ data, list }, container) => booleanInject a function which will returnboolean, and Kekkai will check thisTodois able to execute on the right time.Parameters
data: KekkaiModel - Single target data, this parameter will be actived whentriggeris set asTriggerOpts.ROW_MENUorTriggerOpts.ROW_DBCLICK.list: KekkaiModel[] - Multiple target data, this parameter will be actived whentriggeris set asTriggerOpts.TOOLBARorTriggerOpts.SELECTION.container:<KekkaiContainer />- Owner<KekkaiContainer />.
Return:
booleanReturn abooleanvalue to tell Kekkai thisTodocould be executed.
*
execute:({ data, list }, container) => voidInject a function to define thisTodoneed to do what.Parameters
data: KekkaiModel - Single target data, this parameter will be actived whentriggeris set asTriggerOpts.ROW_MENUorTriggerOpts.ROW_DBCLICK.list: KekkaiModel[] - Multiple target data, this parameter will be actived whentriggeris set asTriggerOpts.TOOLBARorTriggerOpts.SELECTION.container:<KekkaiContainer />- Owner<KekkaiContainer />.
onSuccess:({ success, msg }, container) => voidThis option is actived whenTodois a request case, and it will be used in<KekkaiContainer />'s Method - doCommit. For make sure this option could work, don't forget to call<KekkaiContainer />'s Method - setExecuting inexecute.Parameters
success:boolean- It means the result of request, and it's from the return value in<KekkaiContainer />'s Method - doCommit. Set it asfalseand give amsgindoCommit, Kekkai will popup a message tip.truemeans request is fine.msg:string- This message is from the return value in<KekkaiContainer />'s Method - doCommit.container:<KekkaiContainer />- Owner<KekkaiContainer />.
confirmMsg:{ type, icon, title, content }This option will effect Kekkai should popup a confirmed message or not before executing.Parameters
type:string- Define the background-color of message box, and the allowed values areinfo/success/warning/danger. Default isinfo.icon:string- Set as a icon class name, such as Font-Awesome. By the way, Kekkai use Font Awesome 4.7 as default icon.title:string- The message title.*
content:string|React Element- The message content.
responseMsg:{ type, icon, title, content }This option will effect Kekkai should popup a message tip afteronSuccess, and this option is actived when request result is successfully.Parameters
type:string- Define the background-color of message box, and the allowed values areinfo/success/warning/danger. Default isinfo.icon:string- Set as a icon class name, such as Font-Awesome. By the way, Kekkai use Font Awesome 4.7 as default icon.title:string- The message title.*
content:string|React Element- The message content.
TodoScripts
Finally, even we could build data manipulations by Todo, but the options still seem some complicated. Never mind, that's why I add these TodoScripts, to build data manipulations more and more quickly. After the explanation about Todo, you will understand this part more easier.
All the data manipulations are relational with CRUD. Read is built in <KekkaiContainer />, so I built Create, Update and Delete in TodoScripts, and packed the <KekkaiContainer /> method in these scripts(it means we don't have to know when need to call setExecuting / add / edit ...). As follows:
- CREATE:
TodoScripts.CREATE(options)Use this script to build created manipulation, the most important is you need to return a JSON values inoverrideParams. Let's see what options are allowed or what you can override in this script:
TodoScripts.CREATE({
editingMode, // Default is EditingOpts.POPUP
ref, // * Required
text, // * Required
concat,
icon,
executable,
onSuccess,
responseMsg, // Default is { type: 'success', icon: 'fa fa-check-square-o', content: '資料已完成新增' }
overrideParams() { // * Required, must return new JSON data in this function
return {
fullName: 'Tom',
age: 13
};
}
});- UPDATE:
TodoScripts.UPDATE(options)In this script,overrideParamsis not a required option, and addtriggercould be override.
TodoScripts.UPDATE({
trigger, // Default is TriggerOpts.ROW_DBCLICK
editingMode, // Default is EditingOpts.POPUP
ref, // * Required
text, // * Required
concat,
icon,
executable,
onSuccess,
responseMsg, // Default is { type: 'success', icon: 'fa fa-check-square-o', content: '資料已完成修改' }
overrideParams(data) { // Set default value before editing.
data.age = parseFloat(data.age);
}
})- DELETE:
TodoScripts.DELETE(options)There are too many ways to triggerDELETE, so this script doesn't provide a default value. If you don't need to confirm with user when executing, please setconfirmMsgasundefinedorfalse. TheoverrideParamsisn't also a required option, but if you wanna change data values before sending request, you'll need it.
TodoScripts.DELETE({
trigger, // * Required
ref, // * Required
text, // * Required
concat,
icon,
executable,
onSuccess,
confirmMsg, // Default is { type: 'info', icon: 'fa fa-question-circle', title: '資料即將被刪除', content: '確定刪除所選取之資料 ?' }
responseMsg, // Default is { type: 'success', icon: 'fa fa-check-square-o', content: '資料已完成修改' }
overrideParams(data) { // Change data values before sending request
data.age = null;
}
})by Taco (tabacotaco@gmail.com)
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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