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 {DesignerHost} from "../../DesignerHost"; import {FlatButton} from "material-ui"; import {Dialog} from "material-ui"; import {DataSourceModel} from "../ui-model/DataSourceModel"; import DataSourceTreeView from "../data-source-view/DataSourceTreeView"; import {transaction} from "mobx"; @observer export class TextFieldEditor extends Editor<{}> { state = { value: '', modify: false, focus: false, openDialog: false, errorMsg: '' }; private _dataSourceModel: DataSourceModel; componentWillMount() { this.initialize(this.props); } componentWillReceiveProps(props) { this.initialize(props); } initialize(props) { // 初始化model this._dataSourceModel = DesignerHost.instance().uiModel.dataSourceModel; let value = this.getValueByProps(props); const dataSource = this._dataSourceModel.dataSource; if (value && value.indexOf('=') === 0) { value = value.substr(1); // 转换成英文校验 const regexp = /.*[\u4e00-\u9fa5]+.*$/; const isHasCn = regexp.test(value); const result: any = isHasCn ? dataSource.convertCnToEng(value) : dataSource.convertEngToCn(value); if (result.errorMsg) { this.handleError(result.errorMsg); } else { this.setState({ value: `=${result.fieldPath}`, errorMsg: '' }); } } else { this.setState({ value: value, errorMsg: '' }); } } 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; } getValueByProps(props) { if (!props) { props = this.props; } const items = props.items; const property = 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 { let fieldValue, validateInputVal = '', inputValue = event.target.value; if (inputValue.substr(0, 1) !== '=') { fieldValue = inputValue; this.setState({ errorMsg: '' }); } else { const dataSource = this._dataSourceModel.dataSource; validateInputVal = inputValue.substr(1); // 转换成英文校验 const result: any = dataSource.convertCnToEng(validateInputVal); if (result.errorMsg) { fieldValue = inputValue; this.handleError(result.errorMsg); } else { this.setState({ errorMsg: '' }); fieldValue = `=${result.fieldPath}`; } } const {items, property} = this.props; items.map(item => { item.setPropertyValue(property, fieldValue); }); DesignerHost.instance().historyService.snap(); } @autobind handleConfirm(): void { const dataSourceTreeModel = DesignerHost.instance().uiModel.dataSourceTreeModel; const {selectedFieldPath} = dataSourceTreeModel; if (selectedFieldPath.length > 0) { this.handleSelectDataField(selectedFieldPath); } else { this.setState({openDialog: false}); } } @autobind handleCancel(): void { this.setState({openDialog: false}); } @autobind handleSelectDataField(value: Array) { const {items, property} = this.props; transaction(() => { items.map(item => { item.setPropertyValue(property, `=${value.join('.')}`); }); this.setState({ openDialog: false, errorMsg: '' }); }); DesignerHost.instance().historyService.snap(); } @autobind handleOpen(): void { this.setState({openDialog: true}); }; @autobind onFocus(): void { this.setState({focus: true}); } handleError(errorMsg) { this.setState({ errorMsg: errorMsg }); } protected renderValueEditor() { const {items, property} = this.props; let value = this.getValue(); const dataSource = this._dataSourceModel.dataSource; // 去掉等号 if (value.indexOf('=') === 0) { value = value.substr(1); const regexp = /.*[\u4e00-\u9fa5]+.*$/; const isHasCn = regexp.test(value); if (!isHasCn) { // render用的value const fieldValue = dataSource.convertEngToCn(value); value = fieldValue ? `=${fieldValue}` : `=${value}`; } else { value = `=${value}`; } } const actions = [ , , ]; // 为什么 value 在没有值的时候要默认设置为 '' ? // https://facebook.github.io/react/docs/forms.html#controlled-components return (
) } }