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

formini

v0.0.0-beta.15

Published

mini form core with vanilla js

Downloads

25

Readme

Formini

size

纯 js 迷你表单核心,包含值管理与简单的错误校验,基于 Proxy 实现表单关联功能

在线示例

安装

npm install formini --save
# or
yarn add formini

使用方式

import createForm from 'formini'

const form = createForm({
  fields: [
    {
      name: 'key1',
      defaultValue: 1,
      rules: [
        (value) => (typeof value === 'undefined' ? 'key1 必填' : undefined),
      ],
    },
    { name: 'key2', defaultValue: 2 },
    {
      name: 'key3',
      rules: [
        (value) => (typeof value === 'undefined' ? 'key3 必填' : undefined),
      ],
    },
  ],
  // 关联参数,基于 Proxy 响应式数据自动收集依赖,定向更新
  relatives: {
    hasKey1: (values) => typeof values.key1 !== 'undefined',
    hasKey2: (values) => 'key2' in values,
    hasKey5: (values) => 'key5' in values,
    hasError: (values, errors) => Object.values(errors).some(Boolean),
    // 可用作表单项关联控制
    showField2: (values) => values.key1 === 'magic'
  },
})

// 写值与校验
form.setValue({
  key1: undefined
})
await form.validate()
form.getErrors() // { key1: 'key1 必填', key2: undefined, key3: 'key3 必填' }

form.reset() // 重置

// 定向写值与校验
form.setValue('key1', undefined)
await form.validate('key1')
form.getError('key1') // key1 必填

const stop0 = form.watch(() => { // 监听 form 的所有变化,包括 values、errors、relatives
  console.log('form 发生了变化')
})

const stop1 = form.watchValues((values) => { // 监听全部 values
  console.log(values)
})
const stop2 = form.watchValue('key1', (value) => { // 监听指定 value
  console.log(value)
})

const stop3 = form.watchErrors((errors) => { // 监听全部 errors
  console.log(errors)
})
const stop4 = form.watchError('key1', (error) => { // 监听指定 error
  console.log(error)
})

const stop5 = form.watchRelatives((relatives) => { // 监听全部 relatives(关联)
  console.log(relatives)
})
const stop6 = form.watchRelative('hasKey1', (relative) => { // 监听指定 relative(关联)
  console.log(relative)
})