@3rd1t/formal-native 中文文档教程

发布于 5年前 浏览 20 项目主页 更新于 3年前

@kevinwolf/formal-native@kevinwolf/formal-native

@kevinwolf/formal-native

@kevinwolf/formal 的原生扩展。

Table of Contents

Install

yarn add @kevinwolf/formal-native

Usage

import React from "react";
import { Alert, View, Text, TextInput, Button } from "react-native";
import useFormal from "@kevinwolf/formal-native";
import * as yup from "yup";

const schema = yup.object().shape({
  firstName: yup.string().required(),
  lastName: yup.string().required(),
  email: yup
    .string()
    .email()
    .required()
});

const initialValues = {
  firstName: "Tony",
  lastName: "Stark",
  email: "ironman@avengers.io"
};

export default function App() {
  const formal = useFormal(initialValues, {
    schema,
    onSubmit: values => Alert.alert(JSON.stringify(values))
  });

  return (
    <View>
      <View>
        <Text>First Name</Text>
        <TextInput {...formal.getFieldProps("firstName")} />
        {formal.errors.firstName && <Text>{formal.errors.firstName}</Text>}
      </View>

      <View>
        <Text>Last Name</Text>
        <TextInput {...formal.getFieldProps("lastName")} />
        {formal.errors.lastName && <Text>{formal.errors.lastName}</Text>}
      </View>

      <View>
        <Text>Email</Text>
        <TextInput {...formal.getFieldProps("email")} autoCapitalize="none" />
        {formal.errors.email && <Text>{formal.errors.email}</Text>}
      </View>

      <Button {...formal.getSubmitButtonProps()} title="Submit" />
    </View>
  );
}

Tips

如您所见,由于字段代码的重复,上述代码变得有些冗长,为了抽象出重复的代码,您可以创建一个 Field 组件并将所有这些调用替换为 App. js。

Field.js

import React from "react";
import { View, Text, TextInput } from "react-native";

export default function Field({ label, error, ...props }) {
  return (
    <View>
      <Text>{label}</Text>
      <TextInput {...props} />
      {error && <Text>{error}</Text>}
    </View>
  );
}

App.js

import React from "react";
-import { Alert, View, Text, TextInput, Button } from "react-native";
+import { Alert, View } from 'react-native'
import useFormal from "@kevinwolf/formal-native";
import * as yup from "yup";

+import Field from './components/field'
+import Button from './components/button'

const schema = yup.object().shape({
  firstName: yup.string().required(),
  lastName: yup.string().required(),
  email: yup
    .string()
    .email()
    .required()
});

const initialValues = {
  firstName: "Tony",
  lastName: "Stark",
  email: "ironman@avengers.io"
};

export default function App() {
  const formal = useFormal(initialValues, {
    schema,
    onSubmit: values => Alert.alert(JSON.stringify(values))
  });

  return (
    <View>
-      <View>
-        <Text>First Name</Text>
-        <TextInput {...formal.getFieldProps("firstName")} />
-        {formal.errors.firstName && (<Text>{formal.errors.firstName}</Text>)}
-      </View>
+      <Field {...formal.getFieldProps('firstName')} label="First name" />

-      <View>
-        <Text>Last Name</Text>
-        <TextInput {...formal.getFieldProps("lastName")} />
-        {formal.errors.lastName && (<Text>{formal.errors.lastName}</Text>)}
-      </View>
+      <Field {...formal.getFieldProps('lastName')} label="Last name" />

-      <View>
-        <Text>Email</Text>
-        <TextInput {...formal.getFieldProps("email")} autoCapitalize="none" />
-        {formal.errors.email && (<Text>{formal.errors.email}</Text>)}
-      </View>
+      <Field {...formal.getFieldProps('email')} label="Email" autoCapitalize="none" />

      <Button {...formal.getSubmitButtonProps()} title="Submit" />
    </View>
  );
}

Extended documentation

有关扩展文档、示例和贡献指南,请参阅 包含这个包的 monorepo

@kevinwolf/formal-native@kevinwolf/formal-native

@kevinwolf/formal-native

???? Native extension for @kevinwolf/formal.

Table of Contents

Install

yarn add @kevinwolf/formal-native

Usage

import React from "react";
import { Alert, View, Text, TextInput, Button } from "react-native";
import useFormal from "@kevinwolf/formal-native";
import * as yup from "yup";

const schema = yup.object().shape({
  firstName: yup.string().required(),
  lastName: yup.string().required(),
  email: yup
    .string()
    .email()
    .required()
});

const initialValues = {
  firstName: "Tony",
  lastName: "Stark",
  email: "ironman@avengers.io"
};

export default function App() {
  const formal = useFormal(initialValues, {
    schema,
    onSubmit: values => Alert.alert(JSON.stringify(values))
  });

  return (
    <View>
      <View>
        <Text>First Name</Text>
        <TextInput {...formal.getFieldProps("firstName")} />
        {formal.errors.firstName && <Text>{formal.errors.firstName}</Text>}
      </View>

      <View>
        <Text>Last Name</Text>
        <TextInput {...formal.getFieldProps("lastName")} />
        {formal.errors.lastName && <Text>{formal.errors.lastName}</Text>}
      </View>

      <View>
        <Text>Email</Text>
        <TextInput {...formal.getFieldProps("email")} autoCapitalize="none" />
        {formal.errors.email && <Text>{formal.errors.email}</Text>}
      </View>

      <Button {...formal.getSubmitButtonProps()} title="Submit" />
    </View>
  );
}

Tips

As you can see, the above code became a little verbose due to the repetition of the fields code, in order to abstract that repeated code, you can create a Field component and replace all those calls in App.js.

Field.js

import React from "react";
import { View, Text, TextInput } from "react-native";

export default function Field({ label, error, ...props }) {
  return (
    <View>
      <Text>{label}</Text>
      <TextInput {...props} />
      {error && <Text>{error}</Text>}
    </View>
  );
}

App.js

import React from "react";
-import { Alert, View, Text, TextInput, Button } from "react-native";
+import { Alert, View } from 'react-native'
import useFormal from "@kevinwolf/formal-native";
import * as yup from "yup";

+import Field from './components/field'
+import Button from './components/button'

const schema = yup.object().shape({
  firstName: yup.string().required(),
  lastName: yup.string().required(),
  email: yup
    .string()
    .email()
    .required()
});

const initialValues = {
  firstName: "Tony",
  lastName: "Stark",
  email: "ironman@avengers.io"
};

export default function App() {
  const formal = useFormal(initialValues, {
    schema,
    onSubmit: values => Alert.alert(JSON.stringify(values))
  });

  return (
    <View>
-      <View>
-        <Text>First Name</Text>
-        <TextInput {...formal.getFieldProps("firstName")} />
-        {formal.errors.firstName && (<Text>{formal.errors.firstName}</Text>)}
-      </View>
+      <Field {...formal.getFieldProps('firstName')} label="First name" />

-      <View>
-        <Text>Last Name</Text>
-        <TextInput {...formal.getFieldProps("lastName")} />
-        {formal.errors.lastName && (<Text>{formal.errors.lastName}</Text>)}
-      </View>
+      <Field {...formal.getFieldProps('lastName')} label="Last name" />

-      <View>
-        <Text>Email</Text>
-        <TextInput {...formal.getFieldProps("email")} autoCapitalize="none" />
-        {formal.errors.email && (<Text>{formal.errors.email}</Text>)}
-      </View>
+      <Field {...formal.getFieldProps('email')} label="Email" autoCapitalize="none" />

      <Button {...formal.getSubmitButtonProps()} title="Submit" />
    </View>
  );
}

Extended documentation

For extended documentation, examples and contributing guidelines, please refer to the monorepo containing this package.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文