-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add context for data communication
- Loading branch information
Showing
6 changed files
with
146 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { CodeBlockComponent } from '@antv/gpt-vis'; | ||
import { GPTVisContext, GPTVisLite, useGPTVisContext, withChartCode } from '@antv/gpt-vis'; | ||
import React, { useCallback, useMemo, useState } from 'react'; | ||
|
||
/** | ||
* 自定义代码块渲染器 | ||
*/ | ||
const MyUIRenderer: CodeBlockComponent = ({ children }) => { | ||
const context = useGPTVisContext(); | ||
console.log('context: ', context); | ||
return ( | ||
<div style={{ backgroundColor: '#f0f0f0', padding: '10px' }}> | ||
<p>{children}</p> | ||
<button onClick={context?.onClick} type="button"> | ||
click | ||
</button> | ||
</div> | ||
); | ||
}; | ||
const customRenderers = { 'my-ui': MyUIRenderer }; | ||
const components = { | ||
code: withChartCode({ | ||
languageRenderers: customRenderers, // register custom block renderer | ||
}), | ||
}; | ||
|
||
const content = ` | ||
\`\`\`my-ui | ||
my ui data ... | ||
\`\`\` | ||
`; | ||
export default () => { | ||
const [count, setCount] = useState(0); | ||
const handleClick = useCallback(() => { | ||
console.log('handleClick'); | ||
setCount((pre) => pre + 1); | ||
// do something | ||
}, []); | ||
const context = useMemo(() => ({ count: count, onClick: handleClick }), [count]); | ||
|
||
return ( | ||
<> | ||
<p>count: {count}</p> | ||
<GPTVisContext.Provider value={context}> | ||
<div> | ||
{/* other component ... */} | ||
<div> | ||
{/* other component ... */} | ||
<GPTVisLite components={components}>{content}</GPTVisLite> | ||
</div> | ||
</div> | ||
</GPTVisContext.Provider> | ||
</> | ||
); | ||
}; |
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,49 @@ | ||
import type { CodeBlockComponent } from '@antv/gpt-vis'; | ||
import { GPTVisLite, useGPTVisContext, withChartCode } from '@antv/gpt-vis'; | ||
import React, { useCallback, useMemo, useState } from 'react'; | ||
|
||
/** | ||
* 自定义代码块渲染器 | ||
*/ | ||
const MyUIRenderer: CodeBlockComponent = ({ children }) => { | ||
const context = useGPTVisContext(); | ||
console.log('context: ', context); | ||
return ( | ||
<div style={{ backgroundColor: '#f0f0f0', padding: '10px' }}> | ||
<p>{children}</p> | ||
<button onClick={context?.onClick} type="button"> | ||
click | ||
</button> | ||
</div> | ||
); | ||
}; | ||
const customRenderers = { 'my-ui': MyUIRenderer }; | ||
const components = { | ||
code: withChartCode({ | ||
languageRenderers: customRenderers, // register custom block renderer | ||
}), | ||
}; | ||
|
||
const content = ` | ||
\`\`\`my-ui | ||
my ui data ... | ||
\`\`\` | ||
`; | ||
export default () => { | ||
const [count, setCount] = useState(0); | ||
const handleClick = useCallback(() => { | ||
console.log('handleClick'); | ||
setCount((pre) => pre + 1); | ||
// do something | ||
}, []); | ||
const context = useMemo(() => ({ count: count, onClick: handleClick }), [count]); | ||
|
||
return ( | ||
<> | ||
<p>count: {count}</p> | ||
<GPTVisLite context={context} components={components}> | ||
{content} | ||
</GPTVisLite> | ||
</> | ||
); | ||
}; |
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,14 @@ | ||
import React from 'react'; | ||
|
||
type GPTVisContextValue = Record<string, any>; | ||
|
||
export const GPTVisContext = React.createContext<GPTVisContextValue>(null as any); | ||
|
||
export function useGPTVisContext<T = GPTVisContextValue>() { | ||
const context = React.useContext(GPTVisContext); | ||
if (context === null) { | ||
throw new Error(`useGPTVisContext must be used within a GPTVisContext.Provider`); | ||
} | ||
|
||
return context as T; | ||
} |
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