
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
react-auto-edit
Advanced tools
React Auto Edit is a library of components for producing low-code, schema driven editing UIs. You define the data schema, provide the REST APIs, and React Auto Edit automagically provides the UI. Validation of data against the schema is included automat
React Auto Edit is a library of components for producing low-code, schema driven editing UIs. You define the data schema, provide the REST APIs, and React Auto Edit automagically provides the UI. Validation of data against the schema is included automatically.
It is suitable for use cases where you need a basic CRUD editor UI, e.g. line of business / admininstrative apps.
npx create-react-app myapp
cd myapp
npm install --save react-auto-edit joi joi-key-extensions react-router-dom
npm start
Edit myapp/src/App.js:
import React from 'react';
import VanillaJoi from 'joi';
import { fkExtension, pkExtension } from 'joi-key-extensions';
import { BrowserRouter as Router, Redirect, Route } from 'react-router-dom';
import {
ApiProxy, Controller, EditRoutes, Menu, Loading,
} from 'react-auto-edit';
import 'react-auto-edit/static/Edit.css';
const Joi = VanillaJoi
.extend(fkExtension.number)
.extend(pkExtension.number);
const schema = Joi.object({
albums: Joi.array().items({
id: Joi.number().integer().pk(),
userId: Joi.number().integer(),
title: Joi.string().meta({ displayName: true }),
}),
posts: Joi.array().items({
id: Joi.number().integer().pk(),
userId: Joi.number().integer(),
title: Joi.string().meta({ displayName: true }),
body: Joi.string().max(5000),
}),
comments: Joi.array().items({
id: Joi.number().integer().pk(),
postId: Joi.number().integer().fk('posts.[].id'),
name: Joi.string().meta({ displayName: true }),
email: Joi.string().email(),
body: Joi.string().max(5000),
}),
todos: Joi.array().items({
id: Joi.number().integer().pk(),
userId: Joi.number().integer(),
title: Joi.string().meta({ displayName: true }),
completed: Joi.boolean(),
}),
});
const apiProxy = new ApiProxy(schema, 'https://jsonplaceholder.typicode.com', {
pageSize: 10,
concurrentFetchesLimit: 1,
});
const controller = new Controller(schema, apiProxy)
const App = () => <Router>
<React.Fragment>
<Menu controller={controller} />
<EditRoutes basePath="" controller={controller} />
<Loading controller={controller} />
<Route exact={true} path="/" component={() => <Redirect to="/posts" />}/>
</React.Fragment>
</Router>;
export default App;
Unfortunately Joi is now being published with the "browser" property in its package.json pointing to a cut down version of Joi that does not support some required features. Also unfortunately, a workaround is required with Create React App to get this property ignored.
npm install --save craco
Edit myapp/package.json:
/* package.json */
"scripts": {
- "start": "react-scripts start",
+ "start": "craco start",
- "build": "react-scripts build",
+ "build": "craco build"
- "test": "react-scripts test",
+ "test": "craco test"
}
Add craco.config.js at the same level as your package.json:
const path = require('path');
module.exports = {
webpack: {
configure: {
resolve: {
alias: {
// ignore the cut down browser distribution that
// joi's package.json steers webpack to
joi: path.resolve(__dirname, 'node_modules/joi/lib/index.js'),
},
},
},
},
};
React Auto Edit has some expectations of the Joi schema that controls it:
myfield: Joi.string().pk()
)myfield: Joi.string().meta({ displayName: true })
)React Auto Edit also expects the target REST API to conform to some conventions:
/<EntityName>/<EntityPK>
/<ParentEntityName>/<ParentEntityPK>/<ChildCollectionFieldName>/<ChildEntityPK>
/<EntityName>/<EntityPK1>/<EntityPK2>
){ totalPages: 1, items: [] }
If need be you can work around these conventions by subclassing ApiProxy
The ApiProxy class is in charge of mapping data requests to REST API calls
The UIFactory class creates instances of the React UI components. If you use the standard React UI components then you don't need to worry about this class.
The Controller class is in charge of managing application state
Returns a set of React Router routes supporting url based navigation to items in the schema tree. Note that you must wrap this with a React Router "BrowserRouter" component.
Props:
Provides a menu to allow navigation to the top-level fields of the schema. This assumes you are using the EditRoutes component. Save and Cancel buttons are included for convenience - if you don't want to use the Menu component you can add them seperately.
Props:
Displays a loading spinner when API calls are in flight
Props:
You do not need to explicitly use the following components for simple use cases, as they are encapsulated by EditRoutes and Menu.
Displays a button to trigger saving pending changes to the target API. If you use the Menu component you will not need to add a SaveButton.
Props:
Displays a button that can trigger discarding pending changes. If you use the Menu component you will not need to add a CancelButton.
Props:
Displays a tabular (columns and rows) editor for a collection. Note that this editor is not suitable for all data types - for other collections use EditCollection.
Static Method:
Props:
[{ zooId: 3}, { enclosureId: 4 }]
.Displays a master/detail editor for a collection.
Props:
[{ zooId: 3}, { enclosureId: 4 }]
.React Auto Edit is designed to be highly customisable. The project in examples/customisation
demonstrates some of the available options.
You can override the classes in the supplied CSS file (static/Edit.css), or leave it out of your project altogether.
If you want to return different React components than standard, subclass UIFactory, override the methods as needed and pass an instance of the subclass in the Controller options argument. Refer to the examples/customisation
project for examples of usage.
If your target API does not correspond to the conventions expected by ApiProxy you can subclass ApiProxy and pass an instance of the subclass to Controller.
e.g.
import { ApiProxy, Controller} from 'react-auto-edit';
import schema from './schema';
import getAuthToken from './somewhere';
class MyApiProxy extends ApiProxy {
constructor(schema, baseApiPath, options, getAuthToken) {
super(schema, baseApiPath, options);
this.getAuthToken = getAuthToken;
}
getPageAndFilterParams(page, filter) {
let result = super.getPageAndFilterParams(page, filter);
// say our target API has the page number parameter called 'pageNumber'
// instead of 'page'
result = result.filter(p => !p.startsWith('page'));
result.push(`pageNumber=${page}`);
return result;
}
async fetchJson(url, options) {
// for this example we need an authentication token to access the API
const authToken = this.getAuthToken();
const headers = { Authorization: `Bearer ${token}` };
const newOptions = Object.assign({ headers }, options);
return super.fetchJson(newUrl, newOptions);
}
}
const apiProxy = new MyApiProxy(schema, 'http://localhost:8080', null, getAuthToken);
const controller = new Controller(schema, apiProxy);
You use the React Router 'Switch' component to seize a route back from React Auto Edit and do whatever you please with it.
e.g.
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
// etc...
const App = () => <Router>
<React.Fragment>
<Menu controller={controller} />
<Switch>
<Route path="/comments" exact={true}
component={() => <div>Mine!</div>}
/>
<EditRoutes basePath="" controller={controller} />
</Switch>
<Loading controller={controller} />
</React.Fragment>
</Router>;
React Auto Edit is based on:
The example apps uses https://jsonplaceholder.typicode.com as a sample API, with thanks.
FAQs
React Auto Edit is a library of components for producing low-code, schema driven editing UIs. You define the data schema, provide the REST APIs, and React Auto Edit automagically provides the UI. Validation of data against the schema is included automat
The npm package react-auto-edit receives a total of 1 weekly downloads. As such, react-auto-edit popularity was classified as not popular.
We found that react-auto-edit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.