Skip to content

Commit

Permalink
fix: respect original value type in radio and select fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhisperer authored Dec 13, 2024
1 parent 70b3204 commit 00ccd1d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const DefaultField = ({
className={getClassName("input")}
autoComplete="off"
type={field.type}
title={label || name}
name={name}
value={typeof value === "undefined" ? "" : value.toString()}
onChange={(e) => {
Expand Down
15 changes: 4 additions & 11 deletions packages/core/components/AutoField/fields/RadioField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import getClassNameFactory from "../../../../lib/get-class-name-factory";
import styles from "../../styles.module.css";
import { CheckCircle } from "lucide-react";
import { FieldPropsInternal } from "../..";
import { safeJsonParse } from "../../../../lib/safe-json-parse";

const getClassName = getClassNameFactory("Input", styles);

Expand Down Expand Up @@ -37,17 +38,9 @@ export const RadioField = ({
className={getClassName("radioInput")}
value={option.value as string | number}
name={name}
onChange={(e) => {
if (
e.currentTarget.value === "true" ||
e.currentTarget.value === "false"
) {
onChange(JSON.parse(e.currentTarget.value));
return;
}

onChange(e.currentTarget.value);
}}
onChange={({ target: { value } }) =>
onChange(safeJsonParse(value) || value)
}
disabled={readOnly}
checked={value === option.value}
/>
Expand Down
16 changes: 5 additions & 11 deletions packages/core/components/AutoField/fields/SelectField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import getClassNameFactory from "../../../../lib/get-class-name-factory";
import styles from "../../styles.module.css";
import { ChevronDown } from "lucide-react";
import { FieldPropsInternal } from "../..";
import { safeJsonParse } from "../../../../lib/safe-json-parse";

const getClassName = getClassNameFactory("Input", styles);

Expand All @@ -27,19 +28,12 @@ export const SelectField = ({
>
<select
id={id}
title={label || name}
className={getClassName("input")}
disabled={readOnly}
onChange={(e) => {
if (
e.currentTarget.value === "true" ||
e.currentTarget.value === "false"
) {
onChange(JSON.parse(e.currentTarget.value));
return;
}

onChange(e.currentTarget.value);
}}
onChange={({ target: { value } }) =>
onChange(safeJsonParse(value) || value)
}
value={value}
>
{field.options.map((option) => (
Expand Down
8 changes: 8 additions & 0 deletions packages/core/lib/safe-json-parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const safeJsonParse = <T>(str: string) => {
try {
const jsonValue: T = JSON.parse(str);
return jsonValue;
} catch {
return str;
}
};

0 comments on commit 00ccd1d

Please sign in to comment.