Repository navigation
feat(admin): add date-only content field #1365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5b76a6b
feat: add date-only field type
masonjames 7ee4133
fix: address date field review feedback
masonjames d89f25e
Merge main and validate date-only fields against current schema APIs
masonjames e4f0f69
Merge remote-tracking branch 'origin/main' into codex/refresh-1365
masonjames File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "emdash": patch | ||
| "@emdash-cms/admin": patch | ||
| --- | ||
|
|
||
| Add a date-only content field type that stores `YYYY-MM-DD` values and renders as a date picker in the admin. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { z } from "astro/zod"; | ||
|
|
||
| import type { FieldDefinition, FieldUIHints } from "./types.js"; | ||
|
|
||
| const DATE_ONLY_PATTERN = /^\d{4}-\d{2}-\d{2}$/; | ||
|
|
||
| export interface DateOptions { | ||
| required?: boolean; | ||
| min?: string | Date; | ||
| max?: string | Date; | ||
| helpText?: string; | ||
| } | ||
|
|
||
| function toDateOnly(value: string | Date): string { | ||
| if (value instanceof Date) { | ||
| const year = value.getFullYear(); | ||
| const month = String(value.getMonth() + 1).padStart(2, "0"); | ||
| const day = String(value.getDate()).padStart(2, "0"); | ||
| return `${year}-${month}-${day}`; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| function isValidDateOnly(value: string): boolean { | ||
| if (!DATE_ONLY_PATTERN.test(value)) return false; | ||
| const parsed = new Date(`${value}T00:00:00.000Z`); | ||
| return !Number.isNaN(parsed.getTime()) && parsed.toISOString().slice(0, 10) === value; | ||
| } | ||
|
|
||
| /** | ||
| * Date field - date picker without a time component. | ||
| */ | ||
| export function date(options: DateOptions = {}): FieldDefinition<string> { | ||
| const min = options.min ? toDateOnly(options.min) : undefined; | ||
| const max = options.max ? toDateOnly(options.max) : undefined; | ||
| let dateSchema = z | ||
| .string() | ||
| .regex(DATE_ONLY_PATTERN, "Must be a date in YYYY-MM-DD format") | ||
| .refine(isValidDateOnly, "Invalid date"); | ||
|
|
||
| if (min !== undefined) { | ||
| dateSchema = dateSchema.refine((value) => value >= min, "Date is too early"); | ||
| } | ||
|
|
||
| if (max !== undefined) { | ||
| dateSchema = dateSchema.refine((value) => value <= max, "Date is too late"); | ||
| } | ||
|
|
||
| const markedSchema = dateSchema as z.ZodTypeAny & { isDateOnly?: true }; | ||
| markedSchema.isDateOnly = true; | ||
|
|
||
| const schema: z.ZodTypeAny = options.required ? markedSchema : markedSchema.optional(); | ||
|
|
||
| const ui: FieldUIHints = { | ||
| widget: "date", | ||
| helpText: options.helpText, | ||
| min, | ||
| max, | ||
| }; | ||
|
|
||
| return { | ||
| type: "date", | ||
| columnType: "TEXT", | ||
| schema, | ||
| options, | ||
| ui, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,6 +94,7 @@ const FIELD_TYPES = [ | |
| "number", | ||
| "integer", | ||
| "boolean", | ||
| "date", | ||
| "datetime", | ||
| "select", | ||
| "multiSelect", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.