我正在创建一个UI,允许用户使用可以使用输入从计算机加载的文件创建播放列表。它正在工作,看起来像这样:
这是我的代码:
class DownloadTest extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.inputRef = React.createRef();
this.state = {
files: [],
audio: '',
};
}
handleClick = event => {
// Helper code to read file and return promise
const readFile = (file) => {
const fileList = [];
const fileReader = new FileReader();
// create the promise and return it
return new Promise((resolve, reject) => {
// if file reader has an error, report it
fileReader.onerror = (error) => {
reject({ error })
}
// if success, resolve the promise
fileReader.onload = (e) => {
resolve({
name: file.name,
link: e.target.result
})
}
// start reading the file
fileReader.readAsDataURL(file);
})
}
// create all the file reader promises
// create an array from the files list and use map to generate
// an array of promises
const allReaders = Array.from(event.target.files).map(readFile)
// Now handle the array of promises we just created
Promise.all(allReaders)
.then(fileList => {
console.log(fileList)
// set the state that we have all the files
this.setState({ files: fileList });
})
.catch(error => {
console.error(error)
});
}
render() {
return (
<div className="downloadMusic">
<div className="input">
<input
onChange={this.handleClick}
id="upload-file"
className="inputName"
type="file"
multiple
ref={this.inputRef}
/>
</div>
<div className="audio-player">
<audio
controls
autoPlay
src={this.state.audio}
/>
</div>
<div>
<ul ref={this.ulRef}>
{this.state.files.map((file, index) => (
<li key={index} onClick={() => {
this.setState({ audio: file.link })
}}>
<a href={file.link}>{file.name}</a>
</li>
))}
</ul>
</div>
</div>
);
}
}
export default DownloadTest;
我的问题是如何使用Atlassian的react-beautiful-dnd将这段代码转换为拖放列表?我复制了他们在教程中使用的大多数代码,并试图一一插入我的代码片段,但是效果不如我想像。