All files / src Form.js

100% Statements 5/5
66.67% Branches 4/6
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                                                          2x   2x     2x   2x   2x                      
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import AuthenticityToken from './AuthenticityToken'
import classNames from 'classnames'
 
export default class Form extends Component {
  static propTypes = {
    className: PropTypes.string,
    children: PropTypes.node,
    enableMethodOverride: PropTypes.bool,
    methodOverrideInputName: PropTypes.string,
    method: PropTypes.string,
  }
 
  static defaultProps = {
    className: '',
    enableMethodOverride: true,
    methodOverrideInputName: '_method',
    method: 'post',
  }
  /* eslint complexity: [2, 4] */
  render() {
    const {
      className,
      children,
      enableMethodOverride,
      methodOverrideInputName,
      method,
      ...props
    } = this.props
 
    const formClassName = classNames(className, 'rev-Form')
 
    const overrideMethod = // only override the method name if its enabled and the method is not post or get.
      enableMethodOverride && !['get', 'post'].includes(method.toLowerCase())
 
    const actualMethod = overrideMethod ? 'post' : method
 
    return (
      <form {...props} className={formClassName} method={actualMethod}>
        <AuthenticityToken />
        {overrideMethod && (
          <input type="hidden" name={methodOverrideInputName} value={method} />
        )}
        {children}
      </form>
    )
  }
}