React component tabs and URLs - reactjs

I'm fairly new to React so please bare with me.
We have a list of elements which all load their own content based on a query string parameter (let's call it CustomerID) passed in to the page. When clicking on the tabs, the URL remains the same. Let's say the URL is /customer/customerid
Within one of the tabs, the user has an opportunity to click on other items relevant to this Customer, which in turn changes the URL and another id is passed into the query string, let's call it OrderID and the URL is /customer/order/orderid.
When viewing the order, I would like the customer Tab menu to still appear, but I'm not sure the best way to handle this. Should I abandon tabs, and just create some normal links that look and feel like tabs? Or should I make the menu a class that is imported into each page, passing in the CustomerID from the relevant payloads each page contains?
EDIT: to include some code as a reference:
<Formik
initialValues={customer}
enableReinitialize={true}
validationSchema={validationSchema}
onSubmit={handleFormSubmit}>
{({ isSubmitting, values }) => (
<FormikForm autoComplete="off">
<AppCard>
<Tabs defaultActiveKey="details">
<Tab eventKey="details" title="Details">
<Row>
<Col md>
<InputText name="firstName" label="First Name" />
</Col>
<Col md>
<InputText name="lastName" label="Last Name" />
</Col>
</Row>
</Tab>
<Tab eventKey="orders" title="Orders">
<CustomerOrders
onCustomerOrderClick={onCustomerOrderClick}
customerId={customer.customerId}/>
</Tab>
</Tabs>
</AppCard>
</FormikForm>
)}
</Formik>
function onCustomerOrderClick(orderId: any){
history.push(`/customer/order/edit/${orderId}`);
}
The CustomerOrders class contains links to each individual order... Something like the following:
<Row>
<Col className="DataRowFunctions" md="2">
<Link
to={onCustomerOrderClick}
onClick={(e) => {
e.preventDefault();
onCustomerOrderClick();
}}
>
Edit
</Link>
</Col>
</Row>
Opening the order is then changing the URL, which leads me back to my question.

Related

Each Child in a list needs a key prop, React

I'm getting the key prop error and have tried several solutions to fix it. I tried the key={i} prop, and dropping the .name in the example below on a suggestion from a google search. Not sure why this isn't working, when the rest of the code works great. Any ideas? Thanks!
<Col className="coin-links">
<Title level={3} className="coin-details-heading">{cryptoDetails.name} Links</Title>
{cryptoDetails.links?.map((link) => (
<Row className="coin-link" key={link.name}>
<Title level={5} className="link-name">{link.type}</Title>
{link.name}
</Row>
))}
</Col>
The link.name may not be unique. That's why you are getting the error. You can make you of link.url since it's more likely unique.
<Col className="coin-links">
<Title level={3} className="coin-details-heading">{cryptoDetails.name} Links</Title>
{cryptoDetails.links?.map((link) => (
<Row className="coin-link" key={link.url}>
<Title level={5} className="link-name">{link.type}</Title>
{link.name}
</Row>
))}
</Col>

how can I delete a specific item from dropdown field in react

Previously I had one dropdown field for whitelist where user could select options from dropdown. Whatever user select from the list, it shows in the field with a remove icon. By clicking remove icon in front of the each items user could simply remove the specific item from the field. It looks like this:
For this feature code looks like this:
protected getRenderItemFunc = () => {
return (arg: OptionValue) => {
const label = arg.label
const removeItem = (e: any) => {
this.removeItem(e, arg)
}
return (
<div className="simple-value">
<span>{label}</span>
<span
onClick={removeItem}
onMouseDown={this.stopEventPropagation}
className="glyphicon glyphicon-remove"
>
{' '}
</span>
</div>
)
}
}
public render() {
return (
<h3 className="h4">White list</h3>
<Row>
<Col md={3} sm={5} xs={6}>
<MultiSelect
placeholder="Select groups"
values={this.state.private_marketplace_groups}
options={this.state.private_marketplace_suggestions}
onValuesChange={(values: any) => this.privateMarketplaceOnChange(values)}
theme="bootstrap3"
className="w-100p"
renderValue={this.getRenderItemFunc()}
/>
</Col>
</Row>
)}
The above code simply works fine.
Now I want to add similar dropdown feature which is called black list which means one user can black listed other users. Like this:
Here is the code I have added for this:
<h3 className="h4">Black list</h3>
<Row>
<Col md={3} sm={5} xs={6}>
<MultiSelect
placeholder="Select groups"
values={this.state.blacklist_groups}
options={this.state.private_marketplace_suggestions}
onValuesChange={(values: any) => this.blackListOnChange(values)}
theme="bootstrap3"
className="w-100p"
renderValue={this.getRenderItemFunc()}
/>
</Col>
</Row>
Here, I am using the same function this.getRenderItemFunc() for both white list and black list to remove a specific item. Removing a specific item simply works fine for white list but removing specific item is not working for black list. Could any one tell me what I am doing wrong here?

Datepicker not clearing the edge of table

I'm having a rather stubborn DatePicker that I can't seem to get to behave.
I'm using react-datepicker's DatePicker component.
I have a table, where each row has a DatePicker. Opening the calendar view of the date picker seems to not clear the edge of the table:
I have tried setting style={{zIndex: "9999 !important"}} on the table to no avail.
This appears to be the only solution anyone ever recommends.
The bit of code using the DatePicker component looks like this:
<Row>
<Col>
<DatePicker
onChange={onChangeCallback(user.id)}
autoComplete="off"
shouldCloseOnSelect={false}
dateFormat="dd-MM-yyyy"
selected={date}
placeholderText="Vælg Dato..."
clearButtonTitle="Ryd"
isClearable
/>
</Col>
<Col>
<Calendar
color="#ff7e01"
className="align-middle"
size={18}
/>
</Col>
</Row>
with Row and Col imported from reactstrap.
Interestingly, the DatePicker acts correctly when not using Row and Col, so something in there must be causing this interference.
Any clues?
I know you've already tried the position: fixed option, but as ccallendar alludes to, the library #popperjs/core used by datepicker-react has changed since then, more specifically:
"7. Change positionFixed
In Popper 2, this is now the strategy option:
createPopper(reference, popper, {
strategy: 'fixed',
});
"
-- https://popper.js.org/docs/v2/migration-guide/#7-change-positionfixed
Applying that to your question, then this should work:
<Row>
<Col>
<DatePicker
onChange={onChangeCallback(user.id)}
autoComplete="off"
shouldCloseOnSelect={false}
dateFormat="dd-MM-yyyy"
selected={date}
placeholderText="Vælg Dato..."
clearButtonTitle="Ryd"
isClearable
popperProps={{ strategy: "fixed" }}
/>
</Col>
<Col>
<Calendar
color="#ff7e01"
className="align-middle"
size={18}
/>
</Col>
</Row>

dynamically add and delete input fields in react

I am creating a React form where user can upload an image. I am using a function createObjectURL() as given below which i set as the state.
This is the image component
<Row className="form-row">
<Col md={8} className="image-field">
<div className="input-image-file">
<img src={this.state.image} className="myimage" alt="choose an image"></img>
</div>
</Col>
<Col md={4} className="image-buttons">
<Row className="form-row">
<FormControl type="file" onChange={this.onImageChange.bind(this)} ></FormControl>
</Row>
<Row className="form-row">
<Button className="delete-image" onClick={this.deleteImage.bind(this)}><i className="fa fa-trash"></i> Delete</Button>
</Row>
</Col>
</Row>
this is the onImageChange and deleteImage function
onImageChange(event){
this.setState({image: URL.createObjectURL(event.target.files[0])},()=>{
console.log(this.state.image)
})
}
deleteImage(){
this.setState({image : null})
}
the URL is like
"blob:http://localhost:3000/e5531071-c8fc-46db-a934-98678c1cec3a"
I send this data to backend and create an entry in mongodb also where is the URL is saved correctly. I have an option called edit which retrieves all the contents of the form for the user to edit. All contents are getting displayed except the Image.
I get an error like
GET blob:http://localhost:3000/e5531071-c8fc-46db-a934-98678c1cec3a 404 (Not Found)
please do help me thanks in advance this is the console error log

React Auto Complete Element along with options to add value to the list if not present

i have to create an autocomplete react component. Initially there must be 5 autocomplete fields. I set a state as an array of 5 input elements like
this.state {activities : ['','','','','']}
i apply a map function over these to display 5 autocomplete boxes.
I have a list of values to be listed as options of autocomplete.
Row>
{this.state.activities.map((newrow,index)=>{
return (
<Col md={12}>
<Row className="form-row">
<Col md={11} sm={10} xs={9}>
<div className="form-input">
<AutoComplete
className="autosearch"
dataSource={options}
onSelect={this.onSelect.bind(this, index)}
filterOption={(inputValue, option) => option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1} />
</div>
</Col>
<Col md={1} sm={2} xs={3}>
<Button className="delete-row" onClick={this.deleterow.bind(this,index)}><i className="fa fa-trash"></i></Button>
</Col>
</Row>
</Col>)
})
}
</Row>
now i have to create an autocomplete which also has the option to allow me add an option if it is not present in the list .
please help me out. i am new to react and thanks in advance.
This is the website i referred for the autocomplete boxenter link description here
and i referred the basic example code of it

Resources