css - 使用 TypeScript react 如何在 Textfield Material-UI 中使用图标

标签 css reactjs typescript material-ui formik

我设计了一个使用 TypeScript Material UI 和 Formik 进行验证的表单。我希望在我的文本字段区域中出现一个 Material UI 图标,这是我的代码:


import React from 'react'
import { Formik, Form, FieldAttributes,useField} from 'formik'
import { TextField } from '@material-ui/core'
import CalendarTodayIcon from '@material-ui/icons/CalendarToday'
import * as yup from 'yup'
import './MainInfo.css'

const MyTextField: React.FC<FieldAttributes<{}>> = ({
  placeholder,type,className,style,
  ...props
}) => {
  const [field, meta] = useField<{}>(props);
  const errorText = meta.error && meta.touched ? meta.error : "";
  return (
    <div className='container'>
    <TextField
    placeholder={placeholder}
    className={className}
    style={style}
    type={type}
    {...field}
    helperText={errorText}
    error={!!errorText}
    id="outlined-basic" 
    variant="outlined"
  />
    </div>
  );
};

export function MainInfo() {

    return (
      <div>
        <Formik
          validateOnChange={true} validationSchema={validationSchema} initialValues={{ Title: '', ActivationDate: '', ExpirationDate: '', DirectManager: '', HRBP: '' }} onSubmit={(data) => {
            console.log(data)
          }}
        >
          {({values, errors}) => (
            <Form id='my-form' >
              <div>
                <label className='label'>عنوان</label>
                <div >            
                  <MyTextField style={{width:'60%'}}  placeholder='طراح' name='Title' type='input' />
                </div>
                  ...
              </div>
            </Form>
          )}
        </Formik>
      </div>
    )
}

但问题是我无法添加新的 Icon 属性或 InputProp,因为 <FieldAttributes<{}>>不接受。如何为 FieldAttributes 定义新属性或解决此问题?

最佳答案

使用 TextField Prop 输入 Prop 自定义输入字段

并使用 startAdornment , endAdornment自定义前缀/后缀

最后使用里面的图标InputAdornment会好的

import { TextField, InputAdornment } from "@material-ui/core";
import ExpandLess from "@material-ui/icons/ExpandLess";
import ExpandMore from "@material-ui/icons/ExpandMore";

<TextField
  id="standard-basic"
  label="Standard"
  InputProps={{
    startAdornment: (
      <InputAdornment position="start">
        <ExpandLess />
      </InputAdornment>
    ),
    endAdornment: (
      <InputAdornment position="end">
        <ExpandMore />
      </InputAdornment>
    )
  }}
/>

enter image description here

引用:
  • MUI TextField Props API : InputProps, startAdornment, endAdornment
  • MUI InputInputAdornment Props API
  • 在线演示:https://stackblitz.com/edit/gzlbzm
  • 关于css - 使用 TypeScript react 如何在 Textfield Material-UI 中使用图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61216003/

    相关文章:

    javascript - Z 索引不起作用

    javascript - 如何使用 JavaScript 禁用主菜单?

    jquery - 页面加载时的 Fancybox 叠加颜色

    javascript - 如何在 native react 中数组过滤器响应值后禁用按钮

    javascript - 循环数组并填充矩阵 - JS

    javascript - 弹出图像不会停留在同一个地方

    node.js - ReactJS webapp 中的业务逻辑

    node.js - 使用 graphql 文件编译 Typescript 时出错

    javascript - 使用 TypeDoc 记录外部类型

    javascript - JSON.parse 返回错误