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

Tag Use Case #8

Merged
merged 30 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6b93e6e
add useDebounce
krfong916 Nov 17, 2021
845e5a0
update tag editor to use useCombobox hook edit type support for combobox
krfong916 Nov 17, 2021
0c9d2c0
fix onStateChange capitalization bug, add editor use case UI
krfong916 Nov 18, 2021
8d1b902
add spacing and screen-size standards for scss
krfong916 Nov 18, 2021
53bb3e0
implement loader, redo editor css, fix a11y bugs
krfong916 Nov 19, 2021
152a810
xed isopen state when user input changes, prevent default when user n…
krfong916 Nov 20, 2021
4d572e8
* fixed input isopen state when user input changes - our tests didn't…
krfong916 Nov 23, 2021
2d30eee
init useMultipleSelection, create generic types file
krfong916 Nov 23, 2021
41dc125
add keyboard tests for usemultiselect
krfong916 Nov 24, 2021
7166dd6
change keyboard navigation API for usemultiselect
krfong916 Nov 27, 2021
d4a79ee
implement all tests for usemultiselection
krfong916 Nov 29, 2021
0b1b344
fix combobox tests and cleanup type namespace
krfong916 Nov 29, 2021
a74ce35
move combobox folder
krfong916 Nov 29, 2021
5ffc4e7
implement tag use case, fix arrow navigation remove selectedItemListP…
krfong916 Nov 30, 2021
e033268
fix merge refs to include functions, this allows for composeable refs…
krfong916 Nov 30, 2021
96783d4
fix initial state, highlighted index
krfong916 Nov 30, 2021
67276b6
implement current item selection index and accessible navigation
krfong916 Nov 30, 2021
3b450a2
add cancel debounce callback and tests for tag editor
krfong916 Dec 1, 2021
655f397
add msw for mocking fetch and init tag tests, add event listeners for…
krfong916 Dec 2, 2021
aaded5a
add mock server setup for jest tests
krfong916 Dec 2, 2021
5e7e8b9
implement final tag editor tests
krfong916 Dec 2, 2021
d13e762
WIP: cursor position and highlighting selected items, implement creat…
krfong916 Dec 3, 2021
bad0890
fix keydown navigation for usemultislection, when we can focus a mult…
krfong916 Dec 6, 2021
9c6a1e9
WIP: question styling, implemented error-handling
krfong916 Jan 7, 2022
5434c43
implement focus for editor, button spacing and color, remove extran c…
krfong916 Jan 8, 2022
ff867ef
add link and editor bindings, refactor focus management for q compone…
krfong916 Jan 12, 2022
4e55f63
WIP: layout and styling, todo: spacing on large screens
krfong916 Jan 12, 2022
7d0d0b3
WIP: error handling, implement review and spacing
krfong916 Jan 13, 2022
f72f4ca
implement error handling, validate on change
krfong916 Jan 18, 2022
b9f7697
implement a11y for form and base tests, TODO: network error message
krfong916 Jan 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions proof-of-concepts/features/stories/combobox/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ export namespace BL {
export type ItemsList = Record<string, any>;

// Prop Getters

export interface ComboboxInputProps {
ariaAutoComplete?: ComboboxAriaAutoComplete;
}

export interface ComboboxLabelGetterProps {
id?: string;
inputId?: string;
}

export interface ComboboxInputGetterProps {
export interface ComboboxInputGetterProps<T> {
controlDispatch?: (...args: any[]) => any;
ref?: React.MutableRefObject<T>;
onFocus?: (...args: any[]) => any;
onBlur?: (...args: any[]) => any;
}

export interface ComboboxGetterProps {
Expand Down
25 changes: 15 additions & 10 deletions proof-of-concepts/features/stories/combobox/hooks/useCombobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {}) {
Expand Down Expand Up @@ -92,7 +93,7 @@ export function useCombobox<Item>(props: BL.ComboboxProps<Item> = {}) {
*
*/
React.useEffect(() => {
if (inputRef.current && isOpen) {
if (inputRef.current && (isOpen || props.initialIsOpen)) {
Copy link
Owner Author

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

inputRef.current.focus();
}
}, [isOpen]);
Expand Down Expand Up @@ -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) {
Expand All @@ -241,22 +246,21 @@ export function useCombobox<Item>(props: BL.ComboboxProps<Item> = {}) {

const inputBlurHandler = (e: React.FocusEvent<HTMLInputElement>) => {
e.preventDefault();
Copy link
Owner Author

Choose a reason for hiding this comment

The 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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
border-radius: 6px;
}

.tag-search-container:focus {
.tag-search-container--focused {
border: 1px solid #0978d9;
box-shadow: 0px 0px 0px 2px rgba(9, 120, 217, 0.3);
outline: none;
Expand Down Expand Up @@ -79,7 +79,7 @@

.tag-result {
padding: spacing.$Size-1;
margin: 0;
margin: spacing.$Size-0;
border-radius: 4px;
display: inline-flex;
flex-direction: column;
Expand All @@ -91,7 +91,7 @@
}
.tag-result--focused {
outline: none;
box-shadow: 0px 0px 0px 2px rgba(9, 120, 217, 0.3);
box-shadow: 0px 0px 0px 2px rgba(9, 120, 217, 1);
background-color: colors.$Gray-1;
}

Expand Down
41 changes: 29 additions & 12 deletions proof-of-concepts/features/stories/tags/TagEditor/TagEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ export const TagEditor = () => {
const [selectedTags, setSelectedTags] = React.useState<BottomlineTags>({});
const [tagSuggestions, setTagSuggestions] = React.useState<
BottomlineTag[] | undefined
>(undefined);
>();

// we define state and change handler callbacks instead of a ref because we don't need to handle
// we need the "appearance" of focus handling for the container when the input element is focused
const [inputFocused, setInputFocused] = React.useState(false);
const inputOnFocus = () => setInputFocused(true);
const inputOnBlur = () => setInputFocused(false);
let derivedLoaderState = false;

const debounce = useDebouncedCallback(
Expand All @@ -66,16 +72,10 @@ export const TagEditor = () => {
run(fetchTags(input));
}, [input, run]);

if (status === UseAsyncStatus.PENDING) {
console.log('[TAG_EDITOR_PENDING]');
derivedLoaderState = true;
}
if (status === UseAsyncStatus.PENDING) derivedLoaderState = true;

React.useEffect(() => {
if (tags) {
console.log('Use Effect, Setting Tags');
setTagSuggestions(tags);
}
if (tags) setTagSuggestions(tags);
}, [tags]);

function stateReducer(
Expand Down Expand Up @@ -123,6 +123,10 @@ export const TagEditor = () => {
initialIsOpen: tagSuggestions ? true : false
});

// place our own ref on the input
// we use a useeffect to detect when the input ref is focused
// when the input ref is focused, then we focus the div

return (
<section className="tag-editor-section">
<div className="tag-editor">
Expand All @@ -149,9 +153,20 @@ export const TagEditor = () => {
) : null}
</div>
{/*{duplicateTagAlert ? <p role="alert">{duplicateTagAlert}</p> : null}*/}
<div className="tag-search-container" {...getComboboxProps()}>
<div
className={
inputFocused
? 'tag-search-container tag-search-container--focused'
: 'tag-search-container'
}
{...getComboboxProps()}
>
<input
{...getInputProps({ controlDispatch: debounce })}
{...getInputProps<HTMLInputElement | undefined>({
controlDispatch: debounce,
onFocus: inputOnFocus,
onBlur: inputOnBlur
})}
type="text"
autoComplete="off"
className="tag-search-input"
Expand All @@ -169,7 +184,9 @@ export const TagEditor = () => {
{tagSuggestions.map((tag, index: number) => (
<li
className={
highlightedIndex === index ? 'tag-result--focused' : 'tag-result'
highlightedIndex === index
? 'tag-result tag-result--focused'
: 'tag-result'
}
{...getItemProps(index)}
>
Expand Down
30 changes: 30 additions & 0 deletions proof-of-concepts/features/stories/utils.ts
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;
});
};
}
Copy link
Owner Author

Choose a reason for hiding this comment

The 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.
This function is important because it allows us to compose multiple components together.
For instance the combox and multiple selection and component that implements both hooks needs a ref to the textbox. This function allows us to compose all hooks/components together.


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
Copy link
Owner Author

Choose a reason for hiding this comment

The 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()}