React Components for Metablock
npm install @metablock/reactReact components, views and utilities for building metablocks.
Disable server-side rendering
``js`
import { NoSsr } from "@metablock/react";
This is useful for private/login-protected pages for example.
`js`
import {Header} from "@metablock/react";
Progressively load background images
Originally from montezume/medium-style-progressively-loaded-images repo.
This hook allows to progressively load images.
A hook for managing Form submission workflow and optionally for rendering form fields
`typescript
import { useForm } from "@metablock/react";
const form = useForm(
{
handleSubmit,
defaultValues,
fieldCallback,
},
[]
);
`
The handleSubmit function is an async function for handling form submissions, defaultValues are the initial/default values of the form and fieldCallback is an optional function for customizing props of form fields.
The optional callback function can be used to further customize the UI. The signature is
`typescript`
const fieldCallback = (
name: str,
extraProps: Record
): Record
return extraProps;
};
where name is the input name and extraProps are the additional props passed to the original FormFromSchema component. If the function returns nothing for a given field name, the field is not displayed in the form.
The library has a tooling for rending Forms specified via JSON schema.
The entry-point component for rendering forms from JSON schema
`js
import {FormFromSchema, useForm} from "@metablock/react";
const MyForm = () => {
const form = useForm({...});
return
};
`
The schema is a valid subset of the JSON schema. For example a simple name input can be defined as:
`json`
{
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 3,
"description": "Please enter your name",
"format": "optional format"
}
}
}
This is a global object which controls how form fields are rendered. By defaults it provide the basic input fields, but it can be extended or overwritten if required.
`js
import { SchemaRegistry } from "@metablock/react";
SchemaRegistry.set("string", StringSchema);
SchemaRegistry.set("boolean", BooleanSchema);
SchemaRegistry.set("integer", NumberSchema);
SchemaRegistry.set("number", NumberSchema);
SchemaRegistry.set("object", ObjectSchema);
`
This is a higher level component to render Forms with side actions. It uses the FormFromSchema component in conjunction with the useForm hook.
A Component for rendering tabular data, it allows to fetch server side data via the ApiData` interface.
- uses react-data-grid