Set TextField height material-ui

后端 未结 7 2171
一生所求
一生所求 2020-12-16 11:04

index.js

import React from \'react\'
import TextField from \'@material-ui/core/TextField\'
import style from \'./style\'
import withStyles          


        
7条回答
  •  独厮守ぢ
    2020-12-16 11:08

    With material-ui v4+, you have to adjust the input padding and the label position to get what you whant.

    
    

    Suppose we want the above TextField to be 48px height (it's default size is 56px), we just have to do (56px - 48px) / 2 = 4px and in our css file:

    .MuiTextField-root input {
      /* 14.5px = 18.5px - 4px (note: 18.5px is the input's default padding top and bottom) */
      padding-top: 14.5px;
      padding-bottom: 14.5px; 
    }
    
    .MuiTextField-root label {
      top: -4px;
    }
    
    .MuiTextField-root label[data-shrink='true'] {
      top: 0;
    }
    

    For styled-components users, all the above block of code can be defined as Sass mixins that can be re-used throughout the code base

    import { css } from 'styled-components'
    
    const muiTextFieldHeight = (height: number) => {
      const offset = (56 - height) / 2
    
      return css`
        input {
          padding-top: calc(18.5px - ${offset}px);
          padding-bottom: calc(18.5px - ${offset}px);
        }
    
        label {
          top: -${offset}px;
        }
    
        label[data-shrink='true'] {
          top: 0;
        }
      `
    }
    

    Then somewhere in your stylesheet

      .MuiTextField-root {
          ${muiTextFieldHeight(40)} /* set TextField height to 40px */
      }
    

提交回复
热议问题