【发布时间】:2022-03-15 16:05:59
【问题描述】:
我有一个样式化的 Material UI 按钮组件。我想这样做,所以当我单击样式按钮时,我可以在我的计算机上选择一个文件。
我认为这可以通过将<Button></Button> 放置在<label> 中来实现<input type="file"/>(文件输入)元素。但这似乎只适用于 div 而不是按钮。我既不能为常规按钮元素打开文件选择器,也不能为材料 UI Button 组件打开文件选择器。
如何让材质uiButton作为文件输入的标签?
import React from 'react';
import { Button } from '@material-ui/core';
export default function InputButton() {
return (
<div>
{/* Button does not open file picker window */}
<div>
<label>
<input type="file" style={{ display: 'none' }} />
<Button>upload file</Button>
</label>
</div>
{/* div does open file picker window */}
<div>
<label>
<input type="file" style={{ display: 'none' }} />
<div>upload file</div>
</label>
</div>
</div>
);
}
【问题讨论】:
标签: html reactjs material-ui