|
| 1 | +// This file is part of React-Invenio-Forms |
| 2 | +// Copyright (C) 2023 CERN. |
| 3 | +// |
| 4 | +// React-Invenio-Forms is free software; you can redistribute it and/or modify it |
| 5 | +// under the terms of the MIT License; see LICENSE file for more details. |
| 6 | + |
| 7 | +import { Message, Icon } from "semantic-ui-react"; |
| 8 | +import PropTypes from "prop-types"; |
| 9 | +import React, { Component } from "react"; |
| 10 | +import _isEmpty from "lodash/isEmpty"; |
| 11 | + |
| 12 | +const FieldErrorList = ({ fieldErrors }) => { |
| 13 | + return ( |
| 14 | + <Message.List> |
| 15 | + {fieldErrors.map((error) => { |
| 16 | + return ( |
| 17 | + <Message.Item key={error}> |
| 18 | + {/* when there is no field Marshmallow returns _schema */} |
| 19 | + {error.field !== "_schema" && <strong>{error.field}: </strong>} |
| 20 | + {error.messages.length === 1 ? ( |
| 21 | + error.messages[0] |
| 22 | + ) : ( |
| 23 | + <Message.List> |
| 24 | + {error.messages.map((message) => ( |
| 25 | + <Message.Item key={message}>{message}</Message.Item> |
| 26 | + ))} |
| 27 | + </Message.List> |
| 28 | + )} |
| 29 | + </Message.Item> |
| 30 | + ); |
| 31 | + })} |
| 32 | + </Message.List> |
| 33 | + ); |
| 34 | +}; |
| 35 | + |
| 36 | +FieldErrorList.propTypes = { |
| 37 | + fieldErrors: PropTypes.array.isRequired, |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * General component for error messages. |
| 42 | + * Handles: |
| 43 | + * - simple error messages: provide custom header, and content - response message of received error; |
| 44 | + * - complex error messages: provide list or received errors. |
| 45 | + * Icon and other ui props are supported. |
| 46 | + */ |
| 47 | +export class ErrorMessage extends Component { |
| 48 | + render() { |
| 49 | + const { header, errors, content, icon, ...uiProps } = this.props; |
| 50 | + |
| 51 | + return ( |
| 52 | + <Message icon {...uiProps}> |
| 53 | + {icon && <Icon name={icon} />} |
| 54 | + <Message.Content role="alert"> |
| 55 | + {header && <Message.Header>{header}</Message.Header>} |
| 56 | + {content} |
| 57 | + {!_isEmpty(errors) && <FieldErrorList fieldErrors={errors} />} |
| 58 | + </Message.Content> |
| 59 | + </Message> |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +ErrorMessage.propTypes = { |
| 65 | + /** |
| 66 | + * The list of errors (In case of multiple errors. ex: form validation result). |
| 67 | + */ |
| 68 | + errors: PropTypes.arrayOf( |
| 69 | + PropTypes.shape({ |
| 70 | + field: PropTypes.string, |
| 71 | + messages: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired, |
| 72 | + }) |
| 73 | + ), |
| 74 | + /** |
| 75 | + * Custom error header. |
| 76 | + */ |
| 77 | + header: PropTypes.string, |
| 78 | + uiProps: PropTypes.object, |
| 79 | + /** |
| 80 | + * Single error message (ex: http error response message). |
| 81 | + */ |
| 82 | + content: PropTypes.string, |
| 83 | + icon: PropTypes.string, |
| 84 | +}; |
| 85 | + |
| 86 | +ErrorMessage.defaultProps = { |
| 87 | + errors: undefined, |
| 88 | + header: undefined, |
| 89 | + uiProps: undefined, |
| 90 | + icon: undefined, |
| 91 | + content: undefined, |
| 92 | +}; |
0 commit comments