-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathuseDownshiftSingleSelectProps.ts
More file actions
129 lines (117 loc) · 4.88 KB
/
Copy pathuseDownshiftSingleSelectProps.ts
File metadata and controls
129 lines (117 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
UseComboboxProps,
UseComboboxReturnValue,
UseComboboxState,
UseComboboxStateChangeOptions,
UseComboboxStateChange,
useCombobox
} from "downshift";
import { useCallback, useMemo } from "react";
import { A11yStatusMessage, SingleSelector } from "../helpers/types";
interface Options {
inputId?: string;
labelId?: string;
}
export function useDownshiftSingleSelectProps(
selector: SingleSelector,
options: Options = {},
a11yStatusMessage: A11yStatusMessage
): UseComboboxReturnValue<string> {
const { inputId, labelId } = options;
const downshiftProps: UseComboboxProps<string> = useMemo(() => {
return {
items: [],
itemToString: (v: string | null) => selector.caption.get(v),
onSelectedItemChange({ selectedItem }: UseComboboxStateChange<string>) {
selector.setValue(selectedItem ?? null);
},
onInputValueChange({ inputValue, type }: UseComboboxStateChange<string>) {
selector.options.setSearchTerm(inputValue!);
if (selector.onFilterInputChange && type === useCombobox.stateChangeTypes.InputChange) {
selector.onFilterInputChange(inputValue!);
}
},
getA11yStatusMessage(options) {
const selectedItem = selector.caption.get(selector.currentId);
let message = selectedItem
? selector.currentId
? `${a11yStatusMessage.a11ySelectedValue} ${selectedItem}. `
: "No options selected."
: "";
if (!options.isOpen) {
return message;
}
if (!options.resultCount) {
return a11yStatusMessage.a11yNoOption;
}
if (options.resultCount > 0) {
message += `${a11yStatusMessage.a11yOptionsAvailable} ${options.resultCount}. ${a11yStatusMessage.a11yInstructions}`;
} else {
return a11yStatusMessage.a11yNoOption;
}
return message;
},
defaultHighlightedIndex: 0,
selectedItem: null,
initialInputValue: selector.caption.get(selector.currentId),
stateReducer(state: UseComboboxState<string>, actionAndChanges: UseComboboxStateChangeOptions<string>) {
const { changes, type } = actionAndChanges;
switch (type) {
case useCombobox.stateChangeTypes.ToggleButtonClick:
return {
...changes,
inputValue:
state.isOpen && selector.currentId ? selector.caption.get(selector.currentId) : ""
};
case useCombobox.stateChangeTypes.ControlledPropUpdatedSelectedItem:
return {
...changes,
inputValue:
changes.inputValue === selector.caption.emptyCaption
? ""
: selector.caption.get(selector.currentId)
};
case useCombobox.stateChangeTypes.InputFocus:
return {
...changes,
isOpen: state.isOpen,
inputValue: "",
highlightedIndex: changes.selectedItem ? -1 : this.defaultHighlightedIndex
};
case useCombobox.stateChangeTypes.InputKeyDownEscape:
case useCombobox.stateChangeTypes.FunctionCloseMenu:
return {
...changes,
isOpen: false,
inputValue:
changes.selectedItem || selector.currentId
? selector.caption.get(selector.currentId)
: ""
};
case useCombobox.stateChangeTypes.InputBlur:
return state;
default:
return { ...changes };
}
},
inputId,
labelId
};
}, [
selector,
inputId,
labelId,
a11yStatusMessage.a11ySelectedValue,
a11yStatusMessage.a11yOptionsAvailable,
a11yStatusMessage.a11yNoOption,
a11yStatusMessage.a11yInstructions
]);
const returnVal = useCombobox({
...downshiftProps,
items: selector.options.getAll() ?? [],
selectedItem: selector.currentId
});
const { closeMenu } = returnVal;
selector.onLeaveEvent = useCallback(closeMenu, [closeMenu]);
return returnVal;
}