Skip to content

Commit

Permalink
fix: Sorting OneOfField options / better default type (#4673)
Browse files Browse the repository at this point in the history
* Sorting OneOfField options based on type weights

* Fix test

* fix test

Co-authored-by: Geoff Cox (Microsoft) <gcox@microsoft.com>
  • Loading branch information
LouisEugeneMSFT and GeoffCoxMSFT committed Nov 5, 2020
1 parent 25f709d commit 0b5edb7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ describe('getOptions', () => {
it('returns all of the types, sorted', () => {
const { options } = getOptions(schema, {});
expect(options).toEqual([
makeOption(schema, 'boolean'),
makeOption(schema, 'number'),
makeOption(schema, 'string'),
makeOption(schema, 'number'),
makeOption(schema, 'boolean'),
]);
});
});
Expand Down Expand Up @@ -71,13 +71,13 @@ describe('getOptions', () => {
const { options } = getOptions(schema, definitions);
const optionKeys = options.map((o) => o.key);
expect(optionKeys).toEqual([
'my awesome string',
'boolean',
'string',
'number',
'boolean',
'my awesome string',
'an enum',
'dropdown',
'another type',
'string',
'unknown',
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ function getOptionLabel(schema: JSONSchema7): string {
return type || 'unknown';
}

const typePriorityWeights = {
string: 5,
number: 4,
boolean: 3,
array: 2,
object: 1,
};

const sortOptionsByTypeWeights = ({ key: type1 }, { key: type2 }): number => {
return (typePriorityWeights[type1] || 0) > (typePriorityWeights[type2] || 0) ? -1 : 1;
};

export function getOptions(
schema: JSONSchema7,
definitions?: SchemaDefinitions
Expand All @@ -40,7 +52,7 @@ export function getOptions(
data: { schema: { ...schema, type: t }, icon: getFieldIconText(t) },
}));

options.sort(({ text: t1 }, { text: t2 }) => (t1 > t2 ? 1 : -1));
options.sort(sortOptionsByTypeWeights);

return { options, isNested };
}
Expand All @@ -64,6 +76,8 @@ export function getOptions(
})
.filter(Boolean) as IDropdownOption[];

options.sort(sortOptionsByTypeWeights);

const expression = (resolvedOneOf as JSONSchema7[]).find(({ $role }) => $role === 'expression');
const merged = merge({}, omit(schema, 'oneOf'), expression);

Expand Down

0 comments on commit 0b5edb7

Please sign in to comment.