Skip to content

Commit

Permalink
Fix alignment on additional properties (#251)
Browse files Browse the repository at this point in the history
# Pull Request

## 📖 Description

Some fixes for the dictionary control (`additionalProperties` keyword):
- Styling alignment fixes
- Allow `true` to generate an un-typed control
- Add tests for the dictionary control

### 🎫 Issues

Closes #134 

## 👩‍💻 Reviewer Notes

Currently I do not have access to a recent macos version so the snapshots check will fail. This will be updated in future.

## ✅ Checklist

### General

<!--- Review the list and put an x in the boxes that apply. -->

- [x] I have added tests for my changes.
- [x] I have tested my changes.
- [ ] I have updated the project documentation to reflect my changes.
  • Loading branch information
janechu authored Mar 21, 2024
1 parent c13624f commit 6e28830
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Styling alignment fixes for the form additionalProperties keyword",
"packageName": "design-to-code",
"email": "7559015+janechu@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Styling alignment fixes for the form additionalProperties keyword",
"packageName": "design-to-code-react",
"email": "7559015+janechu@users.noreply.github.com",
"dependentChangeType": "patch"
}
15 changes: 15 additions & 0 deletions packages/design-to-code-react/app/pages/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ import {
disabledSchema,
generalSchema,
invalidDataSchema,
keywordAdditionalPropertiesAsFalseSchema,
keywordAdditionalPropertiesAsObjectSchema,
keywordAdditionalPropertiesAsTrueSchema,
mergedOneOfSchema,
nestedOneOfSchema,
nullSchema as nullKeywordSchema,
Expand Down Expand Up @@ -402,6 +405,18 @@ export const controlEmailDefault: ExampleComponent = {
schema: controlFormatEmailDefaultSchema,
data: undefined,
};
// Additional Properties As Object
export const keywordAdditionalPropertiesAsObject: ExampleComponent = {
schema: keywordAdditionalPropertiesAsObjectSchema,
};
// Additional Properties As True
export const keywordAdditionalPropertiesAsTrue: ExampleComponent = {
schema: keywordAdditionalPropertiesAsTrueSchema,
};
// Additional Properties As False
export const keywordAdditionalPropertiesAsFalse: ExampleComponent = {
schema: keywordAdditionalPropertiesAsFalseSchema,
};

/**
* Common use case schemas
Expand Down
10 changes: 10 additions & 0 deletions packages/design-to-code-react/src/__tests__/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ import controlFormatTimeSchema from "./control.format.time.schema";
import controlFormatTimeDisabledSchema from "./control.format.time.disabled.schema";
import controlFormatTimeDefaultSchema from "./control.format.time.default.schema";

/**
* Keyword Schemas
*/
import keywordAdditionalPropertiesAsFalseSchema from "./keyword.additional-properties.as-false.schema";
import keywordAdditionalPropertiesAsTrueSchema from "./keyword.additional-properties.as-true.schema";
import keywordAdditionalPropertiesAsObjectSchema from "./keyword.additional-properties.as-object.schema";

/**
* Invalid schema
*/
Expand Down Expand Up @@ -150,6 +157,9 @@ export {
controlUntypedSchema,
controlUntypedDefaultSchema,
controlUntypedDisabledSchema,
keywordAdditionalPropertiesAsFalseSchema,
keywordAdditionalPropertiesAsTrueSchema,
keywordAdditionalPropertiesAsObjectSchema,
defaultsSchema,
definitionsSchema,
dictionarySchema,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
$schema: "http://json-schema.org/schema#",
$id: "typeObjectAdditionalPropertiesAsFalse",
title: "Object with additional properties",
type: "object",
additionalProperties: false
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
$schema: "http://json-schema.org/schema#",
$id: "typeObjectAdditionalPropertiesAsObject",
title: "Object with additional properties",
type: "object",
additionalProperties: {
title: "Additional property",
type: "boolean"
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
$schema: "http://json-schema.org/schema#",
$id: "typeObjectAdditionalPropertiesAsTrue",
title: "Object with additional properties",
type: "object",
additionalProperties: true
};
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ function SectionControl(props: SectionControlProps) {

if (
typeof navigationItem.schema.additionalProperties === "object" ||
navigationItem.schema.additionalProperties === false
navigationItem.schema.additionalProperties === true
) {
return (
<FormDictionary
Expand Down Expand Up @@ -387,6 +387,7 @@ function SectionControl(props: SectionControlProps) {
messageSystemOptions={props.messageSystemOptions}
categories={props.categories}
validate={props.validate}
untitled={props.untitled}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from "../../../__tests__/base-fixtures.js";

test.describe("additionalProperties", () => {
test.describe("snapshot", () => {
test("object", async ({ page }) => {
await page.goto("/form?schema=keywordAdditionalPropertiesAsObject");
await expect(page).toHaveScreenshot();
});
test("true", async ({ page }) => {
await page.goto("/form?schema=keywordAdditionalPropertiesAsTrue");
await expect(page).toHaveScreenshot();
});
test("false", async ({ page }) => {
await page.goto("/form?schema=keywordAdditionalPropertiesAsFalse");
await expect(page).toHaveScreenshot();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { expect, test } from "../../../__tests__/base-fixtures.js";

test.describe("KeywordAdditionalProperties", () => {
test("should generate an add control if additionalProperties is an object with type is specified", async ({
page,
}) => {
await page.goto("/form?schema=keywordAdditionalPropertiesAsObject");

await page.waitForSelector(".dtc-form .dtc-dictionary_control-region");

const dictionary = await page.locator(".dtc-form .dtc-dictionary_control-region");

await expect(await dictionary.count()).toEqual(1);

await expect(
await dictionary.locator("button.dtc-dictionary_control-add-trigger").count()
).toEqual(1);
});
test("should add a control if the object with type is specified", async ({
page,
}) => {
await page.goto("/form?schema=keywordAdditionalPropertiesAsObject");

await page.waitForSelector(".dtc-form .dtc-dictionary_control-region");

const dictionaryItems = page.locator(".dtc-dictionary_item-control-region");

await expect(await dictionaryItems.count()).toEqual(0);

await page.locator("button.dtc-dictionary_control-add-trigger").click();

await expect(await dictionaryItems.count()).toEqual(1);
});
test("should generate an add control if additionalProperties is true", async ({
page,
}) => {
await page.goto("/form?schema=keywordAdditionalPropertiesAsTrue");

await page.waitForSelector(".dtc-form .dtc-dictionary_control-region");

const dictionary = await page.locator(".dtc-form .dtc-dictionary_control-region");

await expect(await dictionary.count()).toEqual(1);

await expect(
await dictionary.locator("button.dtc-dictionary_control-add-trigger").count()
).toEqual(1);
});
test("should add an untyped control if additionalProperties is true", async ({
page,
}) => {
await page.goto("/form?schema=keywordAdditionalPropertiesAsTrue");

await page.waitForSelector(".dtc-form .dtc-dictionary_control-region");

const dictionaryItems = page.locator(".dtc-dictionary_item-control-region");

await expect(await dictionaryItems.count()).toEqual(0);

await page.locator("button.dtc-dictionary_control-add-trigger").click();

await expect(await dictionaryItems.count()).toEqual(1);

await expect(await page.locator(".dtc-untyped-control").count()).toEqual(1);
});
test("should not generate controls if additionalProperties is false", async ({
page,
}) => {
await page.goto("/form?schema=keywordAdditionalPropertiesAsFalse");

const dictionaryControl = await page.locator(
".dtc-form .dtc-dictionary_control-region"
);

await expect(await dictionaryControl.count()).toEqual(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@
}

.dtc-dictionary_item-control-remove-trigger {
bottom: 6px;
bottom: 16px;
top: unset;
}

.dtc-dictionary_control {
flex-grow: 1;
}

.dtc-dictionary_control-region {
position: relative;
height: 30px;
padding-bottom: 5px;
}

.dtc-dictionary_control-label {
width: 100%;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,24 @@ function Dictionary(props: DictionaryProps) {
}

function renderControl(): React.ReactNode {
if (isPlainObject(props.additionalProperties)) {
return (
<div
className={`dtc-dictionary_control-region ${dtcClassName.commonControlRegion}`}
>
<div className={`dtc-dictionary_control`}>
<label
className={`dtc-dictionary_control-label ${dtcClassName.commonLabel}`}
>
{props.label}
</label>
</div>
<button
className={`dtc-dictionary_control-add-trigger ${dtcClassName.commonAddItem}`}
aria-label={"Select to add item"}
onClick={handleOnAddItem}
/>
return (
<div
className={`dtc-dictionary_control-region ${dtcClassName.commonLabelRegion}`}
>
<div className={`dtc-dictionary_control`}>
<label
className={`dtc-dictionary_control-label ${dtcClassName.commonLabel}`}
>
{props.label}
</label>
</div>
);
}
<button
className={`dtc-dictionary_control-add-trigger ${dtcClassName.commonAddItem}`}
aria-label={"Select to add item"}
onClick={handleOnAddItem}
/>
</div>
);
}

function renderItemControl(propertyName: string): React.ReactNode {
Expand Down Expand Up @@ -201,7 +199,9 @@ function Dictionary(props: DictionaryProps) {
props.schemaDictionary[
props.dataDictionary[0][props.dictionaryId].schemaId
],
props.additionalProperties,
props.additionalProperties === true
? { title: props.untitled, type: "string" }
: props.additionalProperties,
""
),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.dtc-common-add-item {
cursor: pointer;
position: relative;
appearance: none;
background: none;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.dtc-common-label-region {
display: flex;
justify-content: space-between;
align-items: center;
column-gap: 8px;
width: 100%;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
padding: 2px;
position: absolute;
border: none;
width: 18px;
width: 20px;
margin: 0;
height: 18px;
z-index: 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.dtc-common-remove-item {
cursor: pointer;
position: absolute;
top: 5px;
right: 2px;
right: 0;
appearance: none;
background: none;
border: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
display: flex;
height: 18px;
min-width: var(--dtc-gutter);
justify-content: center;
justify-content: flex-end;
align-items: center;
}

0 comments on commit 6e28830

Please sign in to comment.