3.0.23 • Published 3 years ago

sm-react-crud v3.0.23

Weekly downloads
199
License
BSD-3-Clause
Repository
github
Last release
3 years ago

This library contains few components to Create, Read, Update and Delete data.

Installing

npm i sm-react-crud

Then you have to connect reducer and saga from the package

import {reducer as crudReducers} from 'sm-react-crud'

...

const store = createStore(
	combineReducers({
		...reducers,
		...crudReducers
	}),
	composeEnhancers(applyMiddleware(...middlewares))
);

...
    ...
    
    import {saga as crudSagas} from 'sm-react-crud'
    
    ...
    
    export default function* devSaga() {
    	yield all([
    	    ...
            crudSagas(),
            ...
    	]);
    }

CrudFull

Renders a list of items with a button to create new one. Items can be sorted/filtred by the each sorted/filtred column.

FRONTEND API

crudCreate

an URL to make a creating item request.

crudRead

an URL to fetch items.

modelName

a name of model, unique identification key for this component.

customActionsFunc

provides a function to customazing action handling

createButtonTitle

create button label

createFormOptions:

options to create form - fields key is required

submitShape

provides a function to set a shape of submit payload form => {data: form, model: 'modelName'}

updateShape

provides a function to set shape of updating payload form => {data: form, model: 'updateModelName'}

createDisabled

disables create mode

iconsProvider

provides a function to choose an icon by id

(id) => {
    switch (id) {
        case 'update':
            return 'edit';
...
btnStyle

styles object for default button component

ButtonComponent

custom button component

tableStyle

style object for table

tableWrapper

component to wrap the table

fixActionColumn

bool prop to fix action column on the right side

iconTheme

antd icon theme (default: 'outline')

size

antd table size (default: 'default')

tdClass

specify class for td tag

initialModal

provides an object to initialize modal form

scrollX

scroll size whian actions column is fixed (default: 1300)

pageSize

items count on one page (default: 20)

getChildrenUrl

url to fetch children

Example

./createFormFields

export default [
{
	name: 'name',
	type: 'text',
	placeholder: 'Enter a name',
	label: <strong>Name</strong>,
	validateFunc: (values, errors) => {
		if(!values.name) errors.name = 'You shoul fill this field';
		return errors;
	},
}, {
	name: 'description',
	type: 'text',
	placeholder: 'Enter a description',
	label: <strong>Description</strong>,
}, {
	name: 'status_id',
	type: 'select',
	placeholder: 'Enter a status',
	label: <strong>Status</strong>,
	options: [
	    {id: 1, name: 'Да'},
	    {id: 2, name: 'Нет'}
	],
	component: props => <Status {...props} />
}]

Use case

import createFormFileds from './createFormFileds'
import {crudFull} from 'sm-react-crud'

const objects = <CrudFull
    crudRead={'https://api/object/list'}
    crudCreate={'https://api/object/create'}
    modelName={'objectsName'}
    createDisabled={false}
    createButtonTitle={"New object"}
    createFormOptions={{
        fields: createFormFileds,
        title: 'Create new',
        editTitle: 'Edit object',
    }}
    submitShape={form => ({
        name: form.name.toUpperCase(), 
        description: form.description.toLowerCase()
    })}
    updateShape={record => ({
        name: record.name, 
        description: record.description.toLowerCase()
    })}
    customActionsFunc={(action, object) => {
        switch(action.id) {
            case 'took':
                //some action
                break;
            case 'return':
                //some action
                break;
            default:
                return null;
        }
    }}
/>

requestSaga && apiAdress

You have to define apiAdress in your store to use requestSaga for requests. It can be used for fetching data or creating a new record.

Modal form logic

Field object

Field object may contains following

name: string - input name,

type: string - input type,

placeholder: string - placeholder text,

label: string or html - label,

component: react class or function - renderer for input,

dropdownRender: react class or function - antd dropdown for input,

optionsKey: string - key in store to get input options arr.

Available field types: text, textarea, number, date, select, checkbox

Redux Store Key

You can specify store key to set options property for select or another data for your field.

   ...
   {
   	name: 'status_id',
   	type: 'select',
   	placeholder: 'Enter a status',
   	label: <strong>Status</strong>,
   	component: props => <Status {...props} />
   	optionsKey: 'statusOptions'
   },
   ... 

Dynamics

Sometimes you need to change fields array dynamically depending on the usability logic.

To impliment it, use createFormOptions and provide modified fields array by the 'fields' key:

createFormOptions={{
    fields: modifiedCreateFormFileds,
    title: 'Create record',
    editTitle: 'Edit record',
}}

BACKEND API

Backend response must contains four fields at least:

  • columns : array;
  • count : number;
  • filter : object;
  • items : array.

Columns

Columns is an array of columns() you want to render into the table. Each column includes several fields:

  • id : number, uniq identificator, which is matched with key in record;
  • title: string, text you want to render as a column name;
  • type: string, type of the data you send under the key;
  • order: object, inlides fields 'can' (setting order available or not) and 'orders' (an array of available orders ).
  • filter: object, inlides fields:
    • can (setting filter available or not),
    • type (type of filter input),
    • defaultValue (default filter input value)
    • query (url to fetch filter options)

    Available filter types:
  • input_number (input type 'number')
  • input_text (input type 'text')
  • date_picker ( antd date picker)
  • select_one (select, can select one option)
  • select (miltiple select)
  • boolean (checkbox)

Items

It's an array of records and a main table info. It must includes keys that are matched with culumns ids and contains the same type of data that are specified in columns type. Example is below.

   {"response":
       {
           "items":[
               {
                   "objectType": 'room',
                   "object":{
                       "id":22,
                       "name":"Ул. Чапаевская 210 / Вилоновская 10, кв 3 (Квартира)"
                   },
                   "worker":{
                       "id":44,
                       "name":"var1232enik163_yandex_ru"
                   },
                   "description":"hh",
                   "action_date":1549224000,
                   "updated_at":1549280956,
               },
               {
                   "objectType": 'flat',
                   "object":{
                       "id":22,
                       "name":"Ул. Чапаевская 210 / Вилоновская 10, кв 3 (Квартира)"},
                       "worker":{
                           "id":25,
                           "name":"DSTS"
                       },
                   "description":"ghjgh",
                   "action_date":1549137600,
                   "updated_at":1549186595,
               }
           ],
           "columns":[
               {
                   "id":"objectType",
                   "title":"Тип",
                   "type":"string",
                   "filter": {
                       "can":true,
                       "type":"select_one",
                       "defaultValue":"",
                       "query":"/v1/owner/tenant/filter/action-types"
                   },
                   "order":{
                       "can":true,
                       "orders":[
                           "SORT_ASC",
                           "SORT_DESC"
                       ]
                   }
               },{
                   "id":"object",
                   "title":"Объект",
                   "type":"object",
                   "filter":{
                       "can":true,
                       "type":"select_one",
                       "defaultValue":"",
                       "query":"/v1/owner/tenant/filter/objects"
                   },
                   "order":{
                       "can":false,
                       "orders":[]
                   }
               }
           ],
           count: 2,
           filter: {}
       }
   }

You can see that column with id 'objectType' has field 'type' with value 'string'. In item object we have a field 'objectType' which is string, what is matched with column field 'type'. There is a ralation column id -> items key.

ArrayCell

You can specify 'array_ext' column type to render an array data as you wish using following parameters:

  • "values", data you wont to render
  • "type", data type ("text", "date", "array_ext"), default 'text'
  • "isHtml", indicates whether the data is html, default 'false'
  • "delimiter", separator for "values" item, default none
  • "dateFormat", indicates format of rendering date, for example 'DD-MM-YYYY', default: 'DD.MM.YYYY'
  • "style", style object for item

Example:

users: {
    values: ['<div><strong><a>Alex</a></strong></div>', '<div><em><a>Masha</a></em></div>'],
    isHtml: true,
    type: 'text',
    delimiter: '----'
}
3.0.23

3 years ago

3.0.22

3 years ago

3.0.21

3 years ago

3.0.20

3 years ago

3.0.18

3 years ago

3.0.19

3 years ago

3.0.16

4 years ago

3.0.17

4 years ago

3.0.14

4 years ago

3.0.15

4 years ago

3.0.13

4 years ago

3.0.12

4 years ago

3.0.11

4 years ago

3.0.10

4 years ago

3.0.9

4 years ago

3.0.8

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.4

4 years ago

3.0.5

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.3.43

4 years ago

2.3.42

4 years ago

2.3.41

5 years ago

2.3.40

5 years ago

2.3.39

5 years ago

2.3.38

5 years ago

2.3.37

5 years ago

2.3.36

5 years ago

2.3.35

5 years ago

2.3.34

5 years ago

2.3.33

5 years ago

2.3.32

5 years ago

2.3.31

5 years ago

2.3.30

5 years ago

2.3.29

5 years ago

2.3.28

5 years ago

2.3.27

5 years ago

2.3.26

5 years ago

2.3.25

5 years ago

2.3.24

5 years ago

2.3.23

5 years ago

2.3.22

5 years ago

2.3.21

5 years ago

2.3.20

5 years ago

2.3.19

5 years ago

2.3.18

5 years ago

2.3.17

5 years ago

2.3.16

5 years ago

2.3.15

5 years ago

2.3.14

5 years ago

2.3.13

5 years ago

2.3.12

5 years ago

2.3.11

5 years ago

2.3.10

5 years ago

2.3.9

5 years ago

2.3.8

5 years ago

2.3.7

5 years ago

2.3.6

5 years ago

2.3.5

5 years ago

2.3.4

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.17

5 years ago

2.1.16

5 years ago

2.1.15

5 years ago

2.1.14

5 years ago

2.1.13

5 years ago

2.1.12

5 years ago

2.1.11

5 years ago

2.1.10

5 years ago

2.1.9

5 years ago

2.1.8

5 years ago

2.1.7

5 years ago

2.1.6

5 years ago

2.1.5

5 years ago

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.122

5 years ago

1.1.121

5 years ago

1.1.120

5 years ago

1.1.119

5 years ago

1.1.118

5 years ago

1.1.117

5 years ago

1.1.116

5 years ago

1.1.115

5 years ago

1.1.114

5 years ago

1.1.113

5 years ago

1.1.112

5 years ago

1.1.111

5 years ago

1.1.110

5 years ago

1.1.109

5 years ago

1.1.108

5 years ago

1.1.107

5 years ago

1.1.106

5 years ago

1.1.105

5 years ago

1.1.104

5 years ago

1.1.103

5 years ago

1.1.102

5 years ago

1.1.101

5 years ago

1.1.100

5 years ago

1.1.99

5 years ago

1.1.98

5 years ago

1.1.97

5 years ago

1.1.96

5 years ago

1.1.95

5 years ago

1.1.94

5 years ago

1.1.93

5 years ago

1.1.92

5 years ago

1.1.91

5 years ago

1.1.86

5 years ago

1.1.87

5 years ago

1.1.85

5 years ago

1.1.84

5 years ago

1.1.83

5 years ago

1.1.82

5 years ago

1.1.81

5 years ago

1.1.80

5 years ago

1.1.79

5 years ago

1.1.78

5 years ago

1.1.77

5 years ago

1.1.76

5 years ago

1.1.75

5 years ago

1.1.74

5 years ago

1.1.73

5 years ago

1.1.72

5 years ago

1.1.71

5 years ago

1.1.70

5 years ago

1.1.69

5 years ago

1.1.68

5 years ago

1.1.67

5 years ago

1.1.66

5 years ago

1.1.65

5 years ago

1.1.64

5 years ago

1.1.63

5 years ago

1.1.62

5 years ago

1.1.61

5 years ago

1.1.60

5 years ago

1.1.59

5 years ago

1.1.58

5 years ago

1.1.57

5 years ago

1.1.56

5 years ago

1.1.54

5 years ago

1.1.53

5 years ago

1.1.52

5 years ago

1.1.51

5 years ago

1.1.50

5 years ago

1.1.49

5 years ago

1.1.48

5 years ago

1.1.47

5 years ago

1.1.46

5 years ago

1.1.45

5 years ago

1.1.44

5 years ago

1.1.43

5 years ago

1.1.42

5 years ago

1.1.41

6 years ago

1.1.40

6 years ago

1.1.39

6 years ago

1.1.38

6 years ago

1.1.37

6 years ago

1.1.36

6 years ago

1.1.35

6 years ago

1.1.34

6 years ago

1.1.33

6 years ago

1.1.32

6 years ago

1.1.31

6 years ago

1.1.30

6 years ago

1.1.29

6 years ago

1.1.28

6 years ago

1.1.27

6 years ago

1.1.26

6 years ago

1.1.25

6 years ago

1.1.24

6 years ago

1.1.23

6 years ago

1.1.22

6 years ago

1.1.21

6 years ago

1.1.20

6 years ago

1.1.19

6 years ago

1.1.18

6 years ago

1.1.17

6 years ago

1.1.16

6 years ago

1.1.15

6 years ago

1.1.14

6 years ago

1.1.13

6 years ago

1.1.12

6 years ago

1.1.11

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago