简体   繁体   English

日期选择器上的 Material UI 自定义文本字段

[英]Material UI custom text field on date picker

Alrighty, so I'm using Material UI on my react project and using their suggested Material UI Pickers for a date picker, now the thing is, for it to be in line with the rest of my fields, I'd like to set the base textfield component it uses to a custom reddit-styled text field component I already have, this is possible via a property in the DatePicker's documentation , TextFieldComponent , however, passing my custom LNTextField in it isn't really giving any changes, let me show you, first here's the code for the LNTextField好吧,所以我在我的 React 项目上使用Material UI ,并使用他们建议的Material UI Pickers作为日期选择器,现在的问题是,为了与我的其他领域保持一致,我想设置它用于我已经拥有的自定义 reddit 样式文本字段组件的基本文本字段组件,这可以通过DatePicker 文档中的属性TextFieldComponent实现,但是,在其中传递我的自定义LNTextField并没有真正提供任何更改,让我展示一下你,首先是LNTextField的代码

import React from "react";
import { TextField, InputAdornment } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";

import styles from "./LNTextField.module.css";

const useStylesReddit = makeStyles(theme => ({
  root: {
    border: "1px solid #D6D7DC",
    overflow: "hidden",
    borderRadius: 4,
    backgroundColor: "#fff",
    transition: theme.transitions.create(["border-color", "box-shadow"]),
    "&:hover": {
      backgroundColor: "#fff"
    },
    "&$focused": {
      backgroundColor: "#fff",
      borderColor: "#46CBAC"
    }
  },
  focused: {}
}));

const LNTextField = props => {
  const classes = useStylesReddit();
  return (
    <TextField
      variant="filled"
      spellCheck="false"
      InputProps={
        props.optional
          ? {
              classes,
              disableUnderline: true,
              endAdornment: (
                <InputAdornment
                  className={styles.optionalAppendedText}
                  position="end"
                >
                  Optional
                </InputAdornment>
              )
            }
          : {
              classes,
              disableUnderline: true
            }
      }
      {...props}
    />
  );
};

export default LNTextField;

and this is the text for my desired datepicker component:这是我想要的日期选择器组件的文本:

import React, { useState } from "react";
import { DatePicker, MuiPickersUtilsProvider } from "@material-ui/pickers";
import MomentUtils from "@date-io/moment";

import styles from "./LNDatePicker.module.css";
import LNTextField from "../LNTextField/LNTextField";

const LNDatePicker = props => {
  return (
    <MuiPickersUtilsProvider utils={MomentUtils}>
      <DatePicker
        clearable
        inputVariant="outlined"
        placeholder="10/10/2018"
        onChange={date => props.change_function(date)}
        format="MM/DD/YYYY"
        TextFieldComponent={LNTextField}
      />
    </MuiPickersUtilsProvider>
  );
};

export default LNDatePicker;

This is the code for the date picker and a preceding text field using my text field component:这是使用我的文本字段组件的日期选择器和前面的文本字段的代码:

<LNTextField
                              name="ssn"
                              label="Social Security Number"
                              error={touched.ssn && errors.ssn ? true : false}
                              helperText={
                                touched.ssn && errors.ssn
                                  ? "* " + errors.ssn
                                  : ""
                              }
                              type="text"
                            />
<LNDatePicker
                          name="dob"
                          change_function={date => {
                            setFieldValue("dob", date.format("MM-DD-YYYY"));
                          }}
                        ></LNDatePicker>

Now if you take a look at the result I'm getting you will notice how the style is not being applied at all现在,如果您看一下我得到的结果,您会注意到根本没有应用样式在此处输入图像描述

Is there something I'm missing or doing wrong?我有什么遗漏或做错了吗? Am I following the docs incorrectly?我是否错误地遵循了文档? thanks in advance.提前致谢。

You need to wrap your LNTextField in a function like this:您需要将LNTextField包装在这样的函数中:

const LNDatePicker = props => {
  const renderInput = props => <LNTextField value={props.value} label={props.lable} />
  return (
    <MuiPickersUtilsProvider utils={MomentUtils}>
      <DatePicker
        clearable
        inputVariant="outlined"
        placeholder="10/10/2018"
        onChange={date => props.change_function(date)}
        format="MM/DD/YYYY"
        TextFieldComponent={renderInput}
      />
    </MuiPickersUtilsProvider>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM