[英]Correctly parametrize event handler for input type file and material ui Button
I'm trying to build a form, which for a given set of options (stored in an array in component's state) gives you the possibility to upload a file and add a note for each option. 我正在尝试构建一个表单,该表单对于给定的一组选项(以组件状态存储在数组中)使您可以上传文件并为每个选项添加注释。 The forms are custom, but the elements are from material UI.
表单是自定义的,但元素来自材料UI。
The code looks something like this (simplified, stripped of other unneeded data). 代码看起来像这样(简化后,去除了其他不需要的数据)。
public renderOptions() {
const { mdFiles } = this.state
return options.map((option, i) =>
<FileUpload
handleChange={(e) => {this.handleFileInputChange(e, i)}}
/>
<TextField
key={`option-${i}`}
name={option.name} */
type="text"
fullWidth
label={item}
placeholder={`Edit ${item}`}
value={option.note || option.originalNote}
onChange={(e) => this.handleChangeFileName(e, i)}
/>
)
}
I want to parametrize the event handlers, so that, besides the event, an index is passed, so I have a connection between the note and/or uploaded file and the option. 我想对事件处理程序进行参数设置,以便除事件外还传递索引,因此注释和/或上载的文件与选项之间具有连接。
Changing the note works as expected, but the problem is in the FileUpload
component. 更改注释可以按预期工作,但是问题出在
FileUpload
组件中。
It contains a simple input
of type file
, but the input is hidden and only the button is visible - example is taken from the official material ui docs ( https://material-ui.com/demos/buttons/ -> https://codesandbox.io/s/qzr18l41ow ) 它包含类型
file
的简单input
,但是输入是隐藏的,只有按钮可见-示例取自官方材料ui docs( https://material-ui.com/demos/buttons/- > https:/ /codesandbox.io/s/qzr18l41ow )
The code looks like this 代码看起来像这样
class FileUpload extends React.Component<Props, {}> {
public render() {
return (
<FileWrapper>
<input
accept={fileType}
id="file-upload"
type="file"
onChange={handleChange}
/>
<label htmlFor="file-upload" id="file-upload">
<Button
variant="contained"
component="span"
>
Upload
</Button>
</label>
</FileWrapper>
)
}
}
The problem is that in handleFileInputChange
method the second argument (index) is always zero - so for whatever option I upload the file for - it is always attached to the first option. 问题在于,在
handleFileInputChange
方法中,第二个参数(索引)始终为零-因此,无论我上传文件的选项是什么,它始终附加在第一个选项上。 Now, when I strip the button (remove it) and just work with the input alone - everything works as expected. 现在,当我剥离按钮(将其删除)并仅使用输入时-一切都会按预期进行。 Seems the button and the label are disabling the proper connection with the passed event handler.
似乎按钮和标签正在禁用与传递的事件处理程序的正确连接。
Does anyone have an idea how to fix this? 有谁知道如何解决这个问题?
I found the problem... 我发现了问题...
Each file input has to have a unique id. 每个文件输入必须具有唯一的ID。 In your code they all have id="file-upload".
在您的代码中,它们都具有id =“ file-upload”。 This is why all of the buttons trigger the onchange handler of the first input.
这就是为什么所有按钮都会触发第一个输入的onchange处理程序的原因。
Here's a modified version where the ID set dynamically. 这是ID会动态设置的修改版本。
class FileUpload extends React.Component {
render() {
const { id, handleChange } = this.props;
return (
<div>
<input id={id} type="file" onChange={handleChange} />
<label htmlFor={id} id={`${id}`}>
<Button variant="contained" component="span">
Upload
</Button>
</label>
</div>
);
}
}
Live example: 现场示例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.