| 1 |
/* |
| 2 |
* Copyright (C) 2020 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 |
|
| 9 |
import { show_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 10 |
|
| 11 |
const _ = cockpit.gettext; |
| 12 |
|
| 13 |
export function Validated({ errors, error_key, children }) { |
| 14 |
const error = errors?.[error_key]; |
| 15 |
// We need to always render the <div> for the has-error |
| 16 |
// class so that the input field keeps the focus when |
| 17 |
// errors are cleared. Otherwise the DOM changes enough |
| 18 |
// for the Browser to remove focus. |
| 19 |
return ( |
| 20 |
<div className={error ? "ct-validation-wrapper has-error" : "ct-validation-wrapper"}> |
| 21 |
{ children } |
| 22 |
{ error ? <span className="help-block dialog-error">{error}</span> : null } |
| 23 |
</div> |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
export function has_errors(errors) { |
| 28 |
for (const field in errors) { |
| 29 |
if (errors[field]) |
| 30 |
return true; |
| 31 |
} |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
function show_error_dialog(title, message) { |
| 36 |
const props = { |
| 37 |
id: "error-popup", |
| 38 |
title, |
| 39 |
body: <p>{message}</p> |
| 40 |
}; |
| 41 |
|
| 42 |
const footer = { |
| 43 |
actions: [], |
| 44 |
cancel_button: { text: _("Close"), variant: "secondary" } |
| 45 |
}; |
| 46 |
|
| 47 |
show_modal_dialog(props, footer); |
| 48 |
} |
| 49 |
|
| 50 |
export function show_unexpected_error(error) { |
| 51 |
show_error_dialog(_("Unexpected error"), error.message || error); |
| 52 |
} |
| 53 |
|
| 54 |
export function is_valid_char_name(c) { |
| 55 |
return (c >= 'a' && c <= 'z') || |
| 56 |
(c >= 'A' && c <= 'Z') || |
| 57 |
(c >= '0' && c <= '9') || |
| 58 |
c == '.' || c == '_' || c == '-'; |
| 59 |
} |