简体   繁体   English

Grommet - FormField 中的自定义组件

[英]Grommet - custom component in FormField

In the Grommet FormField documentation, it states under Component ;在 Grommet FormField文档中,它在Component下声明;

The component may be custom as long it supports the properties of name, value, onChange (event => {}), while event has either event.value or event.target.value.该组件可以是自定义的,只要它支持 name、value、onChange (event => {}) 的属性,而 event 具有 event.value 或 event.target.value。

I'd like to implement a custom component but don't understand how it needs to interact with a controlled Form component to supply its value property.我想实现一个自定义组件,但不明白它需要如何与受控的Form组件交互以提供其value属性。 Is there an example somewhere that shows how to implement a custom component in a FormField ?是否有一个示例显示如何在FormField中实现自定义组件?

Here is a code example of Controlled Form that is using a custom input component.这是使用自定义输入组件的受控表单的代码示例。 I used a plain input tag since it supports the docs specification as mentioned above我使用了一个普通的输入标签,因为它支持上面提到的文档规范

...it supports the properties of name, value, onChange (event => {}), while event has either event.value or event.target.value. ...它支持名称、值、onChange (event => {}) 的属性,而 event 具有 event.value 或 event.target.value。

However, you can feel free to use your own input component implementation using the template example below.但是,您可以使用下面的模板示例随意使用自己的输入组件实现。 The value of the input will be communicated to the controlled Form without extra coding.输入的值将传送到受控表单,无需额外编码。 Run the following and check out the console logs to see how the value is being updated as you type in the custom input.运行以下命令并查看控制台日志,以查看在您输入自定义输入时值是如何更新的。

import React, { useState } from "react";
import { render } from "react-dom";

import { Box, Button, Form, FormField, Grommet, TextInput } from "grommet";
import { grommet } from "grommet/themes";

const defaultValue = {
  name: "",
  custom: ""
};

export const App = () => {
  const [value, setValue] = useState(defaultValue);
  return (
    <Grommet full theme={grommet}>
      <Box fill align="center" justify="center">
        <Box width="medium">
          <Form
            value={value}
            onChange={(nextValue, { touched }) => {
              console.log("Change", nextValue, touched);
              setValue(nextValue);
            }}
            onSubmit={(event) =>
              console.log("Submit", event.value, event.touched)
            }
          >
            <FormField label="TextInput Field" name="name">
              <TextInput name="name" />
            </FormField>
            <FormField
              label="Custom Component Field"
              name="custom"
              component={(props) => <input {...props} />} // custom component
            />

            <Box direction="row" justify="between" margin={{ top: "medium" }}>
              <Button type="submit" label="Update" primary />
            </Box>
          </Form>
        </Box>
      </Box>
    </Grommet>
  );
};

render(<App />, document.getElementById("root"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM