-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): Initial support for React UI extensions
- Loading branch information
1 parent
2425a33
commit 83d5756
Showing
18 changed files
with
339 additions
and
94 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ages/admin-ui/src/lib/core/src/providers/component-registry/component-registry.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
packages/admin-ui/src/lib/react/src/components/react-form-input.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Component, inject, OnInit } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { CustomField, FormInputComponent, INPUT_COMPONENT_OPTIONS } from '@vendure/admin-ui/core'; | ||
import { ReactComponentHostDirective } from '../react-component-host.directive'; | ||
import { ReactFormInputProps } from '../types'; | ||
|
||
@Component({ | ||
selector: 'vdr-react-form-input-component', | ||
template: ` <div [vdrReactComponentHost]="reactComponent" [props]="props"></div> `, | ||
standalone: true, | ||
imports: [ReactComponentHostDirective], | ||
}) | ||
export class ReactFormInputComponent implements FormInputComponent, OnInit { | ||
static readonly id: string = 'react-form-input-component'; | ||
readonly: boolean; | ||
formControl: FormControl; | ||
config: CustomField & Record<string, any>; | ||
|
||
protected props: ReactFormInputProps; | ||
|
||
protected reactComponent = inject(INPUT_COMPONENT_OPTIONS).component; | ||
|
||
ngOnInit() { | ||
this.props = { | ||
formControl: this.formControl, | ||
readonly: this.readonly, | ||
config: this.config, | ||
}; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/admin-ui/src/lib/react/src/components/react-route.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Component, inject, InjectionToken } from '@angular/core'; | ||
import { SharedModule } from '@vendure/admin-ui/core'; | ||
import { ReactComponentHostDirective } from '../react-component-host.directive'; | ||
|
||
export const ROUTE_COMPONENT_OPTIONS = new InjectionToken<{ | ||
component: any; | ||
title?: string; | ||
props?: Record<string, any>; | ||
}>('ROUTE_COMPONENT_OPTIONS'); | ||
|
||
@Component({ | ||
selector: 'vdr-react-route-component', | ||
template: ` | ||
<vdr-page-header> | ||
<vdr-page-title *ngIf="title" [title]="title"></vdr-page-title> | ||
</vdr-page-header> | ||
<vdr-page-body><div [vdrReactComponentHost]="reactComponent" [props]="props"></div></vdr-page-body> | ||
`, | ||
standalone: true, | ||
imports: [ReactComponentHostDirective, SharedModule], | ||
}) | ||
export class ReactRouteComponent { | ||
protected title = inject(ROUTE_COMPONENT_OPTIONS).title; | ||
protected props = inject(ROUTE_COMPONENT_OPTIONS).props; | ||
protected reactComponent = inject(ROUTE_COMPONENT_OPTIONS).component; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { ProviderToken } from '@angular/core'; | ||
import { useContext } from 'react'; | ||
import { HostedComponentContext } from '../react-component-host.directive'; | ||
|
||
export function useInjector(token: any) { | ||
export function useInjector<T = any>(token: ProviderToken<T>): T { | ||
const context = useContext(HostedComponentContext); | ||
const instance = context?.injector.get(token); | ||
if (!instance) { | ||
throw new Error(`Could not inject ${token.name ?? token.toString()}`); | ||
throw new Error(`Could not inject ${(token as any).name ?? token.toString()}`); | ||
} | ||
return instance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { APP_INITIALIZER, FactoryProvider } from '@angular/core'; | ||
import { Route } from '@angular/router'; | ||
import { ComponentRegistryService } from '@vendure/admin-ui/core'; | ||
import { ElementType } from 'react'; | ||
import { ReactFormInputComponent } from './components/react-form-input.component'; | ||
import { ReactRouteComponent, ROUTE_COMPONENT_OPTIONS } from './components/react-route.component'; | ||
|
||
export function registerReactFormInputComponent(id: string, component: ElementType): FactoryProvider { | ||
return { | ||
provide: APP_INITIALIZER, | ||
multi: true, | ||
useFactory: (registry: ComponentRegistryService) => () => { | ||
registry.registerInputComponent(id, ReactFormInputComponent, { component }); | ||
}, | ||
deps: [ComponentRegistryService], | ||
}; | ||
} | ||
|
||
export function registerReactRouteComponent(options: { | ||
component: ElementType; | ||
title?: string; | ||
breadcrumb?: string; | ||
path?: string; | ||
props?: Record<string, any>; | ||
routeConfig?: Route; | ||
}): Route { | ||
return { | ||
path: options.path ?? '', | ||
providers: [ | ||
{ | ||
provide: ROUTE_COMPONENT_OPTIONS, | ||
useValue: { | ||
component: options.component, | ||
title: options.title, | ||
props: options.props, | ||
}, | ||
}, | ||
...(options.routeConfig?.providers ?? []), | ||
], | ||
data: { | ||
breadcrumb: options.breadcrumb, | ||
...(options.routeConfig?.data ?? {}), | ||
}, | ||
...(options.routeConfig ?? {}), | ||
component: ReactRouteComponent, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// This file was generated by the build-public-api.ts script | ||
export * from './adapters'; | ||
export * from './components/react-form-input.component'; | ||
export * from './components/react-route.component'; | ||
export * from './hooks/use-form-control'; | ||
export * from './hooks/use-injector'; | ||
export * from './hooks/use-query'; | ||
export * from './providers'; | ||
export * from './react-component-host.directive'; | ||
export * from './types'; |
19 changes: 19 additions & 0 deletions
19
packages/admin-ui/src/lib/react/src/react-components/Link.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Router } from '@angular/router'; | ||
import React, { PropsWithChildren } from 'react'; | ||
import { useInjector } from '../hooks/use-injector'; | ||
|
||
export function Link(props: PropsWithChildren<{ href: string; [props: string]: any }>) { | ||
const router = useInjector(Router); | ||
const { href, ...rest } = props; | ||
|
||
function onClick(e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) { | ||
e.preventDefault(); | ||
void router.navigateByUrl(href); | ||
} | ||
|
||
return ( | ||
<a href={href} onClick={onClick} {...rest}> | ||
{props.children} | ||
</a> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/dev-server/test-plugins/experimental-ui/components/Greeter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NotificationService } from '@vendure/admin-ui/core'; | ||
import { useInjector } from '@vendure/admin-ui/react'; | ||
import React from 'react'; | ||
|
||
export function Greeter(props: { name: string }) { | ||
const notificationService = useInjector(NotificationService); | ||
|
||
function handleClick() { | ||
notificationService.success('You clicked me!'); | ||
} | ||
return ( | ||
<div className="page-block"> | ||
<h2>Hello {props.name}</h2> | ||
<button className="button primary" onClick={handleClick}> | ||
Click me | ||
</button> | ||
</div> | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/dev-server/test-plugins/experimental-ui/components/Link.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Router } from '@angular/router'; | ||
import { useInjector } from '@vendure/admin-ui/react'; | ||
import React, { PropsWithChildren } from 'react'; | ||
|
||
export function Link(props: PropsWithChildren<{ href: string; [props: string]: any }>) { | ||
const router = useInjector(Router); | ||
const { href, ...rest } = props; | ||
function onClick(e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) { | ||
e.preventDefault(); | ||
void router.navigateByUrl(href); | ||
} | ||
return ( | ||
<a href={href} onClick={onClick} {...rest}> | ||
{props.children} | ||
</a> | ||
); | ||
} |
Oops, something went wrong.