Skip to content

Commit

Permalink
feat(Input & InputNumber): 增加 autofocus 配置项 (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean-gao authored Apr 8, 2024
1 parent 5da5598 commit 2c6f549
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions components/input-number/input-number.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div :class="classes" @dragstart.prevent>
<InputInner
ref="inputRef"
:modelValue="displayValue"
:disabled="innerDisabled"
:placeholder="placeholder"
Expand Down Expand Up @@ -58,6 +59,7 @@ import {
ref,
nextTick,
defineComponent,
onMounted,
type ComponentObjectPropsOptions,
} from 'vue';
import { isNumber } from 'lodash-es';
Expand Down Expand Up @@ -97,6 +99,10 @@ export const inputNumberProps = {
precision: Number,
disabled: Boolean,
placeholder: String,
autofocus: {
type: Boolean,
default: false,
},
} as const satisfies ComponentObjectPropsOptions;
export type InputNumberProps = ExtractPublicPropTypes<typeof inputNumberProps>;
Expand Down Expand Up @@ -129,6 +135,7 @@ export default defineComponent({
),
);
const inputRef = ref();
const tempValue = ref();
const displayValue = computed(() => {
if (tempValue.value != null) return tempValue.value;
Expand Down Expand Up @@ -246,6 +253,15 @@ export default defineComponent({
setCurrentValue(_calculationNum(currentValue.value || 0, type));
};
const focus = () => {
inputRef.value.focus();
};
onMounted(() => {
if (props.autofocus) {
focus();
}
});
return {
prefixCls,
isError,
Expand All @@ -260,6 +276,7 @@ export default defineComponent({
displayValue,
minDisabled,
maxDisabled,
inputRef,
};
},
});
Expand Down
10 changes: 10 additions & 0 deletions components/input/input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export const inputProps = {
type: [Boolean, Object] as PropType<boolean | Autosize>,
default: false as boolean | Autosize,
},
autofocus: {
type: Boolean,
default: false,
},
resize: String as PropType<
'none' | 'both' | 'horizontal' | 'vertical' | 'block' | 'inline'
>,
Expand Down Expand Up @@ -257,6 +261,12 @@ export default defineComponent({
emit('clear');
};
onMounted(() => {
if (props.autofocus) {
focus();
}
});
return {
innerDisabled,
isError,
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/components/input/common.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ref="inputRef"
v-model="inputValue"
placeholder="请输入"
:autofocus="true"
@keydown="handleKeydown"
@input="handleInput"
@change="handleChange"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export default {
};
const handleInputBlur = (e) => {
console.log('[input.autofocus] [blur] e:', e);
console.log('[input.handleFocus] [blur] e:', e);
};
const handleInputFocus = (e) => {
console.log('[input.autofocus] [focus] e:', e);
console.log('[input.handleFocus] [focus] e:', e);
};
return {
Expand Down
3 changes: 2 additions & 1 deletion docs/.vitepress/components/input/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ event.vue
### 聚焦和失焦

:::demo
autofocus.vue
handleFocus.vue
:::

## Props
Expand All @@ -88,6 +88,7 @@ autofocus.vue
| showWordLimit | 是否显示输入数字统计,只在 `type="textarea"` 时有效 | boolean | false |
| showPassword | 是否显示切换密码图标,仅`type``textarea`时有效 | boolean | false |
| autosize | 自适应内容高度,只在 `type="textarea"` 时有效,可输入对象,入 `{ minRows: 2, maxRows: 3 }` | boolean、object | false |
| autofocus | 是否自动获取焦点 | boolean | `false` |

## Slots

Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/components/inputNumber/common.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
v-model="val"
:max="100"
:showStepAction="showStepAction"
:autofocus="true"
></FInputNumber>
</template>

Expand Down
3 changes: 2 additions & 1 deletion docs/.vitepress/components/inputNumber/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ app.use(FInputNumber);
## Props

| 属性 | 说明 | 类型 | 默认值 |
|:---------------|:---------------|:--------|:------------|
| :------------- | :--------------- | :------ | :---------- |
| modelValue | v-model 双向绑定 | number | - |
| min | 计数器最小值 | number | `-infinity` |
| max | 计数器最大值 | number | `infinity` |
Expand All @@ -56,6 +56,7 @@ app.use(FInputNumber);
| disabled | 是否禁用 | boolean | `false` |
| placeholder | 输入框默认提示 | string | - |
| precision | 数值精度 | number | - |
| autofocus | 是否自动获取焦点 | boolean | `false` |

## Slots

Expand Down

0 comments on commit 2c6f549

Please sign in to comment.