Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select: getOptions async and isOptionEqualToValue #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/components/Control/Control.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const Control = ({
triggerRecalculation,
updateNodeConnections,
getOptions,
isOptionEqualToValue,
setValue,
defaultValue,
isMonoControl
Expand Down Expand Up @@ -56,8 +57,9 @@ const Control = ({
<Select
{...commonProps}
options={
getOptions ? getOptions(inputData, executionContext) : options
getOptions ? getOptions(inputData, executionContext, nodeId) : options
}
isOptionEqualToValue={isOptionEqualToValue}
placeholder={placeholder}
/>
);
Expand All @@ -75,8 +77,9 @@ const Control = ({
allowMultiple
{...commonProps}
options={
getOptions ? getOptions(inputData, executionContext) : options
getOptions ? getOptions(inputData, executionContext, nodeId) : options
}
isOptionEqualToValue={isOptionEqualToValue}
placeholder={placeholder}
label={label}
/>
Expand Down
32 changes: 25 additions & 7 deletions src/components/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import styles from "./Select.css";

const MAX_LABEL_LENGTH = 50;

const isOptionEqualToValueDefault = (option, value) => option === value

const Select = ({
options = [],
isOptionEqualToValue,
placeholder = "[Select an option]",
onChange,
data,
Expand All @@ -19,6 +22,15 @@ const Select = ({
y: 0
});
const wrapper = React.useRef();
const [ theOptions, setTheOptions ] = React.useState([])

React.useEffect(() => {
if (options.then) {
options.then(setTheOptions)
} else {
setTheOptions(options)
}
}, [ options ])

const closeDrawer = () => {
setDrawerOpen(false);
Expand Down Expand Up @@ -47,14 +59,17 @@ const Select = ({
onChange([...data.slice(0, optionIndex), ...data.slice(optionIndex + 1)]);
};

const getFilteredOptions = () => (
const getFilteredOptions = React.useCallback(() => (
allowMultiple ?
options.filter(opt => !data.includes(opt.value))
: options
)
theOptions.filter(opt => !data.includes(opt.value))
: theOptions
), [ theOptions, data ])

const selectedOption = React.useMemo(() => {
const option = options.find(o => o.value === data);
const option = theOptions.find(o => isOptionEqualToValue
? isOptionEqualToValue(o.value, data)
: isOptionEqualToValueDefault(o.value, data)
);
if (option) {
return {
...option,
Expand All @@ -64,7 +79,7 @@ const Select = ({
: option.label
};
}
}, [options, data]);
}, [theOptions, data]);

return (
<React.Fragment>
Expand All @@ -73,7 +88,10 @@ const Select = ({
<div className={styles.chipsWrapper}>
{data.map((val, i) => {
const optLabel =
(options.find(opt => opt.value === val) || {}).label || "";
(theOptions.find(opt => isOptionEqualToValue
? isOptionEqualToValue(opt.value, val)
: isOptionEqualToValueDefault(opt.value, val)
) || {}).label || "";
return (
<OptionChip
onRequestDelete={() => handleOptionDeleted(i)}
Expand Down