Skip to content

Commit

Permalink
feat: export FileList component's editBtnClick
Browse files Browse the repository at this point in the history
  • Loading branch information
HOUSHENGREN committed May 17, 2023
1 parent b002426 commit 0e0269e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function App() {
const [openedFileIDs, setOpenedFileIDs] = useState([])
const [unsaveFileIDs, setUnsaveFileIDs] = useState([])
const [searchedFiles, setSearchedFiles] = useState([])
const fileListNode = useRef(null)
const fileListNode = useRef()

console.log(openedFileIDs, '88899998')
const openedFiles = openedFileIDs.map(ID => {
Expand Down Expand Up @@ -106,21 +106,21 @@ function App() {
const fileListArr = searchedFiles.length ? searchedFiles : files

const createNewFile = () => {
const id = v4()
const item = {
id: v4(),
title: '',
body: '## 请输入 markdown',
createdAt: new Date().getTime()
}

const newFiles = [
...files,
{
id,
title: '',
body: '## 请输入 markdown',
createdAt: new Date().getTime()
}
...files,
item
]
setFiles(newFiles)
console.log('fileListNode', fileListNode)

// fileListNode.current.onFileClick(id) // todo ? 没有用
fileListNode.current.editBtnClick(item) // todo ? 没有用
}


Expand Down
16 changes: 13 additions & 3 deletions src/components/FileList.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { useState, useEffect, useRef } from "react"
import { useState, useEffect, useRef, forwardRef, useImperativeHandle } from "react"
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faEdit, faTrash, faTimes } from '@fortawesome/free-solid-svg-icons'
import { faMarkdown } from '@fortawesome/free-brands-svg-icons'
import propTypes from 'prop-types'
import useKeyPress from "../hooks/useKeyPress"

const FileList = ({files, onFileClick, onSaveEdit, onFileDelete}) => {
const FileList = forwardRef(
({files, onFileClick, onSaveEdit, onFileDelete}, ref) => {

//暴露方法 handelValid 给父组件
useImperativeHandle(ref, () => ({
editBtnClick
}))

const [editId, setEditId] = useState(null)
const [value, setValue] = useState('')
const node = useRef(null)

const editBtnClick = (file) => { setValue(file.title); setEditId(file.id) }

const enterPressed = useKeyPress(13)
const escPressed = useKeyPress(27)

Expand Down Expand Up @@ -54,7 +63,7 @@ const FileList = ({files, onFileClick, onSaveEdit, onFileDelete}) => {
<button
type="button"
className="icon-button col-2"
onClick={() => { setValue(file.title); setEditId(file.id)}}
onClick={() => editBtnClick(file)}
>
<FontAwesomeIcon title="编辑" size="lg" icon={faEdit} />
</button>
Expand Down Expand Up @@ -97,6 +106,7 @@ const FileList = ({files, onFileClick, onSaveEdit, onFileDelete}) => {
</ul>
)
}
)

FileList.propTypes = {
files: propTypes.array,
Expand Down
4 changes: 4 additions & 0 deletions src/components/FileSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import useKeyPress from "../hooks/useKeyPress"
*
*/

// #region
// 56465
// #endregion

const FileSearch = ({ title, onFileSearch=(() => {}) }) => {
const [inputActive, setInputActive] = useState(false)
const [value, setValue] = useState('')
Expand Down

0 comments on commit 0e0269e

Please sign in to comment.