Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TELESTION-462 Make widgets configurable #418

Merged
merged 1 commit into from
Jan 26, 2024

Commits on Jan 26, 2024

  1. TELESTION-462 Make widgets configurable

    Uses a context to make widgets configurable. While currently, configuration is only possible in the edit dashboard page, this context can, in the future, be used to also allow editing the configuration in other places, such as within the widget itself.
    
    For convenience, components are provided for basic confiugration fields such as textfields and checkboxes. This makes configurability as easy as this:
    
    ```tsx
    {
    	...
    	configElement: (
    		<WidgetConfigWrapper>
    			<WidgetConfigCheckboxField label={'Bool value'} name={'bool'} />
    			<WidgetConfigTextField label={'Test Text'} name={'text'} />
    		</WidgetConfigWrapper>
    	)
    }
    ```
    
    It is also possible to create custom configuration fields (using `useConfigureWidgetField(name, validator)`) or even fully custom configuration UIs (using `useConfigureWidget()`). Both of these hooks return both the current configuration and a function that works the same way a `useState()`-setter works. Note that any congiuration passed into or out of the confiuration controls automatically, controlled by the context, get validated using the widget's `createConfig` function.
    
    Example of using the `useConfiugreWidgetField()` hook:
    
    ```tsx
    function WidgetConfigTextField(props: { label: string; name: string }) {
    	const [value, setValue] = useConfigureWidgetField(props.name, s =>
    		z.string().parse(s)
    	);
    
    	return (
    		<FormGroup className={'mb-3'}>
    			<FormLabel>{props.label}</FormLabel>
    			<FormControl
    				data-name={props.name}
    				value={value}
    				onChange={e => setValue(e.target.value)}
    			/>
    		</FormGroup>
    	);
    }
    ```
    
    Everything related to widget configuration can be imported from `@wuespace/telestion/widget`.
    
    Note that this also adjusts the user data to use a `Record<string, jsonSchema>` instead of a `Record<string, unknown>` as the widget instance configuration type. The `jsonSchema` implementation is taken from the zod documentation (`README.md`) wiwhere https://github.com/ggoodman is credited; thank you for this great implementation!
    pklaschka committed Jan 26, 2024
    Configuration menu
    Copy the full SHA
    c271b9e View commit details
    Browse the repository at this point in the history