UiProvider
SourceUiProvider is used to supply the UI library specific widgets, formatters, form components etc that are used throughout the system.
To use with @prestojs/ui-antd
see AntdUiProvider.
import React from 'react'; import { UiProvider, getFormatterForField } from '@prestojs/ui'; import { Input } from 'antd'; const DefaultWidget = ({ input }) => <input {...input} />; const DefaultFormatter = ({ value }) => value; function getWidgetForField(field) { // Add any app specific customisations here, eg // if (field instanceof BooleanField) { // return [CustomBooleanWidget, field.getWidgetProps()]; // } // Otherwise return default widget. If you would prefer an error if no explicit widget defined for // a field then don't return anything. return [DefaultWidget, field.getWidgetProps()]; } function getFormatterForField(field) { // Add any app specific customisations here, eg // if (field instanceof BooleanField) { // return CustomBooleanFormatter; // } return DefaultFormatter; } function FormItemWrapper({ children, label, help, required }) { return ( <div> <label> {label} {children} </label> {required ? '(required)' : '(optional)'} {help && <span className="help">{help}</span>} </div> ); } export default function Root() { return ( <UiProvider getFormatterForField={getFormatterForField} getWidgetForField={getWidgetForField} formItemComponent={FormItemWrapper} > <YourApp /> </UiProvider> ); }
Component Props:
Prop | Type | Description | |
---|---|---|---|
* | props An object with the properties below | ||
* | props.children | any | Children to render |
props.formComponent | React.ComponentType | A component to use to render the form. This is the component that will be rendered by
Form. Defaults to | |
props.formItemComponent | React.ComponentType | A component to use to render items in a form. This is the component that will be rendered by Form.Item. | |
props.getFormatterForField | GetFormatterForFieldWithNull | A function that is passed an instance of | |
props.getWidgetForField | GetWidgetForFieldWithNull | A function that is passed an instance of This function can return a 2-tuple of the form [component, props] where props is an object of
any extra props to pass through to the component. It is recommended you include
This allows Field definitions to supply extra props the widget can use. |