You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/input/Search.jsx

107 lines
2.9 KiB

7 years ago
import classNames from 'classnames'
import Input from './Input'
7 years ago
import Icon from '../icon'
import inputProps from './inputProps'
7 years ago
import Button from '../button'
import { cloneElement } from '../_util/vnode'
import { getOptionProps, getComponentFromProp } from '../_util/props-util'
7 years ago
import PropTypes from '../_util/vue-types'
7 years ago
export default {
name: 'InputSearch',
props: {
...inputProps,
7 years ago
prefixCls: {
default: 'ant-input-search',
type: String,
},
inputPrefixCls: {
default: 'ant-input',
type: String,
},
7 years ago
enterButton: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, PropTypes.object]),
7 years ago
},
computed: {
},
methods: {
onSearch (e) {
7 years ago
this.$emit('search', this.$refs.input.stateValue)
this.$refs.input.focus()
7 years ago
},
7 years ago
getButtonOrIcon () {
const { prefixCls, size } = this
const enterButton = getComponentFromProp(this, 'enterButton')
if (!enterButton) {
return <Icon class={`${prefixCls}-icon`} type='search' key='searchIcon' />
}
const enterButtonAsElement = Array.isArray(enterButton) ? enterButton[0] : enterButton
if (enterButtonAsElement.componentOptions && enterButtonAsElement.componentOptions.Ctor.extendOptions.__ANT_BUTTON) {
return cloneElement(enterButtonAsElement, {
class: `${prefixCls}-button`,
props: { size },
on: {
click: this.onSearch,
},
})
} else if (enterButtonAsElement.tag === 'button') {
return cloneElement(enterButtonAsElement[0], {
on: {
click: this.onSearch,
},
})
}
return (
<Button
class={`${prefixCls}-button`}
type='primary'
size={size}
onClick={this.onSearch}
key='enterButton'
>
{enterButton === true ? <Icon type='search' /> : enterButton}
</Button>
)
},
7 years ago
},
render () {
7 years ago
const { prefixCls, inputPrefixCls, size,
suffix, ...others
} = getOptionProps(this)
const enterButton = getComponentFromProp(this, 'enterButton')
const buttonOrIcon = this.getButtonOrIcon()
const searchSuffix = suffix ? [suffix, buttonOrIcon] : buttonOrIcon
const inputClassName = classNames(prefixCls, {
[`${prefixCls}-enter-button`]: !!enterButton,
[`${prefixCls}-${size}`]: !!size,
})
7 years ago
const inputProps = {
props: {
...others,
prefixCls: inputPrefixCls,
7 years ago
size,
suffix: searchSuffix,
7 years ago
},
attrs: this.$attrs,
7 years ago
on: {
pressEnter: this.onSearch,
},
7 years ago
}
7 years ago
return (
<Input
7 years ago
{...inputProps}
7 years ago
class={inputClassName}
7 years ago
ref='input'
>
7 years ago
{/* <Icon
7 years ago
slot='suffix'
class={`${prefixCls}-icon`}
onClick={this.onSearch}
type='search'
7 years ago
/> */}
7 years ago
</Input>
)
},
}