Grouping components to make sub-components - reactjs

I am trying to make what I think is called sub components in React though I can't find any official up to date documentation on how to do this. My issue is I would like to make a large form with different sections broken up as bootstrap cards, within a card I want an arbitrary number of rows of inputs.
Ex)
Section 1
First_Name Last_Name
Email_Address
Section 2
Address
City State Zip
The code looks something like this for Section 1:
<div className="MyCard floatingCard card">
<h5 className="MyCardHeader card-header">Section 1</h5>
<div className="MyCardBody card-body">
{/* Row 1 */}
<div className="row">
<div className="form-group col-md-6" style={{ marginBottom: "2em" }}>
<input />
</div>
<div className="form-group col-md-6" style={{ marginBottom: "2em" }}>
<input />
</div>
</div>
{/* Row 2 */}
<div className="row">
<div className="form-group col-md-12" style={{ marginBottom: "2em" }}>
<input />
</div>
</div>
</div>
</div>
The above is not very clean, and I would also need to do the same for section 2, is there a way to make this look like:
<FormSection header="Section1">
<FormRow>
<Input name="firstName" label="First Name" />
<Input name="lastName" label="Last Name" />
</FormRow>
<FormRow>
<Input name="email" label="Email address" />
</FormRow>
</FormSection>
<FormSection header="Section2">
<FormRow>
<Input name="email" label="Email address" />
</FormRow>
<FormRow>
<Input name="city" label="City" />
<Input name="state" label="State" />
<Input name="zip" label="Zip" />
</FormRow>
</FormSection>

Related

How do I make the input value be "required" in React-Bootstrap?

Here is my code, I did write "required" inside <Form.Control>.
I also checked the documentation https://react-bootstrap.github.io/forms/validation/
But still doesn't work, could someone give me some suggestions?
Thank you so much :)
return (
<>
<Helmet>
<title>Add Media</title>
</Helmet>
<Container className="d-flex justify-content-center mt-3">
<Row className="col-12 col-sm-12 col-lg-6">
<Form action="/api/concerts/add-new" method="post">
<Form.Group className="mb-3" controlId="exampleForm.ControlInput1">
<Form.Label>Title</Form.Label>
<Form.Control
required
type="text"
placeholder="title"
name="title"
value={title}
id=""
onChange={(e) => setTitle(e.target.value)}
/>
</Form.Group>
<Form.Group className="mb-3" controlId="exampleForm.ControlInput1">
<Form.Label>YouTube Link</Form.Label>
<Form.Control
required
type="text"
placeholder="paste the YouTube link here"
name="title"
value={youTubeSrc}
id=""
onChange={(e) => setYouTubeSrc(e.target.value)}
/>
</Form.Group>
<Modal.Footer>
<Button variant="dark text-white col-12 mx-auto" type="submit" onClick={handleSubmit}>
<TbUpload /> Upload
</Button>
</Modal.Footer>
</Form>
</Row>
</Container>
</>
);
You have to pass noValidate and validated=false to the Form so it can be handled by library.
<Form
action="/api/concerts/add-new"
method="post"
noValidate
validated=false
/>
...

How to add image to bootstrap card as a preview in react?

I am trying to create a bootstrap form and i want to show as a preview in bootstrap card what it will look like
Here is my code for form
<div className="container">
<div className="row">
<div className="col p-5">
<div className="card" style={{ width: "18rem" }}>
<img data-src={img} className="card-img-top" alt="any image"/>
<div className="card-body">
<h5 className="card-title">{title}</h5>
<p className="card-text">{place}</p>
<p className="card-text">{year}</p>
<a href="#" className="btn btn-primary">
Go somewhere
</a>
</div>
</div>
</div>
<div className="col p-5">
<form type="form">
<div className="form-group">
<label>Title</label>
<input
name="title"
type="text"
className="form-control mt-2"
placeholder="Enter name of car"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
</div>
<div className="form-group">
<label>Place</label>
<input
name="place"
type="text"
className="form-control mt-2"
placeholder="Enter city name"
value={place}
onChange={(e) => setPlace(e.target.value)}
/>
</div>
<div className="form-group">
<label>Year</label>
<input
name="year"
type="number"
className="form-control mt-2"
placeholder="Enter mfg year"
value={year}
onChange={(e) => setYear(e.target.value)}
/>
</div>
<div className="form-group">
<input
className=" mt-2"
type="file"
name="file"
value={img}
onChange={changeHandler}
/>
</div>
{error && <div>{error}</div>}
{file && <div>{file.name}</div>}
<button
onClick={uploadFile}
type="submit"
className="btn btn-primary mt-2"
>
Submit
</button>
</form>
</div>
</div>
</div>
I can preview title as given in input field by passing as value parameter but i don't know how to do it with image. Also i am storing value of title,place etc in variable do i need to do the same for image too?

How can I align my labels, input, and textarea on same line?

How can I align my labels, inputs, and textarea on the same line?
export default function Display() {
...
return (
<form onSubmit={handleChange}>
<div className="mb-4 align-middle">
<label>
Title :
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Title"
/>
</label>
<label>
{" "}
Comment :
<textarea
value={comment}
placeholder="comment"
onChange={(e) => setComment(e.target.value)}
cols="50"
rows="3"
/>
</label>
</div>
</form>
);
}
Here a picture (what I have vs expectations):
Set the parent div to display: flex and flex-direction: row
...
return (
<form onSubmit={handleChange}>
<div className="mb-4 align-middle" style={{display: 'flex', flexDirection: 'row'}}>
<label>
Title :
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Title"
/>
</label>
<label>
{" "}
Comment :
<textarea
value={comment}
placeholder="comment"
onChange={(e) => setComment(e.target.value)}
cols="50"
rows="3"
/>
</label>
</div>
</form>
);
}```
You can use flex properties of tailwind css to align them in a same row. Also I have wrapped the the two inputs in two separate rate divs.
export default function Display() {
...
return (
<form onSubmit={handleChange}>
<div className="mb-4 align-middle flex flex-row items-center justify-center space-x-10">
<div>
<label>
Title :
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Title"
/>
</label>
</div>
<div>
<label>
{" "}
Comment :
<textarea
value={comment}
placeholder="comment"
onChange={(e) => setComment(e.target.value)}
cols="50"
rows="3"
/>
</label>
</div>
</div>
</form>
);
}

how do I fix this ant design datepicker component in my react project .I have shared a screenshot

I'm working on a project which required ant design datepicker along with bootstrap.I'm not sure whats making this datepicker move out of screen. Is there something I need to do with bootstrap classes to fix this? I have shared my screenshot.thanenter image description here]1]1
const form= () => (
<form onSubmit={onFormSubmit}>
<div className="form-group">
<label className="btn btn-outline-secondary btn-block m-3">Add image
<input type="file" name="image" onChange={handleImageChange} accept="image/*" hidden/>
</label>
<input className="form-control m-2" type="text" name="title" placeholder="title" onChange={handleChange} value={title}/>
<textarea className="form-control m-2" type="text" name="content" placeholder="content" onChange={handleChange} value={content}/>
<AlgoliaPlaces className="form-control ml-2" placeholder="location" defaultValue={location} options={config} onChange={handleSearch} style={{height:"50px"}} />
<input className="form-control m-2" type="number" name="price" placeholder="price" onChange={handleChange} value={price}/>
<input className="form-control m-2" type="number" name="bed" placeholder="Number of beds" onChange={handleChange} value={bed}/>
</div>
**<DatePicker className="form-control m-2"/>**
<button className="btn btn-outline-primary m-2">Add</button>
</form>
)
return(
<div>
<div className="container-fluid p-5 text-center">
<h2>Add </h2>
</div>
<div className="container-fluid">
<div className="row">
<div className="col-md-10">
{hotelForm()}
</div
<div className="col-md-2">
<img src={preview} alt="preview-image" className="img img-fluid m-2" />
<pre>{JSON.stringify(values,null, 2)}</pre>
</div>
</div>
</div>
</div>
)
It looks like you are missing ant design css stylesheet.
Try to add the following line at the root of your app :
import 'antd/dist/antd.css';

How to set state value in each in each input field via get method

I have an React form with 10 input field.I have a below code to set state for each field and finally I have submit this state to database.(insert record in table using post method via state).I would like to know how to set value in each in each input field via get method.
initial State :-
this.state = {
UserId : this.props.match.params.id,
FirstName: '',
LastName : '',
EmailId : '',
MobileNo : '',
DivisionId :'',
UserName : '',
Password : '',
ConfirmPassword : '',
RoleId : '',
UpdateUserName : localStorage.getItem("UserId"),
GridState : []
};
Setting State Value in each input Field before submitting :-
onChangeHandler = (event) => {
let nam = event.target.name;
let val = event.target.value;
this.setState({[nam]: val});
}
Submit Record to Database via Post Method :-
onSubmitHandler = (event) => {
axios.post(ConfigItem[0].APIPath+'Users/',this.state)
.then(res => {
this.props.history.push('/UserList')
})
}
Get inserted record from API via get method :-
axios.get(ConfigItem[0].APIPath+'users/' + this.props.match.params.id)
.then(res => {
console.log(res.data.data); ***// I would like to know how to set value in each input field***
HTML render :-
render() {
return (
<form onSubmit={this.onSubmitHandler}>
<div className="animated fadeIn">
<Row>
<Col xs="12" sm="12">
<Card>
<CardHeader>
<Row>
<Col xs="8" sm="8">
<strong> <i> User Creation Entry </i> </strong>
</Col>
<Col xs="4" sm="4">
<Button type="button" text="Back" size="sm" onClick={this.onBackButtonClick} color="danger" className="pull-right ml-1 "><i className="fa fa-ban"></i> Back</Button>
<Button type="submit" size="sm" color="success" className="pull-right ml-1 "><i className="fa fa-dot-circle-o"></i> Save</Button>
</Col>
</Row>
</CardHeader>
<CardBody>
<div className="form-group row">
<label className="col-sm-1 col-form-label col-form-label-sm">First Name</label>
<div className="col-sm-3">
<input type="text" name="FirstName" className="form-control form-control-sm" id="TxtFirstName" placeholder="First Name" required onChange={this.onChangeHandler}/>
</div>
<label className="col-sm-1 col-form-label col-form-label-sm">Last Name</label>
<div className="col-sm-3">
<input type="text" name="LastName" className="form-control form-control-sm" id="TxtLastName" placeholder="Last Name" required onChange={this.onChangeHandler}/>
</div>
<label className="col-sm-1 col-form-label col-form-label-sm">Email Id</label>
<div className="col-sm-3">
<input type="email" name ="EmailId" className="form-control form-control-sm" id="TxtEmailId" placeholder="EmailId" required onChange={this.onChangeHandler}/>
</div>
</div>
<div className="form-group row">
<label className="col-sm-1 col-form-label col-form-label-sm">Mobile No</label>
<div className="col-sm-3">
<input type="text" name ="MobileNo" className="form-control form-control-sm" id="TxtMobileNo" placeholder="Mobile No" onChange={this.onChangeHandler} />
</div>
<label className="col-sm-1 col-form-label col-form-label-sm">Division</label>
<div className="col-sm-3">
<select name ="DivisionId" className="form-control" required onChange={this.onChangeHandler}>
{DivisionList}
</select>
</div>
<label className="col-sm-1 col-form-label col-form-label-sm">User Name</label>
<div className="col-sm-3">
<input type="text" name ="UserName" className="form-control form-control-sm" id="TxtUserName" placeholder="User Name" required onChange={this.onChangeHandler}/>
</div>
</div>
<div className="form-group row">
<label className="col-sm-1 col-form-label col-form-label-sm">Password</label>
<div className="col-sm-3">
<input type="password" name ="Password" className="form-control form-control-sm" id="TxtPassword" placeholder="Password" required onChange={this.onChangeHandler}/>
</div>
<label className="col-sm-1 col-form-label col-form-label-sm">Confirm</label>
<div className="col-sm-3">
<input type="password" name="ConfirmPassword" className="form-control form-control-sm" id="TxtConfirm" placeholder="Confirm Password" required onChange={this.onChangeHandler}/>
</div>
<label className="col-sm-1 col-form-label col-form-label-sm">Role</label>
<div className="col-sm-3">
<select name ="RoleId" data-show-subtext="true" data-live-search="true" className="form-control selectpicker" id="CboRole" required onChange={this.onChangeHandler}>
{RoleList}
</select>
</div>
</div>
</CardBody>
<CardFooter>
<div id="data-grid-demo">
<DataGrid
dataSource={this.state.GridState}
ref={ref => this.dataGrid = ref}
keyExpr="MenuId"
showBorders={true}
onToolbarPreparing={this.onToolbarPreparing}
onRowUpdated={this.onRowUpdated}
>
<Paging enabled={false} />
<Editing
mode="batch"
allowUpdating={true}
selectTextOnEditStart={true}
startEditAction='click' />
<Column dataField="UserAccessId" visible={false} />
<Column dataField="MenuId" visible={false} />
<Column dataField="Menu" />
<Column dataField="SubMenu" />
<Column dataField="ViewAccess" caption="ViewAccess" dataType="boolean" >
<CheckBox defaultValue={false} />
</Column>
<Column dataField="ZohoParameter" />
<Column dataField="Remarks" />
</DataGrid>
</div>
</CardFooter>
</Card>
</Col>
</Row>
</div>
</form>
);
}
Set the value to each <input /> that is equal to the class state, for example:
<input type="text" name="FirstName" className="form-control form-control-sm" id="TxtFirstName" value={this.state.FirstName} placeholder="First Name" required onChange={this.onChangeHandler}/>
After that, simply save your data from the axios call into the state:
this.setState({
FirstName: result.data.FirstName,
//Add the others by yourself
})
If your query result properties have the same name as the ones in your state, you could simply do:
this.setState({
...result.data.data //depending on your result object structure
})
the second statement is not working,please check my below result of json data and advise how to do this..
[{"UserId":2009,"FirstName":"jj","LastName":"kk","EmailId":"kk.biz#itytu.com","MobileNo":"9789684772","DivisionId":4,"UserName":"khizer11","Password":"$2b$10$JRG/Zp5TQOJkZPIj2bXFset3gx8DF/OFnrGzZqR4rnC7Bc/a9.yyO","DPassword":"ii#123","RoleId":8,"LoginTag":null,"LoginTime":null,"LogOutTime":null,"SysName":"DELLNETBOOK","UpdateUserName":"","UpdateSysName":null,"UpdateDateTime":null,"DefaultDateTime":

Resources