I am using 'Creatable' props of react-virtualized-select. When I write a custom option(e.g. Test code 4), It shows the text 'create option "Test code 4". But, When I click on that option, dropdown becomes blank and that option also not added in option list.
Library Link to demo : https://bvaughn.github.io/react-virtualized-select/
Library Link to demo Source-code : https://github.com/bvaughn/react-virtualized-select/blob/master/source/VirtualizedSelect/VirtualizedSelect.example.js
Options: Below list is coming from database.
dataSource = [
{name: "Test1", label: "Test code 1", type: "text"},
{name: "Test2", label: "Test code 2", type: "text"},
{name: "Test3", label: "Test code 3", type: "text"}
]
Component:
import { Creatable } from 'react-select'
handleOptionChange( selectedValue ) {
this.setState({
selectedValue: selectedValue
});
}
<VirtualizedSelect
labelKey='label'
clearable={ clearable }
disabled={ disabled }
multi={multi}
handleOptionChange={this.handleOptionChange}
options={ dataSource }
searchable={ searchable }
selectedValue={ selectedValue }
selectComponent={Creatable}
valueKey='name'
/>
I could not figure out what else I am missing here ?
Thanks in advance.
I had the same issue. In my case the problem was caused by the version of react-select. I upgraded react-select from 0.9.2 to v1.2.1 and this did the trick:
"react-select": "v1.2.1"
I also added following imports which are present in some examples (I don't know if this is crucial):
import React, { Component, PropTypes } from 'react';
Related
The code is as follows
<TextField {...form.$("phone.countryCode").bind()} value={222} />
The form setup is as follows
{
name: "phone",
fields: [
{
name: "countryCode",
label: "Country Code:",
bindings: "TextField",
placeholder: "+",
rules: "required",
}
]
}
The value displays correctly in the textfield. But when i submit the form, it shows "required" error.
I assume field.input.onChange is not triggered when we set the value like this and hence the mobx field is not updated with the value.
Any idea on how to fix this?
Edit:
The value is a dynamic one based on the country selection. So you cannot set the value during initialise. That is why i had to go for this approach.
Don't need to override value on the TextField, mobx-react-form (I am assuming you are using it) can't possibly know that you are doing it. Just pass default value while initialising the form, like so:
const fields = [
{
name: "countryCode",
label: "Country Code:",
bindings: "TextField",
placeholder: "+",
rules: "required",
value: 'YOUR_DEFAULT_VALUE_HERE'
}
]
new MobxReactForm({ fields });
If the value depends on the other value then you can use useEffect, for example:
useEffect(() => {
// set countryCode value manually with needed value
FormModel.$("countryCode").set(country.code);
// Add needed deps
}, [country])
Hi
I am currently trying to display elements of a list in react and I'm not able to load images using require.
I am using CRA and haven't changed webpack.config.js.
The list
import img1 from "../../assets/work-in-progress.png";
const projects = [
{
id: 1,
image_path: img1,
title: "t1",
category: "testing"
},
{
id: 2,
image_path: require("../../assets/work-in-progress.png"),
title: "t2",
category: "testing"
},
]
How I am displaying the images
<img src={ entry.image_path } alt="Project's" className="rounded" />
Currently, the first image is being displayed correctly but the second doesn't load.
I have already tried using src={ "" + entry.image_path } and got the same result.
I think your path isn't correct. It's working in my end. Here is the working code
I am attempting to use react-semantic-redux-form SelectField with the multiple options so a user can select multiple options and if there is one already set then this should show as checked.
I am also using redux-form with semantic0ui-react.
I am getting an error attempting to include multiple selections.
My include statement is:
import { SelectField } from "react-semantic-redux-form";
My state is:
state = {
relationships: ["some entry"],
relationshipOptions: [],
};
The element code is:
<Grid.Column>
<Field
component={SelectField}
name="relationships"
label="Your Relationships"
options={relationshipOptions}
multiple
placeholder="Select to add a relationship"
/>
I get the error as below
Dropdown `value` must be an array when `multiple` is set. Received type: `[object String]`.
in Dropdown
The way you have relationshipOptions is wrong, it is supposed array of objects,
const relationshipOptions = [
{ key: "single", value: "single", text: "single" },
{ key: "married", value: "married", text: "married" }
];
Here is the working example, Code Sandbox
Also if you have single, married in array. You can do something like this,
let relationshipOptions = ["single", "married"].map((x) => {
return ({
key: x,
value: x,
text: x
});
});
I am work on adding more than one language to my Application using i18n-js. My concern is how to handle static data like currency, level of experience, ...etc which is written in English to use it by my dropdown list when user select one item from dropdown it should be sent to the back end in English format and how to handle fetched data with English format if user have chosen another language.
Is it regular to save same type of data to backend in different languages.
I have tried to add all my static data in my language files and when user choose a specific language it would fetch it's corresponded data from language file.
Here is a code sample from language files:
English file
export default en = {
educationDropDownList: [
{
label: `Secondary School`,
value: `Secondary School`
},
{
label: `Deploma`,
value: `Deploma`
},
{
label: `Bachelor's degree`,
value: `Bachelor's degree`
},
{
label: `Master's degree`,
value: `Master's degree`
},
{
label: `Doctoral degree`,
value: `Doctoral degree`,
}
]
}
Arabic file
export default ar = {
educationDropDownList: [
{
label: `مدرسة ثانوية`,
value: `مدرسة ثانوية`
},
{
label: `شهادة دبلوم`,
value: `شهادة دبلوم`
},
{
label: `درجة البكالوريوس`,
value: `درجة البكالوريوس`
},
{
label: `درجة الماجيستير`,
value: `درجة الماجيستير`
},
{
label: `درجة الدكتوراه`,
value: `درجة الدكتوراه`,
}
]
}
I have two cases here. first, how to handle sending selected data to Backend with different language format and how to receive that selected data and view it with proper language format.
Using different languages makes things confusing. Keep to the one you developing with. In other words, i18n pulls the language from language files that is linked to a key.
e.g (https://i18njs.com/#language_files) :
{
"values":{
"Yes": "はい",
"No": "いいえ",
"Ok": "Ok",
"Cancel": "キャンセル"
}
}
Regardless of what the language is the software runs on a key and ignores the values. When displaying the value on the browser the dropdown displays whatever the key contains as a value e.g: i18n("Yes") would give you はい if the language was set as Japanese.
Populate your Arabic and English collections dynamically generically such as example:
export default genericdropdown = {
educationDropDownList: [
{
label: i18n("Yes"),
value: "Yes"
},
{
label: i18n("No"),
value: "No"
}, ...
Maybe I'm misunderstanding you, but I hope it helps.
You can do something like this:
your translation files:
English
{
"yes":"Yes"
}
Arabic
{
"yes":"نعم"
}
and then in your dropdown, you only change label's value based on the chosen language. this means that a user who selected Arabic language, will see arabic translation of the dropdown option, but you will send the value which is in english to your backend.
export default genericdropdown = {
educationDropDownList: [
{
label: i18n.t("yes"),
value: "Yes"
},
...
If you are using react-il8n package for translation just do this:
This is your dropdown component file.
import React, { useState } from "react";
import il8n from "./i18n.js";
import { withNamespaces } from "react-i18next";
const LanguageDropdown = ({ t, props }) => {
const changeLanguage = (language) => {
il8n.changeLanguage(language);
};
return (
<li className="nav-item d-none d-md-block">
<a className="nav-link" href="javascript:void(0)">
<div className="customize-input">
<select
className="custom-select form-control bg-white custom-radius custom-shadow border-0"
onChange={(e) => changeLanguage(e.target.value)}
>
<option value="">{t("Choose Language")}....</option>
<option value="en">{t("English")}</option>
<option value="fe">{t("French")}</option>
</select>
</div>
</a>
</li>
);
};
export default withNamespaces()(LanguageDropdown);
I am having problems with setting up grouped multiselect using material-ui in react. As material ui does not have such build-in component, I had to do it myself.
I am attaching codesandbox example of my code.
https://codesandbox.io/s/goofy-sun-2ffyv
The problem is that on onchange event the value I get I get is undefined.
Yet, I am excpected to get the value instead.
Any help and suggestion is appreciated.
According to Material-ui select docs:
The MenuItem elements must be direct descendants when native is false.
You are using div as direct element and MenuItems as children of div.
To fix this issue, i would recommend changing the structure of your data (I would use isDisabled without changing the structure, but I don't know if you want to use it for something else)
const posts = [
{
options: [
{
value: 'Hello',
label: 'Hello',
isOptGroup: true // -> the new flag
},
{
value: "3",
label: "Ойбек",
isDisabled: true
},
{
value: "2",
label: "Чинор",
isDisabled: true
},
{
value: "1",
label: "Озодбош",
isDisabled: true
}
]
}
];
And your MenuItem will be disabled based on that flag:
<MenuItem
key={option.value}
value={option.value}
className={classes.item}
disabled={option.isOptGroup}
>
<ListItemText primary={option.label} />
</MenuItem>