import * as React from 'react'; import classnames from 'classnames'; import './index.scss'; export interface SearchProps { prefix?: string; defaultValue?: string; handlerCallback?: Function; } export interface SearchPropsWidthDefault { prefix: string; defaultValue: string; handlerCallback: Function; } export interface SearchState { inputValue: string; inputFocus: boolean, hasValue: boolean } export default class Search extends React.Component { static defaultProps = { prefix: 'upaas', defaultValue: '' } input: any constructor(props: SearchProps) { super(props); const { defaultValue } = props as SearchPropsWidthDefault; this.state = { inputValue: defaultValue, inputFocus: false, hasValue: false, } } componentDidMount() { } handleClickLabel = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); const { handlerCallback } = this.props as SearchPropsWidthDefault; handlerCallback && handlerCallback({ 'eventType': 'click' }) this.setState({ inputFocus: true }) this.input.focus(); } handleFocus = (e: React.FocusEvent) => { const { handlerCallback } = this.props as SearchPropsWidthDefault; handlerCallback && handlerCallback({ 'eventType': 'focus', 'value': this.state.inputValue }) this.setState({ inputFocus: true }) } handleBlur = (e: React.FocusEvent) => { const { handlerCallback } = this.props as SearchPropsWidthDefault; const { inputValue: value } = this.state; handlerCallback && handlerCallback({ 'eventType': 'blur', value }) } handleChange = (e: React.ChangeEvent) => { const value = e.target.value; const { handlerCallback } = this.props as SearchPropsWidthDefault; handlerCallback && handlerCallback({ 'eventType': 'change', value }) this.setState({ inputValue: value, hasValue: !!value.trim().length }) } handleClear = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); const { handlerCallback } = this.props as SearchPropsWidthDefault; handlerCallback && handlerCallback({ 'eventType': 'change', 'value': '' }) this.setState({ inputValue: '' }) } handleCancle = (e: React.MouseEvent) => { e && e.stopPropagation(); e && e.nativeEvent.stopImmediatePropagation(); const { handlerCallback } = this.props as SearchPropsWidthDefault; handlerCallback && handlerCallback({ "eventType": 'cancle' }) this.setState({ inputFocus: false, hasValue: false, inputValue: '' }) } handleWrapClick = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); } render() { const { props, state } = this; const { prefix } = props; const { inputValue, inputFocus, hasValue } = state; const classStr = classnames(`${prefix}-search`, { 'active': inputFocus }); const borderClass = classnames(`${prefix}-search-border`) const labelClass = classnames(`${prefix}-search-label`) const palceholderClass = classnames(`${prefix}-search-placeholder`, { 'hidden': hasValue }); const palceholderInnerClass = classnames(`${prefix}-search-placeholder-inner`); const inputClass = classnames(`${prefix}-search-input`); const searchIconClass = classnames(`${prefix}-search-search-icon`, { 'font-ic_com_search': true }) const clearIconClass = classnames(`${prefix}-search-close-icon`, { 'font-domain-connot-use-icon': true }) const cancleClass = classnames(`${prefix}-search-cancle`) return (
取消
) } }