简体   繁体   中英

How to create button with text under icon by reactjs?

Now, I have component like this: 在此处输入图片说明

code of it:

import React from "react";
import {withStyles} from "material-ui/styles";
import Settings from "material-ui-icons/Settings"; 
import Button from "material-ui/Button";

const styles = {
button: {
    color: "primary",
    height: 95,
    width: 95,
    disableRipple: "true",
    focusRipple: "true",
},
icon: {
    height: 35,
    width: 35,
    display: "block",
    float: "none",
},
text: {
    height: 35,
    width: 35,
    display: "block",
    float: "none",
    marginTop: 10,
},
};

/* eslint-disable react/prop-types */
const IconedLabel = ({classes}) => (
<section>
    <Button className={classes.iconButton} variant="raised" color="primary">
        <Settings className={classes.icon}/>
        <div className={classes.text}>Message</div>
    </Button>
</section>
);

export default withStyles(styles)(IconedLabel);

But need to button, that in top part contains icon and text message in bottom. I use reactjs and material-ui lib from here https://material-ui-next.com/demos/buttons/

The Button component uses flexbox to control the layout/alignment of content. To align the content vertically (so the icon is above the text), you can simply change the flex-direction to column .

This style needs to be applied to an element inside the button component, not to the root element. You can use the classes property to override all of the styles in a component.

In this case, you want to add flexDirection: column to the label class.

Documentation on class overrides in material ui v1

Here's a working example. Hope it helps.

 const [React, ReactDOM, Button, Settings, withStyles] = [window.React, window.ReactDOM, window['material-ui'].Button, ({className}) => <i className={`material-icons ${className}`}>settings</i>, window['material-ui'].withStyles] // Ignore code above this line const styles = theme => ({ button: { height: 95, // setting height/width is optional }, label: { // Aligns the content of the button vertically. flexDirection: 'column' }, icon: { fontSize: '32px !important', marginBottom: theme.spacing.unit } }) const CustomButton = ({ classes }) => ( <Button /* Use classes property to inject custom styles */ classes={{ root: classes.button, label: classes.label }} variant="raised" color="primary" disableRipple={true} > <Settings className={classes.icon} /> Message </Button> ) const WrappedCustomButton = withStyles(styles)(CustomButton) ReactDOM.render(<WrappedCustomButton />, document.querySelector('#root'))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><script src="https://unpkg.com/material-ui@1.0.0-beta.40/umd/material-ui.production.min.js"></script><link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"><div id="root" />

A (potentially bad) solution would simply be:

.MuiIconButton-label {
  flex-direction: column
}

I say bad, because you might want to use it in it's standard format elsewhere.

What I opted to do was add a class name nav-bar-icon-wrapper to the IconButton & set the flex direction in it's parent:

.nav-bar-icon-wrapper {
  flex-direction: column
}

.MuiIconButton-label {
  flex-direction: inherit
}

If I run into instance later where I want the icon/label button to be standard, I'll just add a new class default-icon-wrapper and css that handles that:

.default-icon-wrapper {
  flex-direction: row
}

FWIW: I preach the BEM http://getbem.com/introduction/ convention AND that whenever you make a component, you add an optional modifier prop.

I have functions in a shared dir that looks these:

export function BEMifyThis(modifier) {
    return (klass) => BEMify(klass, modifier)
}

export function BEMify(klass, modifier=false) {
    if (modifier) {
      klass += ` ${klass}-${modifier}`
    }
    return klass
}

Then I use that everywhere in my component so the user can access the component elements as a group or individually using their modifiers.

import {BEMifyThis} from '../shared/bem'
const BEMify = BEMifyThis(this.props.modifier)

className={"navbar__menu_item")}
becomes
className={BEMify("navbar__menu_item")}

so something like navbar__menu_item becomes navbar__menu_item navbar__menu_item-logout

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