Skip to content

Commit 8cecfec

Browse files
committed
feat(new-hope): add default values for combobox react-hook-form
1 parent 0e9acba commit 8cecfec

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

packages/plasma-new-hope/src/components/Combobox/ComboboxNew/Combobox.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { Ul, base, StyledArrow, IconArrowWrapper, StyledEmptyState } from './Com
3535
import type { ItemContext, ComboboxProps } from './Combobox.types';
3636
import { base as viewCSS } from './variations/_view/base';
3737
import { base as sizeCSS } from './variations/_size/base';
38-
import type { ItemOptionTransformed } from './ui/Inner/ui/Item/Item.types';
38+
import type { ItemOption, ItemOptionTransformed } from './ui/Inner/ui/Item/Item.types';
3939
import { SelectNative } from './ui/SelectNative/SelectNative';
4040

4141
export const Context = createContext<ItemContext>({} as ItemContext);
@@ -382,10 +382,12 @@ export const comboboxRoot = (Root: RootProps<HTMLInputElement, Omit<ComboboxProp
382382
>
383383
{name && (
384384
<SelectNative
385+
items={valueToCheckedMap}
385386
name={name}
386387
value={internalValue}
387388
multiple={multiple}
388389
onChange={onChange}
390+
onSetValue={setInternalValue}
389391
ref={ref as ForwardedRef<HTMLInputElement>}
390392
/>
391393
)}

packages/plasma-new-hope/src/components/Combobox/ComboboxNew/ui/SelectNative/SelectNative.tsx

+47-24
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,57 @@ import { useForkRef } from '@salutejs/plasma-core';
33

44
import { createEvent } from '../../../../../utils';
55
import { ComboboxProps } from '../../Combobox.types';
6+
import { ValueToCheckedMapType } from '../../hooks/getPathMaps';
67

78
import { SelectHidden } from './SelectNative.styles';
89

910
type Props = Pick<ComboboxProps, 'name' | 'value' | 'multiple'> & {
1011
onChange: (value: ChangeEvent<HTMLSelectElement> | null) => void;
12+
onSetValue: (value: string) => void;
13+
items: ValueToCheckedMapType;
1114
};
1215

13-
export const SelectNative = forwardRef<HTMLInputElement, Props>(({ name, multiple, value, onChange }, ref) => {
14-
const values = (multiple ? value : [value]) as string[];
15-
const selectRef = useRef<HTMLSelectElement>(null);
16-
const forkRef = useForkRef(selectRef, ref as any);
17-
18-
useEffect(() => {
19-
const event = createEvent(selectRef);
20-
if (onChange) {
21-
onChange(event);
22-
}
23-
}, [values]);
24-
25-
return (
26-
<>
27-
<SelectHidden ref={forkRef} multiple={multiple} name={name} hidden value={multiple ? values : values[0]}>
28-
{values.map((v) => (
29-
<option key={v} value={v}>
30-
{v}
31-
</option>
32-
))}
33-
</SelectHidden>
34-
</>
35-
);
36-
});
16+
export const SelectNative = forwardRef<HTMLInputElement, Props>(
17+
({ name, multiple, items, value, onChange, onSetValue }, ref) => {
18+
const values = (multiple ? value : [value]) as string[];
19+
const selectRef = useRef<HTMLSelectElement>(null);
20+
const forkRef = useForkRef(selectRef, ref as any);
21+
const options = Array.from(items.keys());
22+
23+
useEffect(() => {
24+
const event = createEvent(selectRef);
25+
if (onChange) {
26+
onChange(event);
27+
}
28+
}, [values]);
29+
30+
useEffect(() => {
31+
if (selectRef.current) {
32+
const valueInit = selectRef.current.value;
33+
const item = options.find((v) => v === valueInit);
34+
35+
if (item) {
36+
onSetValue(item);
37+
}
38+
}
39+
}, [selectRef]);
40+
41+
return (
42+
<>
43+
<SelectHidden
44+
ref={forkRef}
45+
multiple={multiple}
46+
name={name}
47+
hidden
48+
value={multiple ? values : values[0]}
49+
>
50+
{options.map((v) => (
51+
<option key={v} value={v}>
52+
{v}
53+
</option>
54+
))}
55+
</SelectHidden>
56+
</>
57+
);
58+
},
59+
);

packages/plasma-new-hope/src/examples/plasma_b2c/components/Combobox/Combobox.stories.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ const SingleStory = (args: StorySelectProps) => {
357357
<div style={{ width: '400px' }}>
358358
<Combobox
359359
{...args}
360-
name="mau"
361360
items={items}
362361
value={value}
363362
onChange={setValue}

0 commit comments

Comments
 (0)