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

54 lines
1.5 KiB
Vue
Raw Normal View History

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