{"version":3,"file":"mod-widget.cjs","sources":["../src/lib/widget/configuration/hooks.tsx","../src/lib/widget/configuration/index.tsx"],"sourcesContent":["import { BaseWidgetConfiguration } from './model.tsx';\nimport { useConfigureWidget } from './configuration-context.tsx';\nimport { SetStateAction, useMemo } from 'react';\n\n/**\n * A hook to get and set a specific field of the current widget configuration.\n *\n * Only works inside a widget configuration context. Values returned and passed\n * into the setter are always validated and transformed by the widget's\n * {@link Widget.createConfig} function.\n *\n * To validate the type of the individual field, the `validator` function is used.\n *\n * @param name - the name of the field to get and set\n * @param validator - a function to validate the type of the field\n *\n * @see useConfigureWidget\n *\n * @returns the current value of the field and a function to update it\n * @throws Error - if the field does not exist in the widget configuration\n * @throws Error - if the type of the field does not match the validator\n *\n * @example Basic usage\n * ```ts\n * // Config: { text: string }\n * const [text, setText] = useConfigureWidgetField('text', s => z.string().parse(s));\n *\n * return <input value={text} onChange={e => setText(e.target.value)} />;\n * ```\n */\nexport function useConfigureWidgetField<\n\tT extends BaseWidgetConfiguration[string]\n>(name: string, validator: (v: unknown) => T) {\n\tconst [widgetConfiguration, setValue] = useConfigureWidget();\n\treturn useMemo(() => {\n\t\tconst onSetValue = (newValue: SetStateAction<T>) =>\n\t\t\tsetValue(oldWidgetConfiguration => {\n\t\t\t\ttry {\n\t\t\t\t\tif (typeof newValue === 'function')\n\t\t\t\t\t\tnewValue = newValue(validator(oldWidgetConfiguration[name]));\n\t\t\t\t\tnewValue = validator(newValue);\n\t\t\t\t\treturn { ...oldWidgetConfiguration, [name]: newValue };\n\t\t\t\t} catch (e) {\n\t\t\t\t\tif (e instanceof Error)\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`Type error while trying to set widget configuration field \"${name}\". Details: ${e.message}`\n\t\t\t\t\t\t);\n\t\t\t\t\telse throw e;\n\t\t\t\t}\n\t\t\t});\n\n\t\ttry {\n\t\t\tconst validatedField = validator(widgetConfiguration[name]);\n\t\t\treturn [validatedField, onSetValue] as const;\n\t\t} catch (e) {\n\t\t\tif (e instanceof Error)\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Widget configuration does not contain a property named \"${name}\". Please adjust your createConfig function. Details: ${e.message}`\n\t\t\t\t);\n\t\t\telse throw e;\n\t\t}\n\t}, [name, validator, widgetConfiguration, setValue]);\n}\n","import { z } from 'zod';\nimport { FormCheck, FormControl, FormGroup, FormLabel } from 'react-bootstrap';\nimport { useConfigureWidgetField } from './hooks.tsx';\nimport { ReactNode } from 'react';\n\nexport type { BaseWidgetConfiguration } from './model.tsx';\nexport * from './hooks.tsx';\nexport { useConfigureWidget } from './configuration-context.tsx';\n\n// Helper components\n\n/**\n * Wraps the widget configuration controls and gives them the correct margins.\n *\n * Should be used inside the widget configuration element.\n *\n * @see Widget.configElement\n */\nexport function WidgetConfigWrapper({ children }: { children: ReactNode }) {\n\treturn <section className={'px-3'}>{children}</section>;\n}\n\n/**\n * A checkbox field for the widget configuration.\n * @param props - the props for the checkbox field\n *\n * @example\n * ```tsx\n * // Config: { enabled: boolean }\n * configElement: <WidgetConfigWrapper>\n *   <WidgetConfigCheckboxField label={'Enabled'} name={'enabled'} />\n * </WidgetConfigWrapper>\n * ```\n *\n * @see Widget.configElement\n */\nexport function WidgetConfigCheckboxField(props: {\n\tlabel: string;\n\tname: string;\n}) {\n\tconst [checked, setChecked] = useConfigureWidgetField(props.name, b =>\n\t\tz.boolean().parse(b)\n\t);\n\n\treturn (\n\t\t<FormGroup className={'mb-3'}>\n\t\t\t<FormCheck\n\t\t\t\tdata-name={props.name}\n\t\t\t\tlabel={props.label}\n\t\t\t\tchecked={checked}\n\t\t\t\tonChange={e => setChecked(e.target.checked)}\n\t\t\t/>\n\t\t</FormGroup>\n\t);\n}\n\n/**\n * A text field for the widget configuration.\n * @param props - the props for the text field\n *\n * @example\n * ```tsx\n * // Config: { text: string }\n * configElement: <WidgetConfigWrapper>\n *   <WidgetConfigTextField label={'Text'} name={'text'} />\n * </WidgetConfigWrapper>\n * ```\n *\n * @see Widget.configElement\n */\nexport function WidgetConfigTextField(props: { label: string; name: string }) {\n\tconst [value, setValue] = useConfigureWidgetField(props.name, s =>\n\t\tz.string().parse(s)\n\t);\n\n\treturn (\n\t\t<FormGroup className={'mb-3'}>\n\t\t\t<FormLabel>{props.label}</FormLabel>\n\t\t\t<FormControl\n\t\t\t\tdata-name={props.name}\n\t\t\t\tvalue={value}\n\t\t\t\tonChange={e => setValue(e.target.value)}\n\t\t\t/>\n\t\t</FormGroup>\n\t);\n}\n"],"names":["useConfigureWidgetField","name","validator","widgetConfiguration","setValue","useConfigureWidget","useMemo","onSetValue","newValue","oldWidgetConfiguration","e","WidgetConfigWrapper","children","jsx","WidgetConfigCheckboxField","props","checked","setChecked","b","z","FormGroup","FormCheck","WidgetConfigTextField","value","s","jsxs","FormLabel","FormControl"],"mappings":"0WA8BgB,SAAAA,EAEdC,EAAcC,EAA8B,CAC7C,KAAM,CAACC,EAAqBC,CAAQ,EAAIC,EAAmB,mBAAA,EAC3D,OAAOC,UAAQ,IAAM,CACpB,MAAMC,EAAcC,GACnBJ,EAAmCK,GAAA,CAC9B,GAAA,CACH,OAAI,OAAOD,GAAa,aACvBA,EAAWA,EAASN,EAAUO,EAAuBR,CAAI,CAAC,CAAC,GAC5DO,EAAWN,EAAUM,CAAQ,EACtB,CAAE,GAAGC,EAAwB,CAACR,CAAI,EAAGO,CAAS,QAC7CE,EAAG,CACX,MAAIA,aAAa,MACV,IAAI,MACT,8DAA8DT,CAAI,eAAeS,EAAE,OAAO,EAAA,EAEjFA,CACZ,CAAA,CACA,EAEE,GAAA,CAEI,MAAA,CADgBR,EAAUC,EAAoBF,CAAI,CAAC,EAClCM,CAAU,QAC1BG,EAAG,CACX,MAAIA,aAAa,MACV,IAAI,MACT,2DAA2DT,CAAI,yDAAyDS,EAAE,OAAO,EAAA,EAExHA,CACZ,GACE,CAACT,EAAMC,EAAWC,EAAqBC,CAAQ,CAAC,CACpD,CC5CgB,SAAAO,EAAoB,CAAE,SAAAC,GAAqC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