How to convert simple CSS into makeStyles for React? - reactjs

How to convert simple CSS into makeStyles for React?
I have added style to the TextField to make it stretchable vertically, by using the attribute resize. It is working, actually. Below is the original code I have that I need to refactor.
<div className="mb-4 stretchable">
<style>{"\
.stretchable textarea{\
resize:vertical;\
}\
"}
</style>
<TextField
multiline
label="取材メモ"
className={classes.meetingLogContent}
rows={8}
name='content'
value={meetingLogData.content}
onChange={handleChange}
variant="outlined"
fullWidth
/>
</div>
It is just that our client wanted the style I added to be converted into makeStyle. And it could not target the TextField and does not make it stretchable.
const useStyles = makeStyles(() => ({
meetingLogContent: {
resize: 'vertical',
},
}))
After that I instantiated it into classes.
const classes = useStyles()
And applied the newly instantiated variable to the textfield.
<TextField className={classes.meetingLogContent} />
Am I missing something here? Thanks in advance for your help.

You should apply resize to a div that contains TextArea and not to the TextArea itself. Something like:
<div className={classes.meetingLogContent}>
<TextField
multiline
label="取材メモ"
rows={8}
name='content'
value={meetingLogData.content}
onChange={handleChange}
variant="outlined"
fullWidth
/>
</div>
Then your useStyles:
const useStyles = makeStyles(() => ({
meetingLogContent: {
resize: 'vertical',
overflow: 'auto' //<-- don't forget overflow to auto
},
}))
The result is:
Here a codesandbox example.

Related

Remove the border from TexfField

I started using #Mui for React and I used it to create the form but after I focused TextField I see the border linke on the below screen:
My code look like this:
<FormGroup>
<TextField
label="Description"
name="description"
multiline
rows={5}
fullWidth
value={data.description}
variant="standard"
onChange={onHandleChange}
/>
</FormGroup>
How remove that?
#Edit
I resolved my problem. Border around of this element appears becouse I has styles from Breeze
Normally this can be done via CSS as well.
input {
outline: none
}
You can change the styling (remove the underline) of TextField when it is focused with makeStyles using :after
In your case do the following :
import TextField from "#mui/material/TextField";
import FormGroup from "#mui/material/FormGroup";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles({
underline: {
"&&:after": {
borderBottom: "none"
}
}
});
export default function ComboBox() {
const classes = useStyles();
return (
<FormGroup>
<TextField
InputProps={{ classes }}
label="Description"
name="description"
multiline
rows={5}
fullWidth
value={data.description}
variant="standard"
onChange={onHandleChange}
/>
</FormGroup>
);
}
And if you want it with no border at all you can pass InputProps={{ disableUnderline: true }} to your TextField

Textfield's label gets clipped when inside a Dialog

When using a Textfield as the first child of DialogContent:
export default function App() {
return (
<Dialog open={true}>
<DialogTitle>Hey</DialogTitle>
<DialogContent>
<TextField
fullWidth
id='name'
label={'Foo'}
name='name'
required
type='text'
value={'Bar'}
/>
</DialogContent>
</Dialog>
);
}
its label (when using `variant="outlined") gets clipped. See codebox sample. Any way to fix this problem? e.g. by customizing the theme?
You can easily fix that issue by adding some margin to the TextField like the following.
sx={{ marginTop: 2 }}
Or you could wrap the TextField using Box inside the DialogContent like the following.
<Box p={1}>
<TextField
...
/>
</Box>

Render "outlined" input when using react-text-mask with Material-UI inputs

I'm unable to figure out how to render outlined input type(one of the standard Material-UI input styles when used with text mask).
I copied the code sample from here: https://material-ui.com/components/text-fields/
But it only gives a sample for regular(underlined) input but nothing for outlined.
This is what i'm trying to do but it doesnt work:
const ExpirationMask = props => {
const { inputRef, ...other } = props
return <MaskedInput
{...other}
ref={ref => {inputRef(ref ? ref.inputElement : null)}}
mask={[/\d/, /\d/,'/' ,/\d/, /\d/]}
placeholderChar={'\u2000'}
/>
}
<FormControl
variant='outlined' //this doesnt work
fullWidth
>
<InputLabel>Expiration</InputLabel>
<Input
value={ccExpiration}
onChange={(e, v) => setCCExpiration(e.target.value)}
placeholder='MM/YY'
variant='outlined' //this doesnt work either
inputComponent={ExpirationMask}
/>
</FormControl>
I found a solution for it. I didn't realize that TextField is just a wrapper for the Input.
There is also another wrapper for the Input which is called OutlinedInput. So this was exactly what I ended up using:
<FormControl
fullWidth
margin='dense'
>
<InputLabel
classes={{ root: classes.expInputRoot }}
error={ccExpiration.trim().length < 5}
color='primary'>
Expiration
</InputLabel>
<OutlinedInput
value={ccExpiration}
onChange={(e, v) => setCCExpiration(e.target.value)}
label="Expiration"
placeholder='MM/YY'
error={ccExpiration.trim().length < 5}
inputComponent={ExpirationMask}
/>
</FormControl>
In doing so i encountered another problem however with an InputLabel that wasn't aligning properly(not sure if it is a bug or what), so i ended up manually changing styles for it like this:
expInputRoot: {
margin:'-8px 0 0 14px'
}

Incorrect dropdown positioning of Autocomplete component for React Material-UI

At expanding the Material-UI Autocomplete component the dropdown opens as expected. However once it is opened and I scroll it moves instead of staying positioned below the autocomplete box. I found that this behaviour should be fixed for the version of material ui that I use as stated here. I also tested that for a plain new project on sandbox, where it worked fine.
Therefore, I expect to have unwillingly overwritten some important styling settings but could not figure out the mistake. Any ideas where I could look?
Following the relevant code snippet:
The component where I render the Autocomplete component
const useStyles = makeStyles(theme => ({
...,
vinBox: {
width: 320,
marginTop: "4px"
}
}));
...
return (
<Autocomplete
id="vin-selection"
options={allVehicleVins}
className={classes.vinBox}
onChange={(event, data) => setFilter({ ...filter, vin: data })}
value={filter.vin}
renderInput={params => (
<TextField
{...params}
margin={"normal"}
label="VIN"
variant="standard"
InputProps={{
...params.InputProps,
startAdornment: (
<InputAdornment position="start">
<DirectionsCar />
</InputAdornment>
)
}}
helperText={
"Choose a VIN to filter for specific vehicles of your fleet."
}
fullWidth
/>
)}
/>
);
};
I used Chrome Version 80.0.3987.149 (althoug also happened in other Browsers). Some of my dependencies:
...
"#material-ui/core": "^4.8.2",
"#material-ui/icons": "^4.5.1",
"#material-ui/lab": "^4.0.0-alpha.35",
"react": "^16.12.0",
...
sooo..
i found this error, and can't find the right answer.
so, my solution:
const Pop = props => {
const { className, anchorEl, style, ...rest } = props
const bound = anchorEl.getBoundingClientRect()
return <div {...rest} style={{
position: 'absolute',
zIndex: 9999,
width: bound.width
}} />
}
and
<Autocomplete
...
PopperComponent={Pop}
...
/>
Ok, after extensive testing I found my error. I have an index.css right in the src folder. In there I have following styling:
body {
...
overflow-y: scroll;
...
}
changing this does the trick. Hope that helps anyone reading this :)

React Material-UI Textfield padding top - Thai Language

I'm use the library for input. But when use with Thai language, it need additional top padding to display word correctly as Thai has 2 level of vowel. For example, word like "ที่นั่น" will be cut on the top. Below is the code I use.
<Grid item xs={12} md={10}>
<TextField required id="name" label="Remark name" fullWidth />
</Grid>
When i put word "ที่นั่น" inside Textfield will display only this. I try various style to change this but not success.
Screencap run of the code
Thank you for all your comment. For my case, I found out that I need to put paddingTop in InputProps. So, the code I use is:
const styles = theme => ({
thaiTextFieldInputProps:{
paddingTop: '5px'
},
});
and then
<TextField
InputProps={{
classes: {
input: classes.thaiTextFieldInputProps
}
}}
label="Thai Remark"
fullWidth
/>
Since you are using MUI you can use their css wrapper function withStyles like this
const styles = theme => ({
name: {
paddingTop: '50px', // for example
},
});
and then
<Grid item xs={12} md={10}>
<TextField
className={classes.name}
required
id="name"
label="Remark name"
fullWidth
/>
</Grid>
After this you just need to wrapp your component in withStyles HOC:
export default withStyles(styles)(NameOfYourComponent);
To fix you problem need to update default styles:
Add class name for text field
<Grid item xs={12} md={10}>
<TextField
className="fix-thai" // <<<<<
required
id="name"
label="ที่นั่น"
fullWidth
/>
</Grid>
In you styles add this class and reset styles for input:
.fix-thai input {
height: 20px;
}
See live example:
I'm make pull request to fix it. With new release I think fixed it.

Resources