Skip to content

Commit 1f7bc29

Browse files
authored
feat: show combiner for properties of an object (#183)
1 parent 20e90ea commit 1f7bc29

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

src/__fixtures__/combiners/anyOf.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"myProp": {
5+
"type": "array",
6+
"items": {
7+
"anyOf": [
8+
{
9+
"properties": {
10+
"foo": {
11+
"type": "string"
12+
}
13+
}
14+
},
15+
{
16+
"properties": {
17+
"bar": {
18+
"type": "number"
19+
}
20+
}
21+
},
22+
{
23+
"properties": {
24+
"baz": {
25+
"type": "boolean"
26+
}
27+
}
28+
}
29+
],
30+
"type": "object"
31+
}
32+
}
33+
}
34+
}

src/__stories__/JsonSchemaViewer.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const nullRefSchema = require('../__fixtures__/references/nullish.json');
1818
const brokenRefArraySchema = require('../__fixtures__/arrays/of-refs.json');
1919
const oneOfWithArraySchema = require('../__fixtures__/combiners/oneof-with-array-type.json');
2020
const oneOfWithArraySchema2 = require('../__fixtures__/combiners/oneof-within-array-item.json');
21+
const anyOfObject = require('../__fixtures__/combiners/anyOf.json');
2122
const arrayOfComplexObjects = require('../__fixtures__/arrays/of-complex-objects.json');
2223

2324
subscribeTheme({ mode: 'light' });
@@ -159,19 +160,26 @@ storiesOf('JsonSchemaViewer/combiners', module)
159160
onGoToRef={action('onGoToRef')}
160161
/>
161162
))
162-
.add('anyOf-array-schema', () => (
163+
.add('oneOf-array-schema', () => (
163164
<JsonSchemaViewer
164165
schema={oneOfWithArraySchema as JSONSchema4}
165166
defaultExpandedDepth={number('defaultExpandedDepth', 1)}
166167
onGoToRef={action('onGoToRef')}
167168
/>
168169
))
169-
.add('anyOf-array-schema2', () => (
170+
.add('oneOf-array-schema2', () => (
170171
<JsonSchemaViewer
171172
schema={oneOfWithArraySchema2 as JSONSchema4}
172173
defaultExpandedDepth={number('defaultExpandedDepth', 1)}
173174
onGoToRef={action('onGoToRef')}
174175
/>
176+
))
177+
.add('anyOf-object-schema', () => (
178+
<JsonSchemaViewer
179+
schema={anyOfObject as JSONSchema4}
180+
defaultExpandedDepth={number('defaultExpandedDepth', 1)}
181+
onGoToRef={action('onGoToRef')}
182+
/>
175183
));
176184

177185
storiesOf('JsonSchemaViewer/$refs', module)

src/components/SchemaRow/useChoices.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ function makeChoice(node: SchemaNode): Choice {
4141
};
4242
}
4343

44-
function makeArrayChoice(node: SchemaNode): Choice {
44+
function makeArrayChoice(node: SchemaNode, combiner?: string): Choice {
4545
const itemTitle = calculateChoiceTitle(node, true);
46-
const title = itemTitle !== 'any' ? `array[${itemTitle}]` : 'array';
46+
const title = itemTitle !== 'any' ? `array ${combiner ? `(${combiner})` : null} [${itemTitle}]` : 'array';
4747
return {
4848
type: node,
4949
title,
@@ -64,7 +64,9 @@ export const useChoices = (schemaNode: SchemaNode) => {
6464
isNonEmptyParentNode(schemaNode.children[0]) &&
6565
shouldShowChildSelector(schemaNode.children[0])
6666
) {
67-
return schemaNode.children[0].children.map(makeArrayChoice);
67+
return schemaNode.children[0].children.map(child =>
68+
makeArrayChoice(child, schemaNode.children[0].combiners?.[0]),
69+
);
6870
}
6971

7072
// if current node is a combiner, offer its children

src/components/shared/__tests__/Property.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('Property component', () => {
141141

142142
const wrapper = render(schema);
143143
expect(wrapper.html()).toMatchInlineSnapshot(
144-
`"<div class=\\"\\" id=\\"mosaic-provider-react-aria-0-1\\"><div data-overlay-container=\\"true\\" class=\\"\\"><div class=\\"sl-flex sl-max-w-full sl-py-2\\"><div class=\\"sl-stack sl-stack--vertical sl-stack--1 sl-flex sl-flex-1 sl-flex-col sl-items-stretch sl-max-w-full\\"><div class=\\"sl-flex sl-items-center sl-max-w-full\\"><div class=\\"sl-flex sl-items-baseline sl-text-base\\"><div class=\\"sl-select sl-relative\\"><div style=\\"border: 0px; clip-path: inset(50%); height: 1px; margin: 0px -1px -1px 0px; overflow: hidden; padding: 0px; position: absolute; width: 1px; white-space: nowrap;\\" aria-hidden=\\"true\\"><input type=\\"text\\" tabindex=\\"-1\\" style=\\"font-size: 16px;\\"><label><select size=\\"2\\" tabindex=\\"-1\\"><option></option><option value=\\"0\\">array[strings]</option><option value=\\"1\\">array[numbers]</option></select></label></div><div class=\\"sl-relative\\"><button aria-label=\\"Pick a type\\" aria-haspopup=\\"listbox\\" aria-expanded=\\"false\\" id=\\"react-aria-0-4\\" aria-labelledby=\\"react-aria-0-4 react-aria-0-6\\" type=\\"button\\" class=\\"sl-button sl-w-full sl-h-sm sl-text-base sl-font-normal sl-px-1.5 sl-bg-transparent sl-rounded sl-border-transparent hover:sl-border-input focus:sl-border-primary active:sl-border-primary sl-border disabled:sl-opacity-60\\"><div class=\\"sl-flex sl-flex-1 sl-justify-items-start sl-items-center\\"><div class=\\"sl-pr-1\\">array[strings]</div></div><div class=\\"sl-text-xs sl--mr-0.5 sl-ml-1\\"><div class=\\"sl-pt-0.5 sl-pr-0.5\\"><svg aria-hidden=\\"true\\" focusable=\\"false\\" data-prefix=\\"fas\\" data-icon=\\"chevron-down\\" class=\\"svg-inline--fa fa-chevron-down fa-w-14 fa-xs sl-icon\\" role=\\"img\\" xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 448 512\\"><path fill=\\"currentColor\\" d=\\"M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z\\"></path></svg></div></div></button></div></div></div></div></div></div></div></div>"`,
144+
`"<div class=\\"\\" id=\\"mosaic-provider-react-aria-0-1\\"><div data-overlay-container=\\"true\\" class=\\"\\"><div class=\\"sl-flex sl-max-w-full sl-py-2\\"><div class=\\"sl-stack sl-stack--vertical sl-stack--1 sl-flex sl-flex-1 sl-flex-col sl-items-stretch sl-max-w-full\\"><div class=\\"sl-flex sl-items-center sl-max-w-full\\"><div class=\\"sl-flex sl-items-baseline sl-text-base\\"><div class=\\"sl-select sl-relative\\"><div style=\\"border: 0px; clip-path: inset(50%); height: 1px; margin: 0px -1px -1px 0px; overflow: hidden; padding: 0px; position: absolute; width: 1px; white-space: nowrap;\\" aria-hidden=\\"true\\"><input type=\\"text\\" tabindex=\\"-1\\" style=\\"font-size: 16px;\\"><label><select size=\\"2\\" tabindex=\\"-1\\"><option></option><option value=\\"0\\">array (oneOf) [strings]</option><option value=\\"1\\">array (oneOf) [numbers]</option></select></label></div><div class=\\"sl-relative\\"><button aria-label=\\"Pick a type\\" aria-haspopup=\\"listbox\\" aria-expanded=\\"false\\" id=\\"react-aria-0-4\\" aria-labelledby=\\"react-aria-0-4 react-aria-0-6\\" type=\\"button\\" class=\\"sl-button sl-w-full sl-h-sm sl-text-base sl-font-normal sl-px-1.5 sl-bg-transparent sl-rounded sl-border-transparent hover:sl-border-input focus:sl-border-primary active:sl-border-primary sl-border disabled:sl-opacity-60\\"><div class=\\"sl-flex sl-flex-1 sl-justify-items-start sl-items-center\\"><div class=\\"sl-pr-1\\">array (oneOf) [strings]</div></div><div class=\\"sl-text-xs sl--mr-0.5 sl-ml-1\\"><div class=\\"sl-pt-0.5 sl-pr-0.5\\"><svg aria-hidden=\\"true\\" focusable=\\"false\\" data-prefix=\\"fas\\" data-icon=\\"chevron-down\\" class=\\"svg-inline--fa fa-chevron-down fa-w-14 fa-xs sl-icon\\" role=\\"img\\" xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 448 512\\"><path fill=\\"currentColor\\" d=\\"M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z\\"></path></svg></div></div></button></div></div></div></div></div></div></div></div>"`,
145145
);
146146
});
147147

0 commit comments

Comments
 (0)