封装可多选的组件(Autocomplete)

一。组件库Material UI

1.1 地址

https://v4.mui.com/zh/getting-started/installation/

1.2 简介

自称世界上最受欢迎的React UI组件库(能看到这里的基本用法应该都清楚了,我就不重复了)

二。效果展示

三。代码展示

javascript 复制代码
        import React from 'react'
        import { useField, useFormikContext } from 'formik'
        import { TextField as MuiTextField } from '@material-ui/core'
        import { Autocomplete } from '@material-ui/lab'
        const MuitipleSelectFields = (props) => {
            const {
                name,
                style,
                size,
                limitTags,
                disabled = false,
                option = [],
                ...otherProps } = props
            const [field, meta, helpers] = useField('')
            // useField(这里面应该是name,但是我使用name,
            // value的值会是null,导致出错 如果有懂的可以一起探讨)
            const { isSubmitting, setFieldValue } = useFormikContext()
            return (
                < Autocomplete
                    value={field.value}
                    style={style}
                    limitTags={limitTags} // 显示的最大的标签数
                    mulTiple={true}   // 如果为true 就支持多个选项
                    // 如果为true  选择一项就不会关闭弹窗
                    disableCloseOnSelect={true}
                    disabled={disabled}  // 是否禁用
                    noOptionsText="无匹配选项"
                    size="samll"
                    option={option}  // 选择数组
                    onChange={(e, i, r) => {
                        setFieldValue(name, i, true)
                    }}
                    getOptionSelected={(option, value) => {
                        return option.value === value.value
                    }}
                    onBlur={() => helpers.setTouched({ [naem]: true })}
                    // 用于确定给选项的字符串值,它用于填充输入
                    getOptionLable={({ text }) => text}
                    renderInput={(params) => {  // 呈现输入
                        <MuiTextField
                            {...params}
                            {...otherProps}
                            // 如果为true  输入框将显示错位状态
                            error={meta.touched && !!meta.error}
                            // 辅助文本内容
                            helperText={meta.touched && !!meta.error ? meta.error : null}
                            variant="outlined" //想要使用的变体
                        />
                    }}
                />)
        }
        export default MuitipleSelectFields
相关推荐
烬羽1 分钟前
求第 K 大,为什么反而要用最小堆?
javascript·数据结构·算法
jingchao19985 分钟前
Cannot read properties of null (reading ‘insertBefore‘)
前端·javascript·vue.js
计算机魔术师9 分钟前
新文章
前端
爱跳舞的烤冷面12 分钟前
自学嵌入式第N天(Linux篇——文件编程)
java·开发语言·前端
IT_陈寒18 分钟前
Vite的热更新突然失效,原来我忽略了这个配置
前端·人工智能·后端
怪奇云呼军20 分钟前
闪电智能VoiceAgent 如何管理呼入、接听、桥接和挂断状态?
java·前端·网络·数据库·人工智能
云浪22 分钟前
Milvus + RAG 实战:《红楼梦》问答助手
前端·人工智能·后端
梦醒沉醉37 分钟前
2、JavaScript控制流和错误处理
javascript
恋猫de小郭40 分钟前
Dart 3.13 大更新,感觉比 Flutter 更带劲
android·前端·flutter
晓得迷路了41 分钟前
栗子前端技术周刊第 142 期 - DeepSeek Harness、pnpm 12 RC、crypto‑js...
前端·javascript·ai编程