Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/blocks/src/components/site/site-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export const SiteSidebar = forwardRef<HTMLElement, SiteSidebarProps>(function Si
<div className="mt-3">
<NavGroupLabel
title="Application"
count={featurePackLinks.length + applicationBlockLinks.length + sourceBlockLinks.length + 2}
count={featurePackLinks.length + applicationBlockLinks.length + sourceBlockLinks.length + 3}
/>
<ul className="flex flex-col gap-0.5 pb-0.5 pt-0.5">
<li>
Expand Down
38 changes: 38 additions & 0 deletions packages/blocks-ui/src/__tests__/registry.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,44 @@ describe('defaultBlockRegistry', () => {
expect(document.querySelector('[data-custom="yes"]')).not.toBeNull();
});

it('keeps the stored zone when a minute-precision datetime edit is written back', () => {
const onChange = vi.fn();
render(
<DocumentRenderer
document={form(field('DateTimePicker', { name: 'published_at', label: 'Published at' }))}
registry={defaultBlockRegistry}
initialValues={{ published_at: '2026-08-22T10:30:00+02:00' }}
onChange={onChange}
/>,
);

const input = screen.getByLabelText(/Published at/) as HTMLInputElement;
expect(input.value).toBe('2026-08-22T10:30');

fireEvent.change(input, { target: { value: '2026-08-22T11:45' } });
expect(onChange).toHaveBeenCalledWith({ published_at: '2026-08-22T11:45:00+02:00' });
});

it('stores an absent number as null rather than NaN', () => {
const onChange = vi.fn();
render(
<DocumentRenderer
document={form(field('NumberInput', { name: 'reading_time', label: 'Reading time' }))}
registry={defaultBlockRegistry}
onChange={onChange}
/>,
);

const input = screen.getByLabelText(/Reading time/) as HTMLInputElement;

fireEvent.change(input, { target: { value: '12' } });
expect(onChange).toHaveBeenLastCalledWith({ reading_time: 12 });

// A browser reports an unparseable number as an empty value.
fireEvent.change(input, { target: { value: '1e' } });
expect(onChange).toHaveBeenLastCalledWith({ reading_time: null });
});

it('leaves data blocks unregistered, so an unsatisfied node stays visible', () => {
expect(widgetRegistry.DataTable).toBeUndefined();
expect(missingTypes(defaultBlockRegistry, ['DataTable'])).toEqual(['DataTable']);
Expand Down
28 changes: 24 additions & 4 deletions packages/blocks-ui/src/widgets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ function numericConstraints(props: UINodeProps) {
};
}

/** An empty number input is absent, and a half-typed one is not yet a number. */
function numberValue(raw: string): number | string | null {
if (raw === '') return null;
const parsed = Number(raw);
return Number.isFinite(parsed) ? parsed : raw;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 bug · medium

numberValue stores non-finite input as a string

numberValue returns the raw string for any non-finite input, so '1e' is stored as '1e' in the numeric field rather than null (packages/blocks-ui/src/widgets.tsx:41). The new test fires '1e' and expects { reading_time: null }, so the assertion fails and the helper's string fallback can inject a non-numeric value into a number-typed document field.

📋 Prompt for AI Agents

In packages/blocks-ui/src/widgets.tsx line 41, numberValue returns the raw string for non-finite input, which contradicts the new test in registry.test.tsx (line 170) that expects null and can store a string in the numeric reading_time field. Change return Number.isFinite(parsed) ? parsed : raw; to return Number.isFinite(parsed) ? parsed : null; so unparseable input is stored as null, keeping the field value number | null and making the test pass.

}

const ZONE_SUFFIX = /(?:Z|[+-]\d{2}:?\d{2})$/;

/** Re-attaches the zone the stored value carried to a minute-precision edit. */
function zonedValue(local: string, zone: string): string | null {
if (local === '') return null;
if (!zone) return local;
const seconds = local.length > 16 ? '' : ':00';
return `${local}${seconds}${zone}`;
}

/** A text-ish input; `inputType` carries the HTML type a format implies. */
function TextInput({ props, type }: { props: UINodeProps; type?: string }) {
const field = useNodeField(props);
Expand Down Expand Up @@ -114,8 +131,7 @@ export function NumberInputBlock({ props }: BlockProps) {
name={field.name}
type="number"
value={textValue(field.value)}
// An empty number input is absent, not zero.
onChange={(event) => field.setValue(event.target.value === '' ? null : Number(event.target.value))}
onChange={(event) => field.setValue(numberValue(event.target.value))}
disabled={field.disabled}
required={field.required}
{...(field.placeholder ? { placeholder: field.placeholder } : {})}
Expand Down Expand Up @@ -219,12 +235,14 @@ export function DatePickerBlock({ props }: BlockProps) {

/**
* `datetime-local` needs `YYYY-MM-DDTHH:mm`, while a document (and Postgres)
* speaks ISO-8601 with a zone, so the value is trimmed for display only.
* speaks ISO-8601 with a zone, so the value is trimmed for display and the
* incoming zone is reapplied on write-back rather than dropped.
*/
export function DateTimePickerBlock({ props }: BlockProps) {
const field = useNodeField(props);
const raw = textValue(field.value);
const local = raw.length > 16 ? raw.slice(0, 16) : raw;
const zone = ZONE_SUFFIX.exec(raw)?.[0] ?? '';

return (
<FieldShell props={props} id={field.id} error={field.error}>
Expand All @@ -233,7 +251,9 @@ export function DateTimePickerBlock({ props }: BlockProps) {
name={field.name}
type="datetime-local"
value={local}
onChange={(event) => field.setValue(event.target.value)}
onChange={(event) =>
field.setValue(zonedValue(event.target.value, zone))
}
disabled={field.disabled}
required={field.required}
/>
Expand Down
Loading