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

rc-mobx-form

v0.2.1

Published

A react form component depends on mobx with similar api to rc-form.

Downloads

25

Readme

rc-mobx-form

Inspiration from rc-form

Depend on react, mobx and async-validator.

Why use this ?

  • You are familiar with rc-form and want to use it with mobx.
  • Many layers of nested form you want to manage fields together. ep. you can use store to manage nested form fields.

todo

  • [x] Support object path.
  • [x] Support default observer object in form.
  • [x] add doc.
  • [x] Support validateTrigger.
  • [x] remove ant-design dependency
  • [x] reduce example js size

example

online

Usage

npm i -S rc-mobx-form
import { createForm, FormItem, setInternalFormItem } from 'rc-mobx-form'

// if you use ant-design
import { Form } from 'antd'
setInternalFormItem(Form.Item)

// if not, you can implement FormItem instead of default FormItem (see implementation of /src/FormItem.js)
import FormItem from 'your-implement-form-item'
import { observer } from 'mobx-react'

// below is almost same with rc-form, @observer is necessary
@observer
class MyForm extend Component {
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields(fields => console.log('error', fields))
        .then((values) => console.log('success', values))
  }

  render() {
    const { getFieldProps } = this.props.form
    return (
      <Form onSubmit={this.handleSubmit}>
        <FormItem hasFeedback>
          <Input
            {...getFieldProps('nest.input', {
              rules: { required: true, message: 'Please input something!' },
              validateTrigger: 'onBlur',
            })}
            placeholder="Input here"
          />
        </FormItem>
        <Button type="primary" htmlType="submit">Submit</Button>
      </Form>
    )
  }
}

const WrappedForm = createForm()(MyForm)

render(<WrappedForm />, targetEl)

you can also pass a store to WrappedForm

import { observable } from 'mobx'

class DemoForm {
  @observable nest = { input: '' }

  get __options() {
    // you can set options at here
    return {
      'nest.input': {
        rules: { required: true, message: 'Please input something!' },
        validateTrigger: 'onBlur',
        appendProps: {
          placeholder: 'Input here',
        },
      }
    }
  } 
}

// if you set the __options, the above MyForm's render() look like
render() {
  const { getFieldProps } = this.props.form
  return (
    <Form onSubmit={this.handleSubmit}>
      <FormItem hasFeedback>
        <Input {...getFieldProps('nest.input')} />
      </FormItem>
      <Button type="primary" htmlType="submit">Submit</Button>
    </Form>
  )
}

// and you need to pass store to WrappedForm
render(<WrappedForm store={new DemoForm()} />, targetEl)

API

createForm(options)(FormComponent) : WrapForm

options

  • store: you can also pass store here.
  • prefix: common prefix path in store.
  • defaultItemProps: default props assign to FormItem in this FormComponent.
  • displayDefaultLabel: display the default label (passed name) in FormItem.

WrapForm's props

  • store: you can pass store here.
  • rootRef: ref of FormComponent

WrapForm's method

  • validateFields(callback: Function) : Promise
  • getFieldError(name) : Object
  • getFieldsError() : Object
  • getStore()
  • getFieldProps(name, fieldOptions: Object)

fieldOptions

see ant-design's doc but something different:

  • exclusive not supported
  • add parseValue: Function, can be used to format value before set to component
  • add appendProps: Object, for __options of store, this will assign to target component's props

FormItem

  • disabledValidate, if need disable validateStatus