-
Notifications
You must be signed in to change notification settings - Fork 0
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
Tag Use Case #8
Tag Use Case #8
Changes from 1 commit
6b93e6e
845e5a0
0c9d2c0
8d1b902
53bb3e0
152a810
4d572e8
2d30eee
41dc125
7166dd6
d4a79ee
0b1b344
a74ce35
5ffc4e7
e033268
96783d4
67276b6
3b450a2
655f397
aaded5a
5e7e8b9
d13e762
bad0890
9c6a1e9
5434c43
ff867ef
4e55f63
7d0d0b3
f72f4ca
b9f7697
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { | |
useControlledReducer, | ||
computeInitialState | ||
} from './utils'; | ||
import { mergeRefs, callAllEventHandlers, noop } from '../../utils'; | ||
import { BL } from './types'; | ||
|
||
export function useCombobox<Item>(props: BL.ComboboxProps<Item> = {}) { | ||
|
@@ -92,7 +93,7 @@ export function useCombobox<Item>(props: BL.ComboboxProps<Item> = {}) { | |
* | ||
*/ | ||
React.useEffect(() => { | ||
if (inputRef.current && isOpen) { | ||
if (inputRef.current && (isOpen || props.initialIsOpen)) { | ||
inputRef.current.focus(); | ||
} | ||
}, [isOpen]); | ||
|
@@ -231,7 +232,11 @@ export function useCombobox<Item>(props: BL.ComboboxProps<Item> = {}) { | |
// this is the entry point for handling the change event for the input value | ||
// I think that we can add a debounce function here as a prop | ||
// onchange prop will let the user control when the state updates | ||
function getInputProps(props?: BL.ComboboxInputGetterProps) { | ||
function getInputProps<T>({ | ||
onBlur = noop, | ||
onFocus = noop, | ||
controlDispatch | ||
}: BL.ComboboxInputGetterProps<T> = {}) { | ||
const inputKeyDownHandler = (e: React.KeyboardEvent) => { | ||
const keyEvt = normalizeKey(e); | ||
if (keyEvt.name in inputKeyDownHandlers) { | ||
|
@@ -241,22 +246,21 @@ export function useCombobox<Item>(props: BL.ComboboxProps<Item> = {}) { | |
|
||
const inputBlurHandler = (e: React.FocusEvent<HTMLInputElement>) => { | ||
e.preventDefault(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
console.log('[INPUT_BLUR]'); | ||
dispatch({ | ||
type: BL.ComboboxActions.INPUT_BLUR | ||
}); | ||
}; | ||
|
||
const inputChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const val = e.currentTarget.value; | ||
if (props?.controlDispatch) { | ||
if (controlDispatch) { | ||
const fn = () => { | ||
dispatch({ | ||
type: BL.ComboboxActions.INPUT_VALUE_CHANGE, | ||
text: val | ||
}); | ||
}; | ||
props.controlDispatch(fn); | ||
controlDispatch(fn); | ||
} else { | ||
dispatch({ | ||
type: BL.ComboboxActions.INPUT_VALUE_CHANGE, | ||
|
@@ -265,14 +269,15 @@ export function useCombobox<Item>(props: BL.ComboboxProps<Item> = {}) { | |
} | ||
}; | ||
|
||
const eventHandlers = { | ||
onKeyDown: inputKeyDownHandler | ||
let eventHandlers = { | ||
onKeyDown: inputKeyDownHandler, | ||
onChange: inputChangeHandler, | ||
onBlur: callAllEventHandlers(inputBlurHandler, onBlur), | ||
onFocus | ||
}; | ||
|
||
return { | ||
ref: inputRef, | ||
onChange: inputChangeHandler, | ||
onBlur: inputBlurHandler, | ||
ref: props.ref, | ||
role: 'textbox', | ||
'aria-labelledby': elementIds.labelId, | ||
'aria-controls': elementIds.menuId, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
export function mergeRefs(...refs: React.MutableRefObject<any>[]) { | ||
return function(node: React.ReactElement) { | ||
// iterate over every ref | ||
// assign the node to the current ref | ||
refs.forEach((ref) => { | ||
ref.current = node; | ||
}); | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We close over the refs passed as arguments, when React finishes its rendering phase, it looks to assign the ref a value, we iterate over all refs all assign each ref the node value. |
||
|
||
export function callAllEventHandlers(...fns: ((...args: any[]) => any)[]) { | ||
return function(...args: any[]) { | ||
fns.forEach((fn) => { | ||
if (typeof fn === 'function') { | ||
fn(...args); | ||
} | ||
}); | ||
}; | ||
} | ||
Comment on lines
+139
to
+147
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for every fn, call the function with args. Used for composing event handlers, both internal and component usage handlers |
||
|
||
export const noop = () => {}; | ||
// the ref property refers to? | ||
|
||
// ref is a property on a JSX element | ||
// react is a UI runtime that creates predictable UI | ||
// so how is a ref actually assigned? | ||
// well, we wait until all the elements are rendered on the page | ||
// then the ref is assigned that node | ||
// ref={fn()} |
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.
we only want to focus the input when the popup is open OR the user wants the popup to always be open