-
-
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): Support for React-based custom detail components
- Loading branch information
1 parent
c588a1f
commit 55d9ffc
Showing
7 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
packages/admin-ui/src/lib/react/src/components/react-custom-detail.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,39 @@ | ||
import { Component, inject, InjectionToken, OnInit, ViewEncapsulation } from '@angular/core'; | ||
import { FormGroup, UntypedFormGroup } from '@angular/forms'; | ||
import { CustomDetailComponent } from '@vendure/admin-ui/core'; | ||
import { ElementType } from 'react'; | ||
import { Observable } from 'rxjs'; | ||
import { ReactComponentHostDirective } from '../react-component-host.directive'; | ||
|
||
export const REACT_CUSTOM_DETAIL_COMPONENT_OPTIONS = new InjectionToken<{ | ||
component: ElementType; | ||
props?: Record<string, any>; | ||
}>('REACT_CUSTOM_DETAIL_COMPONENT_OPTIONS'); | ||
|
||
export interface ReactCustomDetailComponentContext { | ||
detailForm: FormGroup; | ||
entity$: Observable<any>; | ||
} | ||
|
||
@Component({ | ||
selector: 'vdr-react-custom-detail-component', | ||
template: ` <div [vdrReactComponentHost]="reactComponent" [context]="context" [props]="props"></div> `, | ||
styleUrls: ['./react-global-styles.scss'], | ||
encapsulation: ViewEncapsulation.None, | ||
standalone: true, | ||
imports: [ReactComponentHostDirective], | ||
}) | ||
export class ReactCustomDetailComponent implements CustomDetailComponent, OnInit { | ||
detailForm: UntypedFormGroup; | ||
entity$: Observable<any>; | ||
protected props = inject(REACT_CUSTOM_DETAIL_COMPONENT_OPTIONS).props ?? {}; | ||
protected reactComponent = inject(REACT_CUSTOM_DETAIL_COMPONENT_OPTIONS).component; | ||
protected context: ReactCustomDetailComponentContext; | ||
|
||
ngOnInit() { | ||
this.context = { | ||
detailForm: this.detailForm, | ||
entity$: this.entity$, | ||
}; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/admin-ui/src/lib/react/src/hooks/use-detail-component-data.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,56 @@ | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { ReactCustomDetailComponentContext } from '../components/react-custom-detail.component'; | ||
import { HostedComponentContext } from '../react-component-host.directive'; | ||
import { HostedReactComponentContext } from '../types'; | ||
|
||
/** | ||
* @description | ||
* Provides the data available to React-based CustomDetailComponents. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Card, useDetailComponentData } from '@vendure/admin-ui/react'; | ||
* import React from 'react'; | ||
* | ||
* export function CustomDetailComponent(props: any) { | ||
* const { entity, detailForm } = useDetailComponentData(); | ||
* const updateName = () => { | ||
* detailForm.get('name')?.setValue('New name'); | ||
* detailForm.markAsDirty(); | ||
* }; | ||
* return ( | ||
* <Card title={'Custom Detail Component'}> | ||
* <button className="button" onClick={updateName}> | ||
* Update name | ||
* </button> | ||
* <pre>{JSON.stringify(entity, null, 2)}</pre> | ||
* </Card> | ||
* ); | ||
* } | ||
* ``` | ||
* | ||
* @docsCategory react-hooks | ||
*/ | ||
export function useDetailComponentData() { | ||
const context = useContext( | ||
HostedComponentContext, | ||
) as HostedReactComponentContext<ReactCustomDetailComponentContext>; | ||
|
||
if (!context.detailForm || !context.entity$) { | ||
throw new Error(`The useDetailComponentData hook can only be used within a CustomDetailComponent`); | ||
} | ||
|
||
const [entity, setEntity] = useState(null); | ||
|
||
useEffect(() => { | ||
const subscription = context.entity$.subscribe(value => { | ||
setEntity(value); | ||
}); | ||
return () => subscription.unsubscribe(); | ||
}, []); | ||
|
||
return { | ||
entity, | ||
detailForm: context.detailForm, | ||
}; | ||
} |
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
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