简体   繁体   English

为输入类型文件和材质ui按钮正确参数化事件处理程序

[英]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.

相关问题 检查输入类型文件是否附加了 React.js Material UI - Check if input type file was attached with React.js Material UI 如何在Chrome应用中使用input type =“ file”事件处理程序 - how to use input type=“file” event handler in chrome app 使用 Material UI 访问 renderValue 组件上的反应事件处理程序时出现问题 - Problems accessing react event handler on renderedValue component using Material UI Material UI TextField input type=“date”没有得到event.target.value - Material UI TextField input type=“date” doesn´t get event.target.value 如何在 React 的图标按钮(材质 UI)中使用 input type='text'? - How can I use input type='text' in a Icon Button (Material UI) in React? 在jQuery事件处理程序中检查input [type = checkbox] - Check input[type=checkbox] in jQuery event handler 单选按钮的 OnChange 事件处理程序 (INPUT type="radio") 不能作为一个值工作 - OnChange event handler for radio button (INPUT type="radio") doesn't work as one value VueJS - 元素 UI 输入组件 - 事件处理程序“输入”错误 - VueJS - Element UI Input Component - Event handler "input" error 输入类型=“文件”是否有事件? - Is there an event for input type=“file”? name='' 的无效表单控件不可聚焦错误 Material ui input type file - An invalid form control with name='' is not focusable error Material ui input type file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM