| 1 |
/* |
| 2 |
* Copyright (C) 2025 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import React from "react"; |
| 8 |
import { createRoot, Container } from 'react-dom/client'; |
| 9 |
|
| 10 |
import '../lib/patternfly/patternfly-6-cockpit.scss'; |
| 11 |
|
| 12 |
import { Page, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 13 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 14 |
import { show_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 15 |
|
| 16 |
import { PatternDialogBody } from "./react-demo-dialog"; |
| 17 |
import { FileAcDemo, FileAcDemoPreselected } from "./react-demo-file-autocomplete"; |
| 18 |
import { TypeaheadDemo } from "./react-demo-typeahead"; |
| 19 |
import { MultiTypeaheadDemo } from "./react-demo-multi-typeahead"; |
| 20 |
import { CardsDemo } from "./react-demo-cards"; |
| 21 |
import { UploadDemo } from "./react-demo-file-upload"; |
| 22 |
|
| 23 |
import 'cockpit-dark-theme'; // once per page |
| 24 |
import 'page.scss'; |
| 25 |
|
| 26 |
/* ----------------------------------------------------------------------------- |
| 27 |
Modal Dialog |
| 28 |
----------------------------------------------------------------------------- |
| 29 |
*/ |
| 30 |
|
| 31 |
let lastAction: string = ""; |
| 32 |
|
| 33 |
const onDialogStandardClicked = function(mode: string, progress_cb: (text: string, cancel?: () => void) => void) { |
| 34 |
lastAction = mode; |
| 35 |
|
| 36 |
return new Promise<void>((resolve, reject) => { |
| 37 |
progress_cb("Starting something long"); |
| 38 |
if (mode == 'steps') { |
| 39 |
let interval: number = 0; |
| 40 |
const cancel = function() { |
| 41 |
window.clearTimeout(interval); |
| 42 |
progress_cb("Canceling"); |
| 43 |
window.setTimeout(function() { |
| 44 |
reject(new Error("Action canceled")); |
| 45 |
}, 1000); |
| 46 |
}; |
| 47 |
let count = 0; |
| 48 |
interval = window.setInterval(function() { |
| 49 |
count += 1; |
| 50 |
progress_cb("Step " + count, cancel); |
| 51 |
}, 500); |
| 52 |
window.setTimeout(function() { |
| 53 |
window.clearTimeout(interval); |
| 54 |
resolve(); |
| 55 |
}, 5000); |
| 56 |
} else if (mode == 'reject') { |
| 57 |
reject(new Error("Some error occurred")); |
| 58 |
} else { |
| 59 |
resolve(); |
| 60 |
} |
| 61 |
}); |
| 62 |
}; |
| 63 |
|
| 64 |
const onDialogDone = function(success: boolean) { |
| 65 |
const result = success ? "successful" : "Canceled"; |
| 66 |
const action = success ? lastAction : "no action"; |
| 67 |
document.getElementById("demo-dialog-result")!.textContent = "Dialog closed: " + result + "(" + action + ")"; |
| 68 |
}; |
| 69 |
|
| 70 |
const onStandardDemoClicked = (staticError: React.ReactNode) => { |
| 71 |
const dialogProps = { |
| 72 |
title: "This shouldn't be seen", |
| 73 |
body: React.createElement(PatternDialogBody, { clickNested: onStandardDemoClicked }), |
| 74 |
static_error: staticError, |
| 75 |
}; |
| 76 |
// also test modifying properties in subsequent render calls |
| 77 |
const footerProps = { |
| 78 |
actions: [ |
| 79 |
{ |
| 80 |
clicked: onDialogStandardClicked.bind(null, 'standard action'), |
| 81 |
caption: "OK", |
| 82 |
style: 'primary', |
| 83 |
}, |
| 84 |
{ |
| 85 |
clicked: onDialogStandardClicked.bind(null, 'dangerous action'), |
| 86 |
caption: "Danger", |
| 87 |
style: 'danger', |
| 88 |
}, |
| 89 |
{ |
| 90 |
clicked: onDialogStandardClicked.bind(null, 'steps'), |
| 91 |
caption: "Wait", |
| 92 |
}, |
| 93 |
], |
| 94 |
dialog_done: onDialogDone, |
| 95 |
}; |
| 96 |
const dialogObj = show_modal_dialog(dialogProps, footerProps); |
| 97 |
// if this failed, exit (trying to create a nested dialog) |
| 98 |
if (!dialogObj) |
| 99 |
return; |
| 100 |
footerProps.actions.push( |
| 101 |
{ |
| 102 |
clicked: onDialogStandardClicked.bind(null, 'reject'), |
| 103 |
caption: "Error", |
| 104 |
style: 'primary', |
| 105 |
}); |
| 106 |
dialogObj.setFooterProps(footerProps); |
| 107 |
dialogProps.title = "Example React Dialog"; |
| 108 |
dialogObj.setProps(dialogProps); |
| 109 |
}; |
| 110 |
|
| 111 |
const ReactPatterns = () => { |
| 112 |
const narrow = { maxWidth: 600 }; |
| 113 |
|
| 114 |
return ( |
| 115 |
<Page isContentFilled className="no-masthead-sidebar"> |
| 116 |
<PageSection> |
| 117 |
<h3>Select file</h3> |
| 118 |
<div id="demo-file-ac" style={narrow}> |
| 119 |
<FileAcDemo /> |
| 120 |
</div> |
| 121 |
<div id="demo-file-ac-preselected" style={narrow}> |
| 122 |
<FileAcDemoPreselected /> |
| 123 |
</div> |
| 124 |
</PageSection> |
| 125 |
|
| 126 |
<PageSection id="demo-typeahead"> |
| 127 |
<h3>Typeahead</h3> |
| 128 |
<div style={narrow}> |
| 129 |
<TypeaheadDemo /> |
| 130 |
</div> |
| 131 |
</PageSection> |
| 132 |
|
| 133 |
<PageSection id="demo-multi-typeahead"> |
| 134 |
<h3>Multi Typeahead</h3> |
| 135 |
<div style={narrow}> |
| 136 |
<MultiTypeaheadDemo /> |
| 137 |
</div> |
| 138 |
</PageSection> |
| 139 |
|
| 140 |
<PageSection> |
| 141 |
<h3>Dialogs</h3> |
| 142 |
<Button |
| 143 |
id="demo-show-dialog" |
| 144 |
onClick={onStandardDemoClicked.bind(null, null)} |
| 145 |
> |
| 146 |
Show Dialog |
| 147 |
</Button> |
| 148 |
<Button |
| 149 |
id="demo-show-error-dialog" |
| 150 |
onClick={onStandardDemoClicked.bind(null, 'Some static error')} |
| 151 |
> |
| 152 |
Show Error-Dialog |
| 153 |
</Button> |
| 154 |
<div id="demo-dialog-result" /> |
| 155 |
</PageSection> |
| 156 |
|
| 157 |
<PageSection> |
| 158 |
<h3>Cards</h3> |
| 159 |
<CardsDemo /> |
| 160 |
</PageSection> |
| 161 |
|
| 162 |
<PageSection id="demo-upload"> |
| 163 |
<h3>Upload</h3> |
| 164 |
<div style={narrow}> |
| 165 |
<UploadDemo /> |
| 166 |
</div> |
| 167 |
</PageSection> |
| 168 |
</Page> |
| 169 |
); |
| 170 |
}; |
| 171 |
|
| 172 |
function init_app(rootElement: Container) { |
| 173 |
const root = createRoot(rootElement); |
| 174 |
root.render(<ReactPatterns />); |
| 175 |
} |
| 176 |
|
| 177 |
document.addEventListener("DOMContentLoaded", function() { |
| 178 |
cockpit.transport.wait(function() { |
| 179 |
init_app(document.getElementById('app')!); |
| 180 |
}); |
| 181 |
}); |