Line data Source code
1 : /*
2 : * Copyright (C) 2020 Red Hat, Inc.
3 : * SPDX-License-Identifier: LGPL-2.1-or-later
4 : */
5 :
6 19 : import cockpit from 'cockpit';
7 19 : import React from 'react';
8 :
9 : import { show_modal_dialog } from "cockpit-components-dialog.jsx";
10 :
11 19 : 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 8 : export function has_errors(errors) {
28 6 : for (const field in errors) {
29 6 : if (errors[field])
30 4 : return true;
31 6 : }
32 8 : return false;
33 8 : }
34 :
35 0 : function show_error_dialog(title, message) {
36 0 : const props = {
37 0 : id: "error-popup",
38 0 : title,
39 0 : body: <p>{message}</p>
40 0 : };
41 :
42 0 : const footer = {
43 0 : actions: [],
44 0 : cancel_button: { text: _("Close"), variant: "secondary" }
45 0 : };
46 :
47 0 : show_modal_dialog(props, footer);
48 0 : }
49 :
50 0 : export function show_unexpected_error(error) {
51 0 : show_error_dialog(_("Unexpected error"), error.message || error);
52 0 : }
53 :
54 6 : export function is_valid_char_name(c) {
55 6 : return (c >= 'a' && c <= 'z') ||
56 0 : (c >= 'A' && c <= 'Z') ||
57 1 : (c >= '0' && c <= '9') ||
58 2 : c == '.' || c == '_' || c == '-';
59 6 : }
|