-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add a clear button to FilterPicker, Select, ComboBox #787
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a5b3208
feat: add a clear button to FilterPicker, Select, ComboBox
tenphi 5e64940
feat: add a clear button to FilterPicker, Select, ComboBox * 2
tenphi d127eee
feat: add a clear button to FilterPicker, Select, ComboBox * 3
tenphi dcdb443
feat(ItemAction): add component
tenphi d0ed1cb
chore: add changelog
tenphi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@cube-dev/ui-kit": patch | ||
| --- | ||
|
|
||
| Add a clear button to FilterPicker, Select and ComboBox components. Redesign the clear button in SearchInput component. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ import { | |
| import { Section as BaseSection, useComboBoxState } from 'react-stately'; | ||
|
|
||
| import { useEvent } from '../../../_internal/index'; | ||
| import { DownIcon, LoadingIcon } from '../../../icons'; | ||
| import { CloseIcon, DownIcon, LoadingIcon } from '../../../icons'; | ||
| import { useProviderProps } from '../../../provider'; | ||
| import { FieldBaseProps } from '../../../shared'; | ||
| import { | ||
|
|
@@ -37,6 +37,7 @@ import { | |
| } from '../../../utils/react'; | ||
| import { useFocus } from '../../../utils/react/interactions'; | ||
| import { useEventBus } from '../../../utils/react/useEventBus'; | ||
| import { Button } from '../../actions'; | ||
| import { useFieldProps, useFormProps, wrapWithField } from '../../form'; | ||
| import { Item } from '../../Item'; | ||
| import { OverlayWrapper } from '../../overlays/OverlayWrapper'; | ||
|
|
@@ -70,7 +71,9 @@ const TriggerElement = tasty({ | |
| placeContent: 'center', | ||
| placeSelf: 'stretch', | ||
| radius: '(1r - 1bw) right', | ||
| padding: '0', | ||
| width: '3x', | ||
| boxSizing: 'border-box', | ||
| color: { | ||
| '': '#dark-02', | ||
| hovered: '#dark-02', | ||
|
|
@@ -90,6 +93,16 @@ const TriggerElement = tasty({ | |
| }, | ||
| }); | ||
|
|
||
| const ClearButton = tasty(Button, { | ||
| icon: <CloseIcon />, | ||
| type: 'neutral', | ||
| styles: { | ||
| height: '($size - 1x)', | ||
| width: '($size - 1x)', | ||
| margin: '0 .5x', | ||
| }, | ||
| }); | ||
|
|
||
| export interface CubeComboBoxProps<T> | ||
| extends Omit< | ||
| CubeSelectBaseProps<T>, | ||
|
|
@@ -125,6 +138,8 @@ export interface CubeComboBoxProps<T> | |
| suffixPosition?: 'before' | 'after'; | ||
| menuTrigger?: MenuTriggerAction; | ||
| allowsCustomValue?: boolean; | ||
| /** Whether the combo box is clearable using ESC keyboard button or clear button inside the input */ | ||
| isClearable?: boolean; | ||
| } | ||
|
|
||
| const PROP_STYLES = [...BASE_STYLES, ...OUTER_STYLES, ...COLOR_STYLES]; | ||
|
|
@@ -201,6 +216,7 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>( | |
| labelSuffix, | ||
| selectedKey, | ||
| defaultSelectedKey, | ||
| isClearable, | ||
| ...otherProps | ||
| } = props; | ||
|
|
||
|
|
@@ -306,6 +322,32 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>( | |
| let validationIcon = isInvalid ? InvalidIcon : ValidIcon; | ||
| let validation = cloneElement(validationIcon); | ||
|
|
||
| // Clear button logic | ||
| let hasValue = props.allowsCustomValue | ||
| ? state.inputValue !== '' | ||
| : state.selectedKey != null; | ||
| let showClearButton = | ||
| isClearable && hasValue && !isDisabled && !props.isReadOnly; | ||
|
|
||
| // Clear function | ||
| let clearValue = useEvent(() => { | ||
| // Always clear input value in state so UI resets to placeholder | ||
| state.setInputValue(''); | ||
| // Notify external input value only when custom value mode is enabled | ||
| if (props.allowsCustomValue) { | ||
| props.onInputChange?.(''); | ||
| } | ||
| props.onSelectionChange?.(null); | ||
| state.setSelectedKey(null); | ||
|
Comment on lines
+322
to
+331
|
||
|
|
||
| // Close the popup if it's open | ||
| if (state.isOpen) { | ||
| state.close(); | ||
| } | ||
| // Focus back to the input | ||
| inputRef.current?.focus(); | ||
| }); | ||
tenphi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| let comboBoxWidth = wrapperRef?.current?.offsetWidth; | ||
|
|
||
| if (icon) { | ||
|
|
@@ -333,6 +375,7 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>( | |
| loading: isLoading, | ||
| prefix: !!prefix, | ||
| suffix: true, | ||
| clearable: showClearButton, | ||
| }), | ||
| [ | ||
| isInvalid, | ||
|
|
@@ -342,6 +385,7 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>( | |
| isFocused, | ||
| isLoading, | ||
| prefix, | ||
| showClearButton, | ||
| ], | ||
| ); | ||
|
|
||
|
|
@@ -453,6 +497,15 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>( | |
| </> | ||
| ) : null} | ||
| {suffixPosition === 'after' ? suffix : null} | ||
| {showClearButton && ( | ||
| <ClearButton | ||
| size={size} | ||
| theme={validationState === 'invalid' ? 'danger' : undefined} | ||
| qa="ComboBoxClearButton" | ||
| data-no-trigger={hideTrigger ? '' : undefined} | ||
| onPress={clearValue} | ||
| /> | ||
| )} | ||
| {!hideTrigger ? ( | ||
| <TriggerElement | ||
| data-popover-trigger | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for determining
hasValueis complex and warrants explanation. Consider adding a comment explaining why custom value mode checks input value while regular mode checks selected key.