Skip to content

Commit

Permalink
Add react-velo types
Browse files Browse the repository at this point in the history
  • Loading branch information
yurynix committed Mar 13, 2022
1 parent 788149a commit a6311c1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
4 changes: 3 additions & 1 deletion scripts/generate-wix-module-list/extractDtsModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const extractModules = (dtsPaths: string[] = []): string[] => {
return [];
}

const skippedModules = new Set(["@wix/react-velo"]);
const modules = program
.getSourceFiles()
.filter(
Expand All @@ -56,7 +57,8 @@ const extractModules = (dtsPaths: string[] = []): string[] => {
): source is WithAmbientModuleNames => !!source.ambientModuleNames
)
.map(source => source.ambientModuleNames)
.flat();
.flat()
.filter(m => !skippedModules.has(m));

return modules;
};
Expand Down
58 changes: 58 additions & 0 deletions src_types/common/reactVelo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// <reference path="./$w.d.ts" />
/// Generic helpers
type ExtractNameFromStringWithHash<StringWithHash extends string> =
StringWithHash extends `#${infer Name}` ? Name : never;

type GenericFunction = (...args: any[]) => any;
type NoFunctions<T> = {
[P in keyof T as T[P] extends GenericFunction ? never : P]: T[P]
};
type FilterStartingWith<Set, Needle extends string> = Set extends `${Needle}${infer _X}` ? Set : never;
type EventHandlers<T> = FilterStartingWith<keyof T, 'on'>;
/// END generic helpers

type PageElementsKeys = Extract<keyof PageElementsMap, string>;
type PageElementsMapKeyType = keyof PageElementsMap;
type ConvertElementMapKeyToString<T extends string> = Extract<`#${T}`, keyof PageElementsMap>;
type ReactVeloElementsMap<ElementId extends string> = PageElementsMap[ConvertElementMapKeyToString<ElementId>];
type PageElementIds = Extract<ExtractNameFromStringWithHash<PageElementsKeys>, string>;

type ReactVeloTypeKeys<T> = Extract<keyof NoFunctions<T>, string> | EventHandlers<T>;

type ReactWixElementProp<T, Prop extends keyof T> = T[Prop];

type UnrwapReactWixElementEventHandler<T, Prop extends keyof T> =
ReactWixElementProp<T, Prop> extends GenericFunction ?
Parameters<ReactWixElementProp<T, Prop>>[0] :
ReactWixElementProp<T, Prop>;

type ReactifiedVeloType<T> = Partial<{
[key in keyof T]: UnrwapReactWixElementEventHandler<T, key>;
}> & { id: string };

type ReactWixElement<T extends PageElementIds> = Partial<{
[key in ReactVeloTypeKeys<ReactVeloElementsMap<T>>]: UnrwapReactWixElementEventHandler<ReactVeloElementsMap<T>, key>;
}>


// Repeater is a special case, because in "react" land we'll have a different API for it.
type ReactVeloRepeaterType = {
data?: any[];
renderItem: (data: any) => JSX.Element;
};

type ReactVeloOutputElementsMap = {
[key in PageElementIds]: ReactVeloElementsMap<key> extends $w.Repeater ? (props: ReactVeloRepeaterType) => JSX.Element : (props: ReactWixElement<key>) => JSX.Element;
}

type VeloTypeNames = Extract<keyof TypeNameToSdkType, string>;

type ReactVeloOutputTypesMap = {
[key in VeloTypeNames as Uncapitalize<key>]: TypeNameToSdkType[key] extends $w.Repeater ? ReactVeloRepeaterType : ReactifiedVeloType<TypeNameToSdkType[key]>
}

declare module '@wix/react-velo' {
export function render(rootElement: JSX.Element, $w: Function): void;
export const W: ReactVeloOutputElementsMap;
export const V: ReactVeloOutputTypesMap;
}
3 changes: 2 additions & 1 deletion src_types/pages/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference path="../common/anyProperties.d.ts" />
/// <reference path="../common/emptyBuffer.d.ts" />
/// <reference path="./$w.d.ts" />
/// <reference path="../common/declaration.d.ts" />
/// <reference path="../common/declaration.d.ts" />
/// <reference path="../common/reactVelo.d.ts" />

0 comments on commit a6311c1

Please sign in to comment.