This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make title field editable * add comment about `why innerRef` * use better name for event and action * stop propagation to row component on click save title * add prettier * reformat code * use async/await to read/write dat.json * add prettier * reducers: hint schema of titleUnderEdit * rename titleUnderEdit to titleEditInPlace
- Loading branch information
1 parent
f7e44a6
commit ea732f0
Showing
5 changed files
with
269 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import React, { Component } from 'react' | ||
import styled from 'styled-components' | ||
import Icon from './icon' | ||
import { Plain as PlainButton, Green as GreenButton } from './button' | ||
|
||
const Overlay = styled.div` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100vw; | ||
height: 100vh; | ||
background-color: rgba(0, 0, 0, 0.2); | ||
` | ||
|
||
const EditableFieldWrapper = styled.div` | ||
position: relative; | ||
h2 { | ||
position: relative; | ||
} | ||
.indicator { | ||
position: absolute; | ||
display: none; | ||
top: 0.25rem; | ||
right: 0; | ||
width: 0.75rem; | ||
} | ||
&:hover, | ||
&:focus { | ||
h2 { | ||
color: var(--color-blue); | ||
} | ||
.indicator { | ||
display: block; | ||
} | ||
} | ||
` | ||
|
||
const InputField = styled.input` | ||
:focus { | ||
outline: none; | ||
} | ||
` | ||
|
||
class TitleField extends Component { | ||
constructor (props) { | ||
super(props) | ||
this.onclick = this.onclick.bind(this) | ||
this.handleSave = this.handleSave.bind(this) | ||
this.deactivateTitleEditing = this.deactivateTitleEditing.bind(this) | ||
} | ||
|
||
onclick (ev) { | ||
ev.stopPropagation() | ||
ev.preventDefault() | ||
this.props.activateTitleEditing() | ||
|
||
// setTimeout? Because ref on input is set asynchronously, and later. So we can't do focus, select on it until ref is set | ||
setTimeout(() => { | ||
this.titleInput.focus() | ||
this.titleInput.select() | ||
}, 0) | ||
} | ||
|
||
deactivateTitleEditing () { | ||
this.props.deactivateTitleEditing() | ||
} | ||
|
||
handleSave () { | ||
const { key, path } = this.props.dat | ||
const { editValue } = this.props.titleEditInPlace | ||
if (editValue) this.props.updateTitle(key, path, editValue) | ||
this.props.deactivateTitleEditing() | ||
} | ||
|
||
handleKeyup (ev) { | ||
const oldValue = this.props.titleEditInPlace.editValue | ||
const newValue = ev.target.value | ||
ev.stopPropagation() | ||
|
||
if (ev.key === 'Escape') { | ||
ev.preventDefault() | ||
this.props.deactivateTitleEditing() | ||
} else if (ev.key === 'Enter') { | ||
ev.preventDefault() | ||
this.handleSave() | ||
} else if (oldValue !== newValue) { | ||
this.props.updateTemporaryTitleValue(newValue) | ||
} | ||
} | ||
|
||
render () { | ||
const { dat, titleEditInPlace } = this.props | ||
const { isEditing, editValue } = titleEditInPlace | ||
const { writable, metadata, key } = dat | ||
const { title } = metadata | ||
const placeholderTitle = `#${key}` | ||
|
||
if (isEditing && writable) { | ||
return ( | ||
<div> | ||
<Overlay onClick={() => this.deactivateTitleEditing()} /> | ||
<EditableFieldWrapper className='bg-white nt1 nb1 nl1 shadow-1 flex justify-between'> | ||
{/* why innerRef in following component? check here - styled-components/styled-components#102 */} | ||
<InputField | ||
className='bn f6 pl1 normal w-100' | ||
defaultValue={editValue || title} | ||
onKeyUp={ev => this.handleKeyup(ev)} | ||
innerRef={input => { | ||
this.titleInput = input | ||
}} | ||
/> | ||
{editValue === title ? ( | ||
<PlainButton onClick={ev => this.deactivateTitleEditing(ev)}> | ||
Save | ||
</PlainButton> | ||
) : ( | ||
<GreenButton | ||
onClick={ev => { | ||
ev.stopPropagation() | ||
this.handleSave() | ||
}} | ||
> | ||
Save | ||
</GreenButton> | ||
)} | ||
</EditableFieldWrapper> | ||
</div> | ||
) | ||
} | ||
|
||
if (writable) { | ||
return ( | ||
<EditableFieldWrapper> | ||
<h2 | ||
className='f6 f5-l normal truncate pr3' | ||
onClick={ev => this.onclick(ev)} | ||
> | ||
{title || placeholderTitle} | ||
<Icon | ||
name='edit' | ||
className='absolute top-0 bottom-0 right-0 color-neutral-30 indicator' | ||
/> | ||
</h2> | ||
</EditableFieldWrapper> | ||
) | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2 className='f6 f5-l normal truncate pr3'> | ||
{title || placeholderTitle} | ||
</h2> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default TitleField |
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,38 @@ | ||
'use strict' | ||
|
||
import { connect } from 'react-redux' | ||
import TitleField from '../components/title-field' | ||
import { | ||
updateTitle, | ||
activateTitleEditing, | ||
updateTemporaryTitleValue, | ||
deactivateTitleEditing | ||
} from '../actions' | ||
|
||
const makeMapStateToProps = (initialState, initialProps) => { | ||
const { key } = initialProps.dat | ||
const mapStateToProps = state => ({ | ||
dat: state.dats[key], | ||
titleEditInPlace: state.titleEditInPlace | ||
}) | ||
|
||
return mapStateToProps | ||
} | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return { | ||
updateTitle: (key, path, editValue) => | ||
dispatch(updateTitle(key, path, editValue)), | ||
activateTitleEditing: (title, editable) => | ||
dispatch(activateTitleEditing(title)), | ||
updateTemporaryTitleValue: title => | ||
dispatch(updateTemporaryTitleValue(title)), | ||
deactivateTitleEditing: () => dispatch(deactivateTitleEditing()) | ||
} | ||
} | ||
|
||
const TitleFieldContainer = connect(makeMapStateToProps, mapDispatchToProps)( | ||
TitleField | ||
) | ||
|
||
export default TitleFieldContainer |
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