封装可多选的组件(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
相关推荐
777VG2 小时前
PostgreSQL +martin将多张表输出成一个 MVT
前端·数据库·postgresql
mayaairi3 小时前
JS循环语句深度解析:嵌套for、while与do...while
开发语言·前端·javascript
To_OC3 小时前
啃完流式输出:从一个卡顿的 LLM 接口开始,我搞懂了数据流到底怎么 “流”
前端·javascript·llm
阳光是sunny3 小时前
LangGraph实战教程:defer延迟节点——让收尾工作自动排到最后
前端·人工智能·后端
kyriewen4 小时前
我用了三周Claude Code Skills——总结出5条铁律,第3条最反直觉
前端·ai编程·claude
阳光是sunny4 小时前
LangGraph实战教程:控制流详解
前端·人工智能·后端
格尔曼Noah5 小时前
Safari浏览器中如何只允许指定网站下载
前端·safari
用户059540174465 小时前
用了3年Redis,才发现我一直没搞懂缓存一致性测试
前端·css
swipe5 小时前
07|(前端转后全栈)为什么后端也要缓存?从前端缓存思维理解 Redis
前端·后端·全栈
IT小盘5 小时前
05-企业项目统一接入多个大模型-适配器模式实战
前端·人工智能·适配器模式