insomnia/packages/insomnia-app/app/ui/components/base/editable.js

117 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-06-25 17:42:50 +00:00
import React, { PureComponent } from 'react';
2017-08-10 01:56:27 +00:00
import PropTypes from 'prop-types';
import autobind from 'autobind-decorator';
2016-07-08 06:02:40 +00:00
@autobind
class Editable extends PureComponent {
2018-06-25 17:42:50 +00:00
constructor(props) {
super(props);
this.state = {
editing: false,
};
}
2016-07-08 06:02:40 +00:00
2018-06-25 17:42:50 +00:00
_handleSetInputRef(n) {
this._input = n;
}
2016-11-29 21:28:22 +00:00
2018-06-25 17:42:50 +00:00
_handleSingleClickEditStart() {
2016-11-29 21:28:22 +00:00
if (this.props.singleClick) {
this._handleEditStart();
}
}
2016-11-29 21:28:22 +00:00
2018-06-25 17:42:50 +00:00
_handleEditStart() {
this.setState({ editing: true });
2016-07-08 06:02:40 +00:00
setTimeout(() => {
this._input && this._input.focus();
this._input && this._input.select();
2016-07-08 06:02:40 +00:00
});
if (this.props.onEditStart) {
this.props.onEditStart();
}
}
2016-07-08 06:02:40 +00:00
2018-06-25 17:42:50 +00:00
_handleEditEnd() {
const value = this._input.value.trim();
if (!value) {
// Don't do anything if it's empty
return;
}
2016-11-10 21:03:12 +00:00
this.props.onSubmit(value);
// This timeout prevents the UI from showing the old value after submit.
// It should give the UI enough time to redraw the new value.
2018-06-25 17:42:50 +00:00
setTimeout(async () => this.setState({ editing: false }), 100);
}
2016-07-08 06:02:40 +00:00
2018-06-25 17:42:50 +00:00
_handleEditKeyDown(e) {
2016-07-08 06:02:40 +00:00
if (e.keyCode === 13) {
// Pressed Enter
this._handleEditEnd();
} else if (e.keyCode === 27) {
// Pressed Escape
2016-11-10 21:03:12 +00:00
// NOTE: This blur causes a save because we save on blur
// TODO: Make escape blur without saving
this._input && this._input.blur();
2016-07-08 06:02:40 +00:00
}
}
2016-07-08 06:02:40 +00:00
2018-06-25 17:42:50 +00:00
render() {
const {
value,
singleClick,
onEditStart, // eslint-disable-line no-unused-vars
className,
renderReadView,
...extra
} = this.props;
2018-06-25 17:42:50 +00:00
const { editing } = this.state;
2016-07-08 06:02:40 +00:00
if (editing) {
return (
2018-06-25 17:42:50 +00:00
<input
{...extra}
className={`editable ${className || ''}`}
type="text"
ref={this._handleSetInputRef}
defaultValue={value}
onKeyDown={this._handleEditKeyDown}
onBlur={this._handleEditEnd}
2016-07-08 06:02:40 +00:00
/>
);
2016-07-08 06:02:40 +00:00
} else {
const readViewProps = {
className: `editable ${className}`,
title: singleClick ? 'Click to edit' : 'Double click to edit',
onClick: this._handleSingleClickEditStart,
onDoubleClick: this._handleEditStart,
...extra,
};
2018-05-25 04:08:39 +00:00
if (renderReadView) {
return renderReadView(value, readViewProps);
} else {
return <span {...readViewProps}>{value}</span>;
}
2016-07-08 06:02:40 +00:00
}
}
}
Editable.propTypes = {
onSubmit: PropTypes.func.isRequired,
value: PropTypes.string.isRequired,
// Optional
2018-05-25 04:08:39 +00:00
renderReadView: PropTypes.func,
singleClick: PropTypes.bool,
onEditStart: PropTypes.func,
className: PropTypes.string,
};
2016-07-08 06:02:40 +00:00
export default Editable;