forked from pavol-brunclik-m2ms/fragalysis-frontend
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'remotes/origin/#469' into allfunctionality
- Loading branch information
Showing
13 changed files
with
834 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
import { makeStyles, TextField, IconButton, Tooltip, Grid } from '@material-ui/core'; | ||
import { Edit } from '@material-ui/icons'; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
search: { | ||
width: '100%' | ||
}, | ||
fontSizeSmall: { | ||
fontSize: '0.82rem' | ||
} | ||
})); | ||
|
||
const EditableInput = ({ dataText, index, updateText }) => { | ||
const inputRef = useRef(null); | ||
const [inputVisible, setInputVisible] = useState(false); | ||
const [text, setText] = useState(dataText); | ||
const classes = useStyles(); | ||
|
||
const onClickOutSide = e => { | ||
if (inputRef.current && !inputRef.current.contains(e.target)) { | ||
setInputVisible(false); | ||
if (updateText && text !== dataText) { | ||
updateText(text); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
// Handle outside clicks on mounted state | ||
if (inputVisible) { | ||
document.addEventListener('mousedown', onClickOutSide); | ||
} | ||
|
||
// This is a necessary step to "dismount" unnecessary events when we destroy the component | ||
return () => { | ||
document.removeEventListener('mousedown', onClickOutSide); | ||
}; | ||
}); | ||
|
||
return ( | ||
<React.Fragment> | ||
{inputVisible ? ( | ||
<TextField | ||
className={classes.search} | ||
ref={inputRef} | ||
value={text} | ||
onChange={e => { | ||
setText(e.target.value); | ||
}} | ||
/> | ||
) : ( | ||
<Grid item key={index}> | ||
{<span onClick={() => setInputVisible(true)}>{text}</span>} | ||
{ | ||
<IconButton color={'primary'} size="small" onClick={() => setInputVisible(true)}> | ||
<Tooltip title="Edit"> | ||
<Edit className={classes.fontSizeSmall} /> | ||
</Tooltip> | ||
</IconButton> | ||
} | ||
</Grid> | ||
)} | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
export default EditableInput; |
Oops, something went wrong.