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

formik-enhancer

v1.0.8

Published

Formik generator

Downloads

53

Readme

formik-enhancer

Latest Stable Version License JavaScript Style Guide npm type definitions Code Climate maintainability

Cut the boilerplate, keep what matters.

Instalation

yarn add formik-enhancer

For validations, install Yup

yarn add yup

Usage

import formikEnhancer from 'formik-enhancer'

formikEnhancer({ config, values, schema })

formik-enhancer sets enableReinitialize to true by default. If you have external things that can change your form values, this will keep it up-to-date.

config: Record<string, any>

| Prop | Description | Default | | ---------- |-------------| --------| | onSubmit | [REQUIRED] Form submit method. Receives current form values as parameter. | None | | children | Any React component you want to render. Receives Formik props, so you can do whatever you want. | None |

values?: Record<string, any>

An object containing new values for the form. If you want to load data from an API or something like that, this is your guy.Values inside this prop will overwrite initialValue from schema fields.

schema: Array<Record<string, any>>

generic props:

| Prop | Description | Default | | -------------- |-------------| --------| | name | [REQUIRED] Field name. formik-enhancer will use it to keep track of everything. | None | | type | Input type. Can be text or select (more types coming soon). In the web, you can use password or email too. | text | | placeholder | Text to display while your input dont have a value. | None | | mask | Custom mask function. See examples below. | None | | initialValue | Initial input value. | '' | | validation | Check Yup docs | None | | component | [REQUIRED] As the name indicates, the input component you want to render. | None | | blurEvent | Custom onBlur event. Receives an object of useful props (name, value, error, touched, setFieldValue, setFieldError and setFieldTouched). | None | | changeEvent | Custom onChange event. Receives the input value as parameter. | None |

native-only props:

| Prop | Description | Default | | -------------- |-------------| --------| | secure | Boolean that represents if it's an secure field (for passwords, etc). | false |

select type props:

| Prop | Description | Default | | -------------- |-------------| --------| | items | [REQUIRED] An Array of possible options for your select input. Should have at least two keys, label and value, e.g. { label: 'Foo', value: 'foo_option' } | None | | renderItem | [REQUIRED] A function that returns a component (more details below). | None |

//renderItem will always receive two props: `option` and `index`
// `option` is one of `items` entry
// `index` is the index of `option` in `items`

// React
const _renderItem = (option, index) => {
  return <option key={index} value={option.value}>{option.label}</option>
}

// React Native
const _renderItem = (option, index) => {
  return <Picker.Item key={index} label={option.label} value={option.value} />
}

Besides this props, you can pass anything you want as a prop. Just be sure to keep track of what you doing.

Every field will receive two extra props: error and touched. This props values are managed by formik-enhancer and you can use it to render validation errors, etc.

Example

// Form schema. Based on this array, formik-enhancer will build your form.
const schema = [
  {
    name: 'user',
    label: 'Choose your username',
    placeholder: 'e.g. JohnDoe',
    initialValue: 'Foo', 
    validation: Yup.string().required('Required field'),
    component: TextField
  },
  {
    type: 'select',
    name: 'favoriteColor',
    label: 'What is your favorite color?',
    placeholder: { label: 'Select...' },
    validation: Yup.string().required('Required field'),
    component: SelectField,
    items: [{ label: 'Red', value: 'red' }],
    renderItem: (option, index) => _renderItem(option, index)
  },
  {
    secure: true, // this prop is valid only in React Native. Check the props tables above.
    name: 'password',
    label: 'Inform your password',
    placeholder: 'Use at least 8 characters',
    validation: Yup.string().min(8, 'Too short!').required('Required field'),
    component: TextField
    blurEvent: _ => console.log('This is my custom blur event!')
  }
]
// Configuration object. For now, have only this 2 props, onSubmit and children.
// Consider it a WIP.
// More details in the config props table.
const config = {
  onSubmit: values => console.log('Submitted form: ', values),
  children: formikProps => (
    <React.Fragment>
        <Button
          title='Reset'
          type='outline'
          onPress={_ => formikProps.resetForm()}
          containerStyle={{ width: '40%', marginTop: 15, marginRight: 7.5 }}
        />
    </React.Fragment>
  )
}
import * as Yup from 'yup'
import { Picker } from 'react-native'
import formikEnhancer from 'formik-enhancer'
// You can use any component you want. 
// In this example, we use React Native Elements.
import { Input, Button } from 'react-native-elements'

export default SomeClass extends React.Component {
  render () {
    return (
      <React.Fragment>
        {formikEnhancer({ schema, config })}
      </React.Fragment>
    )
  }
}

// Fields for our form :)
const TextField = ({ ...props }) => (
  <Input
    {...props}
    errorStyle={{ color: 'red' }}
    errorMessage={props.touched && props.error ? props.error : ''}
  />
)

const SelectField = ({ ...props }) => (
  <React.Fragment>
    <Text>{props.label}</Text>
    <Picker {...props} />
    {props.error &&
      props.touched &&
      <Text style={{ color: 'red' }}>{props.error}</Text>}
  </React.Fragment>
)

For masks, you can pass any function that receives a value as parameter:

interface PhoneInterface {
  (input: string): string
}

const phone: PhoneInterface = (input) => {
  return input
    .replace(/\D/g, '')
    .replace(/(\d{2})(\d)/, '($1) $2')
    .replace(/(\d{4})(\d)/, '$1 $2')
    .replace(/(\d{4}) (\d)(\d{4})/, '$1$2 $3')
    .replace(/( \d{4})\d+?$/, '$1')
}

export default phone
  name: 'phone',
  label: 'What is your phone?',
  mask: phoneMask