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

Add simple CycleButton and use it in three way checkboxes #1296

Conversation

Oksamies
Copy link
Contributor

No description provided.

extends Omit<ActionableButtonProps, "primitiveType"> {
noState?: boolean;
value?: string;
onInterract?: () => void;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prop name onInterract contains a typo and should be spelled onInteract (single 't'). This change would align with standard React naming conventions and correct spelling.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

onInterract();
}
}}
onChange={onValueChange ? () => onValueChange(currentValue) : undefined}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onChange handler should be moved inside the onClick handler and called after setCurrentValue. Since setCurrentValue is asynchronous, the current implementation will pass the old value to onValueChange. Moving it after setCurrentValue ensures the new value is passed correctly.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

Copy link
Contributor Author

Oksamies commented Jan 20, 2025

@Oksamies Oksamies force-pushed the 01-20-add_simple_cyclebutton_and_use_it_in_three_way_checkboxes branch from 9502128 to 0c5e861 Compare January 20, 2025 15:18
onCheckedChange={() => toggleCategory(c.id)}
className={styles.checkbox}
<CycleButton
onInterract={() => toggleCategory(c.id)}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prop name onInterract contains a typo and should be onInteract to match the component's interface definition. This will prevent TypeScript errors and maintain consistency with the CycleButton component's API.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

Comment on lines 62 to 82
onClick={() => {
if (options && options.length > 0) {
const currentValueIndex = options?.indexOf(currentValue);
if (
currentValueIndex === -1 ||
currentValueIndex === options.length - 1
) {
setCurrentValue(options[0]);
} else {
setCurrentValue(options[currentValueIndex + 1]);
}
}
if (onInteract) {
onInteract();
}
if (onValueChange) {
onValueChange(currentValue);
}
}}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onValueChange callback is receiving the previous state value because currentValue hasn't been updated when the callback runs. To fix this, calculate the next value once, then use it for both setCurrentValue and onValueChange. This ensures the callback receives the correct new value.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

@Oksamies Oksamies force-pushed the 01-20-add_simple_cyclebutton_and_use_it_in_three_way_checkboxes branch from 0c5e861 to 3cb6694 Compare January 20, 2025 15:22
Comment on lines 63 to 74
let newValue = "";
if (options && options.length > 0) {
const currentValueIndex = options?.indexOf(currentValue);
if (
currentValueIndex === -1 ||
currentValueIndex === options.length - 1
) {
newValue = options[0];
} else {
newValue = options[currentValueIndex + 1];
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newValue calculation should preserve currentValue when options is undefined or empty, rather than defaulting to an empty string. Currently, the code will set newValue = "" in these cases, which could cause unexpected state changes. Consider guarding this logic:

let newValue = currentValue;
if (options?.length > 0) {
  // existing cycling logic
}

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

@Oksamies Oksamies force-pushed the 01-20-add_simple_cyclebutton_and_use_it_in_three_way_checkboxes branch from 3cb6694 to 94efb63 Compare January 20, 2025 16:17
@Oksamies Oksamies merged commit 14140d4 into master Jan 20, 2025
25 checks passed
@Oksamies Oksamies deleted the 01-20-add_simple_cyclebutton_and_use_it_in_three_way_checkboxes branch January 20, 2025 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant