hi I have this component import * as React from 'react'; import * as i from 'types';…
- User
hi I have this component import * as React from 'react'; import * as i from 'types'; import { FormField, SelectDropdown } from 'common/form'; import { Container, InputsWrapper } from './styled'; import { TagItem } from './components';
export const MultiSelect: React.FC = ({ selectedItems, onItemSelection, onChange, label, id, options, name, placeholder = 'Select...', error, description, }) => { const optionsQtyThreshold = 2;
console.log('selectedItems: ', selectedItems);
const checkIfSelected = (option: i.SelectOption) => { console.log('options: ', options); console.log('selectedItems: ', selectedItems); return selectedItems.some((item) => Object.values(item).includes(option.value) ); };
const handleTagSelection = (option: i.SelectOption) => { if (checkIfSelected(option)) { onItemSelection( selectedItems.filter((item) => item.value !== option.value) ); return; }
codeonItemSelection([...selectedItems, option]);};
const formFieldProps = { name, label, error, description };
return ( <FormField {...formFieldProps} zIndex={5}> {options.length <= optionsQtyThreshold ? ( {options.map((option) => ( ))} ) : ( )} ); };
export type MultiSelectProps = i.FormFieldProps & { onItemSelection: (selectedItems: i.SelectOption[]) => void; selectedItems: i.SelectOption[]; placeholder?: string; onChange: () => void; options: i.SelectOption[]; id?: string; };
currently is works with accepting and returning data in format [{label: 'my label', value: 'my_value'}, {label: 'my label2', value: 'my_value2'}] we need to make it work (accept and return) with comma separated string - 'my_value, my_value2'
- Assistant
To make the MultiSelect component accept and return data in the format of a comma-separated string, you can make the following modifications:
First, update the type definition of
MultiSelectPropsto reflect the change:tsx - User
result looks like this - , option1, option2 we don't need comma at the start
- Assistant
Apologies for the oversight. To remove the comma at the start of the result, you can update the way the
selectedStringis constructed in thehandleTagSelectionfunction as follows:tsxconst handleTagSelection = (option: i.SelectOption) => { const selectedValues = parsedSelectedItems.map((item) => item.value); const index = selectedValues.indexOf(option.value); if (index !== -1) { selectedValues.splice(index, 1); } else { selectedValues.push(option.value); } const selectedString = selectedValues.join(', '); onItemSelection(selectedString.startsWith(', ') ? selectedString.slice(2) : selectedString); };This updated code uses the
startsWithmethod to check if theselectedStringstarts with a comma and space. If it does, the slice method is used to remove those characters from the string before passing it to theonItemSelectionfunction.With this change, the resulting comma-separated string will no longer have a leading comma and space.
- User
I think we also need to modify the onChange function provided to , it is a react-select component
- Assistant
Apologies for missing that. You're right; we also need to modify the
onChangefunction passed to theSelectDropdowncomponent.In the
MultiSelectcomponent, you can update theonChangeprop as follows:tsx// Rest of the component code... const handleChange = (selectedOptions: i.SelectOption[]) => { const selectedValues = selectedOptions.map((option) => option.value); const selectedString = selectedValues.join(', '); onItemSelection(selectedString); }; // Rest of the component code... <SelectDropdown placeholder={placeholder} name={name} options={options} onChange={handleChange} // Update this line defaultValue={selectedItems} isMulti /> // Rest of the component code...