import { FunctionComponent, ReactNode } from 'react'; import { DatePickerProps } from '../DatePicker'; import { TextAreaProps } from '../TextArea'; export const schemaTypes = [ 'string', 'localized:string', 'number', 'localized:number', 'boolean', 'localized:boolean', 'object', 'array', ] as const; export type SchemaTypes = typeof schemaTypes[number]; export const UIWidgetsForString = [ 'upload', 'textarea', 'date', 'select', 'color', 'autocomplete', 'radio', 'html', 'slider', ] as const; export const UIWidgetsForLocalizedString = ['textarea'] as const; export const UIWidgetsForNumber = ['slider'] as const; export const UIWidgetsByType = { [schemaTypes[0]]: UIWidgetsForString, [schemaTypes[1]]: UIWidgetsForLocalizedString, [schemaTypes[2]]: UIWidgetsForNumber, [schemaTypes[3]]: UIWidgetsForNumber, }; type StringWithAutocomplete = T | (string & {}); export type UIWidgets = StringWithAutocomplete< typeof UIWidgetsForString[number] >; export type UiOptionsGrid = { grid: string[][][]; }; export type UiOptionsOneOf = { oneOf: { options: { label: string; index: number; value?: Record; }[]; }; }; export type UiOptionsArray = { array: 'row'; }; export type UiOptionsTextArea = { textarea: Omit; }; export type UiOptionsUpload = { upload: { // https://developer.mozilla.org/fr/docs/Web/HTML/Element/Input/file#accept accept?: string; }; }; export type UiOptionsSelect = { select: { options: { label: string | ReactNode; value: any; }[]; }; }; export type UiOptionsDynamicAutocomplete = { autocomplete: string; }; export type UiOptionsAutocomplete = { autocomplete: { options: { label: string | ReactNode; value?: any; options?: { label: string | ReactNode; value?: any }[]; }[]; }; }; export type UiOptionsDate = { date: DatePickerProps; }; export type UiOptionsHTML = { html: { htmlModeOnly?: boolean; }; }; export type UiOptionsSlider = { slider: { steps: { label: string; description: string; value: string | number; className?: string; }[]; step?: number; }; }; export interface Schema extends Record { // Field type type?: SchemaTypes; // Field label title?: string | ReactNode; // Description in a help tooltip description?: string | ReactNode; // Input placeholder placeholder?: string; // Add button label when in a list of items or properties add?: string; // Remove button label when in a list of items or properties remove?: string; // Property key label propertyKey?: string; // Property value label propertyValue?: string; // For object type, object properties properties?: Record; // Schema of object additional properties additionalProperties?: boolean | Schema; // For array type, schema of array items items?: Schema; // List of different schemas available oneOf?: Schema[]; // Value of the oneOf field. value?: any; // List of constrainted values enum?: any[]; // Labels of constrainted values enumNames?: string[]; // Default value default?: any; // Do not display the field hidden?: true; // Disable the field disabled?: true; // Validation by regexp // @deprecated pattern?: string; // Validation errors messages // @deprecated errors?: Record; validators?: Record< 'required' | 'pattern' | 'min' | 'max' | 'tel' | 'email' | 'date', | { value?: string | number | boolean; message?: string; } | true >; // Custom widget 'ui:widget'?: UIWidgets | FunctionComponent; // Options for UI components 'ui:options'?: | UiOptionsGrid | UiOptionsOneOf | UiOptionsArray | UiOptionsTextArea | UiOptionsUpload | UiOptionsSelect | UiOptionsDate | UiOptionsAutocomplete | UiOptionsDynamicAutocomplete | UiOptionsHTML | UiOptionsSlider | Record; } export interface FieldProps { schema: Schema; name: string; label?: string; className?: string; }