insomnia/app/components/base/Editable.js
Gregory Schier 9e84bc4387 Workspaces, Cookies, and More! (#31)
* Start on workspace dropdown and upgrade fontawesome

* WorkspaceDropdown start and Elm components!

* Lots of CSS shit

* Refactor some db stuff and move filter out of sidebar

* Adjust dropdown css

* Handle duplicate header names, and stuff

* Shitty cookies tab

* fixed cookie table a bit

* Modal refactor

* Starteed cookie modal design

* Better cookie storage and filter cookie modal

* Cookie editor round 1

* Fix kve cursor jumping and form encoding templating

* New cookies now show up in filter

* Checkpoint

* Stuff and fix environments css

* Added manage cookies button to cookie pane

* Fix accidental sidebar item drag on sidebar resize

* Environments modal is looking pretty good now

* Pretty much done environments nad cookies

* Some changes

* Fixed codemirror in modals

* Fixed some things

* Add basic proxy support

* Updated shortcuts

* Code snippet generation

* Some style

* bug fix

* Code export now gets cookies for correct domain
2016-08-15 10:04:36 -07:00

85 lines
1.8 KiB
JavaScript

import React, {Component, PropTypes} from 'react';
import {DEBOUNCE_MILLIS} from '../../lib/constants';
class Editable extends Component {
constructor (props) {
super(props);
this.state = {
editing: false
}
}
_handleEditStart () {
this.setState({editing: true});
setTimeout(() => {
this._input && this._input.focus();
this._input && this._input.select();
});
}
_handleEditEnd () {
const value = this._input.value.trim();
if (!value) {
// Don't do anything if it's empty
return;
}
// This timeout prevents the UI from showing the old value after submit.
// It should give the UI enough time to redraw the new value.
setTimeout(() => {
this.setState({editing: false});
}, DEBOUNCE_MILLIS);
this.props.onSubmit(value);
}
_handleEditKeyDown (e) {
if (e.keyCode === 13) {
// Pressed Enter
this._handleEditEnd();
} else if (e.keyCode === 27) {
// Pressed Escape
this._input && this._input.blur();
}
}
render () {
const {value, singleClick, ...extra} = this.props;
const {editing} = this.state;
if (editing) {
return (
<input
className="editable"
type="text"
ref={n => this._input = n}
defaultValue={value}
onKeyDown={e => this._handleEditKeyDown(e)}
onBlur={e => this._handleEditEnd()}
{...extra}
/>
)
} else {
return (
<div className="editable"
onClick={e => singleClick && this._handleEditStart()}
onDoubleClick={e => this._handleEditStart()} {...extra}>
{value}
</div>
)
}
}
}
Editable.propTypes = {
onSubmit: PropTypes.func.isRequired,
value: PropTypes.string.isRequired,
// Optional
singleClick: PropTypes.bool
};
export default Editable;