import React, { Component } from 'react' import styled from 'styled-components' interface Props { name: string onChangeName: Function readonly: boolean } interface State { showEdit:boolean name:string showError:boolean errorText:string } const KrNameTitle = styled.div` word-wrap: break-word; word-break: break-all; white-space:normal; font-size: 18px; color: #33383D; border-bottom:1px solid transparent; &:hover{ border-bottom: 1px solid #acb7bf; cursor: text; } ` const KrNameInput = styled.input` color: #35434c; width: 97%; display: block; background-color: transparent; border: none; font-size: 18px; line-height: 24px; min-height: 24px; &:focus{ border-bottom: 2px solid #1687d9; color: #00599b; } ` const KrNameEditExtra = styled.div` position: relative; z-index: 3; background: #fff; margin-right:10px; box-sizing: border-box; overflow:hidden; height:0px; padding:0px; transition: height 1s,padding 1s ; -moz-transition: height 1s,padding 1s; /* Firefox 4 */ -webkit-transition: height 1s,padding 1s; /* Safari 和 Chrome */ -o-transition: height 1s,padding 1s; ` const NameError = styled.div` position: absolute; top: 0; color: #eb4447; font-size: 12px; line-height: 20px; ` export default class krName extends Component { inputtext:HTMLDivElement; showEdit:HTMLDivElement; state = { showEdit:false, name:this.props.name, showError:false, errorText:'' } ShowEdit =()=>{ this.setState({ showEdit:true },()=>{ this.inputtext.focus(); }) } changeName = (e:any) => { const value = e.target.value; if(!value.length){ this.setState({ showError:true, errorText:'任务名称为必填项' }) }else if( value.length>500){ this.setState({ showError:true, errorText:`您已输${value.length}字,最多可输入500字` }) }else{ this.setState({ showError:false, errorText:'' }) } this.setState({ name:value }) } cancelEdit =() => { this.setState({ showEdit:false, name:this.props.name, showError:false, errorText:'' }) } sureEdit =()=>{ this.props.onChangeName(this.state.name) this.setState({ showEdit:false }) } render() { const {cancelEdit,sureEdit} = this; const {showError,showEdit,errorText,name} = this.state; return (
{ showEdit ? this.inputtext = r} style={showError?{borderBottom:'2px solid #cc2929'}:null}/> :{this.props.name} } { errorText.length ? {errorText}:''}
) } }