Skip to content

Commit d600eee

Browse files
committed
fix: fix select multiple data bug
1 parent 41c6fb1 commit d600eee

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

.changeset/spicy-walls-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@frigade/react": patch
3+
---
4+
5+
Fixes an issue related to form type select/multiple

apps/smithy/.storybook/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import path from "path";
22

33
// SEE: https://storybook.js.org/docs/react/faq#how-do-i-fix-module-resolution-in-special-environments
4-
const getAbsolutePath = (packageName) =>
4+
import { createRequire } from 'module';
5+
const require = createRequire(import.meta.url);
6+
7+
// Utility to resolve the absolute path of a given package. Storybook requires absolute
8+
// addon paths when the config file is executed in an ESM context (Node ≥ 18 / SB 8).
9+
// We can still rely on Node's `require.resolve` by creating a CommonJS `require`
10+
// function that is bound to this ESM module.
11+
const getAbsolutePath = (packageName: string) =>
512
path.dirname(require.resolve(path.join(packageName, "package.json")));
613

714
/** @type { import('@storybook/react-vite').StorybookConfig } */

packages/react/src/components/Form/fields/SelectMultipleField.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,31 @@ import * as baseStyles from '@/components/Form/fields/BaseField.styles'
1111

1212
export function SelectMultipleField(props: FormFieldProps) {
1313
const {
14-
field: { onChange },
14+
field: { onChange, value },
1515
fieldData: { options = [] },
1616
} = props
1717

18-
const [valueArray, setValueArray] = React.useState<string[]>([])
18+
// Ensure we always work with an array of strings coming from RHF
19+
const valueArray: string[] = Array.isArray(value) ? value : []
1920

20-
function setValueInArray(value: string) {
21-
let updatedValueArray = []
22-
if (valueArray.includes(value)) {
23-
updatedValueArray = [...valueArray.filter((v) => v !== value)]
24-
} else {
25-
updatedValueArray = [...valueArray, value]
26-
}
27-
setValueArray(updatedValueArray)
21+
function toggleValue(optionValue: string) {
22+
const updatedValueArray = valueArray.includes(optionValue)
23+
? valueArray.filter((v) => v !== optionValue)
24+
: [...valueArray, optionValue]
25+
26+
// Let react-hook-form handle the new value
2827
onChange(updatedValueArray)
2928
}
3029

3130
return (
3231
<BaseField {...props}>
3332
{() => (
3433
<Flex.Column gap={2} part="field-select-multiple">
35-
{options.map(({ label, value }) => (
34+
{options.map(({ label, value: optionValue }) => (
3635
<Checkbox.Root
37-
value={valueArray.includes(value) ? value : undefined}
38-
onCheckedChange={() => setValueInArray(value)}
39-
key={value}
36+
key={optionValue}
37+
checked={valueArray.includes(optionValue)}
38+
onCheckedChange={() => toggleValue(optionValue)}
4039
asChild
4140
>
4241
<Box
@@ -61,7 +60,7 @@ export function SelectMultipleField(props: FormFieldProps) {
6160
borderColor="neutral.border"
6261
borderRadius="100%"
6362
flex="0 0 auto"
64-
id={value}
63+
id={optionValue}
6564
padding="0"
6665
part="field-check-value"
6766
position="relative"

0 commit comments

Comments
 (0)