react18加antd新手上路使用

第一次使用react和antd组件库,记录过程中实用的几个组件和使用方法;

项目中依赖版本
typescript 复制代码
"react": "^18.2.0",
"antd": "^5.3.0",
Input关闭历史填充
typescript 复制代码
 <Input placeholder="请输入ID/名称" allowClear autoComplete="off"/>  
Popover组件使用
typescript 复制代码
<Popover></Popover>
// 挂载到当前节点父节点
<Popover 
    getPopupContainer={(triggerNode: any) => triggerNode.parentNode}>
</Popover>
切换中文加日期插件汉化
typescript 复制代码
import { ConfigProvider } from 'antd';
import 'dayjs/locale/zh-cn';
import zhCN from 'antd/locale/zh_CN';
import 'moment/dist/locale/zh-cn';
import Router from '@/router/index';
function App() {
  return (
    <div className="App">
      <ConfigProvider theme={{ token: { colorPrimary: '#0078D7'} }} locale={zhCN}>
        <Router></Router>
      </ConfigProvider>
    </div>
  )
}
export default App
InputNumber只能输入整数
typescript 复制代码
<InputNumber min={0} max={100} precision={0} placeholder='请输入' style={{width: '100%'}}/>
From.List 赋值
typescript 复制代码
// 自定义校验
const checkImei = (_: any, value: string) => {
    if (!value) {
        return Promise.reject(new Error('1-20个字符,仅支持数字或字母!'));
    }
    const is = Reg.EN_NUM.test(value);
    if (is) {
        if (value.length > 12) {
            return Promise.reject(new Error('1-20个字符,仅支持数字或字母!'));
        } else {
            return Promise.resolve();
        }
    } else {
        return Promise.reject(new Error('1-20个字符,仅支持数字或字母!'));
    }
}
const checkImei = (_: any, value: string) => {
        if (!value) {
            return Promise.reject(new Error('1-20个字符,仅支持数字或字母!'));
        }
        const is = Reg.EN_NUM.test(value);
        if (is) {
            if (value.length > 12) {
                return Promise.reject(new Error('1-20个字符,仅支持数字或字母!'));
            } else {
                return Promise.resolve();
            }
        } else {
            return Promise.reject(new Error('1-20个字符,仅支持数字或字母!'));
        }
    }
<Form initialValues={{
    IMEIS: [{imei: 1}, {imei: 2}, {imei: 3}]
}}>
    <Form.List name="IMEIS">
        {(fields, { add, remove }) => (
        <>
            {fields.map(({ key, name, ...restField }, index) => (
                <div key={key}>
                    <Form.Item label={`IMEI${index + 1}`} {...restField} name={[name, 'imei']} 
                        rules={[{ required: true, validator: checkImei }]}>
                        <Input placeholder="请输入" allowClear/>
                    </Form.Item>
                </div>
            ))}
        </>
    )}
    </Form.List>
</Form>
表格预制高度
typescript 复制代码
<div className={style.table_wrap}>
    <Table columns={columns} 
        dataSource={data} 
        rowKey={record => record.id} 
        scroll={{ x: 50, y: '360px'  }} 
        pagination={false}>
    </Table>
</div>
:global{
    .ant-table-body{
        height: 360px;
    }
}
相关推荐
万少1 天前
DeepSeek 昨晚刚开源了 Harness:附万少的2 万字保姆级教程
前端·后端·架构
程序员黑豆1 天前
Java字符串详解
java·前端·ai编程
无我Code1 天前
开发者-2026年中总结
前端·面试·程序员
浮生望1 天前
React 受控与非受控组件:从表单状态管理到实时校验的完整实践
前端
糖墨夕1 天前
第二章:AI Agent 到底是个啥?—— 用生活场景把概念讲明白
前端
Csvn1 天前
⚡ content-visibility: auto —— 长页面渲染提速的隐藏神器,白捡的性能优化
前端
用户938515635071 天前
深入理解 Next.js:从 SPA 的 SEO 问题到约定式路由与 SSR 原理
前端·后端·全栈
snow@li1 天前
web:前端500知识点/速查手册
前端
kyriewen1 天前
今年裁了16万技术人——但字节前端岗反而涨了23%
前端·javascript·面试
用户059540174461 天前
把 AI Agent 记忆存储验证从 40 分钟手测压到 3 分钟,pytest + Docker 这套组合太顶了
前端·css