1.7.7 • Published 4 years ago

redux-admin v1.7.7

Weekly downloads
7
License
MIT
Repository
github
Last release
4 years ago

redux-admin

Build fast admin screen with full CRUD functionality inside your react apps

Updates:

  • version 1.6.0
    • Support rtl and localization

Edit redux-admin

Basic example

import React, { Component } from "react";
import { listViews, docViews, Admin, fields } from "redux-admin";

const getDocTitle = function(data) {
  if (data && data._id) {
    return `Item ${data.title|| data.objectId}`;
  } else {
    return "New Item";
  }
};

const getListFields = function() {
  return [
    {
      title: "Title",
      key: "title",
      dataIndex: "title",
      width: 150,
      sorter: true,
      type: String
    },
    {
      title: "Likes",
      key: "likes",
      dataIndex: "likes",
      width: 150,
      sorter: true,
      type: Number
    }
  ];
};

const docFields = function() {
  return (
    <div>
      <fields.Input name={"title"} label={"Title"} />
      <fields.InputNumber name={"likes"} label={"Likes"} />
    </div>
  );
};

const renderDocViewComponent = function({ dataFromServer }) {
  return JSON.stringify(dataFromServer);
};

class Posts extends Component {
  render() {
    return (
      <div>
        <Admin
          title={"Posts"}
          getDocTitle={getDocTitle}
          url="classes/Post"
          list={<listViews.Table getColumns={getListFields} />}
          doc={
            <docViews.SimpleDoc
              getDocFields={docFields}
              renderDocViewComponent={renderDocViewComponent}
            />
          }
          roleConfig={{
            canCreate: true,
            canRead: true,
            canUpdate: true,
            canDelete: true,
            excludeFields: []
          }}
        />
      </div>
    );
  }
}

export default Posts;

Installation

  1. You can install redux-admin with NPM, Yarn
    npm install redux-admin --save
  2. Follow Net-Provider Installation instruction Net-provider is the connection to your api

  3. Router configuration - we sync parameters with URL to achieve a better UX

     // inside your root file (app.js)
     import { setRouteConfig } from  'redux-admin';
    
     setRouteConfig({
      onReplaceParams: () => {...},
      onSetParams: : () => {...},
      onBack: : () => {...},
      goHome: : () => {...},
      onGetParams: : () => {...},
    });

    Examples:

  4. import style

    import  "redux-admin/lib/style.css";
    // -- or rtl style --
    import  "redux-admin/lib/style.rtl.css";
  5. Install antd
  6. Optional - set diffrent locale- default is enUs

    ```jsx
    import { setLocal, locals } from  'redux-admin';
    setLocal(locals.heIl);
    ```
    You can create you own local file
    just copy this [file](https://github.com/doronnahum/redux-admin/blob/master/src/local/en-US.js) and update the content

    Admin Component

    This is the main component that will render a list view and document view, sync the doc view with the list view and keep everything to work nice and updating, display notification, update the list when the document is updating. sync filters with URL. you can build your own list/doc view

    import { Admin } from "redux-admin";
### Props
```jsx
Admin.propTypes  = {
	url:  PropTypes.string.isRequired, // Api endpoint
	list:  PropTypes.element,
	renderList:  PropTypes.func, // alternative to props.list
	doc:  PropTypes.element,
	renderDoc:  PropTypes.func, // alternative to props.doc
	title:  PropTypes.string,
	getDocTitle:  PropTypes.func, // function that get doc data and return the doc title that display inside the breadcrumb (DocData) => 'title'
	queryParamsPrefix:  PropTypes.string, // This is required when you render more then one component at the same page
	getListSource:  PropTypes.func,
	getDocumentSource:  PropTypes.func,
	allowViewMode:  PropTypes.bool, // set false to block user from watching the document
	syncWithUrl:  PropTypes.bool, // true by default, set false to disabled this feature
	showBreadcrumb:  PropTypes.bool, // true by default, set false, relevant when you render more then one component at the same page
	editAfterSaved:  PropTypes.bool, // true by default, when true new document will stay open after submit
	onChangeEnd:  PropTypes.func, // optional Call back After create/update/delete
	initialSearchValue:  PropTypes.string,
	onSearchValueChange:  PropTypes.func, // If You Want To Handle it By Your self
	searchValue:  PropTypes.string, // relevant when you pass onSearchValueChange
	initialLimit:  PropTypes.number, // default is 5, query limit
	initialSort:  PropTypes.object, // { <field1>: 1, <field2>: -1 ... }
	initialSkip:  PropTypes.number, // default is 0
	listClearOnUnMount:  PropTypes.bool, // true by default, when true we remove all data from store when componentWillUnmount
	adminWillUnmount:  PropTypes.func, // optional Call back when componentWillUnmount
	roleConfig:  PropTypes.shape({
	canCreate:  PropTypes.bool, // true by default
	canRead:  PropTypes.bool, // true by default
	canUpdate:  PropTypes.bool, // true by default
	canDelete:  PropTypes.bool, // true by default
	excludeFields:  PropTypes.array  // [] by default
	}),
	useDataFromList:  PropTypes.bool, // false by default, set true to save query and use the data the from list as document data
	getParams:  PropTypes.func.isRequired  // pass function that build query params from the filters parameters getParams({skip, sort, limit, searchValue})
};

List Component

The default list view is Table From antd You can pass other component

How to use the default view?

import { listViews, Admin, helpers } from  "redux-admin";

const getListFields = function(props) {
	return [
			{ title:"Title",
			  key:"title",
			  dataIndex: "title",
			  width:150,
			  sorter:true,
			  type: String
			},
			 // You can use helper to save time
			 helpers.getListField({key: 'body', type: String})
		   ]
}
class  Posts  extends  Component  {
	render() {
		return  (
			<Admin 
				url="classes/Post"
				title={"Posts"}
				getDocTitle={getDocTitle}
				list={<listViews.Table
					   getColumns={getListFields}/>
					  }
...

Each field inside the list columns need to contian this keys

  • title: string
  • key: string
  • dataIndex: string
  • width: number
  • sorter: boolean
  • type: one of: String | Object | Array | Number | Date
  • render : optional fucntion (cell, row) => <MyComponent data={cell} />

listViews.Table props

<Admin \/> will pass the data and handle the row clicks and so on, the is the relevant props to pass directly to the table component

ReTable.propTypes  = {
	getColumns: PropTypes.func.isRequired, // getColumns(this.props) -> [{value: 'title', label: 'Title', type: String, render: () => 'OPTIONAL'}]
	getFilterFields: PropTypes.func, // if empty then we use getColumns
	rowKey: PropTypes.string, // default is _id
	renderInsideRowActions: PropTypes.func, // renderInsideRowActions(cell, row) => <button>Delete</button>
	allowFilters: PropTypes.bool,
	allowAdvanceFilters: PropTypes.bool, // true by default, allow the user to filter data with greater / less than, before/after and more...
	showColumnFilters: PropTypes.bool, // true by default, allow user to hide some of the table columns
	filtersFields: PropTypes.array, // when empty all table fields ar filters fields, pass array of fieldKeys to allow only some of them , Example [{key: 'status', options: {value: 'ctr', label: 'Create'}}]
	allowExportToExcel: PropTypes.bool, // true by default,
	onDownloadExcel: PropTypes.func, // to override local export pass function to handle this ({data, columnsToDisplay, onDownloadExcel}) => {....}
	expandedRowRender: PropTypes.func, // antd table expandedRowRender
	renderHeaders: PropTypes.func, // render content inside headers renderHeaders(props) => <div>Hello</div>
};

This is the full props option if you want to work with this component as a table outside of the <Admin \/> component

ReTable.propTypes  = {
	data: PropTypes.array,
	getColumns: PropTypes.func.isRequired, // getColumns(this.props) -> [{value: 'title', label: 'Title', type: String, render: () => 'OPTIONAL'}]
	getFilterFields: PropTypes.func, // if empty then we use getColumns
	rowKey: PropTypes.string,
	editable: PropTypes.bool,
	canUpdate: PropTypes.bool,
	canDelete: PropTypes.bool,
	renderInsideRowActions: PropTypes.func, // renderInsideRowActions(cell, row) => <button>Delete</button>
	onEditClick: PropTypes.func,
	onDeleteClick: PropTypes.func,
	skip: PropTypes.number,
	limit: PropTypes.number,
	onRow: PropTypes.func,
	onNewClick: PropTypes.func,
	onRefreshClick: PropTypes.func,
	enabledNew: PropTypes.bool,
	searchValue: PropTypes.string,
	showSearchField: PropTypes.bool,
	onSearch: PropTypes.func,
	onSearchValueChange: PropTypes.func,
	onFiltersChanged: PropTypes.func,
	allowAdvanceFilters: PropTypes.bool,
	allowFilters: PropTypes.bool,
	loading: PropTypes.bool,
	count: PropTypes.number,
	onPageChange: PropTypes.func,
	onPageSizeChange: PropTypes.func,
	onViewDocClick: PropTypes.func,
	showColumnFilters: PropTypes.bool,
	filtersFields: PropTypes.array, // when empty all table fields ar filters fields, pass array of fieldKeys to allow only some of them , Example [{key: 'status', options: {value: 'ctr', label: 'Create'}}]
	allowExportToExcel: PropTypes.bool, // true by default,
	onDownloadExcel: PropTypes.func, // to override local export pass function to handle this ({data, columnsToDisplay, onDownloadExcel}) => {....}
	allowExportToPdf: PropTypes.bool, // false by default
	onDownloadPdf: PropTypes.func, // pass function to handle this ({data, columnsToDisplay, onDownloadExcel}) => {....}
	expandedRowRender: PropTypes.func, // antd table expandedRowRender
	renderHeaders: PropTypes.func, // render content inside headers renderHeaders(props) => <div>Hello</div>
};

Document Component

This is the default doc view for edit&create mode. We use formik to handle hower form

You can pass other components if you want to take the controls to your hands

How to use the default document?

import { docViews, Admin, helpers, fields } from  "redux-admin";

const getDocFields = function(props) {
	return (
	<div>
		<fields.Input
			name={'email'}
			required label={'Email}
			key={'email'}
		/>
		<span>{helpers.getDocField({ // You can use helper to save time
				key: 'name',
				type: String,
				label: 'Name',
				required: true,
				documentRollConfig: props.documentRollConfig,
				isNewDoc: props.documentRollConfig,
			 })
		}</span>
	</div>
	)}
class  Posts  extends  Component  {
	render() {
		return  (
			<Admin 
				url="classes/Post"
				title={"Posts"}
				getDocTitle={getDocTitle}
				doc={<docViews.SimpleDoc
					   getDocFields={getDocFields}/>
					  }
...

getDocFields()

getDocfields - return the element that we will render inside the edit/create doc view.

the element can be any HTML element, we wrap your element with Formik/Form to handle the form state, your inputs need to include name, see Formik guide.

you can use redux-admin fields or use any fields that wrapped by Formik/Field

Examples-

  • Simple input

    	```jsx
    	import  { Field }  from  'formik';
    
    	const getDocFields = () => (<Field  type="email"  name="email"  placeholder="Email"  />
    	)
    	```

Fields - to use only inside Formik

import { fields } from  'redux-admin';

const {
	Input,
	InputNumber,
	MultiSelect,
	Reference,
	ParseServerReference,
	FireStoreReference,
	Select,
	ArrayInput,
	Consumer,
	CheckBox,
	DatePicker,
	TimePicker,
	Password,
	TextArea,
	Media,
	AutoComplete,
	ObjectEditor,
	Dropzone,
	BoxSelect,
	CheckboxWithIcon
} =  fields;
1.7.7

4 years ago

1.7.6

4 years ago

1.7.5

4 years ago

1.7.4

4 years ago

1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.5

4 years ago

1.6.4

5 years ago

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.12

5 years ago

1.5.11

5 years ago

1.5.10

5 years ago

1.5.9

5 years ago

1.5.8

5 years ago

1.5.7

5 years ago

1.5.6

5 years ago

1.5.5

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.5

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.1

6 years ago