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

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?

Related

React component tabs and URLs

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.

How to add icons to keys that come from the database in react

I want to add the icons that correspond to each parameter that comes from the database ... it is for a search filter ... here is the code
render() {
let lugares = Object.keys(this.state.filters).map((key) => {
// this.state.filters_selected
return (
<div className="mb-5 ml-2">
<Row>
{Object.values(this.state.icons).map((clave) => {
console.log(clave)
}
)}
<MDBIcon className='mt-1' icon={this.state.icons} />
<h4 className="ml-1 ">{key}</h4>
<hr className="accent-4 ml-1 mt-1 mr-5 grey lighten-5" style={{ width: "150px" }} />
</Row>
icons is a dictionary that you create above icons:[{'Vehiculo':'car', 'Inmueble':'home','Muebles':'couch', 'Otros':'circle'}]
and in filters the same keys that I have icons arrive .. how could I do so that each icon is painted in its corresponding key?
Assuming icons is the object containing the key-value pair for the specific icons, and each key in filters holds the key for each icon, then pretty simply you should be able to replace icon={this.state.icons} with icon={this.state.icons.key} or icon={this.state.icons[key]}. Hope this helps.

How to add button in a specific node of react Sortable Tree

I'm working with react library 'react-sortable-tree' and I don't know how to add buttons only to a certain type of nodes. For example the element in a node has a specific value I need to add a button for do something.
<SortableTree
canDrop={canDrop}
getNodeKey={({ node }) => node.id}
treeData={this.state.treeData}
onChange={this.onChange}
generateNodeProps={({ node, path }) => ({
title: (
<a href={node.url}>{node.title}</a>
),
})}
/>
What can I add to this component in order to add a button only in some specific case?
EDIT
The idea is to add a button only when the node is a Web Content
Actualy I make it in this way:
generateNodeProps={({ node, path }) => ({
title: (
<Row>
<Col xs={6} sm={6} md={6} lg={6}>
<a href={node.url}>{node.title}</a>
</Col>
<Col xs={6} sm={6} md={6} lg={6}>
{node.isWebContent &&
<DefaultButton text='Open editor' />
}
</Col>
</Row>
),
})}
and this is the result:
There isn't a better way to do this? a good pratice, like not use the title property?
You can use generateNodeProps to add buttons to rendered elements. There is a dedicated buttons prop (I checked in versions 2.5.2 and 2.2.0 but I believe it was available earlier as well).
Example:
<SortableTree
canDrop={canDrop}
getNodeKey={({ node }) => node.id}
treeData={this.state.treeData}
onChange={this.onChange}
generateNodeProps={extendedNode => ({
title: (
<a href={extendedNode.node.url}>{extendedNode.node.title}</a>
),
buttons: extendedNode.node.title === "Web Content" ? [<button>X</button>] : [],
})}
/>
I have no idea what your code looks like, but could a function like this make sense for you?
renderButton = (title) => {
return title === 'Web Content' ? <button>Your button</button> : null
}
<SortableTree
canDrop={canDrop}
getNodeKey={({ node }) => node.id}
treeData={this.state.treeData}
onChange={this.onChange}
generateNodeProps={({ node, path }) => ({
title: (
<a href={node.url}>
{node.title}
{renderButton(node.title)}
</a>
),
})}
/>

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