UiProvider

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, 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:

PropTypeDescription
*
props
An object with the properties below
*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.getFormatterForFieldGetFormatterForFieldWithNull

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.getWidgetForFieldGetWidgetForFieldWithNull

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.

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 field.getWidgetProps(), eg.

if (field instanceof BooleanField) {
   return [CustomBooleanWidget, field.getWidgetProps()];
}

This allows Field definitions to supply extra props the widget can use.