import React, { PureComponent } from 'react'; import styled from 'styled-components'; import uuid from 'uuid'; interface Props { placeHolder: string onSearch:Function } interface State { id: string keywords?: string active: boolean } const SearchWrapper = styled.div` width: 174px; height: 22px; ` const InputWrapper = styled.div` position: relative; z-index: 1; padding-right: 40px; padding-left: 5px; overflow: hidden; input { border: 0px none; min-height: 20px; line-height: 20px; margin-bottom: 2px; padding: 0; background-color: transparent; width: 100%; display: inline-block; } &::before { content: ''; position: absolute; bottom: 0; height: 1px; left: 0; right: 0; background-color: #DEDEDE; } &::after { content: ''; position: absolute; bottom: 0; height: 1px; left: ${(p:any) => p.active ? '0%' :'50%'}; right: ${(p:any) => p.active ? '0%' :'50%'}; background-color: #1b85d6; transition: all .3s ease; } ` const PlaceHolder = styled.label` position: absolute; left: 0px; top: 0px; z-index: 2; cursor: text; color: #acb7bf; padding-left: 5px; display: block; ` const IconWrapper = styled.div` position: absolute; width: 40px; height: 20px; top: 0; right: 0; display: flex; align-items: center; justify-content: flex-end; transform: ${(p:any) => p.active ? 'translateX(0)' : 'translateX(19px)'}; transition: transform .3s ease; ` const SearchIcon = styled.i ` font-size: 14px; cursor: pointer; color:${(p:any) => p.active ? '#1b85d6' : '#D8D8D8'}; ` const ClearIcon = styled.i` font-size: 14px; color: #1b85d6; margin-left: 5px; transition: all .5s ease; cursor: pointer; opacity:${(p:any) => p.active ? 1 : 0}; transform:${(p:any) => p.active ? 'scale(1)' : 'scale(0)'}; ` export default class Search extends PureComponent { constructor(p:Props){ super(p); this.state = { id: uuid(), active: false } } static defaultProps = { placeHolder: '请输入关键字' } handleEnterPress = (e:any) => { if(e.nativeEvent.keyCode == 13){ this.handleSearch() } } handleChange = (e:any) => { this.setState({ keywords: e.target.value }) } handleFocus = () => { if(this.state.active) return; this.setState({ active: true }) } handleBlur = () => { const {keywords} = this.state; if(!keywords || !keywords.length){ this.setState({ active: false }) } } handleSearch = () => { const {keywords} = this.state; this.props.onSearch(keywords); } handleClear = () => { this.setState({ keywords:'', active: false }); this.props.onSearch('') } render() { const {placeHolder} = this.props; const {id,keywords,active} = this.state; return ( {placeHolder} ) } }