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
Make table title field editable #501
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,141 @@ | ||
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 EditableField = 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; | ||
} | ||
} | ||
` | ||
|
||
class TitleField extends Component { | ||
constructor (props) { | ||
super(props) | ||
this.onclick = this.onclick.bind(this) | ||
this.deactivate = this.deactivate.bind(this) | ||
} | ||
|
||
onclick (ev) { | ||
ev.stopPropagation() | ||
ev.preventDefault() | ||
this.props.makeEditable() | ||
setTimeout(() => { | ||
this.titleInput.focus() | ||
this.titleInput.select() | ||
}, 0) | ||
} | ||
|
||
deactivate () { | ||
this.props.deactivate() | ||
} | ||
|
||
handleSave () { | ||
const { key, path } = this.props.dat | ||
const editValue = this.props.editing.editValue | ||
this.props.updateTitle(key, path, editValue) | ||
this.props.deactivate() | ||
} | ||
|
||
handleKeypress (ev) { | ||
const oldValue = this.props.editing.editValue | ||
const newValue = ev.target.value | ||
const editValue = newValue | ||
ev.stopPropagation() | ||
|
||
if (ev.code === 'Escape') { | ||
ev.preventDefault() | ||
this.props.deactivate() | ||
} else if (ev.code === 'Enter') { | ||
ev.preventDefault() | ||
const { key, path } = this.props.dat | ||
this.props.updateTitle(key, path, editValue) | ||
} else if (oldValue !== newValue) { | ||
this.props.editTitle(newValue) | ||
} | ||
} | ||
|
||
render () { | ||
const { writable, metadata } = this.props.dat | ||
const { title } = metadata | ||
const { isEditing, placeholderTitle, editValue } = this.props.editing | ||
|
||
if (isEditing && writable) { | ||
return ( | ||
<div> | ||
<Overlay onClick={() => this.deactivate()} /> | ||
<EditableField className='bg-white nt1 nb1 nl1 pl1 shadow-1 flex justify-between'> | ||
<input | ||
className='bn f6 normal w-100' | ||
defaultValue={editValue || title} | ||
onKeyUp={ev => this.handleKeypress(ev)} | ||
ref={input => { | ||
this.titleInput = input | ||
}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can i do it without having to use |
||
/> | ||
{editValue === title ? ( | ||
<PlainButton onClick={ev => this.deactivate(ev)}> | ||
Save | ||
</PlainButton> | ||
) : ( | ||
<GreenButton onClick={() => this.handleSave()}>Save</GreenButton> | ||
)} | ||
</EditableField> | ||
</div> | ||
) | ||
} | ||
|
||
if (writable) { | ||
return ( | ||
<EditableField> | ||
<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> | ||
</EditableField> | ||
) | ||
} | ||
|
||
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like adding above props, any suggestions on how to refactor this?