diff --git a/src/.npmignore b/src/.npmignore new file mode 100644 index 00000000..3753aa02 --- /dev/null +++ b/src/.npmignore @@ -0,0 +1,2 @@ +*.test.* + diff --git a/src/Playroom/Playroom.tsx b/src/Playroom/Playroom.tsx index 703a63f6..05ade235 100644 --- a/src/Playroom/Playroom.tsx +++ b/src/Playroom/Playroom.tsx @@ -22,6 +22,8 @@ import { Box } from './Box/Box'; import { assignInlineVars } from '@vanilla-extract/dynamic'; +const staticTypes = __PLAYROOM_GLOBAL__STATIC_TYPES__; + const resizableConfig = (position: EditorPosition = 'bottom') => ({ top: position === 'bottom', right: false, @@ -59,7 +61,7 @@ const getTitle = (title: string | undefined) => { }; export interface PlayroomProps { - components: Record; + components: Record>; themes: string[]; widths: number[]; snippets: Snippets; @@ -122,7 +124,7 @@ export default ({ components, themes, widths, snippets }: PlayroomProps) => { dispatch({ type: 'updateCode', payload: { code: newCode } }) } previewCode={previewEditorCode} - hints={componentsToHints(components)} + hints={componentsToHints(components, staticTypes)} /> diff --git a/src/index.d.ts b/src/index.d.ts index a81bbdc6..e45f57cd 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -32,4 +32,7 @@ interface Window { } declare const __PLAYROOM_GLOBAL__CONFIG__: InternalPlayroomConfig; -declare const __PLAYROOM_GLOBAL__STATIC_TYPES__: any; +declare const __PLAYROOM_GLOBAL__STATIC_TYPES__: Record< + string, + Record +>; diff --git a/src/utils/componentsToHints.test.ts b/src/utils/componentsToHints.test.ts new file mode 100644 index 00000000..4c34631a --- /dev/null +++ b/src/utils/componentsToHints.test.ts @@ -0,0 +1,70 @@ +import componentsToHints from './componentsToHints'; +// @ts-expect-error +import * as PropTypeComponents from '../../cypress/projects/themed/components'; +import * as TypeScriptComponents from '../../cypress/projects/typescript/components'; + +describe('componentsToHints', () => { + it('should support javascript components with proptypes', () => { + const result = componentsToHints({ + Bar: PropTypeComponents.Bar, + Foo: PropTypeComponents.Foo, + }); + + expect(result).toMatchInlineSnapshot(` + { + "Bar": { + "attrs": { + "color": [ + "red", + "blue", + ], + }, + }, + "Foo": { + "attrs": { + "color": [ + "red", + "blue", + ], + }, + }, + } + `); + }); + + it('should support typescript components when provided with type data', () => { + const result = componentsToHints( + { + Bar: TypeScriptComponents.Bar, + Foo: TypeScriptComponents.Foo, + }, + { + Bar: { color: ['red', 'blue', 'black'] }, + Foo: { color: ['red', 'blue', 'black'] }, + } + ); + + expect(result).toMatchInlineSnapshot(` + { + "Bar": { + "attrs": { + "color": [ + "red", + "blue", + "black", + ], + }, + }, + "Foo": { + "attrs": { + "color": [ + "red", + "blue", + "black", + ], + }, + }, + } + `); + }); +}); diff --git a/src/utils/componentsToHints.ts b/src/utils/componentsToHints.ts index 0e7c2f17..8d02b94a 100644 --- a/src/utils/componentsToHints.ts +++ b/src/utils/componentsToHints.ts @@ -3,9 +3,10 @@ import omit from 'lodash/omit'; import parsePropTypes from 'parse-prop-types'; import type { PlayroomProps } from '../Playroom/Playroom'; -const staticTypes = __PLAYROOM_GLOBAL__STATIC_TYPES__; - -export default (components: PlayroomProps['components']) => { +export default ( + components: PlayroomProps['components'], + staticTypes: typeof __PLAYROOM_GLOBAL__STATIC_TYPES__ = {} +) => { const componentNames = Object.keys(components).sort(); return Object.assign( diff --git a/src/utils/cursor.spec.ts b/src/utils/cursor.test.ts similarity index 100% rename from src/utils/cursor.spec.ts rename to src/utils/cursor.test.ts diff --git a/src/utils/formatting.spec.ts b/src/utils/formatting.test.ts similarity index 100% rename from src/utils/formatting.spec.ts rename to src/utils/formatting.test.ts