How to put border bottom to react-chips in reactjs? - reactjs

I'm new to react and I have created a modal and used react-chips for showing suggestions, but I wasn't able to change the field styling the same way I changed it for the subject input field. I've tried styling but I'm unable to change the styling of the react-chip.
Here is the code:
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
chips: []
};
}
onChange = chips => {
this.setState({ chips });
};
render() {
return (
<Container>
<Modal trigger={<Button>Show Modal</Button>}>
<Modal.Content>
<Form>
<strong>Email</strong>
<Chips
className="f_input"
value={this.state.chips}
onChange={this.onChange}
suggestions={["Your", "Data", "Here"]}
/>
<Form.Field
transparent
className="f_input"
control={Input}
label="Subject"
/>
<TextArea placeholder="Email Format" />
</Form>
<Button>Send</Button>
</Modal.Content>
</Modal>
</Container>
);
}
}
Here is the Code: https://codesandbox.io/s/boring-yalow-sx678
Can anyone help me in this query?

The library encourages styling the chips using the chipTheme prop;
Here is for example how you can add some custom styling to the chips in the example you provided:
<Chips
className="f_input"
value={this.state.chips}
onChange={this.onChange}
suggestions={["Your", "Data", "Here"]}
chipTheme={{
chip: {
color: "white",
background: "blue",
borderRadius: 20
},
chipRemove: {
fontWeight: "bold",
cursor: "pointer",
color: "green",
":hover": {
color: "yellow"
}
}
}}
/>
UPDATE
In case you would like to style the chips-container and input you should use the theme prop.
In your case you should override the chipsContainer style with the following properties in order to get an underline similar to the one you have on the "subject" input :
border: "none",
borderBottom: "1px solid black",
marginBottom: 14,
Here is your forked sandbox with the solution.
And here is a reference to the default theme objects which you can override.

Related

Editing the font color of the text inside ImageListItemBar

I'm new to using Material UI and have integrated into my portfolio website to create an ImageList that redirect to other projects I'd like to show possible employers. I'm having trouble editing the style of the text inside ImageListItemBar. I've tried using the primaryTypographyProps and sx to no avail. Could someone point me in the right direction?
<Typography variant="h3" color="common.red">
<ImageListItemBar
primaryTypographyProps={{fontSize: '30px'}}
sx={{
background: 'black',
}}
position="bottom"
title={item.title}
/>
</Typography>
//tried this as well
<ImageListItemBar
sx={{
color: '#000';
background: 'black',
}}
position="bottom"
title={item.title}
/>
Here is the code You are after:
<ImageListItemBar
title="image title"
subtitle="image subtitle"
sx={{
"& .MuiImageListItemBar-title": { color: "red" }, //styles for title
"& .MuiImageListItemBar-subtitle": { color: "yellow" }, //styles for subtitle
}}
/>
I recommend studying official MUI docs for ImageList and ImageListItemBar API
Hamed's answer is correct, you need to target the specific class applied to the title in order to style it.
I've been developing a pattern of doing this lately, where you can import the component's classes and style accordingly using styled. This allows you to use your IDE to autocomplete the class names and target them accordingly.
const StyledImageListItemBar = styled(ImageListItemBar)(() => ({
[`& .${imageListItemBarClasses.title}`]: {
color: "red"
},
[`& .${imageListItemBarClasses.subtitle}`]: {
color: "yellow"
},
backgroundColor: "black"
}));
Then you can just use the component without having to resort to sx:
export default function App() {
return (
<StyledImageListItemBar
title="This is a title"
subtitle="This is a subtitle"
/>
);
}
Here's a sandbox of this in action if you want to play with it: https://codesandbox.io/s/mui-targetting-classes-8y5kpm?file=/src/App.js
That being said, if you're planning to reuse this component with the custom styling, I would consider overriding the default styling in theme too.

How to style components properly in React

I have the following component Login. What it does is not important but for the styling, you can see inside the render, I added loginStyle, columnStyle, and inputStyle. This works fine but I have another component Register pretty much identical to the Login component with just a few changes, which are not important. But in my Register component I have the same thing columnStyle, inputStyle, and instead of loginStyle I have registerStyle.
The styles I am applying in Login and Register are exactly the same. My question is do I create some sort of component that has all these stylings I created (and just create them once to not have duplicate code) and somehow use that to style anything that needs styling in different components or do I just keep a separate js file that contains all these styles and import it into separate component files. I just want to know the professionally accepted way for this. I am aware that we can use css but I see a lot of projects not using css to style their components.
import React from 'react';
import Button from '../Buttons/Button';
class Login extends React.Component {
constructor(props) {
super(props);
this.state = {
email: '',
password: '',
};
this.onChange = this.onChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}
onChange(event) {
this.setState({
[event.target.name]: event.target.value,
});
}
onSubmit(event) {
event.preventDefault();
alert('Login button clicked');
}
render() {
const loginStyle = {
display: 'flex',
flexDirection: 'row',
rowGap: '15px',
};
const columnStyle = {
display: 'flex',
flexDirection: 'column',
width: '100vw',
rowGap: '30px',
};
const inputStyle = {
fontSize: '40px',
outline: 'none',
border: 'none',
borderBottom: '1px solid black',
};
return (
<form onSubmit={this.onSubmit} style={loginStyle}>
<div style={columnStyle}>
<input
style={inputStyle}
placeholder='Enter your email...'
type='email'
name='email'
value={this.state.email}
onChange={this.onChange}
/>
<input
style={inputStyle}
placeholder='Enter your password...'
type='password'
name='password'
value={this.state.password}
onChange={this.onChange}
/>
<Button type='submit' />
</div>
</form>
);
}
}
export default Login;
The projects you refer to is most likely small ones. Using styling in this way is not scalable. Surely still a valid choice, as react native for example is better styled this way, or even small UI library alike components in projects. But in general, use css and smack preprocessor on top of that for even further enhancing development experience.
Then look up good rule set for your css, as an example Airbnb provides good things regarding this https://github.com/airbnb/css
This can be from simple names you use for classes, to file structure.
Is better approach for each component to make a .css file or modules.css file, this technique will help you to keep your style seperate from your component and you can make it responsive easier.
If your example have same style-css for two or three components or divs you can make this style global and use it in your every js file.
Most of the elements have className attribute that you can write your styles by name it for example
<div className="columnStyle">
<input
className="inputStyle"
placeholder='Enter your email...'
type='email'
name='email'
value={this.state.email}
onChange={this.onChange}
/>
</div>
and in your .css file you can add the styling
.columnStyle {
display: 'flex',
flexDirection: 'column',
width: '100vw',
rowGap: '30px',
};
.inputStyle {
fontSize: '40px',
outline: 'none',
border: 'none',
borderBottom: '1px solid black',
};

Reactjs material-UI TextField apply css properties

I have a similar login page made with material-UI but in my code there are differences with original one. Actually, the problem is starting when I click on the TextField.
The old inputs will shown and I select one of them the background of TextField become white.
In addition, if there are passwords saved on chrome when entering the login page for the first time, it automatically makes the background white by auto-filling.
My login page theme is dark and it is looking bad. How can I make textField background same with Material-UI login page?
Here is my code part:
const CssTextField = withStyles({
root: {
'& .MuiInput-underline:after': {
borderBottomColor: 'yellow',
},
'& .MuiOutlinedInput-root': {
'& fieldset': {
borderColor: 'white',
},
'&:hover fieldset': {
borderColor: 'white',
},
'&.Mui-focused fieldset': {
borderColor: 'yellow',
},'&.Mui-focused ': {
},
},
},
})(TextField);
export default function SignIn(props) {
return (
<form className={classes.form} noValidate>
<CssTextField
focused={true}
variant="outlined"
margin="normal"
required
fullWidth
id="email"
label="userName"
name="text"
type="text"
{...username}
autoComplete="text"
autoFocus
InputLabelProps={{
style: { color: '#fff' },
}}
InputProps={{
style: {
color: "red"
}
}}
/>
</form>
)
}
It is look like this:
This is the solution:
const styles = { WebkitBoxShadow: "0 0 0 1000px white inset" };
<CssTextField inputProps={{ style: inputStyle }} />
Keep in mind, that in material-ui there is a difference between inputProps and InputProps. The capital I changes the component, that is wrapped around the native input and the lowercase i manipulates the native input.

Material UI | How to change the font colour of a disabled input text field?

The colour of a disabled input text field created using material UI is light grey by default and it is not very visible against a white background. Is there any way to change the font colour of a disabled input text field?
Below is an example of how to do this showing the customized version next to the default styling.
import React from "react";
import { withStyles } from "#material-ui/core/styles";
import TextField from "#material-ui/core/TextField";
import Button from "#material-ui/core/Button";
const DarkerDisabledTextField = withStyles({
root: {
marginRight: 8,
"& .MuiInputBase-root.Mui-disabled": {
color: "rgba(0, 0, 0, 0.6)" // (default alpha is 0.38)
}
}
})(TextField);
export default function Demo() {
const [disabled, setDisabled] = React.useState(true);
return (
<>
<Button onClick={() => setDisabled(!disabled)}>Toggle Disabled</Button>
<br />
<br />
<DarkerDisabledTextField
disabled={disabled}
id="outlined-basic"
label="Custom"
value={`Disabled = ${disabled}`}
variant="outlined"
/>
<TextField
disabled={disabled}
id="outlined-basic"
label="Default"
value={`Disabled = ${disabled}`}
variant="outlined"
/>
</>
);
}
I think the simplest way is to create an object to define the font color and pass it to the TextField's inputProps.
const fontColor = {
style: { color: 'rgb(50, 50, 50)' }
}
This way you can toggle the font with the components state as you wish or simply keep it constant.
<TextField
label="Title"
onChange={updateTitle}
value={title}
disabled={true}
inputProps={fontColor}/>
import { TextField, styled } from '#mui/material'
const CustomTextField = styled(TextField)({
'& .MuiInputBase-root.Mui-disabled': {
backgroundColor: '#f0f0f0',
},
});
Then use them as standard component
<CustomTextField
name="manual"
label="Manual"
size="small"
disabled={watch({}).platform === 'manual' ? false : true}
/>
if u use RHF with controller, mean u create custom TextField using RHF, u just change the component inside the styled()
For example:
import RHFTextField from "../RHFTextField"
const CustomTextField = styled(RHFTextField)({
'& .MuiInputBase-root.Mui-disabled': {
backgroundColor: '#f0f0f0',
},
});
with background color changes, it will more visible..
In the Mui-v5 above solution is not working.
Below are the solutions for Mui-v5.
Solution 1:
const styles = theme => ({
input: {
"& input.Mui-disabled": {
color: "green"
}
}
});
Solution: 2 (use sx property for the component)
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}
eg.
<TextField
fullWidth
disabled=true
variant="outlined"
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}
/>
None of all the many answers online worked for me, using MUI v4.
Finally I found a very easy but hacky solution for setting the font color of a disabled TextField component.
The problem is, (at least for Firefox) it will use this weird style cheet:
.css-[...]-MuiInputBase-input-MuiOutlinedInput-input.Mui-disabled {
opacity: 1;
-webkit-text-fill-color: rgba(0, 0, 0, 0.38);
}
I'm too new to this stuff and I don't know how to change it, so my way was to overwrite it:
.css-[...]-MuiFormControl-root-MuiTextField-root input {
-webkit-text-fill-color: rgba(255,255,255,0.6) !important;
color: rgba(255,255,255,0.6);
}
The !important part is important!
In React with MUI v4, it's as simple as that:
const textFieldColor = /*...*/;
const textFieldSX = {
input: {
"-webkit-text-fill-color": `${textFieldColor} !important`,
color: `${textFieldColor} !important`,
},
};
/*...*/
<TextField sx={textFieldSX} />
Just in case, this -webkit-text-fill-color property would turn into color one time, one can use an !important there too.
works (MUIv5):
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}

Changing MUI Chip primary or secondary color

I am trying to programmatically change the color of a MUI Chip without much luck. According to the Chip API you have to set the color via the color prop with one of three values from an enum; default, primary, and secondary. You should then be able to override the the colorPrimary or colorSecondary css classes and the background color should change.
Here is an example of my code:
<Chip key={label.id} color='primary' classes={{ colorPrimary: label.color }} label={label.label} />
And a picture of the element in browser:
https://i.imgur.com/bWYGzzz.png cant inline yet :(
If you look at the selected element, you will see the correct color I am trying to apply, #ff0000, so it is getting the color and putting it somewhere.
I've tried this variation, adding the colorBackground property, but I get an error saying the colorPrimary class expects a string instead of an object
<Chip key={label.id} color='primary' classes={{ colorPrimary: { backgroundColor: label.color } }} label={label.label} />
Again to reiterate my goal: I want to apply a hex code color to the chip to change the background color.
you can make it in many ways.
you can add styles directly
<Chip key={label.id} color='primary' style={{backgroundColor:'green'}} label={label.label} />
or you can override the class:
const StyleChip = withStyles({
root: {
backgroundColor:'salmon'
}
})(Chip);
to use everywhere you only will replace Chip to StyleChip
<StyleChip key={label.id} color='primary' label={label.label} />
but if you wanna put the color by programation, the first way is perfect, because
style={{backgroundColor:_thisCanBeVariable_}}
You can set the primary or secondary color of the Chip component easily using createTheme:
const theme = createTheme({
components: {
MuiChip: {
styleOverrides: {
colorPrimary: {
backgroundColor: 'red',
},
colorSecondary: {
backgroundColor: 'brown',
},
},
},
},
});
<Chip label="primary" color="primary" />
<Chip label="secondary" color="secondary" />
Or if you're using MUI v5, you can quickly change its color with the help of sx prop:
<Chip label="sx" sx={{ bgcolor: 'green', color: 'white' }} />
If you want to be able to specify different color via a prop, you can use styled:
const options = {
shouldForwardProp: (prop) => prop !== 'bgcolor',
};
const StyledChip = styled(
Chip,
options,
)(({ bgcolor }) => ({
color: 'white',
backgroundColor: bgcolor,
}));
<StyledChip label="styled" bgcolor="purple" />
For those trying to get this previous to the v5 (which would need to add a new color to the palette) a simple wrapper will get the job done:
import React from 'react';
import PropTypes from 'prop-types';
import MaterialChip from '#material-ui/core/Chip';
import { withStyles } from '#material-ui/core/styles';
const Chip = (props) => {
const StyledChip = withStyles({
root: {
'color': 'white',
'backgroundColor': `${props.color} !important`,
'&:hover': {
backgroundColor: props.color,
filter: 'brightness(120%)',
},
'&:active': {
boxShadow: 'none',
backgroundColor: props.color,
borderColor: props.color,
},
},
outlined: {
color: props.color,
border: `1px solid ${props.color}`,
backgroundColor: `transparent !important`,
},
icon: {
color: props.variant === 'outlined' ? props.color : 'white',
},
deleteIcon: {
color: props.variant === 'outlined' ? props.color : 'white',
},
})(MaterialChip);
return <StyledChip {...props} />;
};
Chip.propTypes = {
color: PropTypes.string,
variant: PropTypes.oneOf(['regular, outlined']),
};
export default Chip;

Resources