react-formal
Version:
Classy HTML form management for React
54 lines • 2.07 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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, { useCallback } from 'react';
import notify from './utils/notify';
import useFormSubmit from './useFormSubmit';
/**
* A Form submit button, for triggering validations for the entire form or specific fields.
*
* @memberof Form
*/
function Submit(props) {
const {
onClick,
triggers,
as: Component = 'button'
} = props,
rest = _objectWithoutPropertiesLoose(props, ["onClick", "triggers", "as"]);
const [submit] = useFormSubmit({
triggers
});
const handleClick = useCallback((...args) => {
notify(onClick, args);
submit(args);
}, [onClick, submit]);
return /*#__PURE__*/React.createElement(Component, _extends({}, rest, {
onClick: handleClick,
type: triggers && triggers.length ? 'button' : 'submit'
}));
}
Submit.propTypes = {
/**
* Specify particular fields to validate in the related form. If empty the entire form will be validated.
*/
triggers: PropTypes.arrayOf(PropTypes.string.isRequired),
/**
* Control the rendering of the Form Submit component when not using
* the render prop form of `children`.
*
* ```jsx static
* <Form.Submit as={MyButton}>
* Submit
* </Form.Submit>
* ```
*/
as: PropTypes.elementType,
/**
* A string or array of event names that trigger validation.
*
* @default 'onClick'
*/
onClick: PropTypes.func
};
export default Submit;