UiProvider
This is a React component

Source
import { UiProvider } from "@prestojs/ui";
UiProvider(props)

UiProvider 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;
// }
// 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;
}
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>
);
}
ParameterTypeDescription
*props.childrenany

Children to render

props.formComponentReact.ComponentType

A component to use to render the form. This is the component that will be rendered by Form. Defaults to form.

props.formItemComponentReact.ComponentType

A component to use to render items in a form. This is the component that will be rendered by Form.Item.

props.getFormatterForField
Function

A function that is passed an instance of Field and should return the formatter to use for this field. If falsey is returned then it will fall back to a parent UiProvider (if any) or if no parent UiProvider an error will be thrown.

props.getWidgetForField
Function

A function that is passed an instance of Field and should return the widget component to use for this field. If falsey is returned then it will fall back to a parent UiProvider (if any) or if no parent UiProvider an error will be thrown.