-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin-box.tsx
45 lines (38 loc) · 1.19 KB
/
plugin-box.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react';
import { Code } from '@chakra-ui/react';
import { useFormikContext } from 'formik';
import { PluginProvider } from '../context/plugin';
import { Plugin } from '../plugin-utils/plugin';
import { ErrorBox } from './error-box';
import { SchemaField } from '../schema/types';
interface PluginBoxProps {
plugin: Plugin;
children: ({ field }: { field: SchemaField }) => JSX.Element;
}
/**
* Prepares the plugin schema and sets up the plugin context.
* Provides the plugin schema to the children function.
*
* @param props.plugin The plugin to render
* @param props.children The children function to render the plugin schema.
*/
export function PluginBox(props: PluginBoxProps) {
const { plugin, children } = props;
const { values } = useFormikContext();
const editSchema = plugin.editSchema(values);
if (!editSchema) {
return (
<ErrorBox data-testid='plugin-box-error'>
Plugin <Code color='red'>{plugin.name}</Code> has no edit schema.
</ErrorBox>
);
}
if (editSchema === Plugin.HIDDEN) {
return null;
}
return (
<PluginProvider plugin={plugin}>
{children({ field: editSchema as SchemaField })}
</PluginProvider>
);
}