最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to align ListItem text - Stack Overflow

programmeradmin2浏览0评论

I have this material-ui list:

<List dense>
  {this.state.issues.map((item, i) => {
    return <Link target="_blank" to={item.issue_url} key={i}>
      <ListItem button>
        <ListItemText primary={item.issue_state}/>
        <ListItemSecondaryAction>
          <IconButton>
            <ArrowForward/>
          </IconButton>
        </ListItemSecondaryAction>
      </ListItem>
    </Link>
  })
  }
</List>

issue_state is obtained from MongoDB. Can I add another column to my list showing another field from the database? I tried:

<ListItemText primary={item.issue_state}/>
<ListItemText primary={item.issue_title}/>

This displays what I want but the issue_title test is centered. I'd like it left aligned. Can this be done?

I have this material-ui list:

<List dense>
  {this.state.issues.map((item, i) => {
    return <Link target="_blank" to={item.issue_url} key={i}>
      <ListItem button>
        <ListItemText primary={item.issue_state}/>
        <ListItemSecondaryAction>
          <IconButton>
            <ArrowForward/>
          </IconButton>
        </ListItemSecondaryAction>
      </ListItem>
    </Link>
  })
  }
</List>

issue_state is obtained from MongoDB. Can I add another column to my list showing another field from the database? I tried:

<ListItemText primary={item.issue_state}/>
<ListItemText primary={item.issue_title}/>

This displays what I want but the issue_title test is centered. I'd like it left aligned. Can this be done?

Share Improve this question asked Dec 2, 2018 at 19:15 runnerpaulrunnerpaul 7,32410 gold badges70 silver badges161 bronze badges 2
  • Have you tried alignItems property ? – Code Maniac Commented Dec 2, 2018 at 19:18
  • Yes. I tried alignItems='flex-start but no luck. – runnerpaul Commented Dec 2, 2018 at 19:24
Add a ment  | 

1 Answer 1

Reset to default 4

ListItemText ponent renders with the following CSS style that allows it to be flexible to grow and shrink according as the width and height of its content.

flex: 1 1 auto;

Its ancestor ListItem ponent renders as an inline-flex. AFAICT, the results you're looking to achieve can't be done without overriding these styles. Not to worry, there are other ways to go that uses available APIs exposed in the ponent.

Never mind that the name for the ponent seems to be misleading that its usage is specific for rendering text in the list item. A closer look in the ListItemText ponent API documentation shows that the primary property is of the node type.

That implies that the ListItemText can be used to render string, function, and React.Element in a manner similar to the snippet below.

<ListItemText primary={<div>Foo<br/>Bar<br/>Baz</div>} />

There is also the secondary property that renders the elements with textual emphasis.

<ListItemText primary="Foo" secondary={<div>Bar<br/>Baz</div>} />

If you need more control over the children of the ListItemText, the API allows for the flexibility to write it this way

      <ListItemText>
        <Typography variant="subheading">Foo</Typography>
        <Typography variant="body1" color="textSecondary">Bar</Typography>
        <Typography variant="body1" color="textSecondary">Baz</Typography>
      </ListItemText>

发布评论

评论列表(0)

  1. 暂无评论