blob: c946d68e1e29decffb5c6fc27718eb728d0708fc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
import React = require('react');
import { observer } from 'mobx-react';
import { observable, action, trace } from 'mobx';
import "./EditableView.scss";
export interface EditableProps {
/**
* Called to get the initial value for editing
* */
GetValue(): string;
/**
* Called to apply changes
* @param value - The string entered by the user to set the value to
* @returns `true` if setting the value was successful, `false` otherwise
* */
SetValue(value: string): boolean;
OnFillDown?(value: string): void;
/**
* The contents to render when not editing
*/
contents: any;
height?: number;
display?: string;
oneLine?: boolean;
}
/**
* Customizable view that can be given an arbitrary view to render normally,
* but can also be edited with customizable functions to get a string version
* of the content, and set the value based on the entered string.
*/
@observer
export class EditableView extends React.Component<EditableProps> {
@observable
editing: boolean = false;
@action
onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
if (!e.ctrlKey) {
if (this.props.SetValue(e.currentTarget.value)) {
this.editing = false;
}
} else if (this.props.OnFillDown) {
this.props.OnFillDown(e.currentTarget.value);
this.editing = false;
}
} else if (e.key === "Escape") {
this.editing = false;
}
}
@action
onClick = (e: React.MouseEvent) => {
this.editing = true;
e.stopPropagation();
}
render() {
if (this.editing) {
return <input className="editableView-input" defaultValue={this.props.GetValue()} onKeyDown={this.onKeyDown} autoFocus onBlur={action(() => this.editing = false)}
style={{ display: this.props.display }} />;
} else {
return (
<div className={`editableView-container-editing${this.props.oneLine ? "-oneLine" : ""}`} style={{ display: this.props.display, height: "auto", maxHeight: `${this.props.height}` }}
onClick={this.onClick} >
<span>{this.props.contents}</span>
</div>
);
}
}
}
|