material ui : selectfiled multiple property - reactjs

I'm using material ui, and I want to use the mutliple property, but I have an error :
multiple does not exist on the type selectfiled
<SelectField
multiple={true} //it doesn't work
value={this.state.valueProfession}
onChange={this.handleChangeProfession}></SelectField>
I have the version 0.16.5 of material ui. I would like to avoid to update the last version of material ui. Is there other solution ? Thank you

Please update your version 0.16.5 to newer version in order to use this.
I've used this in version 0.18.4
Here is the simple working example
import React from 'react';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
const names = [
'Oliver Hansen',
'Van Henry',
'April Tucker',
'Ralph Hubbard',
'Omar Alexander',
'Carlos Abbott',
'Miriam Wagner',
'Bradley Wilkerson',
'Virginia Andrews',
'Kelly Snyder',
];
/**
* `SelectField` can handle multiple selections. It is enabled with the `multiple` property.
*/
export default class SelectFieldExampleMultiSelect extends React.Component {
state = {
values: [],
};
handleChange = (event, index, values) => this.setState({values});
menuItems(values) {
return names.map((name) => (
<MenuItem
key={name}
insetChildren={true}
checked={values && values.indexOf(name) > -1}
value={name}
primaryText={name}
/>
));
}
render() {
const {values} = this.state;
return (
<SelectField
multiple={true}
hintText="Select a name"
value={values}
onChange={this.handleChange}
>
{this.menuItems(values)}
</SelectField>
);
}
}

Related

Updating Formik State whilst using Headless UI "Listbox" (which manages its own state, internally)

I'm struggling to get a Headless UI Listbox implementation working successfully with Formik. After reading all the docs and scouring StackOverflow, I'm yet to find an answer.
I can get the Listbox to function perfectly well on its own, and Formik is working with other, less complex (but still custom) components. That said, I can't get them working in unison. Whenever I change the selection in the Select component, I can successfully update the Headless UI state (in as much as it updates to the correct value) and the Formik state, but for some reason the active and selected properties (within Headless UI) aren't working correctly (always false).
I assume what I need to do is to make use of the onChange handler so that it updates both the Headless UI state (to keep the active and selected properties updated) and the Formik value, but this doesn't seem to work as expected.
Does anyone have any suggestions? Please see a minimal representation of the code below:
export function TestForm() {
return (
<Formik
initialValues={{ testing: {} }}
onSubmit={ alert("Submitted"); }
>
<Form>
<Select
name="testing"
items={[
{ id: 1, name: "Test 1" },
{ id: 2, name: "Test 2" },
{ id: 3, name: "Test 3" },
]}
/>
</Form>
)
}
export function Select(props) {
// Get field properties
const [field, meta, helpers] = useField(props);
// Set initial value for `Select`
const [selectedItem, setSelectedItem] = useState(null);
// On change, update `Headless UI` and `Formik` values
const handleChange = (newValue) => {
setSelectedItem(newValue); // Update the Headless UI state
helpers.setValue(newValue); // This seems to "break" the Headless UI state
};
// Return `Select` structure
return (
<Listbox
value={selectedItem}
name={props.name}
onChange={handleChange}
// onBlur={field.onBlur} ...Commented out for now as trying to figure out issue with `onChange`
>
<Listbox.Label>Test Label:</Listbox.Label>
<Listbox.Button>
{selectedItem ? selectedItem.name : "-- Select --"}
</Listbox.Button>
<Listbox.Options>
{props.items.map((item) => {
return (
<Listbox.Option
className={({ active }) => (active ? "active" : "")}
key={item.id}
value={item}
>
{({ selected }) => (
<>
{item.name}
{selected ? <CheckIcon /> : null}
</>
)}
</Listbox.Option>
);
})}
</Listbox.Options>
</Listbox>
);
}
I managed to make it work like this:
import { useField } from "formik";
interface SelectProps {
name: string;
label: string;
options: string[];
}
export const Select: React.FC<SelectProps> = ({
name,
label,
options,
}) => {
const [field] = useField({ name });
return (
<Listbox
value={field.value}
onChange={(value: string) => {
field.onChange({ target: { value, name } });
}}
>
...
</Listbox>
);
};

Access selected value from react's Select Component

Hello dear community,
I want to create a very basic website for my school assignment.
I have a dropdown menu with a Select Component as its implementation. I need to access the selected value, which is a currency in my case, in order to update the information displayed on the page once a currency has been selected.
I am kind of frustrated since I wasn't able to find a helpfull solution to my relatively basic problem (I think it's basic :D)
My component class here:
import { Component } from "react";
import Select from 'react-select';
interface DropdownMenuProps {
values: [{}]
defaultValue: string
}
interface DropdownMenuState { }
/**
* Represents a Dropdown Menu
*/
export default class DropdownMenu extends Component<DropdownMenuProps, DropdownMenuState> {
render() {
return (
<div style={{ width: '120px' }}>
<Select id="dropdown-menu"
placeholder={this.props.defaultValue}
options={this.props.values}
// getOptionValue={(option) => option.value}
// getOptionLabel={(option) => option.label}
/>
</div>
)
}
}
This is how I create a dropdown menu compent:
<DropdownMenu defaultValue="Currency" values={[{ label: "EUR", value: "EUR" }, { label: "GBP", value: "GBP" }, { label: "USD", value: "USD" }]} ></DropdownMenu>
I'm glad for any tips :)
You have to use the onChange prop of react-select:
<select
id="dropdown-menu"
onChange={handleSelectChange}
>
and in this function you could handle the changes:
const handleSelectChange = (selectedVal) => console.log(selectedVal)
I recommend you using directly onChange and save it into a state:
1st:
Create state with the name you want:
const [selectValue, setSelectValue] = useState({});
2nd: Create options:
const options = [
{ label: "Tomate", value: 1 },
{ label: "Queso", value: 2 }
];
3rd: add method onChange in select and pass options:
const onChange = (ev) => {
setSelectValue(ev);
//console.log(ev);
console.log(selectValue);
};
<Select
id="dropdown-menu"
placeholder={"e"}
options={options}
onChange={onChange}
/>
I have created demo here:
https://codesandbox.io/s/ancient-pond-96h53?file=/src/App.js

Autocomplete Material UI search based on two attributes

I have this data:
const sample = [
{hero: 'DARTH VADER', faction: 'EMPIRE'},
{hero: 'LUKE SKYWALKER', faction: 'REBEL'},
{hero: 'GREEDO', faction: 'BOUNTY HUNTER'},
];
Using Autocomplete, I am able to search the value via either hero or faction attribute by assigning one of them in getOptionLabel={(option) => {option.hero}} or getOptionLabel={(option) => {option.faction}} prop.
However I would like to be able to search using both of these attributes at the same time (typing either DARTH VADER or EMPIRE will return the same result).
The questions are as follows:
Can I achieve that using a single autocomplete?
If not, can we use a toggle to change which attribute to look for in the autocomplete?
Many thanks
Autocomplete provides the filterOptions prop.
you can use it to provide a custom filter like so (here's a codesandbox):
import React from 'react';
import TextField from '#material-ui/core/TextField';
import Autocomplete, { createFilterOptions } from '#material-ui/lab/Autocomplete';
const filterOptions = createFilterOptions({
matchFrom: 'any',
stringify: (option) => option.hero + option.faction,
});
export default function Filter() {
return (
<Autocomplete
id="filter-demo"
options={sample}
getOptionLabel={(option) => option.hero}
filterOptions={filterOptions}
renderInput={(params) => <TextField {...params} label="Custom filter" variant="outlined" />}
/>
);
}
const sample = [
{hero: 'DARTH VADER', faction: 'EMPIRE'},
{hero: 'LUKE SKYWALKER', faction: 'REBEL'},
{hero: 'GREEDO', faction: 'BOUNTY HUNTER'},
];

Material UI Select Field multiselect

I tried several times the example given in the documentation. but it didn't work well for me.
can any one help me....
this is the code
import React, {Component} from 'react';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
const names = [
'Oliver Hansen',
'Van Henry',
'April Tucker',
'Ralph Hubbard',
'Omar Alexander',
'Carlos Abbott',
'Miriam Wagner',
'Bradley Wilkerson',
'Virginia Andrews',
'Kelly Snyder',
];
/**
* `SelectField` can handle multiple selections. It is enabled with the `multiple` property.
*/
export default class SelectFieldExampleMultiSelect extends Component {
state = {
values: [],
};
handleChange = (event, index, values) => this.setState({values});
menuItems(values) {
return names.map((name) => (
<MenuItem
key={name}
insetChildren={true}
checked={values && values.indexOf(name) > -1}
value={name}
primaryText={name}
/>
));
}
render() {
const {values} = this.state;
return (
<SelectField
multiple={true}
hintText="Select a name"
value={values}
onChange={this.handleChange}
>
{this.menuItems(values)}
</SelectField>
);
}
}
http://www.material-ui.com/#/components/select-field
the select property works but it doesnt select multiple options. when i check the states.value it only includes a single value not a array of values
This example didn't work for me either. To add the multi-select feature you have to manually add the new value to the state, so the handleChange function from the example would look something like this:
handleChange(event, index, values) {
this.setState({
values: [...this.state.values , values]
});
}
EDIT: I updated my version of material-ui to the latest stable version and their example worked like a charm
A much better approach is possible. In the lasted version of material UI.
import Select from '#material-ui/core/Select';
import MenuText from '#material-ui/core/MenuText';
import {useState} from 'react';
const App = () => {
const [selected,setSelected] = useState([]);
return <Select multiple={true} value={selected} onChange={(event) => setSelected(event.target.value)}>
<MenuItem>Val - 1</MenuItem>
<MenuItem>Val - 2</MenuItem>
<MenuItem>Val - 3</MenuItem>
<MenuItem>Val - 4</MenuItem>
</Select>
}
Make sure to set the value of select as an array. Otherwise, It will not work
This worked for me for old material ui version. You can add multiple items and deselect to remove them.
handleChange(event, index, values) {
this.setState({
values: [...values]
});
}

Multi select from react-select with redux-form not working as intended

So iam using multi select from react-select with redux-form with the onBlur hack(?) which is working fine behind the curtain because when i submit it I have the selected data in values
BUT after visiting any multi select field (even if i dont select anything) I end up with no values at all (nothing is displayed but this
))
const options = [
{ value: 'one', label: 'One' },
{ value: 'two', label: 'Two' }
];
<Select
value="one"
options={options}
multi={true}
{...input}
onBlur={() => {
input.onBlur({...input.value})
}
}
/>
So i end up with values in the redux state but I cant see any values in the field. Anyone know why is this happening?
Here's example how to make it works. react-select (1.0.0-rc.2), redux-form (5.3.4)
SelectInput.js
import React from 'react';
import Select from 'react-select';
import 'react-select/dist/react-select.css';
export default (props) => (
<Select
{...props}
value={props.input.value}
onChange={(value) => props.input.onChange(value)}
onBlur={() => props.input.onBlur(props.input.value)}
options={props.options}
/>
);
MyAwesomeComponent.js
import React, {PureComponent} from 'react';
import SelectInput from './SelectInput.js';
class MyAwesomeComponent extends PureComponent {
render() {
const options = [
{'label': 'Germany', 'value': 'DE'},
{'label': 'Russian Federation', 'value': 'RU'},
{'label': 'United States', 'value': 'US'}
];
return (
<Field
name='countries'
options={options}
component={SelectInput}
multi
/>
)
};
I have not used more modern versions of react-select, but a year ago, there were some issues with having to split() the string value by the delimiter to get the values as an array, and then join() them again to give to the component.
Stuff like this:
if (multi) {
selectValue = value ? value.join(delimiter) : ''
} else {
selectValue = value || null
}
I recommend examining exactly what the value is in Redux, using Redux DevTools, and what value prop is getting passed to react-select. I'm sure you'll find the problem in there.

Resources