@xqmsg/ui-core 0.16.0 → 0.16.1

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.16.0",
2
+ "version": "0.16.1",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -5,7 +5,6 @@ import { Input, InputProps } from '.';
5
5
  import { useFormHandler } from '../form/hooks/useFormHandler';
6
6
  import * as Yup from 'yup';
7
7
  import { Form } from '../form';
8
- import { Box } from '@chakra-ui/react';
9
8
 
10
9
  const meta: Meta<InputProps<StoryFormSchema>> = {
11
10
  title: 'Input example',
@@ -1,5 +1,12 @@
1
- import React, { useEffect, useRef, useState } from 'react';
1
+ import React, {
2
+ KeyboardEventHandler,
3
+ useEffect,
4
+ useMemo,
5
+ useRef,
6
+ useState,
7
+ } from 'react';
2
8
  import { Box, Flex, Text, Image, Input } from '@chakra-ui/react';
9
+ import { debounce } from 'lodash';
3
10
  import {
4
11
  FieldOption,
5
12
  FieldOptions,
@@ -44,7 +51,13 @@ const StackedMultiSelect = React.forwardRef<
44
51
  const [localOptions, setLocalOptions] = useState<FieldOptions>(options);
45
52
  const [isFocussed, setIsFocussed] = useState(false);
46
53
  const [shouldSideScroll, setShouldSideScroll] = useState(false);
54
+ const [optionIndex, setOptionIndex] = useState<number | null>(null);
55
+
47
56
  const [position, setPosition] = useState<'top' | 'bottom'>('top');
57
+ const [searchValue, setSearchValue] = useState('');
58
+ const [debouncedSearchValue, setDebouncedSearchValue] = useState('');
59
+
60
+ console.log({ searchValue, debouncedSearchValue });
48
61
 
49
62
  const boundingClientRect = dropdownRef.current?.getBoundingClientRect() as DOMRect;
50
63
 
@@ -122,27 +135,105 @@ const StackedMultiSelect = React.forwardRef<
122
135
  );
123
136
  };
124
137
 
125
- return (
126
- <Box
127
- ref={dropdownRef}
128
- position="relative"
129
- onKeyDown={e => {
130
- if (isFocussed) {
131
- if (e.key === 'Tab') {
132
- return setIsFocussed(false);
133
- }
138
+ const handleOnKeyDown: KeyboardEventHandler<HTMLInputElement> = e => {
139
+ const initialOptionIndex = options[0].value === 'section_header' ? 1 : 0;
140
+
141
+ if (
142
+ !isFocussed &&
143
+ (e.key === 'Enter' || e.key === 'ArrowUp' || e.key === 'ArrowDown')
144
+ ) {
145
+ setIsFocussed(true);
146
+ return setOptionIndex(initialOptionIndex);
147
+ }
148
+
149
+ if (isFocussed) {
150
+ if (
151
+ optionIndex === null &&
152
+ (e.key === 'Enter' || e.key === 'ArrowUp' || e.key === 'ArrowDown')
153
+ ) {
154
+ return setOptionIndex(initialOptionIndex);
155
+ }
156
+
157
+ if (e.key === 'ArrowUp' && optionIndex !== null && optionIndex > 0) {
158
+ const incrementValue =
159
+ localOptions[optionIndex - 1] &&
160
+ localOptions[optionIndex - 1].value === 'section_header'
161
+ ? 2
162
+ : 1;
163
+ setOptionIndex(optionIndex - incrementValue);
164
+
165
+ return dropdownMenuRef.current?.scrollTo({
166
+ top: optionIndex * 24,
167
+ behavior: 'smooth',
168
+ });
169
+ }
170
+
171
+ if (
172
+ e.key === 'ArrowDown' &&
173
+ optionIndex !== null &&
174
+ optionIndex < localOptions.length
175
+ ) {
176
+ const incrementValue =
177
+ localOptions[optionIndex + 1] &&
178
+ localOptions[optionIndex + 1].value === 'section_header'
179
+ ? 2
180
+ : 1;
181
+ setOptionIndex(optionIndex + incrementValue);
182
+
183
+ return dropdownMenuRef.current?.scrollTo({
184
+ top: optionIndex * 24,
185
+ behavior: 'smooth',
186
+ });
187
+ }
188
+
189
+ if (e.key === 'Enter' && optionIndex !== null) {
190
+ const option = localOptions.find((_, idx) => optionIndex === idx);
191
+ if (!option) return;
134
192
 
135
- const idx = options.findIndex(
136
- option => option.label[0].toLocaleLowerCase() === e.key
137
- );
138
-
139
- dropdownMenuRef.current?.scrollTo({
140
- top: idx * 27,
141
- behavior: 'smooth',
142
- });
143
- }
144
- }}
145
- >
193
+ handleChange(option);
194
+
195
+ return setIsFocussed(false);
196
+ }
197
+
198
+ if (e.key === 'Tab') {
199
+ return setIsFocussed(false);
200
+ }
201
+
202
+ return update(debouncedSearchValue.concat(e.key));
203
+ }
204
+ };
205
+
206
+ useEffect(() => {
207
+ if (searchValue.length) {
208
+ const idx = options.findIndex(
209
+ option =>
210
+ option.label.substring(0, searchValue.length).toLowerCase() ===
211
+ searchValue.toLowerCase()
212
+ );
213
+
214
+ dropdownMenuRef.current?.scrollTo({
215
+ top: idx * 24,
216
+ behavior: 'smooth',
217
+ });
218
+
219
+ setSearchValue('');
220
+ setDebouncedSearchValue('');
221
+ }
222
+ }, [options, searchValue]);
223
+
224
+ const updateSearchValue = useMemo(() => {
225
+ return debounce(val => {
226
+ setSearchValue(val);
227
+ }, 1000);
228
+ }, []);
229
+
230
+ const update = (value: string) => {
231
+ updateSearchValue(value);
232
+ setDebouncedSearchValue(value);
233
+ };
234
+
235
+ return (
236
+ <Box ref={dropdownRef} position="relative" onKeyDown={handleOnKeyDown}>
146
237
  <Flex
147
238
  fontSize="13px"
148
239
  h="26px"
@@ -218,6 +309,7 @@ const StackedMultiSelect = React.forwardRef<
218
309
  onSelectItem={option => handleChange(option)}
219
310
  options={localOptions}
220
311
  position={position}
312
+ optionIndex={optionIndex}
221
313
  />
222
314
  )}
223
315
  </Box>
@@ -1,4 +1,10 @@
1
- import React, { useEffect, useRef, useState } from 'react';
1
+ import React, {
2
+ KeyboardEventHandler,
3
+ useEffect,
4
+ useRef,
5
+ useMemo,
6
+ useState,
7
+ } from 'react';
2
8
  import {
3
9
  Box,
4
10
  Image,
@@ -14,6 +20,7 @@ import SubtractIcon from './assets/svg/subtract.svg';
14
20
  import { Dropdown } from '../components/dropdown';
15
21
  import useDidMountEffect from '../../../hooks/useDidMountEffect';
16
22
  import { useOnClickOutside } from '../../../hooks/useOnOutsideClick';
23
+ import { debounce } from 'lodash';
17
24
 
18
25
  export interface StackedSelectProps extends StackedInputProps {
19
26
  options: FieldOptions;
@@ -46,7 +53,10 @@ const StackedSelect = React.forwardRef<HTMLInputElement, StackedSelectProps>(
46
53
  const [selectedOption, setSelectedOption] = useState(
47
54
  options.find(option => option.value === value)?.label ?? ''
48
55
  );
56
+ const [optionIndex, setOptionIndex] = useState<number | null>(null);
49
57
  const [position, setPosition] = useState<'top' | 'bottom'>('top');
58
+ const [searchValue, setSearchValue] = useState('');
59
+ const [debouncedSearchValue, setDebouncedSearchValue] = useState('');
50
60
 
51
61
  const boundingClientRect = dropdownRef.current?.getBoundingClientRect() as DOMRect;
52
62
 
@@ -81,6 +91,109 @@ const StackedSelect = React.forwardRef<HTMLInputElement, StackedSelectProps>(
81
91
  setIsFocussed(false);
82
92
  };
83
93
 
94
+ const handleOnKeyDown: KeyboardEventHandler<HTMLInputElement> = e => {
95
+ const initialOptionIndex = options[0].value === 'section_header' ? 1 : 0;
96
+
97
+ if (
98
+ !isFocussed &&
99
+ (e.key === 'Enter' || e.key === 'ArrowUp' || e.key === 'ArrowDown')
100
+ ) {
101
+ setIsFocussed(true);
102
+ return setOptionIndex(initialOptionIndex);
103
+ }
104
+
105
+ if (isFocussed) {
106
+ if (
107
+ optionIndex === null &&
108
+ (e.key === 'Enter' || e.key === 'ArrowUp' || e.key === 'ArrowDown')
109
+ ) {
110
+ return setOptionIndex(initialOptionIndex);
111
+ }
112
+
113
+ if (e.key === 'ArrowUp' && optionIndex !== null && optionIndex > 0) {
114
+ const incrementValue =
115
+ options[optionIndex - 1] &&
116
+ options[optionIndex - 1].value === 'section_header'
117
+ ? 2
118
+ : 1;
119
+ setOptionIndex(optionIndex - incrementValue);
120
+
121
+ return dropdownMenuRef.current?.scrollTo({
122
+ top: optionIndex * 24,
123
+ behavior: 'smooth',
124
+ });
125
+ }
126
+
127
+ if (
128
+ e.key === 'ArrowDown' &&
129
+ optionIndex !== null &&
130
+ optionIndex < options.length
131
+ ) {
132
+ const incrementValue =
133
+ options[optionIndex + 1] &&
134
+ options[optionIndex + 1].value === 'section_header'
135
+ ? 2
136
+ : 1;
137
+ setOptionIndex(optionIndex + incrementValue);
138
+
139
+ return dropdownMenuRef.current?.scrollTo({
140
+ top: optionIndex * 24,
141
+ behavior: 'smooth',
142
+ });
143
+ }
144
+
145
+ if (e.key === 'Enter' && optionIndex !== null) {
146
+ const option = options.find((_, idx) => optionIndex === idx);
147
+ if (!option) return;
148
+
149
+ if (handleOnChange) {
150
+ handleOnChange(option.value);
151
+ }
152
+
153
+ setSelectedOption(option?.label);
154
+ setValue(name as string, option.value, {
155
+ shouldDirty: true,
156
+ shouldValidate: true,
157
+ });
158
+
159
+ return setIsFocussed(false);
160
+ }
161
+
162
+ if (e.key === 'Tab') {
163
+ return setIsFocussed(false);
164
+ }
165
+ }
166
+ };
167
+
168
+ useEffect(() => {
169
+ if (searchValue.length) {
170
+ const idx = options.findIndex(
171
+ option =>
172
+ option.label.substring(0, searchValue.length).toLowerCase() ===
173
+ searchValue.toLowerCase()
174
+ );
175
+
176
+ dropdownMenuRef.current?.scrollTo({
177
+ top: idx * 24,
178
+ behavior: 'smooth',
179
+ });
180
+
181
+ setSearchValue('');
182
+ setDebouncedSearchValue('');
183
+ }
184
+ }, [options, searchValue]);
185
+
186
+ const updateSearchValue = useMemo(() => {
187
+ return debounce(val => {
188
+ setSearchValue(val);
189
+ }, 1000);
190
+ }, []);
191
+
192
+ const update = (value: string) => {
193
+ updateSearchValue(value);
194
+ setDebouncedSearchValue(value);
195
+ };
196
+
84
197
  return (
85
198
  <Box ref={dropdownRef} position="relative">
86
199
  <InputGroup>
@@ -96,23 +209,8 @@ const StackedSelect = React.forwardRef<HTMLInputElement, StackedSelectProps>(
96
209
  value={selectedOption}
97
210
  disabled={disabled}
98
211
  autoComplete="off"
99
- onKeyDown={e => {
100
- if (isFocussed) {
101
- if (e.key === 'Tab') {
102
- return setIsFocussed(false);
103
- }
104
-
105
- const idx = options.findIndex(
106
- option => option.label[0].toLocaleLowerCase() === e.key
107
- );
108
- console.log(idx);
109
-
110
- dropdownMenuRef.current?.scrollTo({
111
- top: idx * 27,
112
- behavior: 'smooth',
113
- });
114
- }
115
- }}
212
+ onChange={e => update(debouncedSearchValue.concat(e.target.value))}
213
+ onKeyDown={handleOnKeyDown}
116
214
  />
117
215
  <InputRightElement
118
216
  cursor={disabled ? 'not-allowed' : 'pointer'}
@@ -127,6 +225,7 @@ const StackedSelect = React.forwardRef<HTMLInputElement, StackedSelectProps>(
127
225
  dropdownRef={dropdownMenuRef}
128
226
  onSelectItem={option => handleOnSelectItem(option)}
129
227
  options={options}
228
+ optionIndex={optionIndex}
130
229
  />
131
230
  )}
132
231
  </Box>
@@ -8,6 +8,7 @@ export interface DropdownProps {
8
8
  options: FieldOptions;
9
9
  dropdownRef: RefObject<HTMLDivElement>;
10
10
  position: 'top' | 'bottom';
11
+ optionIndex?: number | null;
11
12
  }
12
13
 
13
14
  /**
@@ -18,6 +19,7 @@ export const Dropdown: React.FC<DropdownProps> = ({
18
19
  options,
19
20
  dropdownRef,
20
21
  position,
22
+ optionIndex,
21
23
  }) => {
22
24
  const DropdownContent = useMemo(() => {
23
25
  return options.map((option, idx) => (
@@ -55,22 +57,27 @@ export const Dropdown: React.FC<DropdownProps> = ({
55
57
  px="8px"
56
58
  py="4px"
57
59
  width="100%"
58
- color={colors.label.primary.light}
60
+ color={
61
+ optionIndex === idx
62
+ ? colors.label.primary.dark
63
+ : colors.label.primary.light
64
+ }
59
65
  _hover={{
60
66
  color: colors.label.primary.dark,
61
67
  bg: colors.fill.action,
62
68
  borderRadius: '4px',
63
69
  width: '100%',
64
70
  }}
65
- bg="inherit"
71
+ bg={optionIndex === idx ? colors.fill.action : 'inherit'}
66
72
  whiteSpace="nowrap"
73
+ id={option.value}
67
74
  >
68
75
  {option.label}
69
76
  </Box>
70
77
  )}
71
78
  </>
72
79
  ));
73
- }, [onSelectItem, options]);
80
+ }, [onSelectItem, optionIndex, options]);
74
81
 
75
82
  return (
76
83
  <Flex