diff options
author | tschicke-brown <tyler_schicke@brown.edu> | 2019-02-07 23:50:47 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-07 23:50:47 -0500 |
commit | 35574735a60fbc7b1c7051c59db56a8485f50a21 (patch) | |
tree | 42dc1f8fbe639d53e7c6d3db144bd30d984c21de /src/views/EditableView.tsx | |
parent | 8d264be35a511204449c22d0a4b1754e241a3421 (diff) | |
parent | 90296f23320df43e73fb1bd936428f19f0f705a9 (diff) |
Merge pull request #5 from browngraphicslab/schema
Schema
Diffstat (limited to 'src/views/EditableView.tsx')
-rw-r--r-- | src/views/EditableView.tsx | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/views/EditableView.tsx b/src/views/EditableView.tsx new file mode 100644 index 000000000..357c97af2 --- /dev/null +++ b/src/views/EditableView.tsx @@ -0,0 +1,38 @@ +import React = require('react') +import { observer } from 'mobx-react'; +import { observable, action } from 'mobx'; + +export interface EditableProps { + GetValue(): string; + SetValue(value: string): boolean; + contents: any; +} + +@observer +export class EditableView extends React.Component<EditableProps> { + @observable + editing: boolean = false; + + @action + onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { + if (e.key == "Enter" && !e.ctrlKey) { + this.props.SetValue(e.currentTarget.value); + this.editing = false; + } else if (e.key == "Escape") { + this.editing = false; + } + } + + render() { + if (this.editing) { + return <input defaultValue={this.props.GetValue()} onKeyDown={this.onKeyDown} autoFocus onBlur={action(() => this.editing = false)}></input> + } else { + return ( + <div> + {this.props.contents} + <button onClick={action(() => this.editing = true)}>Edit</button> + </div> + ) + } + } +}
\ No newline at end of file |