Code Monkey home page Code Monkey logo

xstate-form-machine's Introduction

xstate-form-machine

xstate common form machine using yup validations

Super quick start

npm install xstate
npm install xstate-form-machine
//validation.js
import * as yup from "yup";

const phoneRegExp = /^[1][3-8][0-9]{9}$/;

export const nameShape = yup.string().required("请填写名称");

export const phoneShape = yup
  .string()
  .length(11, "请输入手机号必须为11位")
  .required("请输入手机号")
  .matches(phoneRegExp, "手机号码格式不正确");

export const addressShape = yup.string().required("请填写详细地址");


//machine.js

import { assign, Machine } from "xstate";
import FormMachineBuilder from "xstate-form-machine";

const onSubmit = async context => {
  const { id, inputFields } = context;
  const { name, phone, address } = inputFields;
  //todo
});

const onSuccess = (...args) => {
  //todo
};

const onFail = (...args) => {
  //todo
};

const shapes = {
  name: nameShape,
  phone: phoneShape,
  address: addressShape
};

export default new FormMachineBuilder(
  "address",
  { assign, Machine },
  shapes
)
  .setOnSubmit(onSubmit)
  .setOnSuccess(onSuccess)
  .setOnFail(onFail)
  .build();

///

////useMachine


  const [current, send] = useMachine(
    machine.withContext({
      id,
      inputFields: {
        name,
        phone,
        address
      }
    })
  );

  const { context } = current;
  const { inputFields, inputErrors = {} } = context;

    <View className="container">
      <InputBox
        inputFields={inputFields}
        inputErrors={inputErrors}
        onChange={({name, phone, address}) => {
           //send("ONCHANGE", {payload: {name, phone, address}}) to update inputs
           send("ONCHANGE", {payload: {name, phone, address}})
          }
        }
      />
      <View
        className="confirmButton"
        onClick={() => {
          //send("SUMBIT", to submit
          send("SUMBIT");
        }}
      >
        <Text className="confirmText">{"保存"}</Text>
      </View>
    </View>

xstate-form-machine's People

Contributors

xuchong0211 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

xstate-form-machine's Issues

Working example

Hi, from what I'm looking at, there's no working example, except the quick intro, which I can't get working because of some typos and unknown libraries/dependencies.

Would it be possible for you to get more example or get the readme example in a working condition?

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.