rfc: added useClearButton proposal#4651
Conversation
|
Hey! thank you for the interest and appreciate the discussion. I don't think we need an entirely new hook for clear buttons. The reason that useToggleButton is its own hook, is because it has state that it's dealing with and has unique aria attributes. Clear button is specific to textfields that can also be cleared with the "Escape" key. It's just a button that is excluded from the tab order. We could consider adding it to the useTextfield hook in order to attach the clearing behavior onPress, much like has been done in useSearchField https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/searchfield/src/useSearchField.ts#L109 However, it isn't really a pattern specific to textfield's, so I feel like it should be added as needed to those higher level hooks or just added when needed in a React Aria Component implementation, this is also the level where the "Escape" key is currently handled, which is mandatory if you're using a clear button. The reverse isn't true though. For example, a combobox can be cleared with "Escape", but it doesn't have a clear button at the moment. Maybe this is something we should allow for, not sure. Could you provide examples that you're trying to address? Or can you use a searchfield instead? |
|
You are right, this is already quite a solution rather than a problem statement. The problem is that all these places need some form of reset functionality, which currently people just somehow hack together, I would rather see some provided, accessible approach that is easy to use.
|
|
Also, something like this would be nice. import {NumberField, Label, Group, Input, Button} from 'react-aria-components';
<NumberField defaultValue={1024} minValue={0}>
<Label>Width</Label>
<Group>
<Button slot="decrement">-</Button>
<Input />
+ <Button slot="clear">x</Button>
<Button slot="increment">+</Button>
</Group>
</NumberField> |
|
I see, could you check out #4640 and see if resetting form fields this way addresses your concerns. I don't think we want to add various button props to all of these inputs. For instance, which one do we add to a NumberField? clear? or reset? Because of this, I would advocate that people combine our hooks and the components they build with our hooks. It would be helpful if there was a showcase of how our library is misused when creating an accessible form field with a reset and/or clear button. Dialogs and Popovers aren't form elements, so I feel like they are a different class of issue altogether. Let's keep this focused on forms and form elements for now. |
Both are okay IMO,
I wouldn't call it misused, it's just always custom stuff. https://storiesv2.nextui.org/?path=/story/components-input--clearable And there is always something that is not 100%.
|
|
Thanks for the reply and the example. You are right that being excluded from the tab order is not a big concern for this example. And because it's reachable by keyboard, it's not a big issue that the I think we could consider this, as the button should probably be labeled as The last thing I'm a bit hung up on is this. How do we know if we should turn on the "Escape" clears? I do not think we should always have that turned on in every textfield. I think that we should allow people to build a pattern that looks like either our SearchField OR nextui's email field. |
That's a great point @snowystinger, I would love if we could have something like that. const { labelProps } = useLabel(…);
const { clearButtonProps } = useClearButton({
labeledBy: labelProps.id,
disableEscape: true, // esc clears by default
disableTab: true // is reachable by default
});I see 2 problems
|
I don't quite see how the useClearButton hook helps us determine if a "Clear button" has been used in the dom though, and I'd prefer not to add props to all of our hooks involving textfields for this. Including a codesandbox of the use case, we're talking about https://codesandbox.io/s/musing-dhawan-2k4mvm?file=/src/App.js Will have some other team members have a look as well. |
|
If I remember correctly, I do agree though that it would helpful if there was a way to spin up an accessible clear/reset button quickly, but we would need to decide if we want our hooks to handle that case directly (and thus become more opinionated), if it should live in a separate hook and place the onus on the user for using it correctly (aka |
|
Hey again, I wanted to let you know we discussed this more as a team today. We don't want to move forward with this for a couple of reasons:
Again, thank you for the discussion. |
This is a proposal for a new hook called
useClearButton.The points also apply when thinking about creating something like auseCloseButtonhook.