How to move the MultiValue to sit just above the input #4850
-
Hello! I've been struggling for a little while now to move the Any help would be greatly appreciated! thanks 😄 🎉 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @LaurenQurashi Moving the MultiValue lovelies around may or may not be as complicated as it sounds depending on the context. Just wanted to get some clarification on what you are asking by "look and act the same exact way". Is the intention to move the MultiValues out of the ValueContainer or to keep them within the control but simply on a new line?
Here's what a possible implementation looks like: import React, { useCallback, useState } from "react";
import Select, { components } from "react-select";
import { compliments } from "../data/options";
const App = (props) => {
const [value, setValue] = useState();
const onChange = useCallback((newValue) => setValue(newValue), []);
const removeValue = useCallback(
(removed) => setValue(value.filter((v) => v.value !== removed.value)),
[value]
);
const styles = {
valueContainer: (css) => ({ ...css, display: "grid" })
};
return (
<Select
isMulti
value={value}
onChange={onChange}
removeValue={removeValue}
options={compliments}
components={{ SelectContainer }}
controlShouldRenderValue={false}
styles={styles}
/>
);
};
const SelectedValuesContainer = ({ isDisabled, getValue, ...props }) => {
const { getOptionValue, formatOptionLabel, removeValue } = props.selectProps;
const getValueLabel = (opt) => formatOptionLabel?.(opt, "value") || opt.label;
const getKey = (opt, index) => `${getOptionValue(opt)}-${index}`;
const toMultiValue = (opt, index) => (
<components.MultiValue
getValue={getValue}
{...props}
components={{
Container: components.MultiValueContainer,
Label: components.MultiValueLabel,
Remove: components.MultiValueRemove
}}
//isFocused={isOptionFocused}
isDisabled={isDisabled}
key={getKey(opt, index)}
index={index}
removeProps={{
onClick: () => removeValue(opt),
onTouchEnd: () => removeValue(opt),
onMouseDown: (e) => {
e.preventDefault();
e.stopPropagation();
}
}}
data={opt}
>
{getValueLabel(opt)}
</components.MultiValue>
);
return (
<div style={{ margin: ".5rem 0", display: "flex", flexFlow: "row wrap" }}>
{getValue().map(toMultiValue)}
</div>
);
};
const SelectContainer = ({
children,
className,
innerProps,
isFocused,
...commonProps
}) => {
const selectContainerProps = {
...commonProps
};
return (
<components.SelectContainer
className={className}
innerProps={innerProps}
isFocused={isFocused}
{...selectContainerProps}
>
<SelectedValuesContainer {...commonProps} />
{children}
</components.SelectContainer>
);
};
export default App; Hope this is a helpful start for what you are working on. Cheers! |
Beta Was this translation helpful? Give feedback.
-
There are tons of typescript errors with this example |
Beta Was this translation helpful? Give feedback.
Hi @LaurenQurashi
Moving the MultiValue lovelies around may or may not be as complicated as it sounds depending on the context.
Just wanted to get some clarification on what you are asking by "look and act the same exact way". Is the intention to move the MultiValues out of the ValueContainer or to keep them within the control but simply on a new line?
If the intention is to keep the input on its own line but keep the values inside the
ValueContainer
component, then you should be able to apply styling flex properties to theInput
component (ie:flex: 1 1 100%, order: 1 | 9999
depending on whether you want the input above or below the values.If the intention is to move the values out …