Files
redash/client/app/components/EditInPlace.jsx
koooge 2da511021e Frontend lint update (#3253)
* client: Add lint command

Signed-off-by: koooge <koooooge@gmail.com>

* client: Override eslint rule object-curly-newline to keep current style

Signed-off-by: koooge <koooooge@gmail.com>

* client: Override eslint rule no-else-return to keep current style

Signed-off-by: koooge <koooooge@gmail.com>

* client: Fix eslint import/named

Signed-off-by: koooge <koooooge@gmail.com>

* client: eslint-5

Signed-off-by: koooge <koooooge@gmail.com>

* codeclimate: Delete the old setting

Signed-off-by: koooge <koooooge@gmail.com>

* client: Downgrade eslint 5 to 4 in codeclimate

Signed-off-by: koooge <koooooge@gmail.com>

* client: npx install-peerdeps --dev eslint-config-airbnb

Signed-off-by: koooge <koooooge@gmail.com>

* client: Enbale .jsx lint

Signed-off-by: koooge <koooooge@gmail.com>

* client: Set warn

Signed-off-by: koooge <koooooge@gmail.com>

* client: Fix lint indent, implicit-arrow-linebreak, lines-between-class-members

Signed-off-by: koooge <koooooge@gmail.com>

* client: Disable eslint operator-linebreak

Signed-off-by: koooge <koooooge@gmail.com>

* Revert "client: Downgrade eslint 5 to 4 in codeclimate"

This reverts commit f0fb0f0059.

* client: Fix react/button-has-type

Signed-off-by: koooge <koooooge@gmail.com>

* client: Disable an eslint rule react/jsx-one-expression-per-line

Signed-off-by: koooge <koooooge@gmail.com>

* codeclimate: Disable no-multiple-empty-lines

Signed-off-by: koooge <koooooge@gmail.com>

* client: Disable eslint react/destructuring-assignment

Signed-off-by: koooge <koooooge@gmail.com>
2019-01-29 17:25:58 +02:00

93 lines
2.2 KiB
JavaScript

import React from 'react';
import PropTypes from 'prop-types';
import { react2angular } from 'react2angular';
export class EditInPlace extends React.Component {
static propTypes = {
ignoreBlanks: PropTypes.bool,
isEditable: PropTypes.bool,
editor: PropTypes.string.isRequired,
placeholder: PropTypes.string,
value: PropTypes.string,
onDone: PropTypes.func.isRequired,
};
static defaultProps = {
ignoreBlanks: false,
isEditable: true,
placeholder: '',
value: '',
};
constructor(props) {
super(props);
this.state = {
editing: false,
};
this.inputRef = React.createRef();
const self = this;
this.componentDidUpdate = (prevProps, prevState) => {
if (self.state.editing && !prevState.editing) {
self.inputRef.current.focus();
}
};
}
startEditing = () => {
if (this.props.isEditable) {
this.setState({ editing: true });
}
};
stopEditing = () => {
const newValue = this.inputRef.current.value;
const ignorableBlank = this.props.ignoreBlanks && newValue === '';
if (!ignorableBlank && newValue !== this.props.value) {
this.props.onDone(newValue);
}
this.setState({ editing: false });
};
keyDown = (event) => {
if (event.keyCode === 13 && !event.shiftKey) {
event.preventDefault();
this.stopEditing();
} else if (event.keyCode === 27) {
this.setState({ editing: false });
}
};
renderNormal = () => (
<span
role="presentation"
onFocus={this.startEditing}
onClick={this.startEditing}
className={this.props.isEditable ? 'editable' : ''}
>
{this.props.value || this.props.placeholder}
</span>
);
renderEdit = () => React.createElement(this.props.editor, {
ref: this.inputRef,
className: 'rd-form-control',
defaultValue: this.props.value,
onBlur: this.stopEditing,
onKeyDown: this.keyDown,
});
render() {
return (
<span className={'edit-in-place' + (this.state.editing ? ' active' : '')}>
{this.state.editing ? this.renderEdit() : this.renderNormal()}
</span>
);
}
}
export default function init(ngModule) {
ngModule.component('editInPlace', react2angular(EditInPlace));
}
init.init = true;