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

fix: multiselect read-only #992

Merged
merged 5 commits into from
Jan 12, 2024
Merged
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
8 changes: 7 additions & 1 deletion src/inputs/TreeSelectField/TreeSelectField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ function Template(args: TreeSelectFieldProps<HasIdAndName, string>) {
})),
}));

const multipleValuesWithParents = [
options[0]!.id, // Development:0
options[0]!.children![0]!.id, // Cohort:0
options[1]!.children![1]!.id, // Cohort:1
options[2]!.children![0]!.children![2]!.id, // Project:3
];
const singleValueId = [options[0]!.children![0]!.children![0].id];
const multipleValueIds = [...singleValueId, options[0]!.children![0].children![1].id];

Expand Down Expand Up @@ -69,7 +75,7 @@ function Template(args: TreeSelectFieldProps<HasIdAndName, string>) {
{...args}
readOnly="Read-only reason tooltip text"
label="Read-only"
values={singleValueId}
values={multipleValuesWithParents}
options={options}
/>

Expand Down
12 changes: 12 additions & 0 deletions src/inputs/TreeSelectField/TreeSelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,23 @@ function TreeSelectFieldBase<O, V extends Value>(props: TreeSelectFieldProps<O,

initialOptions.forEach(areAllChildrenSelected);

// Given a child option, determine if the parent is selected.
const isParentSelected = (option: NestedOption<O>): boolean => {
const parents = findOption(initialOptions, valueToKey(getOptionValue(option)), getOptionValue)?.parents;
if (!parents) return false;
return parents.some((parent) => selectedKeys.includes(valueToKey(getOptionValue(parent))));
};

return {
selectedKeys,
inputValue:
selectedOptions.length === 1
? getOptionLabel(selectedOptions[0])
: isReadOnly && selectedOptions.length > 0
? selectedOptions
.filter((o) => !isParentSelected(o))
.map(getOptionLabel)
.join(", ")
: selectedOptions.length === 0
? nothingSelectedText
: "",
Expand Down
9 changes: 6 additions & 3 deletions src/inputs/internal/ComboBoxBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function ComboBoxBase<O, V extends Value>(props: ComboBoxBaseProps<O, V>)
// Do a one-time initialize of fieldState
const [fieldState, setFieldState] = useState<FieldState>(() => {
return {
inputValue: getInputValue(selectedOptions, getOptionLabel, multiselect, nothingSelectedText),
inputValue: getInputValue(selectedOptions, getOptionLabel, multiselect, nothingSelectedText, isReadOnly),
searchValue: undefined,
optionsLoading: false,
};
Expand Down Expand Up @@ -281,10 +281,10 @@ export function ComboBoxBase<O, V extends Value>(props: ComboBoxBaseProps<O, V>)
} else {
setFieldState((prevState) => ({
...prevState,
inputValue: getInputValue(selectedOptions, getOptionLabel, multiselect, nothingSelectedText),
inputValue: getInputValue(selectedOptions, getOptionLabel, multiselect, nothingSelectedText, isReadOnly),
}));
}
}, [state.isOpen, selectedOptions, getOptionLabel, multiselect, nothingSelectedText]);
}, [state.isOpen, selectedOptions, getOptionLabel, multiselect, nothingSelectedText, isReadOnly]);

// For the most part, the returned props contain `aria-*` and `id` attributes for accessibility purposes.
const {
Expand Down Expand Up @@ -403,9 +403,12 @@ function getInputValue<O>(
getOptionLabel: (o: O) => string,
multiselect: boolean,
nothingSelectedText: string,
readOnly?: boolean,
) {
return selectedOptions.length === 1
? getOptionLabel(selectedOptions[0])
: readOnly && selectedOptions.length > 0
? selectedOptions.map(getOptionLabel).join(", ")
0ces marked this conversation as resolved.
Show resolved Hide resolved
: multiselect && selectedOptions.length === 0
? nothingSelectedText
: "";
Expand Down
Loading