I have created a form where I am using mui textfields to build the form. I want my textField for the description to be double the width of all the other textFields.
How can I achieve this? To only target one of the textFields
I want to target the last textField in the form and I want that one to be double the width
Below is my code for my form:
import { makeStyles, TextField } from '#material-ui/core';
import { Formik, FormikHelpers } from 'formik';
const useStyles = makeStyles(() => ({
input: {
fontFamily: 'nunito, sans-serif',
color: 'black',
border: '1px solid #393939',
borderRadius: '5px',
marginBottom: '5px',
width: '300px',
padding: '7px',
},
}));
return (
<Formik
initialValues={initialValues}
validationSchema={editUserValidation}
validateOnBlur
validateOnChange={false}
onSubmit={_handleSubmission}
enableReinitialize
>
{({
handleSubmit,
isSubmitting,
values,
touched,
errors,
handleChange,
handleBlur,
handleReset,
}) => (
<form onReset={handleReset} onSubmit={handleSubmit} className="edit-form-body">
<div className="flex">
<TextField
id="firstName"
placeholder="First Name"
type="string"
value={values.firstName}
error={touched.firstName && !!errors.firstName}
helperText={touched.firstName ? errors.firstName : ''}
onChange={handleChange('firstName')}
onBlur={handleBlur('firstName')}
InputProps={{ className: classes.input }}
/>
<div className="mx-5" />
<TextField
id="lastName"
placeholder="Last Name"
type="string"
value={values.lastName}
error={touched.lastName && !!errors.lastName}
helperText={touched.lastName ? errors.lastName : ''}
onChange={handleChange('lastName')}
onBlur={handleBlur('lastName')}
InputProps={{ className: classes.input }}
/>
</div>
<div className="flex">
<TextField
id="email"
placeholder="Email"
type="email"
value={values.email}
error={touched.email && !!errors.email}
helperText={touched.email ? errors.email : ''}
onChange={handleChange('email')}
onBlur={handleBlur('email')}
InputProps={{ className: classes.input }}
/>
<div className="mx-5" />
<TextField
id="mobileNumber"
placeholder="Contact No."
type="string"
value={values.mobileNumber}
error={touched.mobileNumber && !!errors.mobileNumber}
helperText={touched.mobileNumber ? errors.mobileNumber : ''}
onChange={handleChange('mobileNumber')}
onBlur={handleBlur('mobileNumber')}
InputProps={{ className: classes.input }}
/>
</div>
<div className="w-full">
<TextField
id="description"
placeholder="Description"
type="string"
value={values.description}
error={touched.description && !!errors.description}
helperText={touched.description ? errors.description : ''}
onChange={handleChange('description')}
onBlur={handleBlur('description')}
InputProps={{ className: classes.input }}
multiline
maxRows={10}
minRows={5}
/>
</div>
<div className="flex justify-end">
<Button
isLoading={isSubmitting}
onClick={handleSubmit}
className="font-nunito font-bold edit-user-button mt-4"
>
SAVE
</Button>
</div>
</form>
)}
</Formik>
);
};
Related
If validation fails, so id is not githubid1, or githubid2 then I would show a custom message like Please set an existing GihHub ID. Is it possible to set it?
<FormControl fullWidth sx={{ mb: 6 }}>
<Controller
name="username"
control={control}
rules={{
validate: (v) => { // <-----------
return v == "githubid1" || v == "githubid2";
},
}}
render={({ field: { value, onChange } }) => (
<TextField
value={value}
label="Username"
onChange={onChange}
placeholder="johndoe"
error={Boolean(errors.username)}
/>
)}
/>
{errors.username && (
<FormHelperText sx={{ color: "error.main" }}>
{errors.username.message}
</FormHelperText>
)}
</FormControl>
const [customError, setCustomError] = useState('');
<FormControl fullWidth sx={{ mb: 6 }}>
<Controller
name="username"
control={control}
rules={{
validate: (v) => {
if (v !== "githubid1" || v !== "githubid2") {
setCustomError('Your custom text here');
}
return v == "githubid1" || v == "githubid2";
},
}}
render={({ field: { value, onChange } }) => (
<TextField
value={value}
label="Username"
onChange={onChange}
placeholder="johndoe"
error={Boolean(errors.username)}
/>
)}
/>
{errors.username || customError && (
<FormHelperText sx={{ color: "error.main" }}>
{errors.username.message || customError}
</FormHelperText>
)}
</FormControl>
When I clicked into the text field it is showing blue outlined.
I want to hide this line. Also remove the mouse hovering.
<GridItem lg={ 6 } xs={ 12 } md={ 3 } className={ classes.textfieldsgrid } >
<TextField
className={ classes.textfields }
label="Email"
variant="outlined"
id="outlined-size-small"
size="small"
style={ styles }
type="email"
name="email"
id="email"
value={this.state.email }
onChange={ this.handleInputChange }
onBlur={ event => this.validateEmail(event.target.value)}
title="Email"
autoComplete="off"
required
/>
<p class="error" style={ { color: 'red' } }>{ this.state.errors.email }</p>
</GridItem>
can anyone help me to replace this Segment with Modal in React JS, here is the code, thanks in advance
return (
<Segment clearing style={{fontWeight: 'bold', fontSize: '20px', backgroundColor: '#f5f7fb'}}> ADD APPOINTMENT
<Form onSubmit={handleSubmit} autoComplete='off'>
<Form.Input placeholder='Customer Name' value={appointment.customerName} name='customerName' onChange={handleInputChange} />
<Form.Input type='date' placeholder='Appointment Date' value={appointment.appointmentDate} name='appointmentDate' onChange={handleInputChange} />
<Form.Input placeholder='Doctor Name' value={appointment.doctorName} name='doctorName' onChange={handleInputChange} />
<Form.Input placeholder='Service' value={appointment.service} name='service' onChange={handleInputChange} />
<Form.Input placeholder='Status' value={appointment.status} name='status' onChange={handleInputChange} />
<Button onClick={submitting} floated='right' positive type='submit' content='Submit' />
<Button onClick={closeForm} floated='right' type='submit' content='Cancel' />
</Form>
</Segment>
)
The below package seems perfect for your needs
https://www.npmjs.com/package/react-modal
below can be your implementation
import React from 'react';
import ReactDOM from 'react-dom';
import Modal from 'react-modal';
const customStyles = {
content: {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)',
},
};
// Make sure to bind modal to your appElement (https://reactcommunity.org/react-modal/accessibility/)
Modal.setAppElement('#root');
function App() {
let subtitle;
const [modalIsOpen, setIsOpen] = React.useState(false);
function openModal() {
setIsOpen(true);
}
function afterOpenModal() {
// references are now sync'd and can be accessed.
subtitle.style.color = '#f00';
}
function closeModal() {
setIsOpen(false);
}
return (
<div>
<button onClick={openModal}>Open Modal</button>
<Modal
isOpen={modalIsOpen}
onAfterOpen={afterOpenModal}
onRequestClose={closeModal}
style={customStyles}
contentLabel="Example Modal"
>
<Segment clearing style={{fontWeight: 'bold', fontSize: '20px', backgroundColor: '#f5f7fb'}}> ADD APPOINTMENT
<Form onSubmit={handleSubmit} autoComplete='off'>
<Form.Input placeholder='Customer Name' value={appointment.customerName} name='customerName' onChange={handleInputChange} />
<Form.Input type='date' placeholder='Appointment Date' value={appointment.appointmentDate} name='appointmentDate' onChange={handleInputChange} />
<Form.Input placeholder='Doctor Name' value={appointment.doctorName} name='doctorName' onChange={handleInputChange} />
<Form.Input placeholder='Service' value={appointment.service} name='service' onChange={handleInputChange} />
<Form.Input placeholder='Status' value={appointment.status} name='status' onChange={handleInputChange} />
<Button onClick={submitting} floated='right' positive type='submit' content='Submit' />
<Button onClick={closeForm} floated='right' type='submit' content='Cancel' />
</Form>
</Segment>
</Modal>
</div>
);
}
ReactDOM.render(<App />, Document.getElementById("root"));
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?
Single FormItem Demo in antd:https://ant.design/components/form/#components-form-demo-dynamic-form-item. There only one FormItem generated dynamically everytime.
And, here is
But I always got some errors like wrong interaction, wrong validation or wrong form value.
My code:
<Form.List name="passenger">
{(fields, { add, remove }) => {
return (
<div>
{fields.map((field, index) => (
<Form.Item
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
label={index === 0 ? 'Passengers' : ''}
required={false}
key={field.key}
>
<Form.Item
{...field}
validateTrigger={['onChange', 'onBlur']}
rules={[
{
required: true,
whitespace: true,
message: "Please input passenger's name or delete this field.",
},
]}
noStyle
>
<Input placeholder="passenger name" style={{ width: '40%' }} />
</Form.Item>
<Form.Item
name={['age', index]}
validateTrigger={['onChange', 'onBlur']}
rules={[
{
required: true,
whitespace: true,
message: "Please input passenger's age or delete this field.",
},
]}
noStyle
>
<Input placeholder="passenger age" style={{ width: '40%', marginRight: 8 }} />
</Form.Item>
{fields.length > 1 ? (
<MinusCircleOutlined
className="dynamic-delete-button"
onClick={() => {
remove(field.name);
}}
/>
) : null}
</Form.Item>
))}
<Form.Item>
<Button
type="dashed"
onClick={() => {
add();
}}
style={{ width: '60%' }}
>
<PlusOutlined /> Add field
</Button>
</Form.Item>
</div>
);
}}
</Form.List>
From the example provided by the antd team given by #Sushilzzz:
The trick is to use field.name in the name props of the Form.Item. You don't have to nest the sub items in another item.
<Form.List name="users">
{(fields, { add, remove }) => (
<div>
{fields.map((field) => (
<Form.Item name={[field.name, "name"]}>
<Input placeholder="Name" />
</Form.Item>
...