简体   繁体   English

在表格中时如何使Material UI TextField的宽度减小

[英]How to make Material UI TextField less wide when in Table

I've got a Material UI Table . 我有一个材料UI表

I build it like this: 我这样构建它:

tableValues.map((curRow, row) => {
    tableRows.push(
        <TableRow key={this.state.key + "_row_" + row}> 
        {curRow.map((cellContent, col) => {
        let adHocProps = {...this.props, type:"Text", label:"", value:cellContent}

        return (
            <TableCell className={classes.tableCell} key={this.props.key + "_row:" + row + "_col:" + col}>
                {col===0 && this.props.rowHeaders ?
                <div className={classes.header}>{cellContent}</div> :
                <Question {...adHocProps} stateChangeHandler={this.handleTableChange("in build")} />}
            </TableCell>
        )})}
        </TableRow>
        );
    return null;
});

return (
    <Table key={this.props.key + "_table"} className={classes.table}>
        <TableHead>
            <TableRow>
                {this.props.colHeaders.map((header) => <TableCell className={classes.tableCell} key={this.props.id + header}><div className={classes.header}>{header}</div></TableCell>)}
            </TableRow>
        </TableHead>
    <TableBody>
        {tableRows}
    </TableBody>
</Table>
);

The Question is actually a glorified [TextField] 2 created thusly: Question实际上是这样创建的美化[TextField] 2

            <div>
                <TextField
                    value={this.state.value}
                    onChange={this.handleTextChange(this.props.key)}
                    key={this.props.key}
                    id={this.props.id}
                    label={this.props.label}
                    placeholder={realPlaceholder}
                    className={classes.textField}
                    fullWidth
                    xmlvalue={this.props.XMLValue}                      
                />
            </div>

... and then wrapped in Paper . ...然后用Paper

The styles are: 样式是:

tableCell: {
    padding: 5,
},
textField: {
    padding: 0,
    margin: 0,
    backgroundColor: "#191",
}

This works, I get the appropriate content in each cell.... but the Question element is way wider than needed, and appear to have a min width and some padding I can't remove. 这工作,我得到的每个单元中的相应内容....但Question元素比需要的方式更广泛,而且似乎有一个最小宽度和一些填充我不能删除。

The table is full-width until you get to a certain point, then notice here: 该表是全角的,直到到达特定点为止,然后在这里注意:

在此处输入图片说明

that when the window is shrunk below a certain level, the table doesn't shrink any further. 当窗口缩小到特定水平以下时,表格不会进一步缩小。 Acting as if the elements inside have a minimum width. 好像内部的元素具有最小宽度一样。

As a process of investigation, I change the Question element to simply return "Hi". 作为调查的过程,我将Question元素更改为仅返回“ Hi”。 When it does, the table then looks like this: 完成后,该表将如下所示:

在此处输入图片说明

(which is to say, it condenses nicely... still too much padding on the tops and bottom and right, but WAY better) (也就是说,它凝结得很好……在顶部,底部和右侧仍然填充过多,但是效果更好)

So that leads me to believe the issue is with my Question component. 因此,我认为问题出在我的Question组件上。 I should note this happens on other Questions as well -- they all appear to have a min width when a width is not defined for them... UNLESS they are placed inside a container that has a designated width such as a Material UI Grid . 我还应该注意,这在其他Questions上也会发生-当未为它们定义宽度时,它们似乎都具有最小宽度...除非它们被放置在具有指定宽度的容器(例如Material UI Grid)中 For example, when placed in a `Grid and the window is shrunk, they shrink appropriately: 例如,当放置在“网格”中且窗口缩小时,它们会适当缩小:

在此处输入图片说明

So why isn't the Table/TableCell also shrinking the TextField like the Grid does? 那么,为什么Table / TableCell也像Grid一样收缩TextField? (or: how do I remove the apparent "MinWidth" on my textFields?) Do Material UI TextFields have a minimum width if one isn't otherwise specified? (或:如何删除textField上明显的“ MinWidth”?)如果未另行指定,则Material UI TextField是否具有最小宽度?

For what it's worth, I have tried specifying the column widths of the table -- with success when the table is wide, but it still doesn't solve the apparent minimum width issue. 对于它的价值,我尝试指定表的列宽-当表宽时成功,但是它仍然不能解决明显的最小宽度问题。

I have also tried changing the Question component to <input type="text" name="fname" /> and still have the same problem. 我也尝试过将Question组件更改为<input type="text" name="fname" /> ,但仍然<input type="text" name="fname" />相同的问题。 It's interesting that that the Question component is simply "hi" the problem disappears but that when it's an input, it shows up. 有趣的是, Question组件只是“嗨”,问题消失了,但是当它是输入时,它就会显示出来。

I have discovered that the native input fields default width is 20 characters: https://www.w3schools.com/tags/att_input_size.asp 我发现本机输入字段的默认宽度为20个字符: https : //www.w3schools.com/tags/att_input_size.asp

The 'size' property is key here: 'size'属性在这里很关键:

Specifies the width of an element, in characters. 指定元素的宽度(以字符为单位)。 Default value is 20 默认值为20

To set the width of the TextField , you must pass properties to the native input field. 要设置TextField的宽度,必须将属性传递给本机输入字段。

If you wish to alter the properties applied to the native input, you can do so as follows: 如果要更改应用于本机输入的属性,可以执行以下操作:

const inputProps = {
  step: 300,
};

return <TextField id="time" type="time" inputProps={inputProps} />;

For my use case, the following modified the sizes of the TextFields to be 10 characters in size: 对于我的用例,以下内容将TextField的大小修改为10个字符:

<TextField
                    value={this.state.value}
                    onChange={this.handleTextChange(this.props.key)}
                    key={this.props.key}
                    id={this.props.id}
                    label={this.props.label}
                    placeholder={realPlaceholder}
                    className={classes.textField}
                    fullWidth
                    xmlvalue={this.props.XMLValue}   
                    inputProps={{
                        size: 10
                    }}                      
                />

Unfortunately, this is a bit squishy... it neither holds the input field at exactly size nor does it treat it like a minimum size.... There appears to be some heirarchy of sizing in play between GridItems , table Columns, free-flow flex areas, and the actual TextField elements... and I'm not well versed enough to know what always 'wins'. 不幸的是,这有点糊涂……既不将输入字段保持在正确的size也不将其视为最小大小。...似乎在GridItems ,table Columns,free-流动的柔韧性区域,以及实际的TextField元素...而且我还不太了解什么总是“赢”。

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

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