2018-03-19 02:16:27 +00:00
|
|
|
|
2018-04-07 06:29:59 +00:00
|
|
|
import classNames from 'classnames'
|
2018-03-19 13:23:38 +00:00
|
|
|
import Input from './Input'
|
2017-12-06 10:54:20 +00:00
|
|
|
import Icon from '../icon'
|
2018-03-19 13:23:38 +00:00
|
|
|
import inputProps from './inputProps'
|
2018-04-07 06:29:59 +00:00
|
|
|
import Button from '../button'
|
|
|
|
import { cloneElement } from '../_util/vnode'
|
2018-12-13 13:26:21 +00:00
|
|
|
import { getOptionProps, getComponentFromProp, isValidElement } from '../_util/props-util'
|
2018-04-07 06:31:41 +00:00
|
|
|
import PropTypes from '../_util/vue-types'
|
2017-12-06 10:54:20 +00:00
|
|
|
|
|
|
|
export default {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'AInputSearch',
|
2017-12-06 10:54:20 +00:00
|
|
|
props: {
|
2018-03-19 13:23:38 +00:00
|
|
|
...inputProps,
|
2017-12-06 10:54:20 +00:00
|
|
|
prefixCls: {
|
|
|
|
default: 'ant-input-search',
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
inputPrefixCls: {
|
|
|
|
default: 'ant-input',
|
|
|
|
type: String,
|
|
|
|
},
|
2018-07-17 14:05:18 +00:00
|
|
|
enterButton: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, PropTypes.object]),
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
2018-06-28 14:17:08 +00:00
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
event: 'change.value',
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onSearch (e) {
|
2018-09-05 13:28:54 +00:00
|
|
|
this.$emit('search', this.$refs.input.stateValue, e)
|
2017-12-07 04:31:12 +00:00
|
|
|
this.$refs.input.focus()
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
2018-06-10 01:29:18 +00:00
|
|
|
focus () {
|
|
|
|
this.$refs.input.focus()
|
|
|
|
},
|
|
|
|
|
|
|
|
blur () {
|
|
|
|
this.$refs.input.blur()
|
|
|
|
},
|
2018-04-07 06:29:59 +00:00
|
|
|
getButtonOrIcon () {
|
2018-09-05 13:28:54 +00:00
|
|
|
const { prefixCls, size, disabled } = this
|
2018-04-07 06:29:59 +00:00
|
|
|
const enterButton = getComponentFromProp(this, 'enterButton')
|
|
|
|
const enterButtonAsElement = Array.isArray(enterButton) ? enterButton[0] : enterButton
|
2018-09-05 13:28:54 +00:00
|
|
|
let node
|
|
|
|
if (!enterButton) {
|
|
|
|
node = <Icon class={`${prefixCls}-icon`} type='search' key='searchIcon' />
|
|
|
|
} else if (enterButtonAsElement.tag === 'button' || (enterButtonAsElement.componentOptions && enterButtonAsElement.componentOptions.Ctor.extendOptions.__ANT_BUTTON)) {
|
|
|
|
node = cloneElement(enterButtonAsElement, {
|
2018-04-07 06:29:59 +00:00
|
|
|
class: `${prefixCls}-button`,
|
|
|
|
props: { size },
|
|
|
|
})
|
2018-09-05 13:28:54 +00:00
|
|
|
} else {
|
|
|
|
node = (
|
|
|
|
<Button
|
|
|
|
class={`${prefixCls}-button`}
|
|
|
|
type='primary'
|
|
|
|
size={size}
|
|
|
|
disabled={disabled}
|
|
|
|
key='enterButton'
|
|
|
|
>
|
|
|
|
{enterButton === true ? <Icon type='search' /> : enterButton}
|
|
|
|
</Button>
|
|
|
|
)
|
2018-04-07 06:29:59 +00:00
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
return cloneElement(node, {
|
|
|
|
on: {
|
|
|
|
click: this.onSearch,
|
|
|
|
},
|
|
|
|
})
|
2018-04-07 06:29:59 +00:00
|
|
|
},
|
2017-12-06 10:54:20 +00:00
|
|
|
},
|
|
|
|
render () {
|
2018-04-07 06:29:59 +00:00
|
|
|
const { prefixCls, inputPrefixCls, size,
|
2018-12-13 13:26:21 +00:00
|
|
|
...others
|
2018-04-07 06:29:59 +00:00
|
|
|
} = getOptionProps(this)
|
2018-12-13 13:26:21 +00:00
|
|
|
const suffix = getComponentFromProp(this, 'suffix')
|
2018-04-07 06:29:59 +00:00
|
|
|
const enterButton = getComponentFromProp(this, 'enterButton')
|
|
|
|
const buttonOrIcon = this.getButtonOrIcon()
|
2018-12-13 13:26:21 +00:00
|
|
|
let searchSuffix = suffix ? [suffix, buttonOrIcon] : buttonOrIcon
|
|
|
|
if (Array.isArray(searchSuffix)) {
|
|
|
|
searchSuffix = searchSuffix.map((item, index) => {
|
|
|
|
if (!isValidElement(item) || item.key) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
return cloneElement(item, { key: index })
|
|
|
|
})
|
|
|
|
}
|
2018-04-07 06:29:59 +00:00
|
|
|
const inputClassName = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-enter-button`]: !!enterButton,
|
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
|
|
|
})
|
2018-09-05 13:28:54 +00:00
|
|
|
const on = { ...this.$listeners }
|
|
|
|
delete on.search
|
2017-12-07 04:31:12 +00:00
|
|
|
const inputProps = {
|
|
|
|
props: {
|
|
|
|
...others,
|
|
|
|
prefixCls: inputPrefixCls,
|
2018-04-07 06:29:59 +00:00
|
|
|
size,
|
|
|
|
suffix: searchSuffix,
|
2017-12-07 04:31:12 +00:00
|
|
|
},
|
|
|
|
attrs: this.$attrs,
|
2018-04-07 06:29:59 +00:00
|
|
|
on: {
|
|
|
|
pressEnter: this.onSearch,
|
2018-09-05 13:28:54 +00:00
|
|
|
...on,
|
2018-04-07 06:29:59 +00:00
|
|
|
},
|
2017-12-07 04:31:12 +00:00
|
|
|
}
|
2017-12-06 10:54:20 +00:00
|
|
|
return (
|
|
|
|
<Input
|
2017-12-07 04:31:12 +00:00
|
|
|
{...inputProps}
|
2018-04-07 06:29:59 +00:00
|
|
|
class={inputClassName}
|
2017-12-06 10:54:20 +00:00
|
|
|
ref='input'
|
2018-06-10 01:29:18 +00:00
|
|
|
/>
|
2017-12-06 10:54:20 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
2018-03-19 02:16:27 +00:00
|
|
|
|