Skip to content

Latest commit

 

History

History
149 lines (112 loc) · 5.79 KB

WIDGETS.md

File metadata and controls

149 lines (112 loc) · 5.79 KB

STAC-Manager 📡 📄 — WIDGETS

The app will render a default widget for each field type, but this can be changed by adding a ui:widget property to the field.

There are different widgets for different field types. The existent widgets can be found in data-widgets/config and the WidgetRenderer is the component responsible for rendering the widgets based on the field type and associated conditions.

Existent widgets

Below is a list of the default widgets and the field types they are used for.

text

Source :: Default for string fields.

String

number

Source :: Default for number fields. Has the same appearance as a text widget but with a different data type.

radio

Source :: Default for string fields with an enum property.

checkbox

Source :: Default for array fields of string items with an enum property.

array

Source :: Default for array fields.
This widget doesn't render any input field, but works in tandem with the type defined in its items object to render the correct widget for each one.

Array

array:string

Source :: Default for array fields of string items.
This widget is an special case of the array widget, where the items object is of type string.

String label

select

Source

The select widget can be used for string fields with an enum property to select a single value from a list.

Array enum select

Or with an array fields of string items with an enum property to select multiple values from a list.

Array enum select

tagger

Source
The tagger widget render a select like input that allows the user to select an option from the list or add a new one.

It can be used for string fields with an enum property: String tagger

Or with array fields of string items where it allows the user to add multiple strings without the need of multiple inputs like with the array:string widget. Array Tagger

Or event with array fields of string items with an enum property: Array enum tagger

object

Source :: Default for object fields.
Much like the array widget, this one doesn't render any input field. It groups the fields defined in its properties object while allowing the user to add new properties if enabled.

Object Image show the object widget with the additionalProperties enabled and two string fields as properties.

json

Source
Renders a JSON editor for any data type. Provides some basic validation, formatting, and undo/redo functionality.

JSON

Creating a new widget

The widgets are powered by Formik so it is important to get familiar with it before creating a new widget. Although not mandatory, it is recommended to use Chakra UI v2 for the UI components.

In essence a widget is a React component that receives the following props:

  • pointer - The field name which is used by Formik to identify the field.
  • field - The field object from the schema.
  • isRequired - Whether the field is required or not.

The form related props are available through the Formik methods. To ensure good performance it is recommended to use Formik's FastField component to wrap the widget.

Example of a simple widget for a string field:

import React from 'react';
import {
  FormControl,
  FormErrorMessage,
  FormLabel,
  Input
} from '@chakra-ui/react';
import { FastField, FastFieldProps } from 'formik';
import { SchemaFieldString, WidgetProps } from '@stac-manager/data-core';

export function WidgetInput(props: WidgetProps) {
  const { pointer, isRequired } = props;
  const field = props.field as SchemaFieldString;

  return (
    <FastField name={pointer}>
      {({
        field: { value, onBlur },
        meta,
        form: { setFieldValue }
      }: FastFieldProps) => (
        <FormControl
          isRequired={isRequired}
          isInvalid={!!(meta.touched && meta.error)}
        >
          <FormLabel>{field.label}</FormLabel>
          <Input
            type='text'
            name={pointer}
            value={value === null ? '' : value}
            onBlur={onBlur}
            onChange={(e) => {
              setFieldValue(pointer, e.target.value);
            }}
          />
          <FormErrorMessage>{meta.error}</FormErrorMessage>
        </FormControl>
      )}
    </FastField>
  );
}

After the widget is created it should be added to the plugin configuration. See README for the config details.