ant-design-vue/components/auto-complete/InputElement.jsx

54 lines
1.5 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import { cloneElement } from '../_util/vnode';
2020-01-18 21:34:23 +08:00
import { getOptionProps, getListeners } from '../_util/props-util';
2019-01-12 11:33:27 +08:00
function chaining(...fns) {
return function(...args) {
// eslint-disable-line
2018-02-28 23:30:40 +08:00
// eslint-disable-line
for (let i = 0; i < fns.length; i++) {
if (fns[i] && typeof fns[i] === 'function') {
2019-01-12 11:33:27 +08:00
fns[i].apply(this, args);
2018-02-28 23:30:40 +08:00
}
}
2019-01-12 11:33:27 +08:00
};
2018-02-28 23:30:40 +08:00
}
2018-02-27 19:08:49 +08:00
export default {
name: 'InputElement',
2020-01-18 21:34:23 +08:00
inheritAttrs: false,
2018-02-28 19:07:04 +08:00
props: {
value: PropTypes.any,
disabled: PropTypes.bool,
placeholder: PropTypes.string,
2018-02-28 19:07:04 +08:00
},
2019-01-12 11:33:27 +08:00
render() {
const { $slots = {}, $attrs = {}, placeholder } = this;
2020-01-18 21:34:23 +08:00
const listeners = getListeners(this);
const props = getOptionProps(this);
const value = props.value === undefined ? '' : props.value;
2019-01-12 11:33:27 +08:00
const children = $slots.default[0];
const { componentOptions = {} } = $slots.default[0];
2020-01-18 21:34:23 +08:00
const { listeners: events = {} } = componentOptions;
const newEvent = { ...events };
2018-02-28 23:30:40 +08:00
2020-01-18 21:34:23 +08:00
for (const [eventName, event] of Object.entries(listeners)) {
newEvent[eventName] = chaining(event, events[eventName]);
2018-02-28 23:30:40 +08:00
}
const attrs = { ...$attrs, value };
// https://github.com/vueComponent/ant-design-vue/issues/1761
delete props.placeholder;
if (placeholder) {
props.placeholder = placeholder;
attrs.placeholder = placeholder;
}
2018-02-28 23:30:40 +08:00
return cloneElement(children, {
2018-02-27 19:08:49 +08:00
domProps: {
2018-02-28 19:07:04 +08:00
value,
2018-02-27 19:08:49 +08:00
},
props,
2018-02-28 23:30:40 +08:00
on: newEvent,
attrs,
2018-02-27 19:08:49 +08:00
ref: 'ele',
2019-01-12 11:33:27 +08:00
});
2018-02-27 19:08:49 +08:00
},
2019-01-12 11:33:27 +08:00
};