Date Edit

Date editor with two variations: the standard digit-by-digit input (DD-MM-RRRR, eight digit boxes) with auto-navigation and validation, and a preset calendar picker. Both support [(ngModel)] and ControlValueAccessor forms.

This component has a wrapper (<smart-date-edit>) that picks its variation through the variant input, and an abstract base class for custom extensions. It has no injection token.


Usage

<smart-date-edit
  variant="standard"
  [(ngModel)]="birthDate"
  (validChange)="onValidChange($event)"
/>
@if (!isValid()) {
  <p>Enter a valid date of birth.</p>
}

The preview renders the code above in an application that registers every preset (provideSmartPresets(), see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.

Default Component Usage

Selector

<smart-date-edit>

Properties

PropertyTypeDefaultDescription
variantDateEditVariantName'standard''standard' renders <smart-date-edit-standard> (eight digit inputs); 'preset' renders <smart-date-edit-preset> (a calendar picker).
classstring''Extra CSS classes on the rendered variation.
ngModelModelSignal<string>'2001-01-01'Date value in YYYY-MM-DD format. Written back after every edit, also when the edited date is not valid.
validChangeOutputEmitter<boolean>—Emits whether the edited date is valid, after every digit edit (standard) or picked day (preset).

DateEditVariantName is 'standard' | 'preset'.

Usage

Both binding styles ([(ngModel)] and formControlName) work, with or without FormsModule in the consuming component. <smart-date-edit> renders from an internal signal rather than straight off ngModel, so when FormsModule is imported and Angular's own NgModel directive also matches the element, the null it seeds its FormControl with cannot leak back into your bound property. A bound form control receives every edit, also a date that is not valid: check validChange before using the value.

Features of the standard variation

  • 8 individual digit inputs (DD + MM + YYYY)
  • Auto-focus to next field on input
  • Moment.js validation
  • Invalid state: red border + red text
  • Dark mode support
  • ControlValueAccessor for forms

Base Class API (for Extension)

Key Members

MemberTypeDescription
DEFAULT_DATEstring'2001-01-01': the initial ngModel, and the date a digit edit starts from when the value is empty
ngModelModelSignal<string>Date value (YYYY-MM-DD)
validDatebooleanWhether the current value is a valid date (a plain field, not a signal)
validChangeOutputEmitter<boolean>Emits the validity after an edit
d1/d2string | null (get/set)Day digit accessors (index 8, 9)
m1/m2string | null (get/set)Month digit accessors (index 5, 6)
y1/y2/y3/y4string | null (get/set)Year digit accessors (index 0-3)
propagateChange(val: any) => voidThe form's change callback (set by registerOnChange); a digit edit passes the date, or null when it is not valid
propagateTouched() => voidThe form's touched callback (set by registerOnTouched)
moveTo()methodHandles keyup: validates digit, moves focus
select()methodSelects input content for overwrite
writeValue()methodControlValueAccessor: sets ngModel and runs change detection
registerOnChange()methodControlValueAccessor: stores propagateChange
registerOnTouched()methodControlValueAccessor: stores propagateTouched

A subclass registers itself as NG_VALUE_ACCESSOR to be usable with formControlName; the base implements every method the contract needs.

Extending the base class

import {
  ChangeDetectionStrategy,
  Component,
  forwardRef,
  signal,
  ViewEncapsulation,
} from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';

import { DateEditBaseComponent } from '@smartsoft001/angular';

/**
 * A custom date editor built on `DateEditBaseComponent`.
 *
 * The base owns the `YYYY-MM-DD` value (`ngModel`), the per-digit accessors
 * (`d1`, `m1`, `y1`, ...), the `validDate` flag and the whole
 * `ControlValueAccessor` contract, so a custom implementation only has to
 * provide a template and decide how a new value reaches the model. This one
 * swaps the eight digit boxes of the standard variation for one native picker.
 */
@Component({
  selector: 'docs-custom-date-edit',
  template: `
    <label class="docs-date-edit">
      <span class="docs-date-edit__label">Start date</span>
      <input
        class="docs-date-edit__input"
        type="date"
        [value]="ngModel()"
        [attr.aria-invalid]="!validDate"
        (change)="onPicked($event)"
      />
    </label>
  `,
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
  // Registering NG_VALUE_ACCESSOR is what makes the component usable with
  // formControlName; the base already implements every method it needs.
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => CustomDateEditComponent),
      multi: true,
    },
  ],
})
export class CustomDateEditComponent extends DateEditBaseComponent {
  onPicked(event: Event): void {
    const value = (event.target as HTMLInputElement).value;

    this.validDate = !!value;
    // ngModel is a model() signal, so setting it also notifies the consumer.
    this.ngModel.set(value);
    this.propagateChange(this.validDate ? value : null);
    this.propagateTouched();
    this.validChange.emit(this.validDate);
  }
}

/**
 * Date edit has no injection token: `<smart-date-edit>` picks between its
 * `standard` and `preset` variations through the `variant` input, so a custom
 * implementation is rendered directly rather than registered.
 */
@Component({
  selector: 'docs-date-edit-custom-example',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [CustomDateEditComponent],
  template: `
    <docs-custom-date-edit
      [ngModel]="date()"
      (ngModelChange)="date.set($event)"
    />
    <p class="docs-date-edit__value">Selected: {{ date() }}</p>
  `,
})
export class DateEditCustomExampleComponent {
  date = signal('2026-04-07');
}

Variations

DateEditStandardComponent (<smart-date-edit-standard>)

The default variation: eight number inputs bound to the digit accessors, with the invalid state in red. <smart-date-edit> renders it while variant is 'standard'; it can also be used directly.

DateEditPresetComponent (<smart-date-edit-preset>)

A fully-styled Preline single datepicker variation: a read-only trigger input plus a calendar popover. Drop-in replacement for the standard editor.

Unlike token-based components, <smart-date-edit> selects its variation through the variant input (NOT an InjectionToken / NgComponentOutlet), so provideSmartPresets() does not change it: set variant="preset" on each <smart-date-edit> that should render the preset, or use <smart-date-edit-preset> directly.

The preset extends DateEditBaseComponent, so it shares ngModel (YYYY-MM-DD), validDate, and validChange. Preline's JS plugin is not installed, so the popover (open/close, prev/next month, day selection) is driven entirely by Angular signals — no external runtime. Month/year pickers are native <select> elements styled to match Preline. Picking a day closes the popover, and so does a click outside the component.

Tailwind Classes

All classes use smart: prefix. Standard inputs: smart:w-8 smart:h-10 smart:text-center smart:border smart:rounded-md. Invalid: smart:border-red-500 smart:text-red-600. Dark: smart:dark:bg-gray-800 smart:dark:text-white.

Source

The component lives in packages/shared/angular/src/lib/components/date-edit and is documented for Claude Code by the angular-components-date-edit skill.

SmartDateEdit edits one date stored as a YYYY-MM-DD string. Its variant picks the rendering: standard (default) is eight single-digit inputs spelling DD-MM-RRRR where every digit moves the focus on, preset is a read-only trigger opening a calendar popover with month navigation and month / year selects. Bind it with value + onValueChange (or defaultValue uncontrolled), or pass a form control. Every edit is emitted, even an invalid date; onValidChange reports whether the edited date is valid.


Usage

import { useState } from 'react';

import { SmartDateEdit } from '@smartsoft001/react';

export function DateEditUsageExample() {
  const [birthDate, setBirthDate] = useState('1990-04-07');
  const [isValid, setIsValid] = useState(true);

  return (
    <>
      <SmartDateEdit
        variant="standard"
        value={birthDate}
        onValueChange={setBirthDate}
        onValidChange={setIsValid}
      />
      {!isValid && <p>Enter a valid date of birth.</p>}
    </>
  );
}

The preview renders the code above in an application that registers every preset (SMART_PRESET_COMPONENTS on SmartProvider, see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.

Exports

All from @smartsoft001/react.

ExportKindWhat it is
SmartDateEditcomponentThe date editor in the variant rendering (standard, eight digit inputs, by default; or preset, a calendar popover).
SmartDateEditPresetcomponentStyled date-edit variation (preset) — a Preline single datepicker.
SmartDateEditStandardcomponentThe default date-edit rendering: eight single-digit inputs spelling DD-MM-RRRR.
useDateEdithookThe behaviour every date-edit variant shares: the YYYY-MM-DD value, its single digits and the validity of the edited date.
DATE_EDIT_DEFAULT_DATEconst'2001-01-01', the value an uncontrolled editor without defaultValue starts from.

The preset's class helpers (getDateEditDayClasses, DATE_EDIT_TRIGGER_WRAPPER, DATE_EDIT_TRIGGER_INPUT, DATE_EDIT_TRIGGER_INVALID, DATE_EDIT_TRIGGER_ICON, DATE_EDIT_POPOVER, DATE_EDIT_NAV_BUTTON, DATE_EDIT_SELECT, DATE_EDIT_WEEKDAY) are exported too, for a custom implementation that wants the preset look.

Props and Types

SmartDateEditProps

Props of <SmartDateEdit>. Extends SmartDateEditVariantProps.

PropTypeDefaultDescription
variant?DateEditVariantName'standard'standard (digit inputs) or preset (calendar popover).
control?SmartAbstractControl—Binds the date to a form control: the control's value is shown, an edit sets it and marks the control dirty and touched.

SmartDateEditVariantProps

Props of the date-edit variants: the date as a controlled value + onValueChange pair, or kept internally from defaultValue.

PropTypeDefaultDescription
value?string | null—The date as YYYY-MM-DD. undefined leaves the component uncontrolled, starting from defaultValue; null is an empty controlled value.
defaultValue?string | null'2001-01-01' (DATE_EDIT_DEFAULT_DATE)The initial value when uncontrolled (useDateEdit falls back to DATE_EDIT_DEFAULT_DATE, '2001-01-01').
onValueChange?(value: string) => void—Emits the edited date, even an invalid one.
onValidChange?(valid: boolean) => void—Emits whether the edited date is valid.
className?string—Classes on the root element.

DateEditDay

A cell of the preset's calendar grid.

FieldTypeDefaultDescription
datestringrequiredThe day as YYYY-MM-DD.
daynumberrequiredDay of the month.
inMonthbooleanrequiredThe day belongs to the shown month.
selectedbooleanrequiredThe day is the current value.
  • DateEditVariantName: 'standard' \| 'preset' — The renderings SmartDateEdit can pick.

SmartAbstractControl is described in the react-forms and react-provider skills.

Binding

  • Controlled: value (a YYYY-MM-DD string, null for empty) and onValueChange. A new value from the parent always wins, so resetting the parent's state also resets the editor.
  • Uncontrolled: leave value undefined; the editor starts from defaultValue.
  • Form control: control={someControl} shows the control's value; every edit sets it, marks the control dirty and touched, and still calls onValueChange.
  • onValueChange receives the edited text even when it is not a valid date; use onValidChange to know.

Replacing the Implementation

SmartDateEdit has no SmartProvider registry key: the variant prop picks SmartDateEditStandard or SmartDateEditPreset, and both can be rendered directly with SmartDateEditVariantProps. SMART_PRESET_COMPONENTS therefore does not change it; set variant="preset" where the calendar popover should render. A form's date / dateWithEdit fields render it through the input field components (see react-components-input). For another look, write a component on useDateEdit and render it in place of SmartDateEdit.

The useDateEdit hook

The behaviour every date-edit variant shares: the YYYY-MM-DD value, its single digits and the validity of the edited date.

ReturnsTypeDescription
valuestring | nullThe current YYYY-MM-DD value (controlled or internal), null when empty.
setValue(next: string) => voidSets the value and calls onValueChange (no call when unchanged).
validDatebooleanWhether the current value is a valid date.
setValidDateDispatch<SetStateAction<boolean>>Sets the validity flag only; setValueAt is what reports to onValidChange, so a custom editor that calls this reports it itself.
setValueAt(val: string | number | null, index: number) => voidWrites one digit at a position of the DD-MM-RRRR mask.
moveTo(event: KeyboardEvent<HTMLInputElement>, el: HTMLInputElement | null) => voidKeyup handler of a digit input: a digit moves the focus to the next input.
select(el: HTMLInputElement | null) => voidTrims an input to one character and selects it, so the next key replaces the digit.
d1string | nullFirst day digit.
d2string | nullSecond day digit.
m1string | nullFirst month digit.
m2string | nullSecond month digit.
y1string | nullFirst year digit.
y2string | nullSecond year digit.
y3string | nullThird year digit.
y4string | nullFourth year digit.
import { useState } from 'react';

import { SmartDateEditVariantProps, useDateEdit } from '@smartsoft001/react';

/**
 * A custom date editor built on `useDateEdit`.
 *
 * The hook owns the `YYYY-MM-DD` value (controlled or not), the per-digit
 * accessors (`d1`, `m1`, `y1`, ...) and the `validDate` flag, so a custom
 * implementation only renders the editor and decides how a new value reaches
 * the hook. This one swaps the eight digit boxes of the standard rendering for
 * one native picker.
 */
export function CustomDateEdit(props: SmartDateEditVariantProps) {
  const { value, setValue, validDate, setValidDate } = useDateEdit(props);

  const onPicked = (next: string) => {
    const valid = next !== '';

    setValidDate(valid);
    // setValue also reports the new value through onValueChange.
    setValue(next);
    props.onValidChange?.(valid);
  };

  return (
    <label
      className={['docs-date-edit', props.className].filter(Boolean).join(' ')}
    >
      <span className="docs-date-edit__label">Start date</span>
      <input
        className="docs-date-edit__input"
        type="date"
        value={value ?? ''}
        aria-invalid={!validDate}
        onChange={(event) => onPicked(event.target.value)}
      />
    </label>
  );
}

// SmartDateEdit has no SmartProvider registry key: its `variant` prop picks
// the standard or preset rendering, so a custom implementation is rendered
// directly in place of <SmartDateEdit>.
export function DateEditCustomExample() {
  const [date, setDate] = useState('2026-04-07');

  return (
    <>
      <CustomDateEdit value={date} onValueChange={setDate} />
      <p className="docs-date-edit__value">Selected: {date}</p>
    </>
  );
}

Styling

  • The standard digit inputs and the preset trigger / popover carry smart:dark: variants; the preset trigger shows an invalid state when the date is not valid.
  • className is appended to the root element.

Source

The component lives in packages/shared/react/src/lib/components/date-edit and is documented for Claude Code by the react-components-date-edit skill.