ant-design-vue/components/vc-select/Selector/Input.tsx

193 lines
5.5 KiB
Vue
Raw Normal View History

2020-10-07 14:49:01 +00:00
import { cloneElement } from '../../_util/vnode';
2021-06-26 01:35:40 +00:00
import type { VNode, VNodeChild } from 'vue';
import { defineComponent, getCurrentInstance, inject, onMounted, withDirectives } from 'vue';
2020-10-07 14:49:01 +00:00
import PropTypes from '../../_util/vue-types';
2021-06-26 01:35:40 +00:00
import type { RefObject } from '../../_util/createRef';
2020-10-07 14:49:01 +00:00
import antInput from '../../_util/antInputDirective';
2021-06-22 05:26:19 +00:00
import classNames from '../../_util/classNames';
2020-10-07 14:49:01 +00:00
interface InputProps {
prefixCls: string;
id: string;
inputElement: VNodeChild;
disabled: boolean;
autofocus: boolean;
autocomplete: string;
editable: boolean;
accessibilityIndex: number;
value: string;
open: boolean;
refactor: Anchor、Alert、Avatar、Badge、BackTop、Col、Form、Layout、Menu、Space、Spin、Switch、Row、Result、Rate (#4171) * chore: remove resize-observer-polyfill * refactor: align * refactor(v3/avatar): refactor using composition api (#4052) * refactor(avatar): refactor using composition api * refactor: update props define * fix: avatar src scale not update * refactor: resizeObserver * refactor: divider * refactor: localeProvider * refactor(v3/back-top): use composition api (#4060) * refactor: backtop * refactor: empty * refactor: transButton * feat(v3/avatar): add avatar group (#4062) * feat(avatar): add avatar group * refactor: update * refactor: update Co-authored-by: tangjinzhou <415800467@qq.com> * refactor: avatar * refactor: avatar * style: rename useProvide * refactor: menu (#4110) * fix: menu * refactor: menu * refactor: remove rc-menu * fix: menu rtl error * style: lint * refactor(Anchor): use composition api (#4054) * refactor: anchor * refactor: anchor * refactor: anchor * feat: update * fix: icon class lose * refactor(v3/badge): use composition api (#4076) * refactor: badge * fix: badge inheritAttrs * refactor: grid * refactor: layout * fix: menu not close * refactor: space * refactor: result * refactor: affix * refactor: comment * refactor: form * feat: spin add rtl * feat: export spin type * refactor: pageHeader * refactor: page-header * refactor: skeleton * refactor: typography * refactor(v3/rate): use composition api * fix: add useRef hook * refactor: form * fix: menu not update * refactor: form * refactor: form * fix: slide animate not work * fix: menu mode error * fix: menu icon * refactor: rate * perf: remove rate * feat: add vc-overflow * refactor: menu * fix: remove flex check (#4165) * fix: dist locale file lose #3684 * release 2.2.0-beta.1 * dcos: update changelog * chore: update type * docs: update changelog Co-authored-by: John <John60676@qq.com> Co-authored-by: 言肆 <18x@loacg.com> Co-authored-by: zkwolf <chenhao5866@gmail.com>
2021-06-07 09:35:03 +00:00
tabindex: number | string;
2020-10-07 14:49:01 +00:00
/** Pass accessibility props to input */
attrs: object;
inputRef: RefObject;
onKeydown: EventHandlerNonNull;
onMousedown: EventHandlerNonNull;
onChange: EventHandlerNonNull;
onPaste: EventHandlerNonNull;
onCompositionstart: EventHandlerNonNull;
onCompositionend: EventHandlerNonNull;
2021-06-22 02:47:33 +00:00
onFocus: EventHandlerNonNull;
onBlur: EventHandlerNonNull;
2020-10-07 14:49:01 +00:00
}
2020-11-01 10:07:23 +00:00
const Input = defineComponent<InputProps, { VCSelectContainerEvent: any; blurTimeout: any }>({
2020-10-07 14:49:01 +00:00
name: 'Input',
inheritAttrs: false,
2020-10-26 14:14:00 +00:00
setup(props) {
if (process.env.NODE_ENV === 'test') {
onMounted(() => {
const ins = getCurrentInstance();
if (props.autofocus) {
if (ins.vnode && ins.vnode.el) {
ins.vnode.el.focus();
}
}
});
}
2020-10-07 14:49:01 +00:00
return {
2020-11-01 10:07:23 +00:00
blurTimeout: null,
2020-10-07 14:49:01 +00:00
VCSelectContainerEvent: inject('VCSelectContainerEvent'),
};
},
render() {
const {
prefixCls,
id,
inputElement,
disabled,
tabindex,
autofocus,
autocomplete,
editable,
accessibilityIndex,
value,
onKeydown,
onMousedown,
onChange,
onPaste,
onCompositionstart,
onCompositionend,
2021-06-22 02:47:33 +00:00
onFocus,
onBlur,
2020-10-07 14:49:01 +00:00
open,
inputRef,
attrs,
} = this.$props as InputProps;
let inputNode: any = withDirectives((inputElement || <input />) as VNode, [[antInput]]);
const inputProps = inputNode.props || {};
const {
onKeydown: onOriginKeyDown,
onInput: onOriginInput,
2021-06-22 02:47:33 +00:00
onFocus: onOriginFocus,
onBlur: onOriginBlur,
2020-10-07 14:49:01 +00:00
onMousedown: onOriginMouseDown,
onCompositionstart: onOriginCompositionStart,
onCompositionend: onOriginCompositionEnd,
style,
} = inputProps;
2020-10-08 14:51:09 +00:00
inputNode = cloneElement(
inputNode,
Object.assign(
{
id,
ref: inputRef,
disabled,
tabindex,
autocomplete: autocomplete || 'off',
autofocus,
2021-06-22 02:47:33 +00:00
class: classNames(`${prefixCls}-selection-search-input`, inputNode?.props?.className),
2020-10-08 14:51:09 +00:00
style: { ...style, opacity: editable ? null : 0 },
role: 'combobox',
'aria-expanded': open,
'aria-haspopup': 'listbox',
'aria-owns': `${id}_list`,
'aria-autocomplete': 'list',
'aria-controls': `${id}_list`,
'aria-activedescendant': `${id}_list_${accessibilityIndex}`,
...attrs,
value: editable ? value : '',
readonly: !editable,
unselectable: !editable ? 'on' : null,
onKeydown: (event: KeyboardEvent) => {
onKeydown(event);
if (onOriginKeyDown) {
onOriginKeyDown(event);
}
},
onMousedown: (event: MouseEvent) => {
onMousedown(event);
if (onOriginMouseDown) {
onOriginMouseDown(event);
}
},
onInput: (event: Event) => {
onChange(event);
if (onOriginInput) {
onOriginInput(event);
}
},
onCompositionstart(event: CompositionEvent) {
onCompositionstart(event);
if (onOriginCompositionStart) {
onOriginCompositionStart(event);
}
},
onCompositionend(event: CompositionEvent) {
onCompositionend(event);
if (onOriginCompositionEnd) {
onOriginCompositionEnd(event);
}
},
onPaste,
onFocus: (...args: any[]) => {
2020-11-01 10:07:23 +00:00
clearTimeout(this.blurTimeout);
2021-06-22 02:47:33 +00:00
onOriginFocus && onOriginFocus(args[0]);
onFocus && onFocus(args[0]);
2020-10-08 14:51:09 +00:00
this.VCSelectContainerEvent?.focus(args[0]);
},
onBlur: (...args: any[]) => {
2020-11-01 10:07:23 +00:00
this.blurTimeout = setTimeout(() => {
2021-06-22 02:47:33 +00:00
onOriginBlur && onOriginBlur(args[0]);
onBlur && onBlur(args[0]);
2020-11-01 10:07:23 +00:00
this.VCSelectContainerEvent?.blur(args[0]);
}, 200);
2020-10-08 14:51:09 +00:00
},
},
inputNode.type === 'textarea' ? {} : { type: 'search' },
),
true,
true,
) as VNode;
2020-10-07 14:49:01 +00:00
return inputNode;
},
});
Input.props = {
inputRef: PropTypes.any,
prefixCls: PropTypes.string,
id: PropTypes.string,
inputElement: PropTypes.any,
disabled: PropTypes.looseBool,
autofocus: PropTypes.looseBool,
2020-10-07 14:49:01 +00:00
autocomplete: PropTypes.string,
editable: PropTypes.looseBool,
2020-10-07 14:49:01 +00:00
accessibilityIndex: PropTypes.number,
value: PropTypes.string,
open: PropTypes.looseBool,
refactor: Anchor、Alert、Avatar、Badge、BackTop、Col、Form、Layout、Menu、Space、Spin、Switch、Row、Result、Rate (#4171) * chore: remove resize-observer-polyfill * refactor: align * refactor(v3/avatar): refactor using composition api (#4052) * refactor(avatar): refactor using composition api * refactor: update props define * fix: avatar src scale not update * refactor: resizeObserver * refactor: divider * refactor: localeProvider * refactor(v3/back-top): use composition api (#4060) * refactor: backtop * refactor: empty * refactor: transButton * feat(v3/avatar): add avatar group (#4062) * feat(avatar): add avatar group * refactor: update * refactor: update Co-authored-by: tangjinzhou <415800467@qq.com> * refactor: avatar * refactor: avatar * style: rename useProvide * refactor: menu (#4110) * fix: menu * refactor: menu * refactor: remove rc-menu * fix: menu rtl error * style: lint * refactor(Anchor): use composition api (#4054) * refactor: anchor * refactor: anchor * refactor: anchor * feat: update * fix: icon class lose * refactor(v3/badge): use composition api (#4076) * refactor: badge * fix: badge inheritAttrs * refactor: grid * refactor: layout * fix: menu not close * refactor: space * refactor: result * refactor: affix * refactor: comment * refactor: form * feat: spin add rtl * feat: export spin type * refactor: pageHeader * refactor: page-header * refactor: skeleton * refactor: typography * refactor(v3/rate): use composition api * fix: add useRef hook * refactor: form * fix: menu not update * refactor: form * refactor: form * fix: slide animate not work * fix: menu mode error * fix: menu icon * refactor: rate * perf: remove rate * feat: add vc-overflow * refactor: menu * fix: remove flex check (#4165) * fix: dist locale file lose #3684 * release 2.2.0-beta.1 * dcos: update changelog * chore: update type * docs: update changelog Co-authored-by: John <John60676@qq.com> Co-authored-by: 言肆 <18x@loacg.com> Co-authored-by: zkwolf <chenhao5866@gmail.com>
2021-06-07 09:35:03 +00:00
tabindex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
2020-10-07 14:49:01 +00:00
/** Pass accessibility props to input */
attrs: PropTypes.object,
onKeydown: PropTypes.func,
onMousedown: PropTypes.func,
onChange: PropTypes.func,
onPaste: PropTypes.func,
onCompositionstart: PropTypes.func,
onCompositionend: PropTypes.func,
2021-06-22 02:47:33 +00:00
onFocus: PropTypes.func,
onBlur: PropTypes.func,
2020-10-07 14:49:01 +00:00
};
export default Input;