Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 45 additions & 33 deletions src/utils/componentsToHints.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,61 @@
import omit from 'lodash/omit';
import get from 'lodash/get';
import uniq from 'lodash/uniq';
// @ts-ignore
import parsePropTypes from 'parse-prop-types';
import { PlayroomProps } from '../Playroom/Playroom';

const staticTypes = __PLAYROOM_GLOBAL__STATIC_TYPES__;

export default (components: PlayroomProps['components']) => {
const componentNames = Object.keys(components).sort();
const componentNames = uniq([
...Object.keys(components),
...Object.keys(staticTypes),
]).sort();

return Object.assign(
{},
...componentNames.map((componentName) => {
const staticTypesForComponent = staticTypes[componentName];
if (
staticTypesForComponent &&
Object.keys(staticTypesForComponent).length > 0
) {
return {
[componentName]: {
attrs: staticTypesForComponent,
},
};
}
...componentNames
.map((componentName) => {
const staticTypesForComponent = staticTypes[componentName];
if (
staticTypesForComponent &&
Object.keys(staticTypesForComponent).length > 0
) {
return {
[componentName]: {
attrs: staticTypesForComponent,
},
};
}

const parsedPropTypes = parsePropTypes(components[componentName]);
const filteredPropTypes = omit(parsedPropTypes, 'children');
const propNames = Object.keys(filteredPropTypes);
const component = get(components, componentName);
if (component) {
const parsedPropTypes = parsePropTypes(component);
const filteredPropTypes = omit(parsedPropTypes, 'children');
const propNames = Object.keys(filteredPropTypes);

return {
[componentName]: {
attrs: Object.assign(
{},
...propNames.map((propName) => {
const propType = filteredPropTypes[propName].type;
return {
[componentName]: {
attrs: Object.assign(
{},
...propNames.map((propName) => {
const propType = filteredPropTypes[propName].type;

return {
[propName]:
propType.name === 'oneOf'
? propType.value.filter((x: any) => typeof x === 'string')
: null,
};
})
),
},
};
})
return {
[propName]:
propType.name === 'oneOf'
? propType.value.filter(
(x: any) => typeof x === 'string'
)
: null,
};
})
),
},
};
}
})
.filter((x) => Boolean(x))
);
};