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

formk

v0.0.20

Published

A powerful form library for ReactJs/ReactNative

Downloads

12

Readme

formk

A flexible React form library

Installation

    npm i formk --save

or yarn

    yarn add formk

Basic Usages

Creating simple form

import formk from "formk";
import { useState } from "react";

const { Form, Field } = formk();

const LoginForm = () => {
  const [formValue, setFormValue] = useState({
    username: "admin",
    password: "admin",
  });

  return (
    <Form value={formValue} onChange={setFormValue}>
      <Field name="username" />
      <Field name="password" />
      <button type="submit">Submit</button>
    </Form>
  );
};

Adding validations

const LoginForm = () => {
  return (
    <Form value={formValue} onChange={handleChange}>
      <Field
        name="username"
        rules={{
          // indicate that username field is required
          required: true,
          // use custom error message
          message: "Username required",
        }}
      />
      <Field
        name="password"
        rules={{
          // no error message specified
          // default error message will be used
          required: true,
        }}
      />
      <button type="submit">Submit</button>
    </Form>
  );
};

Advanced Usages

Using formk with React Native

import React, { useState } from "react";
import { Text, View, StyleSheet, Button, TextInput } from "react-native";
import formk from "formk";

const { Form, Field } = formk({
  noExtraAttrs: true,
});

const initialValue = {
  username: "admin",
  password: "123456",
};

export default function App() {
  const [currentData, setCurrentData] = useState(initialValue);
  const [submittedData, setSubmittedData] = useState(initialValue);

  return (
    <View style={styles.container}>
      <View>
        <Form
          value={currentData}
          initialValue={initialValue}
          onChange={setCurrentData}
          onSuccess={setSubmittedData}
        >
          {({ handleSubmit }) => (
            <>
              <Field name="username" rules={{ required: true }}>
                {({ $props, val }) => (
                  <View>
                    <TextInput
                      {...$props("value", "onChangeText")}
                      style={styles.input}
                      placeholder="Username"
                    />
                    {val.error && (
                      <Text style={styles.error}>{val.error.message}</Text>
                    )}
                  </View>
                )}
              </Field>
              <Field
                name="password"
                label="Custom Field Name"
                rules={{ required: true }}
              >
                {({ $props, val }) => (
                  <View>
                    <TextInput
                      {...$props("value", "onChangeText")}
                      style={styles.input}
                      placeholder="Password"
                    />
                    {val.error && (
                      <Text style={styles.error}>{val.error.message}</Text>
                    )}
                  </View>
                )}
              </Field>
              <Button title="Submit" onPress={handleSubmit} />
            </>
          )}
        </Form>
      </View>
      <View>
        <Text>
          Current Data:{"\n"}
          {JSON.stringify(currentData, null, 2)}
          {"\n"}
          Submitted Data:{"\n"}
          {JSON.stringify(submittedData, null, 2)}
          {"\n"}
        </Text>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 50,
    backgroundColor: "#ecf0f1",
    padding: 8,
  },
  input: {
    borderColor: "silver",
    borderWidth: 1,
    marginBottom: 5,
    padding: 5,
    borderRadius: 3,
  },
  error: {
    color: "red",
    marginBottom: 5,
  },
});

Nested form validation

import formk from "formk";
import faker from "faker";
import { useState } from "react";

const { Form, Field } = formk();

const initialValue = {
  loginInfo: {
    username: faker.internet.userName(),
    password: faker.internet.password(),
  },
  personalInfo: {
    email: faker.internet.email(),
    address: {
      zipCode: parseInt(faker.address.zipCode(), 10),
      city: faker.address.city(),
      street: faker.address.streetAddress(),
      country: faker.address.country(),
    },
  },
};

const LoginInfo = () => (
  <>
    <Field name="username" label="Username" rules={{ required: true }} />
    <Field
      name="password"
      label="Password"
      rules={{ required: true, min: 6 }}
    />
  </>
);

const AddressInfo = () => (
  <>
    <Field name="zipCode" label="ZipCode" rules={{ pattern: /^\d+$/ }} />
    <Field name="city" label="City" />
    <Field name="street" label="Street" />
    <Field name="country" label="Country" />
  </>
);

const PersonalInfo = () => (
  <>
    <Field
      name="email"
      label="Email"
      rules={{ type: "email", required: true }}
    />
    <Form name="address">
      <AddressInfo />
    </Form>
  </>
);

export default function App() {
  const [value, setValue] = useState(initialValue);
  const [submittedValue, setSubmittedValue] = useState(initialValue);

  return (
    <div className="App">
      <Form
        value={value}
        initialValue={initialValue}
        onChange={setValue}
        onSuccess={setSubmittedValue}
      >
        <Form name="loginInfo">
          <LoginInfo />
        </Form>
        <Form name="personalInfo">
          <PersonalInfo />
        </Form>
        <button>Save</button>
      </Form>
      <h2>Editing value</h2>
      <xmp>{JSON.stringify(value, null, 2)}</xmp>
      <h2>Submitted value</h2>
      <xmp>{JSON.stringify(submittedValue, null, 2)}</xmp>
    </div>
  );
}

API References