TS-Antd:组件使用记录

1、Input 组件自动获得焦点

和普通的 input 标签不同,antd 已经给 Input 组件提供了 InputRef 类型。在 Input 组件上添加 ref ,获取到的是 InputRef 类型的值,而不是 HTMLInputElement 类型的值。

1.1 InputRef 类型

interface InputRef {
   
    focus: (options?: InputFocusOptions) => void;
    blur: () => void;
    setSelectionRange: (start: number, end: number, direction?: 'forward' | 'backward' | 'none') => void;
    select: () => void;
    input: HTMLInputElement | null;
}

1.2 代码实现

import React, {
    useRef } from 'react';
import {
    Input, Button } from 'antd';
import type {
    InputRef } from 'antd';
const Test = () => {
   
	const inputRef = useRef<InputRef>(null);
	const inputFocus = () => {
   
		if (inputRef.current) inputRef.current.focus();
	}
	return (
		<div>
      		<Input ref={
   inputRef} />
      		<Button onClick={
   inputFocus}>获取焦点<Button>
    	</div>
	)
}

相关推荐

  1. TS-Antd:组件使用记录

    2024-01-31 17:06:04       57 阅读
  2. vue3+TS使用component 的实例

    2024-01-31 17:06:04       56 阅读
  3. Elasticsearch本地单机配置以及php使用记录

    2024-01-31 17:06:04       56 阅读
  4. antd使用踩坑记录

    2024-01-31 17:06:04       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-31 17:06:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-31 17:06:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-31 17:06:04       87 阅读
  4. Python语言-面向对象

    2024-01-31 17:06:04       96 阅读

热门阅读

  1. DataEaseV2开发指南

    2024-01-31 17:06:04       66 阅读
  2. Redis面试题38

    2024-01-31 17:06:04       54 阅读
  3. Z字型遍历二叉树

    2024-01-31 17:06:04       61 阅读
  4. VUE的v-model与v-bind区别及其使用

    2024-01-31 17:06:04       62 阅读