/** * Created by TonyJiang on 16/8/26. */ import * as React from "react"; import TextField from 'material-ui/TextField'; import {autobind} from "core-decorators"; import {Unit, UnitType} from '../../../../drawing/Unit'; import {UnitSelector} from "../SpinnerEditor/UnitSelector"; export class SpinnerComponent extends React.Component { static propTypes = { showUnit: React.PropTypes.bool, value: React.PropTypes.oneOfType([ React.PropTypes.object, React.PropTypes.number ]), onChange: React.PropTypes.func, floatingLabelText: React.PropTypes.string, floatingLabelFixed: React.PropTypes.bool }; static defaultProps = { showUnit: false, value: Unit.Empty, onChange: () => {}, floatingLabelText: "", floatingLabelFixed: false }; state = { originValue: null, value: null, lastKeyCode: null, modify: false }; static fixedNum = { [UnitType.Pixel]: 0, [UnitType.Inch]: 2, [UnitType.Cm]: 2, [UnitType.Mm]: 1 }; static defaultValue = Unit.Empty; @autobind handleChange(event:any):void { const {value} = this.props; let newValue = event.target.value; if(newValue.length == 0){ newValue = 0.00; }else{ //如果最后一位是点, 并且只有一个点, 则在后边补全两位 if(newValue[newValue.length - 1] == '.'){ //只有一个点,第二个点不显示 if(newValue.indexOf('.') == newValue.lastIndexOf('.')){ if(this.state.lastKeyCode == 8){ newValue = parseFloat(newValue); }else{ newValue = parseFloat(newValue).toFixed(1); } }else{ return; } } } this.setState({ value: new Unit(parseFloat(newValue), value.type), modify: true }); } @autobind onBlur():void { const {onChange} = this.props; if (this.state.modify) { onChange(this.state.value); this.state.modify = false; } } @autobind onKeyDown(event:any):void { const {keyCode} = event; this.state.lastKeyCode = keyCode; if (keyCode == 13) { this.onBlur(); } } @autobind onChangeUnitType(unitType):void { const {value, onChange} = this.props; onChange(value.changeType(unitType)); // this.setValue(); this.setState({ open: false, unitType: unitType }) } @autobind addValue(){ const {value, onChange} = this.props; const hasUnit = value instanceof Unit; const newValue = hasUnit ? new Unit(value.value + 1, value.type) : value + 1; onChange(newValue); // const unitValue = this.getValue(); // this.setValue(new Unit(unitValue.value + 1, unitValue.type)); } @autobind reduceValue(){ const {value, onChange} = this.props; const hasUnit = value instanceof Unit; const newValue = hasUnit ? new Unit(value.value - 1, value.type) : value - 1; onChange(newValue); // const unitValue = this.getValue(); // this.setValue(new Unit(Math.max(0, unitValue.value - 1), unitValue.type)); } render() { //如果正在编辑, 使用state中的值 //如果没有编辑并且props的value不为null, 使用props的值 //否则使用defaultValue const {value, showUnit, floatingLabelText, floatingLabelFixed} = this.props; const hasUnit = value instanceof Unit; let textValue, unitValue:Unit; //如果值是单位类型 if (hasUnit){ unitValue = (this.state.modify ? this.state.value : value || SpinnerComponent.defaultValue) as Unit; textValue = this.state.modify ? unitValue.value : unitValue.value.toFixed(SpinnerComponent.fixedNum[unitValue.type]); } else { textValue = value; } return (
{ hasUnit && showUnit && }
) } }