import * as React from "react"; import TextField from 'material-ui/TextField'; import {observer} from "mobx-react/index"; import {autobind} from "core-decorators"; import Editor from "./Editor"; import {ReportItemBase} from "../../../items/ReportItemBase"; import {Property} from "../../../items/properties/Property"; import {DesignerHost} from "../../DesignerHost"; @observer export class TextEditor extends Editor<{}> { state = { value: '', modify: false, focus: false }; componentWillMount() { this.initialize(this.props); } componentWillReceiveProps(props) { this.initialize(props); } initialize(props) { this.state.value = this.getValue(); } getValue(){ const items = this.props.items; const property = this.props.property; const values = items.map(item => item.getPropertyValue(property)); let val = values[0]; for (var value of values) { if (val != value) { val = '--'; break; } } return val; } @autobind handleChange(event:any):void { const {items, property} = this.props; items.map(item => { item.setPropertyValue(property, event.target.value); }); // this.setState({ // value: event.target.value, // modify: true // }); } @autobind onBlur():void { //如果失去焦点时的值, 已经不等于初始值, 则需要snap if(this.getValue() != this.state.value){ DesignerHost.instance().historyService.snap(); } // if(this.state.modify){ // const {items, property} = this.props; // items.map(item => { // item.setPropertyValue(property, this.state.value); // }); // // this.state.modify = false; // this.state.focus = false; // // DesignerHost.instance().historyService.snap(); // } } @autobind onFocus():void { this.setState({focus: true}); } @autobind onKeyDown(event:any):void { const {charCode, keyCode} = event; if (keyCode == 13) { this.onBlur(); } } protected renderValueEditor() { const {items, property} = this.props; const value = this.getValue(); // 为什么 value 在没有值的时候要默认设置为 '' ? // https://facebook.github.io/react/docs/forms.html#controlled-components return (
) } }