TextField Label to show separately from input - reactjs

Currently using this code of material-ui, the label is showing on the top-left corner of input, but what if I wanted to show the label outside of input separately. I know I can do this using the <InputLabel/> but is there any alternative that can done using <TextField/> only.
<TextField
id="username"
label={<Typography className={classes.label}>Username</Typography>}
fullWidth
margin="normal"
InputLabelProps={{
shrink: true,
}}
InputProps={{
classes: { input: classes.inputs },
}}
name="username"
autoComplete="username"
autoFocus
helperText={touched.username ? errors.username : ''}
error={touched.username && Boolean(errors.username)}
value={values.username}
variant="outlined"
onChange={handleChange}
/>

I used a Grid and InputLabel to be able to make what you want by manually implementing the label and define its position, I have something like this:
<Grid container >
<Grid item xs={12}>
<InputLabel
shrink={false}
htmlFor={"username"}
>
<Typography className={classes.label}>Username</Typography>
</InputLabel>
<TextField
id="username"
fullWidth
margin="normal"
InputLabelProps={{
shrink: true,
}}
InputProps={{
classes: { input: classes.inputs },
}}
name="username"
autoComplete="username"
autoFocus
helperText={touched.username ? errors.username : ''}
error={touched.username && Boolean(errors.username)}
value={values.username}
variant="outlined"
onChange={handleChange}
/>
</Grid>
</Grid>

Related

Cannot get React testing Jest-DOM to select a date from a date picker or time from a time picker

I cannot, for the life of me, figure out how to get Jest-dom in react testing to select a date picker and the same for the time picker. All my other fields are being done correctly but not these. I tried editing the elements but because they are from MaterialUI I had to pass the data-testid through as props which gave access to updating the elements but still is not working for where the input fields have type:date or type:time. I am very new to testing react (this is my first react project). How am I able to access those fields to edit?
The output is below:
{
name: 'P. Sherman',
contactNumber: '98764321265',
checkInDate: '',
checkOutDate: '',
checkInTime: '',
checkOutTime: '',
bookingReference: 'Booking Reference',
buildingNumber: '42',
buildingName: '',
addressLine1: 'Wallaby Way',
addressLine2: '',
city: 'Sydney',
postalCode: '2000',
stateCounty: 'NSW',
countryCode: ''
}
The Jest tests:
const name = "P. Sherman";
fireEvent.change(screen.getByTestId("name"), {
target: { value: name },
});
const contactNumber = 98764321265;
fireEvent.change(screen.getByTestId("contactNumber"), {
target: { value: contactNumber },
});
const checkInDate = "01/01/2023";
fireEvent.change(screen.getByTestId("checkInDateInput"), {
target: { value: checkInDate },
});
const checkOutDate = "02/01/2023";
fireEvent.change(screen.getByTestId("checkOutDateInput"), {
target: { value: checkOutDate },
});
const checkInTime = "17:00";
fireEvent.change(screen.getByTestId("checkOutDateInput"), {
target: { value: checkInTime },
});
const checkOutTime = "11:00";
fireEvent.change(screen.getByTestId("checkOutDateInput"), {
target: { value: checkOutTime },
});
const bookingReference = "Booking Reference";
fireEvent.change(screen.getByTestId("bookingReference"), {
target: { value: bookingReference },
});
const buildingNumber = "42";
fireEvent.change(screen.getByTestId("buildingNumber"), {
target: { value: buildingNumber },
});
const addressLine1 = "Wallaby Way";
fireEvent.change(screen.getByTestId("addressLine1"), {
target: { value: addressLine1 },
});
const city = "Sydney";
fireEvent.change(screen.getByTestId("city"), {
target: { value: city },
});
const postalCode = "2000";
fireEvent.change(screen.getByTestId("postalCode"), {
target: { value: postalCode },
});
const stateCounty = "NSW";
fireEvent.change(screen.getByTestId("stateCounty"), {
target: { value: stateCounty },
});
fireEvent.click(screen.getByTestId("saveAccommodationDetails"));
expect(
screen.getByText("Address: 42, Wallaby Way, Sydney, NSW, 2000")
).toBeInTheDocument();
The return for the function component:
return (
<div>
<Fab size="large" color="secondary" aria-label="add" onClick={handleOpen}>
<AddIcon />
</Fab>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>Accommodation</DialogTitle>
<DialogContent>
<DialogContentText>
Add Accommodation. Fill in the fields to store your Accommodation
details
</DialogContentText>
<TextField
value={accommodation.name}
autoFocus
margin="dense"
id="name"
name="name"
label="Accommodation Name"
type="text"
variant="outlined"
required
onChange={handleChange}
/>
<TextField
value={accommodation.contactNumber}
inputProps={{ "data-testid": "contactNumber" }}
autoFocus
margin="dense"
id="contactNumber"
name="contactNumber"
label="Accommodation Contact Number"
type="number"
variant="outlined"
required
onChange={handleChange}
/>
<TextField
value={accommodation.checkInDate}
data-testid="checkInDate"
inputProps={{ "data-testid": "checkInDateInput" }}
autoFocus
margin="dense"
id="checkInDate"
name="checkInDate"
label="Date of Arrival"
type="date"
variant="outlined"
required
InputLabelProps={{
shrink: true,
}}
onChange={handleChange}
/>
<TextField
value={accommodation.checkOutDate}
inputProps={{ "data-testid": "checkOutDateInput" }}
data-testid="checkOutDate"
autoFocus
margin="dense"
id="checkOutDate"
name="checkOutDate"
label="Date of Departure"
type="date"
variant="outlined"
required
InputLabelProps={{
shrink: true,
}}
onChange={handleChange}
/>
<TextField
value={accommodation.checkInTime}
inputProps={{ "data-testid": "checkInTimeInput" }}
data-testid="checkInTime"
autoFocus
margin="dense"
id="checkInTime"
name="checkInTime"
label="Time of Arrival"
type="time"
variant="outlined"
required
InputLabelProps={{
shrink: true,
}}
onChange={handleChange}
/>
<TextField
value={accommodation.checkOutTime}
inputProps={{ "data-testid": "checkOutTimeInput" }}
data-testid="checkOutTime"
autoFocus
margin="dense"
id="checkOutTime"
name="checkOutTime"
label="Time of Departure"
type="time"
variant="outlined"
required
InputLabelProps={{
shrink: true,
}}
onChange={handleChange}
/>
<TextField
value={accommodation.bookingReference}
inputProps={{ "data-testid": "bookingReference" }}
autoFocus
margin="dense"
id="bookingReference"
name="bookingReference"
label="Booking Reference"
type="text"
variant="outlined"
required
onChange={handleChange}
/>
<TextField
value={accommodation.buildingNumber}
inputProps={{ "data-testid": "buildingNumber" }}
autoFocus
margin="dense"
id="buildingNumber"
name="buildingNumber"
label="Building Number"
type="text"
variant="outlined"
onChange={handleChange}
/>
<TextField
value={accommodation.buildingName}
inputProps={{ "data-testid": "buildingName" }}
autoFocus
margin="dense"
id="buildingName"
name="buildingName"
label="Building Name"
type="text"
variant="outlined"
onChange={handleChange}
/>
<TextField
value={accommodation.addressLine1}
inputProps={{ "data-testid": "addressLine1" }}
autoFocus
margin="dense"
id="addressLine1"
name="addressLine1"
label="Address Line 1"
type="text"
variant="outlined"
required
onChange={handleChange}
/>
<TextField
value={accommodation.addressLine2}
inputProps={{ "data-testid": "addressLine2" }}
autoFocus
margin="dense"
id="addressLine2"
name="addressLine2"
label="Address Line 2"
type="text"
variant="outlined"
onChange={handleChange}
/>
<TextField
value={accommodation.city}
inputProps={{ "data-testid": "city" }}
autoFocus
margin="dense"
id="city"
name="city"
label="City"
type="text"
variant="outlined"
required
onChange={handleChange}
/>
<TextField
value={accommodation.stateCounty}
inputProps={{ "data-testid": "stateCounty" }}
autoFocus
margin="dense"
id="stateCounty"
name="stateCounty"
label="State/Province"
type="text"
variant="outlined"
onChange={handleChange}
/>
<TextField
value={accommodation.postalCode}
inputProps={{ "data-testid": "postalCode" }}
autoFocus
margin="dense"
id="postalCode"
name="postalCode"
label="Postal/Zip Code"
type="text"
variant="outlined"
required
onChange={handleChange}
/>
<TextField
value={accommodation.countryCode}
inputProps={{ "data-testid": "countryCode" }}
autoFocus
margin="dense"
id="countryCode"
name="countryCode"
label="Country"
type="text"
variant="outlined"
onChange={handleChange}
/>
</DialogContent>
<DialogActions>
<Button onClick={handleSubmit} data-testid="saveAccommodationDetails">
Save Accommodation Details
</Button>
<Button onClick={handleClose}>Cancel</Button>
</DialogActions>
</Dialog>
</div>
);

material ui: how to align the Input Label properly

I have the following code for input with inputlable
<FormControl>
<InputLabel htmlFor="my-input">
Photo
</InputLabel>
<input
type="file"
/>
</FormControl>
What I see is
What I want is
I get this using:
<TextField
fullWidth
label="photo"
margin="dense"
accept="image/*"
type="file"
InputLabelProps={{
shrink: true,
}}
/>
So how can get this same effect using the previous code i.e using formcontrol, inputlable etc.
The reason i have to use that intead of textfield is react hook form: materail ui: Textfield: onSubmit, not passing Filelist in the data
Just set shrink property of InputLabel true and add a custom margin:
<FormControl>
<InputLabel shrink={true} htmlFor="my-input">
Photo
</InputLabel>
<input style={{ marginTop: "15px" }} type="file" />
</FormControl>

Console warning with Material UI React TextField: "React does not recognize the `InputProps` prop on a DOM element"

index.js:1 Warning: React does not recognize the `InputProps` prop on a DOM element.
If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `inputprops` instead.
If you accidentally passed it from a parent component, remove it from the DOM element
<form onSubmit={(e) => onSubmit(e)} style={{ margin: "0.5em" }}>
<Grid container>
<Grid item xs={6}>
<TextField
InputProps={{ disableUnderline: true }}
name="USERNAME"
id="USERNAME"
value={USERNAME}
label="username"
error
helperText={error.USERNAME}
onChange={(e) => onInputChange(e)}
/>{" "}
<br />
<TextField
InputProps={{ disableUnderline: true }}
type="text"
name="FIRSTNAME"
label="firstname"
value={FIRSTNAME}
onChange={(e) => onInputChange(e)}
error
helperText={error.FIRSTNAME}
/>{" "}
<br />
<TextField
InputProps={{ disableUnderline: true }}
type="text"
name="LASTNAME"
value={LASTNAME}
label="lastname"
onChange={(e) => onInputChange(e)}
error
helperText={error.LASTNAME}
/>{" "}
<br />
<TextField
InputProps={{ disableUnderline: true }}
type="text"
name="PHONE"
value={PHONE}
label="phone"
onChange={(e) => onInputChange(e)}
error
helperText={error.PHONE}
/>{" "}
<br />
<TextField
InputProps={{ disableUnderline: true }}
id="EMAIL"
name="EMAIL"
value={EMAIL}
label="email"
onChange={(e) => onInputChange(e)}
error
helperText={error.EMAIL}
/>{" "}
// ...
</Grid>
</Grid>
</form>
Why does React think the InputProps prop will be added to the DOM as an attribute?

material ui TextField variant outlined, problem with react-seles

hello i have a problem because, like you can see in the image, when i use react-select and textfiled with variant="outlined" because the label of the textfield shows itself even over the option, there is a class or something to solve this?
<TextField
variant="outlined"
label="First Name"
/>
thank you
I solved this with a zIndex on the <TextField:
<TextField
label="Points"
type="number"
min={1}
max={999}
value={props.question.value}
disabled={!props.question.expanded}
onChange={ev => props.updateValue(props.ix, ev.target.value)}
InputLabelProps={{
shrink: true,
}}
variant="outlined"
style={{ width: '5em', zIndex: -1 }}
margin="dense"
fullWidth
/>

How to make TextField input wider?

I have the following code:
<Grid
container
spacing={0}
direction="column"
alignItems="center"
justify="center"
style={{ minHeight: "100vh" }}
>
<FormControl>
<TextField
label="email"
fullWidth
type="email"
value={email}
onChange={e => setEmail(e.target.value)}
></TextField>
<TextField
label="body"
type="body"
fullWidth
value={body}
onChange={e => setBody(e.target.value)}
multiline={true}
></TextField>
</FormControl>
</Grid>
And this renders:
The reason I'm using Grid is because I wanted to center the form, as suggested from the answers here.
No matter what I do, I can't get the TextField to change width. I've added it into a Container, I've added the width style, nothing works. What am I doing wrong here?
According to Material-UI TextField docs, fullWidth prop:
If true, the input will take up the full width of its container.
The TextField container in this case is FormControl, and the FormControl width is calculated to contain it's children.
If you set FormControl to fullWidth as well, you will get what you want:
<FormControl fullWidth>

Resources