-
Notifications
You must be signed in to change notification settings - Fork 4
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
It'd be nice to be able to shift-click a selection of a large CheckboxGroup #4
Comments
Not a good description, but thanks for the suggestion. I will be working on a shift-click feature, but want feedback on my solution. POC branch: shift-click As a proof of concept I created a function that loops through the children of the CheckboxGroup. const getChildCheckboxes = (childNodes: ReactNode[]): string[] => {
const checkboxList: string[] = [];
if (!Array.isArray(childNodes)){
childNodes = [childNodes];
}
childNodes.forEach((node => {
if (objectIsCheckbox(node)) {
checkboxList.push(node.props.id);
} else if (objectHasChildren(node)) {
checkboxList.push(
...getChildCheckboxes(node.props.children)
);
}
}));
return checkboxList;
};
getChildCheckboxes(children as ReactNode[] || []) To (un)check the checkboxes within the range I created this function, where const toggleShiftGroup = (key: string): void => {
const endCheckbox = checkboxes.get(key);
if (!endCheckbox || lastToggledId === undefined) {
return;
}
const start = orderedIdList.indexOf(lastToggledId);
const end = orderedIdList.indexOf(key);
const toggleCheckboxes = orderedIdList.slice(start < end ? start : end, (end > start ? end : start) + 1);
toggleCheckboxes.forEach((id) => {
const toggleCheckbox = checkboxes.get(id);
if (toggleCheckbox) {
toggleCheckbox.setIsChecked(endCheckbox.isChecked || false);
}
});
onCheckboxChange(key);
}; It seems to be working but I'm not sure about performance and should be tested carefully. |
Well if Im only commenting on the readability of the logic, the getChildCheckboxes could be refactored to use flatMap const getChildCheckboxes = (childNodes: ReactNode[]): string[] => {
if (!Array.isArray(childNodes)){
childNodes = [childNodes];
}
return childNodes.flatMap(node => {
if (objectIsCheckbox(node)) {
return [node.props.id];
} if (objectHasChildren(node)) {
return getChildCheckboxes(node.props.children);
}
});
}; And the orderedIdList.slice() could be refactored to use Math.min and Math.max statement const toggleCheckboxes = orderedIdList.slice(Math.min(start, end), Math.max(end, start) + 1); |
Thanks for the suggestion. I am thinking about another solution, maybe iterate though the dom tree. |
Title is self explanatory
The text was updated successfully, but these errors were encountered: