Skip to content

Commit

Permalink
feat: Handle oneOf when returning meta info
Browse files Browse the repository at this point in the history
jerelmiller committed Jun 7, 2020
1 parent a1e86c2 commit 650818a
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/utils/__tests__/propTypeInfo.js
Original file line number Diff line number Diff line change
@@ -360,4 +360,30 @@ describe('getTypeMeta', () => {
],
});
});

test('returns constants for enum types', () => {
const SIZE = {
SMALL: 'sm',
MEDIUM: 'md',
LARGE: 'lg',
};

const propType = createPropType('oneOf', [Object.values(SIZE)]);

const component = {
name: 'Button',
propTypes: {
size: propType,
},
SIZE,
};

expect(getTypeMeta('size', propType, { component })).toEqual({
constants: [
'Button.SIZE.SMALL',
'Button.SIZE.MEDIUM',
'Button.SIZE.LARGE',
],
});
});
});
9 changes: 9 additions & 0 deletions src/utils/propTypeInfo.js
Original file line number Diff line number Diff line change
@@ -110,6 +110,15 @@ export const getTypeMeta = (name, propType, { component }) => {
),
};
}
case 'oneOf': {
const staticProperty = toStaticPropertyName(name);

return {
constants: Object.keys(component[staticProperty] ?? {}).map(
(name) => `${component.name}.${staticProperty}.${name}`
),
};
}
default:
return null;
}

0 comments on commit 650818a

Please sign in to comment.