npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

with-react-formidable

v2.1.5

Published

A small wrapper of react-router parsing the form params from the location.search

Downloads

6

Readme

with-react-formidable

A small wrapper parsing react-router location and match to provide helpful form contextual properties.

CircleCI npm version

Convention

Your app needs to work with a special react-router pathname syntax. Given the url, withFormidable will find by itself if you are in readOnly, creation or modification state. Render your component with its Route config:

  <Route
    component={Foo}
    path='/foos/:fooId([A-Za-z0-9]{2,}|creation)/:modification(modification)?'
  />

Then :

  • /foos/AE is a readOnly url, for the specific fetch of the entity foo with id=AE,
  • /foos/creation is the creation url for posting a new foo object,
  • /foos/AE/modification is the modification url for patching an already existing foo entity with id AE.

Basic usage with react-final-form and redux-thunk-data

Make your app starting at location.pathname="/foos/AE".

react old school

import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
import { Field, Form } from 'react-final-form'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import { compose } from 'redux'
import { requestData } from 'redux-thunk-data'
import withFormidable from 'with-react-formidable'

class Foo extends PureComponent {

  componentDidMount() {
    const { form, handleRequestFoo } = this.props
    const { apiPath, isCreatedEntity } = form
    if (!isCreatedEntity) {
      handleRequestFoo({ apiPath })
    }
  }

  handleActivateForm = () => {
    const { form, history } = this.props
    const { modificationUrl } = form
    history.push(modificationUrl)
  }

  handleDeactivateForm = formResolver => (state, action) => {
    const { payload } = action
    const { datum } = payload
    const { id: createdId } = datum
    const { form, history } = this.props
    const { getReadOnlyUrl } = form
    formResolver()
    history.push(getReadOnlyUrl(createdId))
  }

  handleFormSubmit = formValues => {
    const { form, handleSubmitFoo } = this.props
    const { apiPath, method } = form
    const formSubmitPromise = new Promise(resolve => {
      handleSubmitFoo({
        apiPath,
        body: { ...formValues },
        handleSuccess: this.handleDeactivateForm(resolve),
        method
      })
    })
    return formSubmitPromise
  }

  renderField = ({ input }) => {
    const { form } = this.props
    const { readOnly } = form
    return (
      <input
        {...input}
        readOnly={readOnly}
        type="text"
      />
    )
  }

  renderForm = ({ handleSubmit }) => {
    const { form } = this.props
    const { readOnly } = form
    return (
      <form onSubmit={handleSubmit}>
        <Field
          name="title"
          render={this.renderField}
        />
        {
          readOnly
          ? (
            <button
              onClick={this.handleActivateForm}
              type="button"
            >
              {'Modify'}
            </button>
          )
          : (
            <button type="submit">
              {'Save'}
            </button>
          )
        }
      </form>
    )
  }

  render () {
    const { form } = this.props
    const { readOnly } = form
    return (
      <Form
        initialValues={initialValues}
        onSubmit={this.onFormSubmit}
        render={this.renderForm}
      />
    )
  }
}

Foo.propTypes = {
  form: PropTypes.shape({
    apiPath: PropTypes.string,
    getReadOnlyUrl: PropTypes.func,
    isCreatedEntity: PropTypes.bool,
    method: PropTypes.string,
    modificationUrl: PropTypes.string,
    readOnly: PropTypes.bool
  }).isRequired,
}

const mapDispatchProps = (dispatch, ownProps) => ({
  handleRequestFoo: config => dispatch(requestData(config)),
  handleSubmitFoo: config => dispatch(requestData(config))
})

export default compose(
  withRouter,
  withFormidable,
  connect(null, mapDispatchProps)
)(Foo)

react hooks school

import PropTypes from 'prop-types'
import React, { useEffect } from 'react'
import { Field, Form } from 'react-final-form'
import { useDispatch, useSelector } from 'react-redux'
import { useLocation, useParams } from 'react-router-dom'
import { requestData } from 'redux-thunk-data'
import { useFormidable } from 'with-react-formidable'

const FooField = ({ input }) => {
  const location = useLocation()
  const params = useParams()
  const { readOnly } = useFormidable(location, params)
  return (
    <input
      {...input}
      readOnly={readOnly}
      type="text"
    />
  )
}

const FooForm = ({ handleSubmit }) => {
  const history = useHistory()
  const location = useLocation()
  const params = useParams()
  const { modificationUrl, readOnly } = useFormidable(location, params)

  const handleActivateForm = useCallback(() => {
    history.push(modificationUrl)
  }, [history, modificationUrl])


  return (
    <form onSubmit={handleSubmit}>
      <Field
        name="title"
        render={FooField}
      />
      {
        readOnly
        ? (
          <button
            onClick={handleActivateForm}
            type="button"
          >
            {'Modify'}
          </button>
        )
        : (
          <button type="submit">
            {'Save'}
          </button>
        )
      }
    </form>
  )
}


const Foo = () => {
  const dispatch = useDispatch()
  const history = useHistory()
  const location = useLocation()
  const params = useParams()
  const {
    apiPath,
    isCreatedEntity,
    getReadOnlyUrl,
    method,
    readOnly
  } = useFormidable(location, params)


  const handleFormSubmit = useCallback(formValues => {
    const formSubmitPromise = new Promise(resolve => {
      dispatch(requestData({
        apiPath,
        body: { ...formValues },
        handleSuccess: (state, action) => {
          const { payload } = action
          const { datum } = payload
          const { id: createdId } = datum
          resolve()
          history.push(getReadOnlyUrl(createdId))
        },
        method
      })})
    return formSubmitPromise
  }, [apiPath, dispatch, getReadOnlyUrl, history, method])


  useEffet(() => {
    if (isCreatedEntity) dispatch(requestData({ apiPath }))
  }, [apiPath, dispatch, isCreatedEntity])


  return (
    <Form
      initialValues={initialValues}
      onSubmit={handleFormSubmit}
      render={FooForm}
    />
  )
}

export default Foo