AntdUiProviderThis is a React component
SourceVersion of UiProvider that supports some extra options specific to antd.
If you use date or time widgets in the system you should pass through the datePickerComponent
and timePickerComponent
props. Without this when DateWidget, TimeWidget or other date/time based components
are used they will throw an error. The reason for this is to configure how you want dates to be handled. The default
DatePicker
and TimePicker
components provided by antd use momentjs but it is recommended you use date-fns or dayjs
instead.
Recommended Setup
To setup form components, widgets and formatters with sensible defaults we recommend the following:
import React from 'react';import { AntdUiProvider } from '@prestojs/ui-antd';import { getFormatterForField as defaultGetFormatterForField } from '@prestojs/ui';import { getWidgetForField as defaultGetWidgetForField } from '@prestojs/ui-antd';const DatePicker = React.lazy(() => import('./DatePicker'));const TimePicker = React.lazy(() => import('./TimePicker'));const FormItemWrapper = React.lazy(() => import('@prestojs/ui-antd/FormItemWrapper'));const FormWrapper = React.lazy(() => import('@prestojs/ui-antd/FormWrapper'));function getWidgetForField(field) {// Add any app specific customisations here, eg// if (field instanceof BooleanField) {// return CustomBooleanWidget;// }// Otherwise fall back to specific UI library defaultslet widget;if ((widget = getAntdWidget(field))) return widget;// ... if integrating any other libraries add them here ...// Fall through to any parent UiProvider. If there is none or they// don't provide a widget for this field then an error will be thrown}function getFormatterForField(field) {// Add any app specific customisations here, eg// if (field instanceof BooleanField) {// return CustomBooleanFormatter;// }// Otherwise fall back to specific UI library defaultslet formatter;if ((formatter = defaultGetFormatterForField(field))) return formatter;// ... if integrating any other libraries add them here ...// Fall through to any parent UiProvider. If there is none or they// don't provide a formatter for this field then an error will be thrown}export default function Root() {return (<AntdUiProviderdatePickerComponent={datePickerComponent}timePickerComponent={timePickerComponent}getFormatterForField={getFormatterForField}getWidgetForField={getWidgetForField}formItemComponent={FormItemWrapper}formComponent={FormWrapper}><YourApp /></AntdUiProvider>);}
This recommended setup sets:
formComponent
is set to FormWrapperformItemComponent
is set to FormItemWrappergetWidgetForField
is set to getWidgetForFieldgetFormatterForField
is set to getFormatterForFielddatePickerComponent
andtimePickerComponent
are components you create according to the antd documentation
NOTE The components here are loaded using React.lazy. Your build must support this otherwise it is recommended to implement your own version of getWidgetForField and getFormatterForField.
Parameter | Type | Description | |
---|---|---|---|
props.datePickerComponent | DatePickerComponent | The DatePicker component to use for components like DateWidget. If you don't use any of these components you don't have to provide this option. You can pass the antd DatePicker directly or create your own version | |
props.timePickerComponent | TimePickerComponent | The TimePicker component to use for components like TimeWidget. If you don't use any of these components you don't have to provide this option. You can pass the antd TimePicker directly or create your own version | |
* | 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 | Function | A function that is passed an instance of | |
props.getWidgetForField | Function | A function that is passed an instance of |