Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support sub-components #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 45 additions & 33 deletions src/utils/componentsToHints.ts
@@ -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))
);
};