UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

90 lines (89 loc) 3.02 kB
"use client"; import React from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { isTrue, makeUniqueId, extendGracefully, extendPropsWithContextInClassComponent, validateDOMAttributes, processChildren, dispatchCustomElementEvent } from "../../shared/component-helper.js"; import { createSpacingClasses } from "../space/SpacingHelper.js"; import Context from "../../shared/Context.js"; import { formRowDefaultProps, formRowPropTypes } from "../form-row/FormRow.js"; export default class FormSet extends React.PureComponent { static contextType = Context; static defaultProps = { element: 'form', no_form: false, disabled: null, skeleton: null, prevent_submit: false, on_submit: null }; static getContent(props) { return processChildren(props); } constructor(props) { super(props); this._id = props.id || makeUniqueId(); } onSubmitHandler = event => { const { prevent_submit } = this.props; if (isTrue(prevent_submit)) { event.preventDefault(); } dispatchCustomElementEvent(this, 'on_submit', { event }); }; render() { const props = extendPropsWithContextInClassComponent(this.props, FormSet.defaultProps, this.context.FormSet); const { element, locale, no_form, prevent_submit, id, className, ...rest } = props; const allowedProps = Object.entries(rest).reduce((acc, [k, v]) => { if (typeof formRowDefaultProps[k] !== 'undefined' && k !== 'id' && k !== 'children' && k !== 'label') { acc[k] = v; } return acc; }, {}); const attributes = Object.entries(rest).reduce((acc, [k, v]) => { if (typeof allowedProps[k] === 'undefined' && k !== 'children') { acc[k] = v; } return acc; }, {}); const params = { className: classnames('dnb-form-set', createSpacingClasses(this.props), className), ...attributes }; if (!isTrue(no_form)) { params.onSubmit = this.onSubmitHandler; } validateDOMAttributes(this.props, params); const content = FormSet.getContent(this.props); const providerContext = extendGracefully(this.context, { locale: locale ? locale : this.context.locale, FormRow: allowedProps }); const Element = isTrue(no_form) ? 'div' : element; return React.createElement(Context.Provider, { value: providerContext }, React.createElement(Element, params, content)); } } process.env.NODE_ENV !== "production" ? FormSet.propTypes = { element: PropTypes.string, no_form: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), disabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), skeleton: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), prevent_submit: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), ...formRowPropTypes, on_submit: PropTypes.func } : void 0; FormSet._supportsSpacingProps = true; //# sourceMappingURL=FormSet.js.map