-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Add arbitrary combobox input hook and fix Autocomplete
- Loading branch information
1 parent
16dc74c
commit b10addb
Showing
9 changed files
with
167 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 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 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
32 changes: 32 additions & 0 deletions
32
modules/react/combobox/lib/hooks/useComboboxInputArbitrary.ts
This file contains 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,32 @@ | ||
import React from 'react'; | ||
import { | ||
createElemPropsHook, | ||
useLocalRef, | ||
dispatchInputEvent, | ||
} from '@workday/canvas-kit-react/common'; | ||
import {useComboboxModel} from './useComboboxModel'; | ||
|
||
/** | ||
* An arbitrary combobox can have any value. The list of options are suggestions to aid the user in | ||
* entering values. A Typeahead or Autocomplete are examples are arbitrary value comboboxes. | ||
*/ | ||
export const useComboboxInputArbitrary = createElemPropsHook(useComboboxModel)((model, ref) => { | ||
const {elementRef, localRef} = useLocalRef(ref as React.Ref<HTMLInputElement>); | ||
|
||
// sync model selection state with inputs | ||
React.useLayoutEffect(() => { | ||
if (localRef.current) { | ||
const formValue = (model.state.selectedIds === 'all' ? [] : model.state.selectedIds).join( | ||
', ' | ||
); | ||
|
||
if (formValue !== localRef.current.value) { | ||
dispatchInputEvent(localRef.current, formValue); | ||
} | ||
} | ||
}, [model.state.selectedIds, localRef]); | ||
|
||
return { | ||
ref: elementRef, | ||
}; | ||
}); |
This file contains 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 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 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 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 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