@rovula/ui 0.0.12 → 0.0.13
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/dist/cjs/bundle.js +3 -3
- package/dist/cjs/bundle.js.map +1 -1
- package/dist/components/Dropdown/Dropdown.js +39 -2
- package/dist/components/Dropdown/Dropdown.stories.js +29 -4
- package/dist/esm/bundle.js +1 -1
- package/dist/esm/bundle.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Dropdown/Dropdown.stories.tsx +25 -3
- package/src/components/Dropdown/Dropdown.tsx +61 -2
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useRef } from "react";
|
|
1
|
+
import React, { useRef, useState } from "react";
|
|
2
2
|
import type { Meta, StoryObj } from "@storybook/react";
|
|
3
3
|
import Dropdown, { Options } from "./Dropdown";
|
|
4
4
|
import Button from "../Button/Button";
|
|
@@ -53,15 +53,37 @@ export const Default = {
|
|
|
53
53
|
const DropdownWithRef = (props: any) => {
|
|
54
54
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
55
55
|
|
|
56
|
+
const [options, setOptions] = useState(customOptions);
|
|
57
|
+
const [value, setValue] = useState<Options>({
|
|
58
|
+
label: "",
|
|
59
|
+
value: "",
|
|
60
|
+
});
|
|
61
|
+
const [text, setText] = useState("");
|
|
62
|
+
|
|
56
63
|
return (
|
|
57
64
|
<Dropdown
|
|
58
65
|
id="1"
|
|
59
66
|
size="lg"
|
|
60
67
|
{...props}
|
|
68
|
+
value={value}
|
|
69
|
+
options={options}
|
|
61
70
|
ref={inputRef}
|
|
62
71
|
labelClassName="peer-focus:bg-red-500"
|
|
72
|
+
onSelect={setValue}
|
|
73
|
+
onChangeText={(e) => setText(e.target.value)}
|
|
63
74
|
onKeyDown={(e) => {
|
|
64
75
|
if (e.code === "Enter") {
|
|
76
|
+
setOptions((options) => [
|
|
77
|
+
...options,
|
|
78
|
+
{
|
|
79
|
+
label: text,
|
|
80
|
+
value: text,
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
setValue({
|
|
84
|
+
label: text,
|
|
85
|
+
value: text,
|
|
86
|
+
});
|
|
65
87
|
inputRef.current?.blur?.();
|
|
66
88
|
}
|
|
67
89
|
}}
|
|
@@ -83,7 +105,7 @@ export const WithRef = {
|
|
|
83
105
|
};
|
|
84
106
|
return (
|
|
85
107
|
<div className="flex flex-row gap-4 w-full">
|
|
86
|
-
<DropdownWithRef id="1" size="lg" options={options} {...args}
|
|
108
|
+
<DropdownWithRef id="1" size="lg" options={options} {...args} />
|
|
87
109
|
</div>
|
|
88
110
|
);
|
|
89
111
|
},
|
|
@@ -126,7 +148,7 @@ export const CustomOption = {
|
|
|
126
148
|
};
|
|
127
149
|
return (
|
|
128
150
|
<div className="flex flex-row gap-4 w-full">
|
|
129
|
-
<DropdownWithRef id="1" size="lg" options={options} {...args}
|
|
151
|
+
<DropdownWithRef id="1" size="lg" options={options} {...args} />
|
|
130
152
|
</div>
|
|
131
153
|
);
|
|
132
154
|
},
|
|
@@ -5,6 +5,7 @@ import React, {
|
|
|
5
5
|
useCallback,
|
|
6
6
|
useEffect,
|
|
7
7
|
useMemo,
|
|
8
|
+
useRef,
|
|
8
9
|
useState,
|
|
9
10
|
} from "react";
|
|
10
11
|
|
|
@@ -80,6 +81,7 @@ const Dropdown = forwardRef<HTMLInputElement, DropdownProps>(
|
|
|
80
81
|
Options | null | undefined
|
|
81
82
|
>(null);
|
|
82
83
|
const [textValue, setTextValue] = useState("");
|
|
84
|
+
const keyCode = useRef("");
|
|
83
85
|
|
|
84
86
|
useEffect(() => {
|
|
85
87
|
if (value && !selectedOption) {
|
|
@@ -147,6 +149,62 @@ const Dropdown = forwardRef<HTMLInputElement, DropdownProps>(
|
|
|
147
149
|
</ul>
|
|
148
150
|
);
|
|
149
151
|
|
|
152
|
+
const handleOnFocus = useCallback(
|
|
153
|
+
(e: React.FocusEvent<HTMLInputElement, Element>) => {
|
|
154
|
+
setIsFocused(true);
|
|
155
|
+
props?.onFocus?.(e);
|
|
156
|
+
},
|
|
157
|
+
[props?.onFocus]
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const clearMismatchValue = useCallback(
|
|
161
|
+
(e: React.FocusEvent<HTMLInputElement, Element>) => {
|
|
162
|
+
const matchSelectedValue = optionsFiltered.find(
|
|
163
|
+
(opt) =>
|
|
164
|
+
opt.value === e.target?.value || opt.label === e.target?.value
|
|
165
|
+
);
|
|
166
|
+
const isMatchSelectedValue = !!matchSelectedValue;
|
|
167
|
+
|
|
168
|
+
let option = matchSelectedValue || {
|
|
169
|
+
value: "",
|
|
170
|
+
label: "",
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
if (!isMatchSelectedValue && textValue) {
|
|
174
|
+
option = {
|
|
175
|
+
value: "",
|
|
176
|
+
label: "",
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (keyCode.current === "Enter") {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
setSelectedOption(option);
|
|
185
|
+
setTextValue(option.label);
|
|
186
|
+
onSelect?.(option);
|
|
187
|
+
},
|
|
188
|
+
[optionsFiltered, textValue]
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const handleOnBlur = useCallback(
|
|
192
|
+
(e: React.FocusEvent<HTMLInputElement, Element>) => {
|
|
193
|
+
setIsFocused(false);
|
|
194
|
+
clearMismatchValue(e);
|
|
195
|
+
props?.onBlur?.(e);
|
|
196
|
+
},
|
|
197
|
+
[props?.onBlur]
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
const handleOnKeyDown = useCallback(
|
|
201
|
+
(e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
202
|
+
keyCode.current = e.code;
|
|
203
|
+
props?.onKeyDown?.(e);
|
|
204
|
+
},
|
|
205
|
+
[props?.onKeyDown]
|
|
206
|
+
);
|
|
207
|
+
|
|
150
208
|
return (
|
|
151
209
|
<div className={`relative ${fullwidth ? "w-full" : ""}`}>
|
|
152
210
|
<TextInput
|
|
@@ -170,8 +228,9 @@ const Dropdown = forwardRef<HTMLInputElement, DropdownProps>(
|
|
|
170
228
|
hasClearIcon={false}
|
|
171
229
|
size={size}
|
|
172
230
|
className={customInputVariant({ size })}
|
|
173
|
-
onFocus={
|
|
174
|
-
onBlur={
|
|
231
|
+
onFocus={handleOnFocus}
|
|
232
|
+
onBlur={handleOnBlur}
|
|
233
|
+
onKeyDown={handleOnKeyDown}
|
|
175
234
|
endIcon={
|
|
176
235
|
<div className={iconWrapperVariant({ size })}>
|
|
177
236
|
<ChevronDownIcon
|