Skip to content

Commit

Permalink
fix: fix a bug with diameter input on edit planet
Browse files Browse the repository at this point in the history
  • Loading branch information
alevidals committed Apr 8, 2024
1 parent ff2345e commit 33fea1a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
11 changes: 3 additions & 8 deletions src/components/planets/edit-planet-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function EditPlanetForm(props: Props) {
resolver: zodResolver(updatePlanetSchema),
defaultValues: {
name: planet.name,
diameter: planet.diameter,
diameter: String(planet.diameter),
climates: planet.climates,
terrains: planet.terrains,
residents: planet.residents,
Expand Down Expand Up @@ -68,6 +68,7 @@ export function EditPlanetForm(props: Props) {
const newPlanet: Planet = {
...planet,
...data,
diameter: Number(data.diameter),
residents: data.residents.map((resident) => ({
id: resident.id || Math.random().toString(),
name: resident.name,
Expand Down Expand Up @@ -114,13 +115,7 @@ export function EditPlanetForm(props: Props) {
<FormItem>
<FormLabel>Diameter</FormLabel>
<FormControl>
<Input
type="number"
{...field}
onChange={(event) =>
field.onChange(Number(event.target.value))
}
/>
<Input type="number" {...field} />
</FormControl>
<FormMessage />
</FormItem>
Expand Down
11 changes: 8 additions & 3 deletions src/lib/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ export const updatePlanetSchema = z.object({
name: z.string().min(1, {
message: "Name is required",
}),
diameter: z.number().min(1, {
message: "Diameter is required",
}),
diameter: z
.string()
.min(1, {
message: "Diameter is required",
})
.refine((value) => !Number.isNaN(value) && Number(value) > 0, {
message: "Diameter must be a number greater than 0",
}),
climates: z
.array(
z.object({
Expand Down

0 comments on commit 33fea1a

Please sign in to comment.