【问题标题】:Material ui Select change background and focused color材质 ui 选择更改背景和焦点颜色
【发布时间】:2022-01-26 17:20:01
【问题描述】:

我想将选择组件的背景颜色更改为“灰色”,并将标签和边框颜色从蓝色更改为另一种颜色,当我单击选择框时会出现。有人可以帮我吗?

我得到了这个错误: TS2322: 类型 '{ root: string; }' 不可分配给类型“Partial”。
对象字面量只能指定已知属性,并且类型“Partial”中不存在“root”。

     import * as React from 'react';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select, { SelectChangeEvent } from '@mui/material/Select';
import makeStyles from '@mui/styles/makeStyles';


const useStyles = makeStyles((theme) => ({
    selectRoot: {
        '&:focus':{
            backgroundColor:'yellow'
        }
    }
}));

    export interface SelectProps {
        label: string;
        value:string | undefined;
        options:any;
        error?: boolean;
        onChange?: (value: string) => void;

    }

    export class FormDDown extends React.Component<SelectProps, {

        value: string;
    }> {


        constructor(props: SelectProps) {
            super(props);

            this.state = {value: ''};
        }

        private handleChange = (event: SelectChangeEvent) => {
            this.setState({value: event.target.value});

            // notify the callback if present
            this.props.onChange?.(event.target.value);
        }

        classes = useStyles();

        render() {
            let id = this.props.value ?? this.props.label;
            let errorBorder = { borderBottom: '2px solid red' };
        return(
            <div className='forminput'>
                <FormControl variant="filled" fullWidth>
                <InputLabel id="demo-simple-select-helper-label">{this.props.label}</InputLabel>
                <Select
                    variant="filled"
                    labelId="demo-simple-select-helper-label"
                    id="demo-simple-select-helper"
                    value={this.props.value}
                    autoWidth
                    onChange={this.handleChange}
                    style={{...(this.props.error ? errorBorder : {})}}
                    classes={{ root: this.classes.selectRoot }}
                >
                    {this.props.options.map((option:any) => {
                        return (
                            <MenuItem key={option.value} value={option.label}>
                                {option.label ?? option.value}
                            </MenuItem>
                        );
                    })}
                </Select>
            </FormControl>
        </div>
        )
}
}


【问题讨论】:

  • 我相信引用文档的select 组件没有“根”CSS API:mui.com/api/select/#css
  • 我的回答解决了你的问题吗?
  • @MajidM。首先我要感谢你。我更改了代码以更简单地应用您的概念,但我遇到了另一个错误:(
  • @NewatLeaflet 随时!有什么问题?

标签: javascript css reactjs typescript material-ui


【解决方案1】:

您遇到的问题是因为使用useStyles()!你不能在类组件中使用classes = useStyles()。它是一个hook,用于功能组件。而不是你应该使用Higher-Order Component API。 以下是根据您的需求提供的示例:

import * as React from "react";
import InputLabel from "@mui/material/InputLabel";
import MenuItem from "@mui/material/MenuItem";
import FormControl from "@mui/material/FormControl";
import Select, {
  SelectChangeEvent,
} from "@mui/material/Select";
import { WithStyles, withStyles } from "@mui/styles";

const styles = {
  root: {
    color: "white !important",
    background: "red !important",
    border: "1px solid yellow !important"
  }
};

export interface SelectProps {
  label: string;
  value: string | undefined;
  options: any;
  error?: boolean;
  className: string | undefined;
  onChange?: (value: string) => void;
}

class FormDDown extends React.Component<
  SelectProps,
  {
    value: string;
  }
> {
  constructor(props: SelectProps) {
    super(props);

    this.state = { value: "" };
  }

  private handleChange = (event: SelectChangeEvent) => {
    this.setState({ value: event.target.value });

    // notify the callback if present
    this.props.onChange?.(event.target.value);
  };

  // this.classes = useStyles();

  render() {
    let id = this.props.value ?? this.props.label;
    let errorBorder = { borderBottom: "2px solid red" };
    return (
      <div className="forminput">
        <FormControl variant="filled" fullWidth>
          <InputLabel id="demo-simple-select-helper-label">
            {this.props.label}
          </InputLabel>
          <Select
            variant="filled"
            labelId="demo-simple-select-helper-label"
            id="demo-simple-select-helper"
            value={this.props.value}
            autoWidth
            onChange={this.handleChange}
            style={{ ...(this.props.error ? errorBorder : {}) }}
            className={this.props.className}
          >
            {this.props.options.map((option: any) => {
              return (
                <MenuItem key={option.value} value={option.label}>
                  {option.label ?? option.value}
                </MenuItem>
              );
            })}
          </Select>
        </FormControl>
      </div>
    );
  }
}

function FormDDownComponent(props: WithStyles<typeof styles>) {
  const { classes } = props;
  return (
    <FormDDown
      options={[
        { value: "single", label: "Single" },
        { value: "married", label: "Married" }
      ]}
      label="Family"
      value="0"
      className={classes.root}
    />
  );
}

export default withStyles(styles)(FormDDownComponent);

【讨论】:

    猜你喜欢
    • 2020-03-07
    • 2021-01-06
    • 2021-12-26
    • 2015-02-12
    • 2019-09-26
    • 2021-08-10
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    相关资源
    最近更新 更多