@workday/canvas-kit-react 10.3.4 → 10.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/collection/lib/useCursorListModel.tsx +86 -90
- package/collection/lib/useListItemAllowChildStrings.ts +3 -3
- package/collection/lib/useListRenderItem.tsx +1 -1
- package/combobox/lib/hooks/useComboboxInput.ts +1 -0
- package/combobox/lib/hooks/useComboboxKeyboardTypeAhead.ts +3 -3
- package/combobox/lib/hooks/useComboboxModel.tsx +1 -2
- package/common/lib/styles/errorRing.ts +2 -2
- package/dist/commonjs/collection/lib/useCursorListModel.d.ts.map +1 -1
- package/dist/commonjs/collection/lib/useListItemAllowChildStrings.d.ts +2 -1
- package/dist/commonjs/collection/lib/useListItemAllowChildStrings.d.ts.map +1 -1
- package/dist/commonjs/collection/lib/useListItemAllowChildStrings.js +1 -1
- package/dist/commonjs/collection/lib/useListRenderItem.js +1 -1
- package/dist/commonjs/combobox/lib/hooks/useComboboxInput.d.ts +1 -0
- package/dist/commonjs/combobox/lib/hooks/useComboboxInput.d.ts.map +1 -1
- package/dist/commonjs/combobox/lib/hooks/useComboboxInput.js +1 -0
- package/dist/commonjs/combobox/lib/hooks/useComboboxKeyboardTypeAhead.js +2 -3
- package/dist/commonjs/combobox/lib/hooks/useComboboxModel.d.ts.map +1 -1
- package/dist/commonjs/combobox/lib/hooks/useComboboxModel.js +1 -2
- package/dist/commonjs/common/lib/styles/errorRing.js +2 -2
- package/dist/commonjs/select/lib/Select.d.ts +2 -1
- package/dist/commonjs/select/lib/Select.d.ts.map +1 -1
- package/dist/commonjs/select/lib/Select.js +4 -2
- package/dist/commonjs/select/lib/hooks/useSelectInput.d.ts +7 -1
- package/dist/commonjs/select/lib/hooks/useSelectInput.d.ts.map +1 -1
- package/dist/commonjs/select/lib/hooks/useSelectInput.js +45 -4
- package/dist/commonjs/select/lib/hooks/useSelectModel.d.ts.map +1 -1
- package/dist/commonjs/select/lib/hooks/useSelectModel.js +1 -2
- package/dist/commonjs/text-input/lib/TextInput.js +3 -3
- package/dist/es6/collection/lib/useCursorListModel.d.ts.map +1 -1
- package/dist/es6/collection/lib/useListItemAllowChildStrings.d.ts +2 -1
- package/dist/es6/collection/lib/useListItemAllowChildStrings.d.ts.map +1 -1
- package/dist/es6/collection/lib/useListItemAllowChildStrings.js +1 -1
- package/dist/es6/collection/lib/useListRenderItem.js +1 -1
- package/dist/es6/combobox/lib/hooks/useComboboxInput.d.ts +1 -0
- package/dist/es6/combobox/lib/hooks/useComboboxInput.d.ts.map +1 -1
- package/dist/es6/combobox/lib/hooks/useComboboxInput.js +1 -0
- package/dist/es6/combobox/lib/hooks/useComboboxKeyboardTypeAhead.js +2 -3
- package/dist/es6/combobox/lib/hooks/useComboboxModel.d.ts.map +1 -1
- package/dist/es6/combobox/lib/hooks/useComboboxModel.js +1 -2
- package/dist/es6/common/lib/styles/errorRing.js +2 -2
- package/dist/es6/select/lib/Select.d.ts +2 -1
- package/dist/es6/select/lib/Select.d.ts.map +1 -1
- package/dist/es6/select/lib/Select.js +4 -2
- package/dist/es6/select/lib/hooks/useSelectInput.d.ts +7 -1
- package/dist/es6/select/lib/hooks/useSelectInput.d.ts.map +1 -1
- package/dist/es6/select/lib/hooks/useSelectInput.js +43 -5
- package/dist/es6/select/lib/hooks/useSelectModel.d.ts.map +1 -1
- package/dist/es6/select/lib/hooks/useSelectModel.js +1 -2
- package/dist/es6/text-input/lib/TextInput.js +3 -3
- package/package.json +4 -4
- package/select/lib/Select.tsx +50 -4
- package/select/lib/hooks/useSelectInput.ts +86 -44
- package/select/lib/hooks/useSelectModel.tsx +1 -2
- package/text-input/lib/TextInput.tsx +3 -3
|
@@ -2,9 +2,9 @@ import React from 'react';
|
|
|
2
2
|
import {
|
|
3
3
|
composeHooks,
|
|
4
4
|
createElemPropsHook,
|
|
5
|
-
useForkRef,
|
|
6
5
|
useLocalRef,
|
|
7
6
|
useResizeObserver,
|
|
7
|
+
dispatchInputEvent,
|
|
8
8
|
} from '@workday/canvas-kit-react/common';
|
|
9
9
|
import {
|
|
10
10
|
useComboboxInput,
|
|
@@ -18,56 +18,98 @@ import {useSelectModel} from './useSelectModel';
|
|
|
18
18
|
* `useSelectInput` extends {@link useComboboxInput useComboboxInput} and {@link useComboboxKeyboardTypeAhead useComboboxKeyboardTypeAhead} and adds type ahead functionality and Select-specific [keyboard support](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/).
|
|
19
19
|
*/
|
|
20
20
|
export const useSelectInput = composeHooks(
|
|
21
|
-
createElemPropsHook(useSelectModel)(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
createElemPropsHook(useSelectModel)(
|
|
22
|
+
(model, ref, elemProps: {keySofar?: string; placeholder?: string} = {}) => {
|
|
23
|
+
const {elementRef} = useLocalRef<HTMLInputElement>(ref as any);
|
|
24
|
+
const textInputRef = React.useRef<HTMLInputElement>(null);
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
// Update the text value of the input
|
|
27
|
+
const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
28
|
+
const value = model.navigation.getItem(event.target.value, model)?.textValue;
|
|
29
|
+
const nativeInputValue = Object.getOwnPropertyDescriptor(
|
|
30
|
+
Object.getPrototypeOf(textInputRef.current),
|
|
31
|
+
'value'
|
|
32
|
+
);
|
|
33
|
+
if (nativeInputValue && nativeInputValue.set) {
|
|
34
|
+
nativeInputValue.set.call(textInputRef.current, value);
|
|
33
35
|
}
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
+
};
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
// if the menu is visible
|
|
38
|
+
useResizeObserver({
|
|
39
|
+
ref: textInputRef,
|
|
40
|
+
onResize: data => {
|
|
41
|
+
if (model.state.visibility === 'visible') {
|
|
42
|
+
// Width of the Input + 2px border + 8px padding
|
|
43
|
+
const calculatedWidth = data.width ? data.width + 42 + 8 : 0;
|
|
44
|
+
model.events.setWidth(calculatedWidth);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
// The intent is if items are loaded after the component is rendered, it will update the input with the value.
|
|
49
|
+
// **Note: We might need to watch for other things or how often we should do this**
|
|
50
|
+
React.useEffect(() => {
|
|
52
51
|
if (
|
|
53
|
-
|
|
54
|
-
model.state.
|
|
52
|
+
model.state.inputRef.current &&
|
|
53
|
+
model.state.items.length > 0 &&
|
|
54
|
+
model.state.selectedIds[0]
|
|
55
55
|
) {
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
});
|
|
61
|
-
model.events.hide();
|
|
56
|
+
const value = model.navigation.getItem(model.state.selectedIds[0], model).id;
|
|
57
|
+
if (model.state.inputRef.current.value !== value) {
|
|
58
|
+
// Programmatically dispatch an onChange once items are loaded. This account for when a consumer wants an initial selected item and they're loading them from a server.
|
|
59
|
+
dispatchInputEvent(model.state.inputRef.current, value);
|
|
62
60
|
}
|
|
63
61
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
62
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
63
|
+
}, [model.state.inputRef, model.state.items.length]);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
onKeyDown(event: React.KeyboardEvent) {
|
|
67
|
+
// Prevent the keys from being enter in the input
|
|
68
|
+
if (event.key !== 'Tab') {
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Select should open if Spacebar is typed and nothing has been typed AND the menu is hidden
|
|
73
|
+
if (
|
|
74
|
+
event.key === 'Spacebar' ||
|
|
75
|
+
(event.key === ' ' && model.state.visibility === 'hidden' && elemProps?.keySofar === '')
|
|
76
|
+
) {
|
|
77
|
+
model.events.show();
|
|
78
|
+
}
|
|
79
|
+
// if the menu is visible
|
|
80
|
+
if (
|
|
81
|
+
(event.key === 'Spacebar' || event.key === ' ') &&
|
|
82
|
+
model.state.visibility === 'visible'
|
|
83
|
+
) {
|
|
84
|
+
// If key so far is empty, they're done typing, select the item where the cursor is located and hide the menu
|
|
85
|
+
if (elemProps?.keySofar === '') {
|
|
86
|
+
model.events.select({
|
|
87
|
+
id: model.state.cursorId,
|
|
88
|
+
});
|
|
89
|
+
model.events.hide();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
onChange: handleOnChange,
|
|
94
|
+
autoComplete: 'off',
|
|
95
|
+
// When the hidden input is focused, we want to show the focus/hover states of the input that sits below it.
|
|
96
|
+
onFocus() {
|
|
97
|
+
textInputRef.current?.focus();
|
|
98
|
+
},
|
|
99
|
+
textInputProps: {
|
|
100
|
+
ref: textInputRef,
|
|
101
|
+
},
|
|
102
|
+
ref: elementRef,
|
|
103
|
+
|
|
104
|
+
// Account for the case where an initial item is selected when the Select first renders
|
|
105
|
+
defaultValue:
|
|
106
|
+
model.state.selectedIds.length > 0 && model.state.items.length > 0
|
|
107
|
+
? model.navigation.getItem(model.state.selectedIds[0], model).textValue ||
|
|
108
|
+
model.state.value
|
|
109
|
+
: undefined,
|
|
110
|
+
} as const;
|
|
111
|
+
}
|
|
112
|
+
),
|
|
71
113
|
useComboboxKeyboardTypeAhead,
|
|
72
114
|
useComboboxResetCursorToSelected,
|
|
73
115
|
useComboboxMoveCursorToSelected,
|
|
@@ -38,10 +38,10 @@ const StyledTextInput = styled('input')<
|
|
|
38
38
|
'&::placeholder': {
|
|
39
39
|
color: inputColors.placeholder,
|
|
40
40
|
},
|
|
41
|
-
'&:hover': {
|
|
41
|
+
'&:hover, &.hover': {
|
|
42
42
|
borderColor: inputColors.hoverBorder,
|
|
43
43
|
},
|
|
44
|
-
'&:focus:not([disabled])': {
|
|
44
|
+
'&:focus-visible:not([disabled]), &.focus:not([disabled]), &:focus:not([disabled])': {
|
|
45
45
|
borderColor: inputColors.focusBorder,
|
|
46
46
|
boxShadow: `inset 0 0 0 1px ${inputColors.focusBorder}`,
|
|
47
47
|
outline: 'none',
|
|
@@ -68,7 +68,7 @@ const StyledTextInput = styled('input')<
|
|
|
68
68
|
},
|
|
69
69
|
({theme, error}) => {
|
|
70
70
|
return {
|
|
71
|
-
'&:focus:not([disabled])': {
|
|
71
|
+
'&:focus-visible:not([disabled]), &.focus:not([disabled])': {
|
|
72
72
|
borderColor: theme.canvas.palette.common.focusOutline,
|
|
73
73
|
boxShadow: `inset 0 0 0 1px ${theme.canvas.palette.common.focusOutline}`,
|
|
74
74
|
outline: 'none',
|