简体   繁体   中英

React: Material-UI - how to style the two item list so items will be next to each other?

I've got a list that consists of two items: 'label' and 'value', this is how it currently looks like:

在此处输入图像描述

How I can move items that are on the right side to the left, so they would stay next to the label?

在此处输入图像描述

This is my code sample:

  <List>
    {details.map((item, index) =>
      <ListItem key={index} divider alignItems="flex-start">

        <ListItemIcon>
          <i className={item.icon}/>
        </ListItemIcon>

        <ListItemText className="br-break" primary={item.label} />

        <ListItemSecondaryAction>
          <ListItemText className="br-break" primary={item.data}/>
        </ListItemSecondaryAction>

      </ListItem>
    )}
  </List>

I've tried to remove <ListItemSecondaryAction> tag - didn't work.

One option is to use material-ui styling to override ListItemText flex value:

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';

const useStyles = makeStyles(theme => ({
  root: {
    flex: '0.2',
    minWidth: '100px'
  },
}));

export default function ListDemo() {
  const classes = useStyles();

  const details = [
    {
      label: 'Issue Number',
      data: 'CCM-1007'
    },
    {
      label: 'Environment',
      data: 'TST'
    },
    {
      label: 'Country',
      data: 'SE'
    },
  ]

  return (
    <List>
    {details.map((item, index) =>
      <ListItem key={index} divider alignItems="flex-start">
        <ListItemIcon>
          <i className={item.icon} />
        </ListItemIcon>
        <ListItemText className={classes.root} primary={item.label} />
        <ListItemText className={classes.root} primary={item.data} />
      </ListItem>
    )}
  </List>
  );
}

编辑隐形背景

  • Another solution without makeStyles.
import React from "react";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import ListItemIcon from "@material-ui/core/ListItemIcon";
import ListItemText from "@material-ui/core/ListItemText";

export default function ListDemo() {

  const details = [
    {
      label: "Issue Number",
      data: "CCM-1007"
    },
    {
      label: "Environment",
      data: "TST"
    },
    {
      label: "Country",
      data: "SE"
    }
  ];

  return (
    <List>
      {details.map((item, index) => (
        <ListItem key={index} divider alignItems="flex-start">
          <ListItemIcon>
            <i className={item.icon} />
          </ListItemIcon>
          <ListItemText
            primary={item.label}
            primaryTypographyProps={{
              display: "inline"
            }}
            secondary={item.data}
            secondaryTypographyProps={{
              display: "inline"
            }}
          />
        </ListItem>
      ))}
    </List>
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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