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

chakra-formik-experiment

v0.2.0

Published

An experiment for tight-knit Chakra+Formik integration with a familiar API

Downloads

198

Readme

Chakra+Formik Integration Experiment

What is this?

This project is a prototype for a Chakra UI+Formik integration library.

The goals of this project:

  • provide an API that is familiar to both Chakra UI and Formik users
  • experiment with and establish patterns that can eventually be turned into an official Chakra UI extension library

As this is a pre-1.0 experiment, the API is subject to change.

Demo

Check out this CodeSandbox for a demonstration of the components in action: https://codesandbox.io/s/interesting-blackburn-oldox?file=/src/App.tsx

Installation

Install Chakra UI and Formik, then:

# with yarn
yarn add chakra-formik-experiment

# with npm
npm install chakra-formik-experiment

Usage

chakra-formik-experiment works by connecting Chakra form components to a Formik form's context using the name prop.

The name prop can be passed directly to the library's form components or provided to them by the FieldControl component.

As individual fields

import * as React from "react"
import { Formik } from "formik"
import { InputFormik } from "chakra-formik-experiment"

const Example = () => (
  <Formik>
    <InputFormik id="name" name="name" placeholder="Enter your name" />
  </Formik>
)

With FieldControl

FieldControl provides the same behavior as Chakra's FormControl, but automatically connects wrapped form elements and FieldErrorMessage components with Formik via the name prop.

import * as React from "react"
import { Formik } from "formik"
import { FormLabel } from "@chakra-ui/react"
import { FieldControl, FieldErrorMessage, InputFormik } from "chakra-formik-experiment"

const Example = () => (
  <FieldErrorMessage id="name" name="name">
    <FormLabel>Name</FormLabel>
    <InputFormik />
    <FieldErrorMessage />
  </FormControl>
)

Components

CheckboxFormik

The CheckboxFormik component is Chakra's Checkbox component except it's automatically connected to Formik via the name prop.

import * as React from "react"
import { Formik } from "formik"
import { CheckboxFormik } from "chakra-formik-experiment"

const CheckboxFormikExample = () => (
  <Formik initialValues={{ checkbox: true }} onSubmit={console.log}>
    <CheckboxFormik name="checkbox" />
  </Formik>
)

CheckboxGroupFormik

The CheckboxGroupFormik component is Chakra's CheckboxGroup component except it's automatically connected to Formik via the name prop.

Note: Use the regular Chakra Checkbox component within CheckboxGroupFormik. We could eventually make CheckboxFormik work with it.

import * as React from "react"
import { Formik } from "formik"
import { Checkbox } from "@chakra-ui/react"
import { CheckboxGroupFormik } from "chakra-formik-experiment"

const CheckboxGroupFormikExample = () => (
  <Formik initialValues={{ radio: "A" }} onSubmit={console.log}>
    <CheckboxGroupFormik name="radio">
      <Checkbox value="A">A</Checkbox>
      <Checkbox value="B">B</Checkbox>
    </CheckboxGroupFormik>
  </Formik>
)

InputFormik

The InputFormik component is Chakra's Input component except it's automatically connected to Formik via the name prop.

import * as React from "react"
import { Formik } from "formik"
import { InputFormik } from "chakra-formik-experiment"

const InputFormikExample = () => (
  <Formik initialValues={{ name: "name" }} onSubmit={console.log}>
    <InputFormik name="name" />
  </Formik>
)

RadioFormik

The RadioFormik component is Chakra's Radio component except it's automatically connected to Formik via the name prop.

import * as React from "react"
import { Formik } from "formik"
import { RadioFormik } from "chakra-formik-experiment"

const RadioFormikExample = () => (
  <Formik initialValues={{ radio: "A" }} onSubmit={console.log}>
    <RadioFormik name="radio" value="A">
      A
    </RadioFormik>
    <RadioFormik name="radio" value="B">
      B
    </RadioFormik>
  </Formik>
)

RadioGroupFormik

The RadioGroupFormik component is Chakra's RadioGroup component except it's automatically connected to Formik via the name prop.

Note: Use the regular Chakra Radio component within RadioGroupFormik. We could eventually make RadioFormik work with it.

import * as React from "react"
import { Formik } from "formik"
import { Radio } from "@chakra-ui/react"
import { RadioGroupFormik } from "chakra-formik-experiment"

const RadioGroupFormikExample = () => (
  <Formik initialValues={{ radio: "A" }} onSubmit={console.log}>
    <RadioGroupFormik name="radio">
      <Radio value="A">A</Radio>
      <Radio value="B">B</Radio>
    </RadioGroupFormik>
  </Formik>
)

SelectFormik

The SelectFormik component is Chakra's Select component except it's automatically connected to Formik via the name prop.

import * as React from "react"
import { Formik } from "formik"
import { SelectFormik } from "chakra-formik-experiment"

const SelectFormikExample = () => (
  <Formik initialValues={{ item: "A" }} onSubmit={console.log}>
    <SelectFormik name="item">
      <option value="A">A</option>
      <option value="B">B</option>
    </SelectFormik>
  </Formik>
)

EditableFormik

The EditableFormik component is Chakra's Editable component except it's automatically connected to Formik via the name prop.

import * as React from "react"
import { Formik } from "formik"
import { EditableFormik } from "chakra-formik-experiment"
import { EditablePreview, EditableInput } from "@chakra-ui/react"

const EditableFormikExample = () => (
  <Formik initialValues={{ name: "Kenichi" }} onSubmit={console.log}>
    <EditableFormik name="name">
      <EditablePreview />
      <EditableInput />
    </EditableFormik>
  </Formik>
)

FieldControl

The FieldControl component has the same API and behavior as FormControl, except that the name prop is used to connect the form element(s) to Formik. This allows you to provide the same accessible form element experience you get from FormControl, with the added benefit that form elements and FieldErrorMessage are automatically associated with a Formik field.

import * as React from "react"
import { Formik } from "formik"
import { FormLabel } from "@chakra-ui/react"
import {
  FieldControl,
  FieldErrorMessage,
  InputFormik,
} from "chakra-formik-experiment"

const Example = () => (
  <FieldControl id="name" name="name">
    <FormLabel>Name</FormLabel>
    <InputFormik />
    <FieldErrorMessage />
  </FieldControl>
)

Note: If a prop exists on both the FieldControl and the form element component, the prop of the component will be used.

// in this example, the input is connected to Formik as `"component"`
const Example = () => (
  <FieldControl name="control">
    <InputFormik name="component">
  </FieldControl>
)

FieldErrorMessage

The FieldErrorMessage component works similarly to the Chakra FormErrorMessage component, except it is automatically connected to the touched and error state of the field, meaning that if the field has been touched and has a validation error, that error message will be displayed.

const FieldErrorMessageExample = () => (
  <FieldControl name="name">
    <InputFormik />
    <FieldErrorMessage />
  </FieldControl>
)

Note: This behavior will not occur if an isInvalid prop is passed to FieldControl.

// in this example, the error message is never displayed because
// `isInvalid={false}` is passed to `FieldControl`
const Example = () => (
  <FieldControl name="name" isInvalid={false}>
    <InputFormik />
    <FieldErrorMessage />
  </FieldControl>
)

NumberInputFormik

The NumberInputFormik component is Chakra's NumberInput component except it's automatically connected to Formik via the name prop. The value is formatted as a number when the form is submitted.

import * as React from "react"
import { Formik } from "formik"
import { NumberInputFormik } from "chakra-formik-experiment"

const NumberInputFormikExample = () => (
  <Formik initialValues={{ number: 123 }} onSubmit={console.log}>
    <NumberInputFormik name="number">
      <NumberInputField />
      <NumberInputStepper>
        <NumberIncrementStepper />
        <NumberDecrementStepper />
      </NumberInputStepper>
    </NumberInputForm>
  </Formik>
)

SwitchFormik

The SwitchFormik component is Chakra's Switch component except it's automatically connected to Formik via the name prop. The value is formatted as a number when the form is submitted.

import * as React from "react"
import { Formik } from "formik"
import { SwitchFormik } from "chakra-formik-experiment"

const SwitchFormikExample = () => (
  <Formik initialValues={{ switch: true }} onSubmit={console.log}>
    <SwitchFormik name="switch" />
  </Formik>
)

TextareaFormik

The TextareaFormik component is Chakra's Textarea component except it's automatically connected to Formik via the name prop.

import * as React from "react"
import { Formik } from "formik"
import { TextareaFormik } from "chakra-formik-experiment"

const TextareaFormikExample = () => (
  <Formik initialValues={{ name: "name" }} onSubmit={console.log}>
    <TextareaFormik name="name" />
  </Formik>
)

Integration Checklist

  • [x] Checkbox (CheckboxFormik)
  • [x] CheckboxGroup (CheckboxGroupFormik)
  • [x] Editable (EditableFormik)
  • [x] FormControl (FieldControl)
  • [x] FormErrorMessage (FieldErrorMessage)
  • [x] Input (InputFormik)
  • [x] NumberInput (NumberInputFormik)
  • [x] Radio (RadioFormik)
  • [x] RadioGroup
  • [x] Select (SelectFormik)
  • [ ] Slider
  • [x] Switch (SwitchFormik)
  • [x] Textarea (TextAreaFormik)