css - 输入的占位符文本仅在使用 ReactJS 选择时显示

标签 css reactjs material-ui

当我将 BootstrapInput 更改为:

<BootstrapInput 
    id="age-customized-input" 
    placeholder="Search Title"
/>

占位符文本仅在选择输入字段时显示。

知道如何让占位符始终可见吗?

这是我在 Material-UI 中使用的示例。

import React from 'react';
import { makeStyles, withStyles } from '@material-ui/core/styles';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';
import NativeSelect from '@material-ui/core/NativeSelect';
import InputBase from '@material-ui/core/InputBase';

const BootstrapInput = withStyles(theme => ({
  root: {
    'label + &': {
      marginTop: theme.spacing(3),
    },
  },
  input: {
    borderRadius: 4,
    position: 'relative',
    backgroundColor: theme.palette.background.paper,
    border: '1px solid #ced4da',
    fontSize: 16,
    width: 'auto',
    padding: '10px 26px 10px 12px',
    transition: theme.transitions.create(['border-color', 'box-shadow']),
    // Use the system font instead of the default Roboto font.
    fontFamily: [
      '-apple-system',
      'BlinkMacSystemFont',
      '"Segoe UI"',
      'Roboto',
      '"Helvetica Neue"',
      'Arial',
      'sans-serif',
      '"Apple Color Emoji"',
      '"Segoe UI Emoji"',
      '"Segoe UI Symbol"',
    ].join(','),
    '&:focus': {
      borderRadius: 4,
      borderColor: '#80bdff',
      boxShadow: '0 0 0 0.2rem rgba(0,123,255,.25)',
    },
  },
}))(InputBase);

const useStyles = makeStyles(theme => ({
  root: {
    display: 'flex',
    flexWrap: 'wrap',
  },
  margin: {
    margin: theme.spacing(1),
  },
}));

export default function CustomizedSelects() {
  const classes = useStyles();
  const [age, setAge] = React.useState('');
  const handleChange = event => {
    setAge(event.target.value);
  };
  return (
    <form className={classes.root} autoComplete="off">
      <FormControl className={classes.margin}>
        <InputLabel htmlFor="age-customized-input">Age</InputLabel>
        <BootstrapInput id="age-customized-input" />
      </FormControl>
      <FormControl className={classes.margin}>
        <InputLabel htmlFor="age-customized-select">Age</InputLabel>
        <Select
          value={age}
          onChange={handleChange}
          input={<BootstrapInput name="age" id="age-customized-select" />}
        >
          <MenuItem value="">
            <em>None</em>
          </MenuItem>
          <MenuItem value={10}>Ten</MenuItem>
          <MenuItem value={20}>Twenty</MenuItem>
          <MenuItem value={30}>Thirty</MenuItem>
        </Select>
      </FormControl>
      <FormControl className={classes.margin}>
        <InputLabel htmlFor="age-customized-native-simple">Age</InputLabel>
        <NativeSelect
          value={age}
          onChange={handleChange}
          input={<BootstrapInput name="age" id="age-customized-native-simple" />}
        >
          <option value="" />
          <option value={10}>Ten</option>
          <option value={20}>Twenty</option>
          <option value={30}>Thirty</option>
        </NativeSelect>
      </FormControl>
    </form>
  );
}

Link to Material-UI example

最佳答案

它是隐藏标签的 backgroundColor。如果您将其注释掉,标签就会显示出来。下面的代码沙箱。

  //backgroundColor: theme.palette.background.paper,

https://codesandbox.io/s/material-demo-p2f7h

关于css - 输入的占位符文本仅在使用 ReactJS 选择时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56654515/

相关文章:

html - onMouseLeave for Popover with hiderBackdrop parameter (material ui)

reactjs - 在material-ui中使用react选择如何通过仅按一次tab键将焦点移动到下一个控件?

javascript - 每次用户滚动到 Chart.js 时,它的大小都会加倍

css - 仅使用 css3 的 pinterest 样式图像布局(包括无限滚动)

php - 如何设置显示为图像的按钮的样式

javascript - 无法在 else 语句中呈现

reactjs - 如何全局设置容器的 maxWidth 为 100% >>

jquery - Div发生溢出时创建.....

javascript - 通过不等式查询firestore

javascript - 如何在 react js中设置按钮点击的值?