-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability for consumers to render custom components (#86)
* Add the ability for consumers to render custom components
- Loading branch information
James Woo
authored
Jul 19, 2021
1 parent
526f863
commit 34c4142
Showing
7 changed files
with
165 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
import {memo} from 'react'; | ||
import {KIND_COMPONENT, KIND_TEXT, RemoteReceiver} from '@remote-ui/core'; | ||
|
||
import type {Controller} from './controller'; | ||
import type {Controller} from './types'; | ||
import {useAttached} from './hooks'; | ||
import {RemoteText} from './RemoteText'; | ||
import {RemoteComponent} from './RemoteComponent'; | ||
|
||
interface Props { | ||
export interface RemoteRendererProps { | ||
receiver: RemoteReceiver; | ||
controller: Controller; | ||
} | ||
|
||
export const RemoteRenderer = memo(({controller, receiver}: Props) => { | ||
const {children} = useAttached(receiver, receiver.attached.root)!; | ||
export const RemoteRenderer = memo( | ||
({controller, receiver}: RemoteRendererProps) => { | ||
const {children} = useAttached(receiver, receiver.attached.root)!; | ||
const {renderComponent, renderText} = controller.renderer; | ||
|
||
return ( | ||
<> | ||
{children.map((child) => { | ||
switch (child.kind) { | ||
case KIND_COMPONENT: | ||
return ( | ||
<RemoteComponent | ||
key={child.id} | ||
component={child} | ||
receiver={receiver} | ||
controller={controller} | ||
__type__={(controller.get(child.type) as any)?.__type__} | ||
/> | ||
); | ||
case KIND_TEXT: | ||
return ( | ||
<RemoteText key={child.id} text={child} receiver={receiver} /> | ||
); | ||
default: | ||
return null; | ||
} | ||
})} | ||
</> | ||
); | ||
}); | ||
return ( | ||
<> | ||
{children.map((child) => { | ||
switch (child.kind) { | ||
case KIND_COMPONENT: | ||
return renderComponent({ | ||
component: child, | ||
receiver, | ||
controller, | ||
key: child.id, | ||
}); | ||
case KIND_TEXT: | ||
return renderText({ | ||
text: child, | ||
receiver, | ||
key: child.id, | ||
}); | ||
default: | ||
return null; | ||
} | ||
})} | ||
</> | ||
); | ||
}, | ||
); |
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,17 +1,13 @@ | ||
import {memo} from 'react'; | ||
import type { | ||
RemoteReceiver, | ||
RemoteReceiverAttachableText, | ||
} from '@remote-ui/core'; | ||
|
||
import type {RemoteTextProps} from './types'; | ||
import {useAttached} from './hooks'; | ||
|
||
interface Props { | ||
text: RemoteReceiverAttachableText; | ||
receiver: RemoteReceiver; | ||
export function renderText({text, receiver, key}: RemoteTextProps) { | ||
return <RemoteText key={key} text={text} receiver={receiver} />; | ||
} | ||
|
||
export const RemoteText = memo(({text, receiver}: Props) => { | ||
export const RemoteText = memo(({text, receiver}: RemoteTextProps) => { | ||
const attached = useAttached(receiver, text); | ||
return attached ? <>{attached.text}</> : null; | ||
}); |
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
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,45 @@ | ||
import type {ComponentType, ReactNode} from 'react'; | ||
import type { | ||
RemoteReceiver, | ||
RemoteReceiverAttachableComponent, | ||
RemoteReceiverAttachableText, | ||
RemoteComponentType, | ||
RemoteReceiverAttachableFragment, | ||
} from '@remote-ui/core'; | ||
|
||
export interface RemoteTextProps { | ||
text: RemoteReceiverAttachableText; | ||
receiver: RemoteReceiver; | ||
key: string | number; | ||
} | ||
|
||
export interface RemoteComponentProps { | ||
receiver: RemoteReceiver; | ||
component: RemoteReceiverAttachableComponent; | ||
controller: Controller; | ||
key: string | number; | ||
} | ||
|
||
export interface RemoteFragmentProps { | ||
receiver: RemoteReceiver; | ||
fragment: RemoteReceiverAttachableFragment; | ||
controller: Controller; | ||
} | ||
|
||
export interface Controller { | ||
get(type: string | RemoteComponentType<string, any, any>): ComponentType<any>; | ||
renderer: Renderer; | ||
} | ||
|
||
export interface Renderer { | ||
renderComponent(props: RemoteComponentProps): ReactNode; | ||
renderText(props: RemoteTextProps): ReactNode; | ||
} | ||
|
||
export interface RenderComponentOptions { | ||
renderDefault(): ReactNode; | ||
} | ||
|
||
export interface RenderTextOptions { | ||
renderDefault(): ReactNode; | ||
} |