UNPKG

react-formal

Version:

Classy HTML form management for React

67 lines (66 loc) 2 kB
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import PropTypes from 'prop-types'; import React from 'react'; import Message from './Message'; /** * Display all Form validation `errors` in a single summary list. * * ```jsx static * <Form * schema={modelSchema} * defaultValue={modelSchema.default()} * > * <Form.Summary/> * * <Form.Field name='name.first' placeholder='first'/> * <Form.Field name='name.last' placeholder='surname'/> * <Form.Field name='dateOfBirth' placeholder='dob'/> * * <Form.Submit>Validate</Form.Submit> * </Form> * ``` * * @memberof Form */ class Summary extends React.PureComponent { render() { let _this$props = this.props, { formatMessage } = _this$props, props = _objectWithoutPropertiesLoose(_this$props, ["formatMessage"]); return /*#__PURE__*/React.createElement(Message, props, errors => errors.map(formatMessage)); } } Summary.propTypes = { /** * An error message renderer, Should return a `ReactElement` * ```ts static * function( * message: string, * idx: number, * errors: array * ): ReactElement * ``` */ formatMessage: PropTypes.func.isRequired, /** * A DOM node tag name or Component class the Message should render as. */ as: PropTypes.elementType.isRequired, /** * A css class that should be always be applied to the Summary container. */ errorClass: PropTypes.string, /** * Specify a group to show errors for, if empty all form errors will be shown in the Summary. */ group: PropTypes.string }; Summary.defaultProps = { as: 'ul', formatMessage: (message, idx) => /*#__PURE__*/React.createElement("li", { key: idx }, message) }; export default Summary;