LCOV - code coverage report
Current view: top level - pkg/playground - dialog.tsx Coverage Total Hit
Test: cockpit Lines: 98.3 % 538 529
Test Date: 2026-06-17 06:28:00

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2025 Red Hat, Inc.
       3              :  *
       4              :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5              :  */
       6              : 
       7            2 : import React, { useState, useReducer } from "react";
       8            2 : import { createRoot } from 'react-dom/client';
       9            2 : import cockpit from 'cockpit';
      10              : 
      11              : import '../lib/patternfly/patternfly-6-cockpit.scss';
      12              : 
      13              : import { Page, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js";
      14              : import { Bullseye } from "@patternfly/react-core/dist/esm/layouts/Bullseye";
      15              : import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
      16              : import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox";
      17              : import { Split, SplitItem } from "@patternfly/react-core/dist/esm/layouts/Split/index.js";
      18              : import { Modal, ModalBody, ModalHeader, ModalFooter } from '@patternfly/react-core/dist/esm/components/Modal';
      19              : import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form";
      20              : import { DescriptionList, DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
      21              : import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner";
      22              : 
      23              : import { WithDialogs, useDialogs } from 'dialogs';
      24              : 
      25              : import {
      26              :     useDialogState, DialogState,
      27              :     useDialogState_async,
      28              :     DialogError,
      29              :     DialogErrorMessage,
      30              :     DialogField,
      31              :     DialogCheckbox,
      32              :     DialogTextInput,
      33              :     DialogRadioSelect,
      34              :     DialogDropdownSelect, DialogDropdownSelectObject,
      35              :     DialogHelperText,
      36              :     DialogActionButton, DialogCancelButton,
      37              : } from 'cockpit/dialog';
      38              : 
      39              : import { DialogFileChooserInput } from "cockpit/file-chooser";
      40              : 
      41              : import 'cockpit-dark-theme'; // once per page
      42              : import 'page.scss';
      43              : 
      44            2 : function List<T>({
      45            2 :     label,
      46            2 :     field,
      47            2 :     Component,
      48            2 :     init,
      49            2 : } : {
      50              :     label: string
      51              :     field: DialogField<T[]>,
      52              :     Component: ({ field } : { field: DialogField<T> }) => React.ReactNode,
      53              :     init: T,
      54            2 : }) {
      55            2 :     return (
      56            2 :         <FormGroup label={label} id={field.id()}>
      57            1 :             { field.map((f, i) => (
      58            1 :                 <Split key={i}>
      59            1 :                     <SplitItem isFilled>
      60            1 :                         <Component field={f} />
      61            1 :                     </SplitItem>
      62            1 :                     <SplitItem>
      63            1 :                         <Button
      64            1 :                             id={f.id("remove")}
      65            1 :                             variant="link"
      66            1 :                             onClick={() => field.remove(i)}
      67            1 :                         >
      68              :                             Remove
      69            1 :                         </Button>
      70            1 :                     </SplitItem>
      71            1 :                 </Split>
      72            2 :             ))}
      73            2 :             <DialogHelperText field={field} />
      74            2 :             <Button
      75            2 :                 id={field.id("add")}
      76            2 :                 variant="link"
      77            1 :                 onClick={() => field.add(init)}
      78            2 :             >
      79              :                 Add
      80            2 :             </Button>
      81            2 :         </FormGroup>
      82              :     );
      83            2 : }
      84              : 
      85            2 : const StringList = ({
      86            2 :     field,
      87            2 :     label,
      88            2 : } : {
      89              :     field: DialogField<string[]>,
      90              :     label: string
      91            2 : }) => {
      92            2 :     return (
      93            2 :         <List
      94            2 :             label={label}
      95            2 :             field={field}
      96            2 :             Component={DialogTextInput}
      97            2 :             init=""
      98            2 :         />
      99              :     );
     100            2 : };
     101              : 
     102              : interface Name {
     103              :     name: string;
     104              :     _length: number;
     105              : }
     106              : 
     107            1 : const NameInput = ({
     108            1 :     field,
     109            1 : } : {
     110              :     field: DialogField<Name>,
     111            1 : }) => {
     112            1 :     return <DialogTextInput field={field.sub("name")} />;
     113            1 : };
     114              : 
     115            1 : function validate_Name(field: DialogField<Name>, countAsyncValidation: () => void) {
     116            1 :     field.sub("name").validate_async(1000, async (n, task) => {
     117            1 :         await async_sleep(2000);
     118            1 :         countAsyncValidation();
     119            1 :         if (!task.is_cancelled())
     120            1 :             field.sub("_length").set(n.length);
     121            1 :         if (n.length % 2)
     122            1 :             return "Must have even number of characters";
     123            1 :     });
     124            1 : }
     125              : 
     126            2 : const NameList = ({
     127            2 :     field,
     128            2 :     label,
     129            2 : } : {
     130              :     field: DialogField<Name[]>,
     131              :     label: string
     132            2 : }) => {
     133            2 :     return (
     134            2 :         <List
     135            2 :             label={label}
     136            2 :             field={field}
     137            2 :             Component={NameInput}
     138            2 :             init={{ name: "", _length: 0 }}
     139            2 :         />
     140              :     );
     141            2 : };
     142              : 
     143            2 : const OptionalTextInput = ({
     144            2 :     field_label,
     145            2 :     checkbox_label,
     146            2 :     field,
     147            2 : } : {
     148              :     field_label: string,
     149              :     checkbox_label: string;
     150              :     field: DialogField<false | string>,
     151            2 : }) => {
     152            2 :     const val = field.get();
     153            2 :     let body;
     154              : 
     155            2 :     if (val === false) {
     156            2 :         body = (
     157            2 :             <Checkbox
     158            2 :                 id={field.id("checkbox")}
     159            2 :                 isChecked={false}
     160            2 :                 label={checkbox_label}
     161            1 :                 onChange={() => field.set("")}
     162            2 :             />
     163              :         );
     164            1 :     } else {
     165            1 :         body = (
     166            1 :             <>
     167            1 :                 <Checkbox
     168            1 :                     id={field.id("checkbox")}
     169            1 :                     isChecked
     170            1 :                     label={checkbox_label}
     171            1 :                     onChange={() => field.set(false)}
     172            1 :                 />
     173            1 :                 <DialogTextInput field={field.at(val)} />
     174            1 :             </>
     175              :         );
     176            1 :     }
     177              : 
     178            2 :     return (
     179            2 :         <FormGroup
     180            2 :             label={field_label}
     181              :         >
     182            2 :             {body}
     183            2 :             <DialogHelperText field={field} />
     184            2 :         </FormGroup>
     185              :     );
     186            2 : };
     187              : 
     188            1 : function async_sleep(n: number) {
     189            1 :     return new Promise(resolve => {
     190            1 :         window.setTimeout(resolve, n);
     191            1 :     });
     192            1 : }
     193              : 
     194              : interface Color {
     195              :     name: string,
     196              :     red: number,
     197              :     green: number,
     198              :     blue: number,
     199              : }
     200              : 
     201            2 : const colors: Color[] = [
     202            2 :     { name: "red", red: 1, green: 0, blue: 0 },
     203            2 :     { name: "green", red: 0, green: 1, blue: 0 },
     204            2 :     { name: "blue", red: 0, green: 0, blue: 1 },
     205            2 : ];
     206              : 
     207              : interface ExampleValues {
     208              :     flag: boolean;
     209              :     text: string;
     210              :     text2: string;
     211              :     radio: string;
     212              :     dropdown: string;
     213              :     text3: string;
     214              :     color: Color,
     215              :     list: string[];
     216              :     async: Name[];
     217              :     alternative: false | string;
     218              :     error: string;
     219              :     file: string;
     220              :     file_explanation: string;
     221              : }
     222              : 
     223            2 : const ExampleDialog = ({
     224            2 :     setResult,
     225            2 :     countAsyncValidation,
     226            2 :     countAsyncUpdate,
     227            2 :     countAsyncCancel,
     228            2 : } : {
     229              :     setResult: (values: ExampleValues) => void,
     230              :     countAsyncValidation: () => void,
     231              :     countAsyncUpdate: () => void,
     232              :     countAsyncCancel: () => void,
     233            2 : }) => {
     234            2 :     const Dialogs = useDialogs();
     235              : 
     236            2 :     const init: ExampleValues = {
     237            2 :         flag: false,
     238            2 :         text: "",
     239            2 :         text2: "",
     240            2 :         radio: "one",
     241            2 :         dropdown: "one",
     242            2 :         text3: "",
     243            2 :         color: colors[0],
     244            2 :         list: [],
     245            2 :         async: [],
     246            2 :         alternative: false,
     247            2 :         error: "none",
     248            2 :         file: "",
     249            2 :         file_explanation: "",
     250            2 :     };
     251              : 
     252            1 :     function validate(dlg: DialogState<ExampleValues>) {
     253            1 :         if (dlg.values.flag) {
     254            1 :             dlg.field("text").validate(v => {
     255            1 :                 if (!v)
     256            1 :                     return "Text can not be empty";
     257            1 :             });
     258            1 :         }
     259            1 :         if (dlg.values.dropdown == "three") {
     260            1 :             dlg.field("text3").validate_async(1000, async v => {
     261            1 :                 if (!v)
     262            1 :                     return "Can't be empty";
     263            1 :             });
     264            1 :         }
     265            1 :         dlg.field("list").forEach(v => {
     266            1 :             v.validate(vv => {
     267            1 :                 if (vv == "magic")
     268            1 :                     dlg.field("text").set("magic");
     269            1 :                 if (vv == ".")
     270            0 :                     return "No dots";
     271            1 :             });
     272            1 :         });
     273            1 :         dlg.field("async").forEach(v => validate_Name(v, countAsyncValidation));
     274            1 :         dlg.field("file").validate(v => {
     275            0 :             if (v && v[0] != "/")
     276            0 :                 return "Must be absolute";
     277            1 :         });
     278            1 :     }
     279              : 
     280            2 :     const dlg = useDialogState(init, validate).set_id_prefix("example");
     281              : 
     282            1 :     async function apply(values: ExampleValues) {
     283            1 :         setResult(values);
     284              : 
     285            1 :         if (values.error == "custom") {
     286            1 :             throw new DialogError("This is a failure", <code>1234-567-98A</code>);
     287            1 :         } else if (values.error == "from") {
     288            1 :             const err = new Error("no such file or scraper");
     289            1 :             throw DialogError.fromError("Tool not found", err);
     290            1 :         } else if (values.error == "from-random") {
     291            1 :             const err = [1, 2, 3, 4];
     292            1 :             throw DialogError.fromError("Too random", err);
     293            1 :         } else if (values.error == "message") {
     294              :             // eslint-disable-next-line no-throw-literal
     295            1 :             throw { message: "segmentation fault" };
     296            1 :         } else if (values.error == "spawn") {
     297            0 :             await cockpit.spawn(["ls", "--no-such-option"], { err: "message" });
     298            0 :         } else if (values.error == "random") {
     299              :             // eslint-disable-next-line no-throw-literal
     300            1 :             throw [1, 2, 3, 4];
     301            1 :         }
     302            1 :     }
     303              : 
     304            1 :     function update_color() {
     305            1 :         dlg.field("color").get_async(0, async (val, task) => {
     306            1 :             task.set_cancel(countAsyncCancel);
     307            1 :             await async_sleep(2000);
     308            1 :             if (!task.is_cancelled()) {
     309            1 :                 countAsyncUpdate();
     310            1 :                 dlg.field("text").set(val.name);
     311            1 :             }
     312            1 :         });
     313            1 :     }
     314              : 
     315            1 :     function update_dropdown(val: string) {
     316            1 :         dlg.field("text2").set_async(0, async () => {
     317            1 :             await async_sleep(2000);
     318            1 :             return val;
     319            1 :         });
     320            1 :     }
     321              : 
     322            1 :     function update_file(val: string) {
     323            1 :         dlg.field("file_explanation").set_async(250, async () => {
     324            1 :             if (val[0] == "/")
     325            0 :                 return cockpit.spawn(["file", "-b", val], { superuser: "try" });
     326              :             else
     327            0 :                 return "--";
     328            1 :         });
     329            1 :     }
     330              : 
     331            2 :     return (
     332            2 :         <Modal
     333            2 :             id="dialog"
     334            2 :             position="top"
     335            2 :             variant="medium"
     336            2 :             isOpen
     337            2 :             onClose={Dialogs.close}
     338              :         >
     339            2 :             <ModalHeader title="Demo" />
     340            2 :             <ModalBody>
     341            2 :                 <DialogErrorMessage dialog={dlg} />
     342            2 :                 <Form isHorizontal>
     343            2 :                     <DialogCheckbox
     344            2 :                         field_label="Checkbox"
     345            2 :                         checkbox_label="Enable text"
     346            2 :                         field={dlg.field("flag")}
     347            2 :                     />
     348            2 :                     <DialogTextInput
     349            2 :                         label="Text"
     350            2 :                         field={dlg.field("text")}
     351            2 :                         excuse={!dlg.values.flag && "Disabled"}
     352            2 :                         explanation="Explanation"
     353            1 :                         warning={dlg.values.text == "warn" ? "Warning" : null}
     354            2 :                     />
     355            2 :                     <DialogTextInput
     356            2 :                         label="Text2"
     357            2 :                         field={dlg.field("text2")}
     358            2 :                     />
     359              :                     {
     360              :                         // Calling "map" on a non-array should just do nothing.
     361            0 :                         dlg.field("text").map((v, i) => <span key={i}>{v.get()}</span>)
     362              :                     }
     363            2 :                     <DialogRadioSelect
     364            2 :                         label="Radio"
     365            2 :                         field={dlg.field("radio")}
     366            2 :                         options={
     367            2 :                             [
     368            2 :                                 {
     369            2 :                                     value: "one",
     370            2 :                                     label: "Eins",
     371            2 :                                     explanation: "One explanation"
     372            2 :                                 },
     373            2 :                                 {
     374            2 :                                     value: "two",
     375            2 :                                     label: "Zwei",
     376            2 :                                     explanation: "Two explanation",
     377            2 :                                     excuse: "disabled",
     378            2 :                                 },
     379            2 :                                 {
     380            2 :                                     value: "three",
     381            2 :                                     label: "Drei",
     382            2 :                                 },
     383            2 :                             ]
     384              :                         }
     385            2 :                     />
     386            2 :                     <DialogDropdownSelect
     387            2 :                         label="Dropdown"
     388            2 :                         field={dlg.field("dropdown", update_dropdown)}
     389            2 :                         options={
     390            2 :                             [
     391            2 :                                 { value: "one", label: "Eins" },
     392            2 :                                 { value: "two", label: "Zwei" },
     393            2 :                                 { value: "three", label: "Drei" },
     394            2 :                             ]
     395              :                         }
     396            1 :                         warning={dlg.field("dropdown").get() == "two" ? "There is a discount if you buy three." : null}
     397            2 :                     />
     398              :                     {
     399            2 :                         dlg.values.dropdown == "three" &&
     400            1 :                             <DialogTextInput label="Text3" field={dlg.field("text3")} />
     401              :                     }
     402            2 :                     <DialogDropdownSelectObject
     403            2 :                         label="DropdownObject"
     404            2 :                         field={dlg.field("color", update_color)}
     405            2 :                         options={colors}
     406            2 :                         option_label={c => c.name}
     407            2 :                     />
     408            2 :                     <StringList label="List" field={dlg.field("list")} />
     409            2 :                     <NameList label="Async" field={dlg.field("async")} />
     410            2 :                     <OptionalTextInput
     411            2 :                         field_label="Alternative"
     412            2 :                         checkbox_label="Custom value"
     413            2 :                         field={dlg.field("alternative")}
     414            2 :                     />
     415            2 :                     <DialogDropdownSelectObject
     416            2 :                         label="Error"
     417            2 :                         field={dlg.field("error")}
     418            2 :                         options={["none", "custom", "from", "from-random", "message", "spawn", "random"]}
     419            1 :                         warning={dlg.field("error").get() != "none" ? "There will be an error" : null}
     420            2 :                     />
     421            2 :                     <DialogFileChooserInput
     422            2 :                         label="File"
     423            2 :                         dialogTitle="Select a file"
     424            2 :                         filters={
     425            2 :                             [
     426            2 :                                 {
     427            2 :                                     label: "No dots",
     428            1 :                                     filter: n => !n.includes("."),
     429            2 :                                 }
     430            2 :                             ]
     431              :                         }
     432            2 :                         field={dlg.field("file", update_file)}
     433            2 :                         explanation={dlg.values.file_explanation}
     434            2 :                         superuser="try"
     435            2 :                     />
     436            2 :                 </Form>
     437            2 :             </ModalBody>
     438            2 :             <ModalFooter>
     439            2 :                 <DialogActionButton dialog={dlg} action={apply} onClose={Dialogs.close}>
     440              :                     Apply
     441            2 :                 </DialogActionButton>
     442            2 :                 <DialogCancelButton dialog={dlg} onClose={Dialogs.close} />
     443            2 :             </ModalFooter>
     444            2 :         </Modal>
     445              :     );
     446            2 : };
     447              : 
     448            2 : const ExampleButton = () => {
     449            2 :     const Dialogs = useDialogs();
     450            2 :     const [values, setValues] = useState<ExampleValues | null>(null);
     451            2 :     const [asyncValidationsBase, setAsyncValidationsBase] = useState<number>(0);
     452            1 :     const [asyncValidations, countAsyncValidation] = useReducer(x => x + 1, 0);
     453            2 :     const [asyncUpdatesBase, setAsyncUpdatesBase] = useState<number>(0);
     454            1 :     const [asyncUpdates, countAsyncUpdate] = useReducer(x => x + 1, 0);
     455            2 :     const [asyncCancelsBase, setAsyncCancelsBase] = useState<number>(0);
     456            1 :     const [asyncCancels, countAsyncCancel] = useReducer(x => x + 1, 0);
     457              : 
     458            1 :     function entry(id: string, val: string) {
     459            1 :         return (
     460            1 :             <DescriptionListGroup>
     461            1 :                 <DescriptionListTerm>{id}</DescriptionListTerm>
     462            1 :                 <DescriptionListDescription id={id}>{val}</DescriptionListDescription>
     463            1 :             </DescriptionListGroup>
     464              :         );
     465            1 :     }
     466              : 
     467            2 :     return (
     468            2 :         <>
     469            2 :             <Button
     470            2 :                 id="open"
     471            2 :                 onClick={
     472            2 :                     () => {
     473            2 :                         setAsyncValidationsBase(asyncValidations);
     474            2 :                         setAsyncUpdatesBase(asyncUpdates);
     475            2 :                         setAsyncCancelsBase(asyncCancels);
     476            2 :                         Dialogs.show(
     477            2 :                             <ExampleDialog
     478            2 :                                 setResult={setValues}
     479            2 :                                 countAsyncValidation={countAsyncValidation}
     480            2 :                                 countAsyncUpdate={countAsyncUpdate}
     481            2 :                                 countAsyncCancel={countAsyncCancel}
     482            2 :                             />
     483            2 :                         );
     484            2 :                     }
     485              :                 }
     486            2 :             >
     487              :                 Open dialog
     488            2 :             </Button>
     489            2 :             { values &&
     490            1 :                 <DescriptionList isHorizontal>
     491            1 :                     { entry("flag", String(values.flag)) }
     492            1 :                     { values.flag && entry("text", values.text) }
     493            1 :                     { entry("text2", values.text2) }
     494            1 :                     { entry("radio", values.radio) }
     495            1 :                     { entry("dropdown", values.dropdown) }
     496            1 :                     { entry("color", values.color.red + "/" + values.color.green + "/" + values.color.blue) }
     497            1 :                     { entry("list", values.list.join("/")) }
     498            1 :                     { entry("async", values.async.map(n => n.name + ":" + String(n._length)).join("/")) }
     499            1 :                     { entry("asyncVals", String(asyncValidations - asyncValidationsBase)) }
     500            1 :                     { entry("asyncUps", String(asyncUpdates - asyncUpdatesBase)) }
     501            1 :                     { entry("asyncCancels", String(asyncCancels - asyncCancelsBase)) }
     502            1 :                     { entry("alternative", JSON.stringify(values.alternative)) }
     503            1 :                 </DescriptionList>
     504              :             }
     505            2 :         </>
     506              :     );
     507            2 : };
     508              : 
     509              : interface ExampleWithInitFuncValues {
     510              :     text: string;
     511              :     text2: string;
     512              : }
     513              : 
     514            1 : const ExampleDialogWithInitFunc = () => {
     515            1 :     const Dialogs = useDialogs();
     516              : 
     517            1 :     function init(): ExampleWithInitFuncValues {
     518            1 :         return {
     519            1 :             text: "foo",
     520            1 :             text2: "bar",
     521            1 :         };
     522            1 :     }
     523              : 
     524            1 :     function validate(dlg: DialogState<ExampleWithInitFuncValues>) {
     525            1 :         dlg.top().validate(v => {
     526            1 :             if (v.text == "foo" && v.text2 != "bar") {
     527            1 :                 return {
     528            1 :                     text: "No foo without bar",
     529            1 :                 };
     530            1 :             }
     531            1 :             if (v.text2 == "bar" && v.text != "foo") {
     532            1 :                 return {
     533            1 :                     text2: { "": "No bar without foo" },
     534            1 :                 };
     535            1 :             }
     536            1 :         });
     537            1 :     }
     538              : 
     539            1 :     const dlg = useDialogState(init, validate);
     540              : 
     541            1 :     return (
     542            1 :         <Modal
     543            1 :             id="dialog"
     544            1 :             position="top"
     545            1 :             variant="medium"
     546            1 :             isOpen
     547            1 :             onClose={Dialogs.close}
     548              :         >
     549            1 :             <ModalHeader title="Demo" />
     550            1 :             <ModalBody>
     551            1 :                 <DialogErrorMessage dialog={dlg} />
     552            1 :                 <Form isHorizontal>
     553            1 :                     <DialogTextInput
     554            1 :                         label="Text"
     555            1 :                         field={dlg.field("text")}
     556            1 :                     />
     557            1 :                     <DialogTextInput
     558            1 :                         label="Text 2"
     559            1 :                         field={dlg.field("text2")}
     560            1 :                     />
     561            1 :                 </Form>
     562            1 :             </ModalBody>
     563            1 :             <ModalFooter>
     564            0 :                 <DialogActionButton dialog={dlg} action={async () => {}} onClose={Dialogs.close}>
     565              :                     Apply
     566            1 :                 </DialogActionButton>
     567            1 :                 <DialogCancelButton dialog={dlg} onClose={Dialogs.close} />
     568            1 :             </ModalFooter>
     569            1 :         </Modal>
     570              :     );
     571            1 : };
     572              : 
     573              : interface AsyncExampleValues {
     574              :     text: string;
     575              : }
     576              : 
     577            1 : const AsyncExampleDialog = ({
     578            1 :     throwError = 0,
     579            1 :     cancelCallback = null,
     580            1 : } : {
     581              :     throwError?: number,
     582              :     cancelCallback?: null | (() => void),
     583            1 : }) => {
     584            1 :     const Dialogs = useDialogs();
     585              : 
     586            1 :     async function init(): Promise<AsyncExampleValues> {
     587            1 :         if (throwError == 1)
     588            1 :             throw new Error("can't get the thing");
     589            1 :         else if (throwError == 2)
     590            1 :             throw new DialogError("Getting the thing failed", <i>can't get it</i>);
     591              : 
     592            1 :         await async_sleep(500);
     593            1 :         return {
     594            1 :             text: "",
     595            1 :         };
     596            1 :     }
     597              : 
     598            1 :     function validate(dlg: DialogState<AsyncExampleValues>) {
     599            1 :         dlg.field("text").validate_async(0, async () => {
     600            1 :             throw Error("upps");
     601            1 :         });
     602            1 :     }
     603              : 
     604            1 :     const dlg = useDialogState_async(init, validate);
     605              : 
     606            1 :     async function apply() {
     607            1 :         cockpit.assert(dlg instanceof DialogState);
     608              : 
     609            1 :         dlg.set_cancel(cancelCallback);
     610              : 
     611            1 :         await async_sleep(1000);
     612            1 :         Dialogs.close();
     613            1 :     }
     614              : 
     615            1 :     function update_top(values: AsyncExampleValues) {
     616            1 :         console.log("TOP", JSON.stringify(values));
     617            1 :     }
     618              : 
     619            1 :     let body;
     620            1 :     if (!dlg) {
     621            1 :         body = (
     622            1 :             <Bullseye>
     623            1 :                 <Spinner />
     624            1 :             </Bullseye>
     625              :         );
     626            1 :     } else if (dlg instanceof DialogError) {
     627            1 :         body = null;
     628            1 :     } else if (dlg instanceof DialogState) {
     629            1 :         const vals = dlg.top(update_top);
     630            1 :         body = (
     631            1 :             <Form isHorizontal>
     632            1 :                 <DialogTextInput label="Text" field={vals.sub("text")} />
     633            1 :             </Form>
     634              :         );
     635            1 :     }
     636              : 
     637            1 :     return (
     638            1 :         <Modal
     639            1 :             id="dialog"
     640            1 :             position="top"
     641            1 :             variant="medium"
     642            1 :             isOpen
     643            1 :             onClose={Dialogs.close}
     644              :         >
     645            1 :             <ModalHeader title="Async Demo" />
     646            1 :             <ModalBody>
     647            1 :                 <DialogErrorMessage dialog={dlg} />
     648            1 :                 { body }
     649            1 :             </ModalBody>
     650            1 :             <ModalFooter>
     651            1 :                 <DialogActionButton dialog={dlg} action={apply}>
     652              :                     Apply
     653            1 :                 </DialogActionButton>
     654            1 :                 <DialogCancelButton dialog={dlg} onClose={Dialogs.close} />
     655            1 :             </ModalFooter>
     656            1 :         </Modal>
     657              :     );
     658            1 : };
     659              : 
     660            2 : const SimpleExampleButtons = () => {
     661            2 :     const Dialogs = useDialogs();
     662            2 :     const [cancelled, setCancelled] = useState(false);
     663              : 
     664            2 :     return (
     665            2 :         <>
     666            2 :             <Button
     667            2 :                 id="open-with-func"
     668            1 :                 onClick={() => Dialogs.show(<ExampleDialogWithInitFunc />)}
     669            2 :             >
     670              :                 Open init-func dialog
     671            2 :             </Button>
     672            2 :             <Button
     673            2 :                 id="open-async"
     674            2 :                 onClick={
     675            1 :                     () => {
     676            1 :                         setCancelled(false);
     677            1 :                         Dialogs.show(<AsyncExampleDialog cancelCallback={() => setCancelled(true)} />);
     678            1 :                     }
     679              :                 }
     680            2 :             >
     681              :                 Open async dialog
     682            2 :             </Button>
     683            2 :             <div id="cancelled">
     684            1 :                 Cancelled: {cancelled ? "yes" : "no"}
     685            2 :             </div>
     686            2 :             <Button
     687            2 :                 id="open-error"
     688            1 :                 onClick={() => Dialogs.show(<AsyncExampleDialog throwError={1} />)}
     689            2 :             >
     690              :                 Open Error dialog
     691            2 :             </Button>
     692            2 :             <Button
     693            2 :                 id="open-dialog-error"
     694            1 :                 onClick={() => Dialogs.show(<AsyncExampleDialog throwError={2} />)}
     695            2 :             >
     696              :                 Open DialogError dialog
     697            2 :             </Button>
     698            2 :         </>
     699              :     );
     700            2 : };
     701              : 
     702            2 : const Demo = () => {
     703            2 :     return (
     704            2 :         <WithDialogs>
     705            2 :             <Page isContentFilled className="no-masthead-sidebar">
     706            2 :                 <PageSection>
     707            2 :                     <ExampleButton />
     708            2 :                     <SimpleExampleButtons />
     709            2 :                 </PageSection>
     710            2 :             </Page>
     711            2 :         </WithDialogs>
     712              :     );
     713            2 : };
     714              : 
     715            2 : document.addEventListener("DOMContentLoaded", function() {
     716            2 :     window.debugging = "dialog";
     717            2 :     createRoot(document.getElementById('app')!).render(<Demo />);
     718            2 : });
        

Generated by: LCOV version 2.0-1