Skip to content

Commit

Permalink
feat: Implement meta for union types
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 7, 2020
1 parent b05948b commit 77022be
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/utils/__tests__/propTypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,60 @@ describe('getTypeMeta', () => {
},
});
});

test('returns nested type info for union types', () => {
const propType = createPropType('oneOfType', [
[
createPropType('string'),
createPropType('shape', [
{
pathname: createPropType('string', undefined, {
docs: { description: 'The name of the path to link to' },
}),
search: createPropType('string'),
},
]),
],
]);

const component = {
propTypes: {
to: propType,
},
};

expect(getTypeMeta('to', propType, { component })).toEqual({
types: [
null,
{
types: [
{
name: 'pathname',
defaultValue: undefined,
description: 'The name of the path to link to',
deprecation: null,
isRequired: false,
type: {
meta: null,
raw: 'string',
name: 'string',
},
},
{
name: 'search',
defaultValue: undefined,
description: undefined,
deprecation: null,
isRequired: false,
type: {
meta: null,
raw: 'string',
name: 'string',
},
},
],
},
],
});
});
});
9 changes: 9 additions & 0 deletions src/utils/propTypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ export const getTypeMeta = (name, propType, { component }) => {
},
};
}
case 'oneOfType': {
const [types] = getArgs(propType);

return {
types: types.map((propType) =>
getTypeMeta(name, propType, { component })
),
};
}
default:
return null;
}
Expand Down

0 comments on commit 77022be

Please sign in to comment.