Skip to content

Support date-time for fieldsets #8

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions common/mockData/formatterMockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,43 @@ export const JSON_SCHEMA_INACTIVE_FIELD_SET_TITLE_MAP_FAKE_DATA = '{\n' +
' "type": "object"\n' +
' }\n' +
'}';

export const JSON_SCHEMA_DATE_TIME_FIELD_SETS = '{\n' +
' "schema": {\n' +
' "$schema": "http://json-schema.org/draft-04/schema#",\n' +
' "definition": [\n' +
' {\n' +
' "htmlClass": "col-lg-6",\n' +
' "items": [\n' +
' {\n' +
' "fieldHtmlClass": "date-time-picker json-schema",\n' +
' "key": "arrival_time"\n' +
' }\n' +
' ],\n' +
' "type": "fieldset"\n' +
' }\n' +
' ],\n' +
' "properties": {\n' +
' "arrival_time": {\n' +
' "key": "Arrival Time",\n' +
' "title": "HWC MU Arrival Time (at scene)"\n' +
' }\n' +
' },\n' +
' "title": "EventType Test Data",\n' +
' "type": "object"\n' +
' }\n' +
'}';

export const UI_SCHEMA_ELEMENT_DATE_TIME_FIELD_SETS = {
"type": "Control",
"scope": "#/properties/arrival_time",
"label": "HWC MU Arrival Time (at scene)",
"options": {
"format": "date-time",
"display": "date-time"
}
};

export const JSON_SCHEMA_INVALID_DEFINITION_LOCATION_FAKE_DATA = '{\n' +
' "schema": {\n' +
' "definition": [\n' +
Expand Down
17 changes: 14 additions & 3 deletions src/generateUISchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
isFieldSetTitleWithoutItems, isPropertyKey,
isSchemaFieldSet, PropertyFormat,
} from './utils/utils';
import { isEmptyString } from './utils/stringUtils';

// Enums
enum SchemaTypes {
Expand Down Expand Up @@ -137,16 +138,16 @@ const getUIElement = (key: string, schema: any, fieldSetItem: any = undefined) =
case SchemaTypes.Number:
return getBaseUIObject(key, schema.schema.properties[key].title || '');

case SchemaTypes.String:
// eslint-disable-next-line no-case-declarations
case SchemaTypes.String: {
const dateTimeFormat = getDateTimeControlFormat(key, schema, fieldSetItem);
if (!isEmpty(dateTimeFormat)) {
if (!isEmptyString(dateTimeFormat)) {
options = getElementOptions(PropertyFormat.DateTime, dateTimeFormat);
} else if (schema.schema.properties[key].display
&& schema.schema.properties[key].display === ElementDisplay.Header) {
options = getElementOptions(PropertyFormat.FormLabel);
}
return getBaseUIObject(key, schema.schema.properties[key].title || '', options);
}

case SchemaTypes.Array:
if (schema.schema.properties[key].items.enum
Expand All @@ -156,6 +157,16 @@ const getUIElement = (key: string, schema: any, fieldSetItem: any = undefined) =
options = getElementOptions(PropertyFormat.RepeatableField);
}
return getBaseUIObject(key, schema.schema.properties[key].title || '', options);

case undefined: {
const dateTimeFormat = getDateTimeControlFormat(key, schema, fieldSetItem);
if (!isEmptyString(dateTimeFormat)) {
options = getElementOptions(PropertyFormat.DateTime, dateTimeFormat);
return getBaseUIObject(key, schema.schema.properties[key].title || '', options);
}
return undefined;
}

default:
break;
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isEmptyString = (value: string | undefined) => (value === undefined
|| value === null
|| value.trim().length === 0);
11 changes: 10 additions & 1 deletion test/JsonFormatter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import {
COLLECTION_FIELD_HEADER_FAKE_DATA,
JSON_SCHEMA_INLINE_REQUIRED_PROPERTIES,
JSON_SCHEMA_INACTIVE_TITLE_MAP_FAKE_DATA,
JSON_SCHEMA_INACTIVE_FIELD_SET_TITLE_MAP_FAKE_DATA, JSON_SCHEMA_DEFAULT_VALUES,
JSON_SCHEMA_INACTIVE_FIELD_SET_TITLE_MAP_FAKE_DATA,
JSON_SCHEMA_DEFAULT_VALUES,
JSON_SCHEMA_DATE_TIME_FIELDSETS, JSON_SCHEMA_DATE_TIME_FIELD_SETS, UI_SCHEMA_ELEMENT_DATE_TIME_FIELD_SETS,
} from "../common/mockData/formatterMockData";
import exp = require('node:constants');

describe('JSON Schema validation', () => {

Expand Down Expand Up @@ -160,6 +163,12 @@ describe('JSON UI Schema generation', () => {
expect(uiSchema).toMatchObject(expectedUISchema);
});

it('Validate UI schema date-time fieldset property', () => {
const validSchema = validateJSONSchema(JSON_SCHEMA_DATE_TIME_FIELD_SETS);
const uiSchema = generateUISchema(validSchema);
expect(uiSchema.elements[0]).toMatchObject(UI_SCHEMA_ELEMENT_DATE_TIME_FIELD_SETS)
});

it('Generate UI Schema for field sets', () => {
const validSchema = validateJSONSchema(JSON.stringify(jsonSchemaFieldSets));
const uiSchema = generateUISchema(validSchema);
Expand Down