diff --git a/CommonUI/src/Components/CategoryCheckbox/Index.tsx b/CommonUI/src/Components/CategoryCheckbox/Index.tsx index 12089a7ab4..cdd88ff621 100644 --- a/CommonUI/src/Components/CategoryCheckbox/Index.tsx +++ b/CommonUI/src/Components/CategoryCheckbox/Index.tsx @@ -2,15 +2,16 @@ import React, { FunctionComponent, ReactElement } from 'react'; import { CategoryCheckboxOption, CheckboxCategory, CategoryCheckboxValue } from './CategoryCheckboxTypes'; import Category from './Category'; -export interface ComponentProps { +export interface CategoryCheckboxProps { categories: Array; options: Array; onChange: (value: Array) => void; initialValue?: undefined | Array; + error?: string | undefined; } -const LabelElement: FunctionComponent = ( - props: ComponentProps +const CategoryCheckbox: FunctionComponent = ( + props: CategoryCheckboxProps ): ReactElement => { const [currentValues, setCurrentValues] = React.useState>(props.initialValue || []); @@ -43,8 +44,16 @@ const LabelElement: FunctionComponent = ( }} />) })} + {props.error && ( +

+ {props.error} +

+ )} ); }; -export default LabelElement; +export default CategoryCheckbox; diff --git a/CommonUI/src/Components/Forms/Fields/FormField.tsx b/CommonUI/src/Components/Forms/Fields/FormField.tsx index ef623c879c..28960862a0 100644 --- a/CommonUI/src/Components/Forms/Fields/FormField.tsx +++ b/CommonUI/src/Components/Forms/Fields/FormField.tsx @@ -21,6 +21,8 @@ import { JSONValue } from 'Common/Types/JSON'; import IDGenerator from '../../ObjectID/IDGenerator'; import ObjectID from 'Common/Types/ObjectID'; import OneUptimeDate from 'Common/Types/Date'; +import CheckboxElement, { CategoryCheckboxValue } from '../../Checkbox/Checkbox'; +import CategoryCheckbox from '../../CategoryCheckbox/Index'; export interface ComponentProps { field: Field; @@ -41,521 +43,593 @@ const FormField: ( ) => ReactElement = ( props: ComponentProps ): ReactElement => { - const getFieldType: Function = (fieldType: FormFieldSchemaType): string => { - switch (fieldType) { - case FormFieldSchemaType.Email: - return 'email'; - case FormFieldSchemaType.Password: - return 'password'; - case FormFieldSchemaType.EncryptedText: - return 'password'; - case FormFieldSchemaType.Number: - return 'number'; - case FormFieldSchemaType.Date: - return 'date'; - case FormFieldSchemaType.DateTime: - return 'datetime-local'; - case FormFieldSchemaType.Time: - return 'time'; - case FormFieldSchemaType.LongText: - return 'textarea'; - case FormFieldSchemaType.Color: - return 'color'; - case FormFieldSchemaType.URL: - return 'url'; - default: - return 'text'; - } - }; + const getFieldType: Function = (fieldType: FormFieldSchemaType): string => { + switch (fieldType) { + case FormFieldSchemaType.Email: + return 'email'; + case FormFieldSchemaType.Password: + return 'password'; + case FormFieldSchemaType.EncryptedText: + return 'password'; + case FormFieldSchemaType.Number: + return 'number'; + case FormFieldSchemaType.Date: + return 'date'; + case FormFieldSchemaType.DateTime: + return 'datetime-local'; + case FormFieldSchemaType.Time: + return 'time'; + case FormFieldSchemaType.LongText: + return 'textarea'; + case FormFieldSchemaType.Color: + return 'color'; + case FormFieldSchemaType.URL: + return 'url'; + default: + return 'text'; + } + }; - const getFormField: Function = (): ReactElement => { - const index: number = props.index + 1; + const getFormField: Function = (): ReactElement => { + const index: number = props.index + 1; - const fieldType: string = props.field.fieldType - ? getFieldType(props.field.fieldType) - : 'text'; + const fieldType: string = props.field.fieldType + ? getFieldType(props.field.fieldType) + : 'text'; - if (Object.keys(props.field.field || {}).length === 0) { - throw new BadDataException('Object cannot be without Field'); - } - - if (props.field.showIf && !props.field.showIf(props.currentValues)) { - return <>; - } - - let codeType: CodeType = CodeType.HTML; - - if (props.field.fieldType === FormFieldSchemaType.CSS) { - codeType = CodeType.CSS; - } - - if (props.field.fieldType === FormFieldSchemaType.JavaScript) { - codeType = CodeType.JavaScript; - } - - let fieldDescription: string | undefined = props.field.description; - - if ( - props.field.fieldType === FormFieldSchemaType.DateTime || - props.field.fieldType === FormFieldSchemaType.Time - ) { - if (!fieldDescription) { - fieldDescription = ''; + if (Object.keys(props.field.field || {}).length === 0) { + throw new BadDataException('Object cannot be without Field'); } - fieldDescription += - ' This is in your local timezone - ' + - OneUptimeDate.getCurrentTimezoneString(); - } + if (props.field.showIf && !props.field.showIf(props.currentValues)) { + return <>; + } - return ( -
- + let codeType: CodeType = CodeType.HTML; -
- {props.field.fieldType === FormFieldSchemaType.Color && ( - { - props.field.onChange && - props.field.onChange(value); - props.setFieldValue(props.fieldName, value); - props.setFieldTouched(props.fieldName, true); - }} - tabIndex={index} - placeholder={props.field.placeholder || ''} - initialValue={ - props.currentValues && - (props.currentValues as any)[props.fieldName] - ? (props.currentValues as any)[ - props.fieldName - ] - : '' - } - /> - )} + if (props.field.fieldType === FormFieldSchemaType.CSS) { + codeType = CodeType.CSS; + } - {(props.field.fieldType === FormFieldSchemaType.Dropdown || - props.field.fieldType === - FormFieldSchemaType.MultiSelectDropdown) && ( - - | null - ) => { - props.field.onChange && - props.field.onChange(value); - props.setFieldValue(props.fieldName, value); - }} - onBlur={async () => { - props.setFieldTouched(props.fieldName, true); - }} - isMultiSelect={ - props.field.fieldType === - FormFieldSchemaType.MultiSelectDropdown - } - options={props.field.dropdownOptions || []} - placeholder={props.field.placeholder || ''} - initialValue={ - props.currentValues && - (props.currentValues as any)[props.fieldName] - ? (props.currentValues as any)[ - props.fieldName - ] - : '' - } - /> - )} + if (props.field.fieldType === FormFieldSchemaType.JavaScript) { + codeType = CodeType.JavaScript; + } - {props.field.fieldType === FormFieldSchemaType.ObjectID && ( - { - props.field.onChange && - props.field.onChange(value); - props.setFieldValue(props.fieldName, value); - }} - onEnterPress={() => { - props.submitForm && props.submitForm(); - }} - initialValue={ - props.currentValues && - (props.currentValues as any)[props.fieldName] - ? (props.currentValues as any)[ - props.fieldName - ] - : props.field.defaultValue || null - } - /> - )} + let fieldDescription: string | undefined = props.field.description; - {props.field.fieldType === - FormFieldSchemaType.RadioButton && ( - { - props.field.onChange && - props.field.onChange(value); - props.setFieldValue(props.fieldName, value); - }} - options={props.field.radioButtonOptions || []} - initialValue={ - props.currentValues && - (props.currentValues as any)[props.fieldName] - ? (props.currentValues as any)[ - props.fieldName - ] - : '' - } - /> - )} + if ( + props.field.fieldType === FormFieldSchemaType.DateTime || + props.field.fieldType === FormFieldSchemaType.Time + ) { + if (!fieldDescription) { + fieldDescription = ''; + } - {props.field.fieldType === FormFieldSchemaType.LongText && ( -