Skip to content

useConfigureWidgetField

@wuespace/telestion / widget / useConfigureWidgetField

useConfigureWidgetField<T>(name, validator): readonly [T, (newValue) => void]

A hook to get and set a specific field of the current widget configuration.

Only works inside a widget configuration context. Values returned and passed into the setter are always validated and transformed by the widget’s Widget.createConfig function.

To validate the type of the individual field, the validator function is used.

Type parameters

T extends Json

Parameters

name: string

the name of the field to get and set

validator: (v) => T

a function to validate the type of the field

Returns

readonly [T, (newValue) => void]

the current value of the field and a function to update it

See

useConfigureWidget

Throws

Error - if the field does not exist in the widget configuration

Throws

Error - if the type of the field does not match the validator

Example

// Config: { text: string }
const [text, setText] = useConfigureWidgetField('text', s => z.string().parse(s));

return <input value={text} onChange={e => setText(e.target.value)} />;