简体   繁体   English

反应最终表单将附加参数传递给表单 State

[英]React Final Form Pass Additional Parameters to Form State

React Final Form uses a render prop pattern in its form component, and I am trying to understand how I can modify it to pass it additional arguments. React Final Form在其表单组件中使用渲染道具模式,我试图了解如何修改它以传递额外的 arguments。

According to it's documentation, it passes the following props to the render function - including (crucially) the FormState .根据它的文档,它将以下道具传递给渲染 function - 包括(至关重要) FormState

Let's take a look at my particular implementation, focusing on the render function:下面看一下我的具体实现,重点是渲染function:

<Form
onSubmit={() => {console.log("wow coolio you submitted a form!")}}
initialValues={initData}
validate={validateMyForm}
render={({ handleSubmit, reset, submitting, pristine, values }) => {
formSubmitHandler = async () => {
await handleSubmit()
return values
}
return(
//... form components with the values prop passed into them below
//...i.e. <Part_1_Of_Form values={values}/>

If I understand correctly, you will note I destruct the render prop object in the JSX {({})} and get the values from the FormState.如果我理解正确,您会注意到我在 JSX {({})}中破坏了渲染道具 object 并从 FormState 中获取values

I then pass them to various modular form components below - like <Part_1_Of_Form/> in order to make each part of the form react to state changes in other parts of the form.然后我将它们传递给下面的各种模块化表单组件 - 例如<Part_1_Of_Form/>以使表单的每个部分对表单其他部分的 state 更改做出反应。 But my problem is I want it to react to more than the values.但我的问题是我希望它对价值做出反应。 For example, if I want to display a label conditionally based on if another label option (or another option) is selected in another part of the form, I can't access it because I only get access to the values - not the labels.例如,如果我想根据是否在表单的另一部分选择了另一个 label 选项(或另一个选项)来有条件地显示 label,我无法访问它,因为我只能访问值而不是标签。

Example , with a currency selection tool to propagate throughout the form using state:示例,使用货币选择工具使用 state 在整个表单中传播:

<Field 
name="part_one.currency"
component={MyRFFInputSelectComponent}
options={[{value: 'USD', label: '$ (USD)', symbol: '$'}, {value: 'EUR', label: '€ (EUR)', symbol: '€'}]}
required={true}
className="form-control">
</Field>

I can pass the value (let's say USD ) around other parts of the form, using the render props and the values part of that returned object - but I don't want to.我可以使用渲染道具和返回的 object 的values部分在表单的其他部分传递值(比如说USD ) - 但我不想这样做。 I want to pass the symbol - the value (USD) belongs to the database (it's only USD because the DB wants it as a string like that) - but the other parts of the form should display the symbol instead of the value once I select my currency in the top part of the form.我想传递symbol - 值(USD)属于数据库(它只是USD ,因为数据库希望它作为这样的字符串) - 但是一旦我 select 时,表单的其他部分应该显示符号而不是值我的货币在表格的顶部。

Example of what I have to do now elsewhere in the form:我现在必须在表格的其他地方做的例子:

<Field
...
append={values['part_one']['currency']}

Example of what I want to do:我想做的例子:

<Field
...
append={symbols['part_one']['currency']}

Or maybe even better:或者甚至更好:

<Field
...
append={allFormData(notjustvalues)['part_one']['currency']['symbol']}

So that each input that needs a price can show a currency in the appended label.这样每个需要价格的输入都可以在附加的 label 中显示一个货币。

Does this usecase make sense?这个用例有意义吗?

Is there another way to pick up the symbol or a way to add it to the FormState?是否有另一种方法来获取符号或将其添加到 FormState 中? I think its probably bad for perf to pass something like allFormData around just to get one custom symbol.我认为 perf 传递allFormData类的东西只是为了获得一个自定义符号可能很糟糕。 I can't use the symbol for the value because my backend developer will cry.我不能使用符号来表示值,因为我的后端开发人员会哭。

If I'm understanding you correctly, your value for part_one.currency needs to be the entire {value: 'USD', label: '$ (USD)', symbol: '$'} object.如果我对您的理解正确,您对part_one.currency的值需要是整个{value: 'USD', label: '$ (USD)', symbol: '$'} object。

Then you can do values.part_one.currency.symbol elsewhere in your form.然后你可以在表单的其他地方做values.part_one.currency.symbol If you only want the value to be 'USD' when you submit, you'll need to handle that in your onSubmit function.如果您只希望提交时的值是'USD' ,则需要在onSubmit function 中处理它。 Does that help?这有帮助吗?

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

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