Skip to content

fix: 修复inputRef指向错误问题 #7523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions components/vc-input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
triggerFocus,
} from './utils/commonUtils';
import BaseInput from './BaseInput';
import BaseInputCore from '../_util/BaseInput';
import BaseInputCore, { type BaseInputExpose } from '../_util/BaseInput';

export default defineComponent({
name: 'VCInput',
Expand All @@ -24,7 +24,7 @@ export default defineComponent({
setup(props, { slots, attrs, expose, emit }) {
const stateValue = shallowRef(props.value === undefined ? props.defaultValue : props.value);
const focused = shallowRef(false);
const inputRef = shallowRef<HTMLInputElement>();
const inputRef = shallowRef<BaseInputExpose>();
const rootRef = shallowRef<ComponentPublicInstance>();
watch(
() => props.value,
Expand All @@ -42,30 +42,30 @@ export default defineComponent({
);
const focus = (option?: InputFocusOptions) => {
if (inputRef.value) {
triggerFocus(inputRef.value, option);
triggerFocus(inputRef.value.input, option);
}
};

const blur = () => {
inputRef.value?.blur();
inputRef.value.input?.blur();
};

const setSelectionRange = (
start: number,
end: number,
direction?: 'forward' | 'backward' | 'none',
) => {
inputRef.value?.setSelectionRange(start, end, direction);
inputRef.value.input?.setSelectionRange(start, end, direction);
};

const select = () => {
inputRef.value?.select();
inputRef.value.input?.select();
};

expose({
focus,
blur,
input: computed(() => (inputRef.value as any)?.input),
input: computed(() => (inputRef.value.input as any)?.input),
stateValue,
setSelectionRange,
select,
Expand All @@ -81,7 +81,7 @@ export default defineComponent({
stateValue.value = value;
} else {
nextTick(() => {
if (inputRef.value.value !== stateValue.value) {
if (inputRef.value.input.value !== stateValue.value) {
rootRef.value?.$forceUpdate();
}
});
Expand All @@ -94,7 +94,7 @@ export default defineComponent({
const { value } = e.target as any;
if (stateValue.value === value) return;
const newVal = e.target.value;
resolveOnChange(inputRef.value, e, triggerChange);
resolveOnChange(inputRef.value.input as HTMLInputElement, e, triggerChange);
setValue(newVal);
};

Expand All @@ -116,7 +116,7 @@ export default defineComponent({
};

const handleReset = (e: MouseEvent) => {
resolveOnChange(inputRef.value, e, triggerChange);
resolveOnChange(inputRef.value.input as HTMLInputElement, e, triggerChange);
setValue('', () => {
focus();
});
Expand Down