Replies: 2 comments
-
|
Thanks for bringing this up. Stripping the Z suffix does mean we lose explicit timezone information, but it probably makes sense as a default to treat everything as UTC. To support a different timezone, people would need a custom serialize function. The only thing I am still not fully sure about is how best to support cases where the datetime needs to be coerced based on another field value (for example, when the timezone is provided by the user in the same form), but overall I am on board with the suggestion. |
Beta Was this translation helpful? Give feedback.
-
|
Hi, I've been working on handling The schema uses const baseSchema = z.object({
updatedAt: z.date(),
});
const clientSchema = coerceFormValue(baseSchema);
export function parseDate(unknown: Date | string | number) {
return typeof unknown === "string"
? new Date(unknown)
: typeof unknown === "number"
? new Date(unknown * 1000)
: unknown;
}
export function formatDateToDatetimeLocal(unknown: Date | number | string): string {
const date = parseDate(unknown);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
return `${year}-${month}-${day}T${hours}:${minutes}`;
}
export default function Component({ loaderData, actionData }: Route.ComponentProps) {
const { user } = loaderData;
const defaultValue = {
updatedAt: user.updatedAt,
};
const { form, fields } = useForm(clientSchema, {
id: "form-zod",
defaultValue,
lastResult: actionData,
});
const dirty = useFormData(form.id, (formData) => {
return isDirty(formData, {
defaultValue,
serialize(value, defaultSerialize) {
if (value instanceof Date) {
return formatDateToDatetimeLocal(value);
}
return defaultSerialize(value);
},
});
});
return (
<div>
<div>Dirty {dirty ? "Yes" : "No"}</div>
<Form method="post" {...form.props} className="flex flex-col gap-2">
<div>
updatedAt
<input
className="border"
type="datetime-local"
defaultValue={formatDateToDatetimeLocal(fields.updatedAt.defaultValue)}
name={fields.updatedAt.name}
/>
</div>
<Button>Save</Button>
</Form>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi team!
I've followed the discussion related to the same topic with conform v1: #738
I'm wondering what could be the best practice for use with the future API.
Context
I would like to use a simple native input
datetime-localwith conform and actually, I need either to:updatedAtin the example below)DateTimeFieldcomponent with another hidden input (for the example I've made the input visible)Reproduction
Question
I think I'm missing something but I can't find what.
I'm wondering why I need to prepare a date for the input (string without the "Z" suffix) but I don't need to prepare a checkbox ("on" string) if I wanted to use a checkbox.
Expected behavior: Being able to pass a
Dateobject directly asdefaultValuefor adatetime-localinput, similar to how checkboxes work.Current behavior: I need to either:
Dateto a string with.toISOString().substring(0, 16)Is this the intended way to work with date inputs, or is there a more idiomatic approach I should be using?
Thanks for your help!
Beta Was this translation helpful? Give feedback.
All reactions