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

[NumberField] Fix disabled state on increment/decrement buttons #1462

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

mj12albert
Copy link
Member

@mj12albert mj12albert commented Feb 18, 2025

Fixes #1443

Before: https://codesandbox.io/p/sandbox/cocky-kepler-y5j9tz

After: https://codesandbox.io/p/devbox/billowing-cookies-gnsfn5

The issue was that pointer event handlers didn't consider the disabled prop on the buttons, only other disabled states (e.g. root)

Also extracted hooks/prop getters from the root hook to individual hooks.

@mj12albert mj12albert added component: number field This is the name of the generic UI component, not the React module! bug 🐛 Something doesn't work labels Feb 18, 2025
Copy link

netlify bot commented Feb 18, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 406d92c
🔍 Latest deploy log https://app.netlify.com/sites/base-ui/deploys/67c98fd694fa570008de90e0
😎 Deploy Preview https://deploy-preview-1462--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mj12albert mj12albert marked this pull request as ready for review February 18, 2025 11:04
@@ -315,4 +316,22 @@ describe('<NumberField.Decrement />', () => {

expect(input).to.have.value('-2');
});

it('prop: disabled', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

nit: the it function should describe the correct behavior. We usually place "prop: X" on the describe blocks.

@@ -452,7 +452,8 @@ export function useNumberFieldRoot(
},
onPointerDown(event) {
const isMainButton = !event.button || event.button === 0;
const isDisabled = disabled || (isIncrement ? isMax : isMin);
const isDisabled =
(externalProps.disabled ?? false) || disabled || (isIncrement ? isMax : isMin);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not entirely comfortable with this pattern. Not because it produces wrong results, but because we generally never rely on anything from externalProps. Is there a good reason for keeping all the buttons' handlers in the root hook instead of moving them closer to components?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a good reason for keeping all the buttons' handlers in the root hook instead of moving them closer to components?

Yeah I agree, I thought reaching into externalProps was starting to get weird 😅
Probably this was the earliest rewritten component and later we moved to have them closer to components, do you think its ok to move this to its own hook now @atomiks ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this was written before we moved hooks/logic into their owning components - it's okay to change it to follow the latest patterns

@mj12albert mj12albert removed the request for review from colmtuite February 20, 2025 06:00
@mj12albert mj12albert force-pushed the fix/disabled-numberfield-buttons branch 6 times, most recently from d168d31 to 0d23753 Compare February 28, 2025 07:00
@mj12albert
Copy link
Member Author

mj12albert commented Feb 28, 2025

I've extracted the hooks/prop getters from the root hook except for the scrub related ones to not conflict with #1485

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 5, 2025
@mj12albert mj12albert force-pushed the fix/disabled-numberfield-buttons branch from 0d23753 to bc73bb5 Compare March 5, 2025 10:18
@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Mar 5, 2025
@mj12albert mj12albert force-pushed the fix/disabled-numberfield-buttons branch from 9b1b572 to 9d56a4e Compare March 5, 2025 13:46
@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Mar 5, 2025
@mj12albert mj12albert force-pushed the fix/disabled-numberfield-buttons branch from 9d56a4e to bc3f6b9 Compare March 6, 2025 10:54
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 6, 2025
Comment on lines 49 to 72
const { getInputProps } = useNumberFieldInput({
allowInputSyncRef,
autoFocus,
disabled,
formatOptionsRef,
getAllowedNonNumericKeys,
getStepAmount,
id,
incrementValue,
inputMode,
inputValue,
invalid,
max,
mergedRef,
min,
name,
readOnly,
required,
setValue,
valueRef,
setInputValue,
locale,
inputRef: forwardedRef,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can just keep the logic inside the components, as this inflates the bundle size by passing all the params (same with the other components)

Copy link
Member Author

Choose a reason for hiding this comment

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

@atomiks Do you mean like this and not even have a hoook at all? 406d92c

It'll work except for the 2 buttons since most of the logic is shared so it can't be contained in one component

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense about the buttons as they previously were shared in the root - I think it's ok then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: number field This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[number field] Increment and Decrement can be clicked when disabled
3 participants