Date picker example

A date field with native form ownership.

Type an unambiguous date, use the calendar, and submit or reset through an external form. Invalid calendar dates never replace a committed value.

Complete React client module · MIT · Next.js and Vite

Interactive example

A date field with native form ownership

Type an unambiguous date, use the calendar, and submit or reset through an external form. Invalid calendar dates never replace a committed value.

Keyboard

  • Type YYYY-MM-DD; Enter or leaving the field commits a valid date.
  • Alt+ArrowDown opens the calendar. Escape restores the committed text.
  • Tab reaches the calendar and clear actions; native form validation focuses invalid text.

States covered

  • Editable date
  • Required and min/max validation
  • External form
  • Read only
  • Disabled
  • Prevented reset

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-date-entry-lifecycle.json
Complete source
React / TypeScript
/*!
MIT License

Copyright (c) 2026 Jaison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useId,useRef,useState} from 'react';
import {Button,Checkbox,DatePicker,parseDateValue,UIProvider} from '@bld8/ui';
export default function DateEntryLifecycle(){
  const formId=useId(),input=useRef<HTMLInputElement>(null);
  const [changes,setChanges]=useState(0),[status,setStatus]=useState('No date submitted.'),[keepOnReset,setKeepOnReset]=useState(false);
  return <UIProvider><div style={{display:'grid',gap:16}}>
    <DatePicker label="Review date" editable name="reviewDate" form={formId} required defaultValue={parseDateValue('2026-11-12')} minDate={parseDateValue('2026-11-01')!} maxDate={parseDateValue('2026-12-31')!} textInputRef={input} onValueChange={()=>setChanges(count=>count+1)} hint="Use YYYY-MM-DD. Dates stay in the local calendar, without UTC conversion."/>
    <DatePicker label="Locked review date" editable name="lockedDate" form={formId} defaultValue={parseDateValue('2026-11-18')} readOnly/>
    <DatePicker label="Unavailable date" editable name="unavailableDate" form={formId} defaultValue={parseDateValue('2026-11-20')} disabled/>
    <Checkbox label="Keep date on reset" checked={keepOnReset} onCheckedChange={setKeepOnReset}/>
    <form id={formId} onReset={event=>{if(keepOnReset){event.preventDefault();setStatus('Reset prevented.');}else setStatus('Reset requested.');}} onSubmit={event=>{event.preventDefault();const data=new FormData(event.currentTarget);setStatus('Review date: '+data.get('reviewDate')+'; locked: '+data.get('lockedDate')+'; unavailable: '+(data.has('unavailableDate')?'included':'excluded'));}}>
      <div style={{display:'flex',gap:8,flexWrap:'wrap'}}><Button type="submit">Read review date</Button><Button type="reset" variant="secondary">Reset review date</Button><Button variant="ghost" onClick={()=>input.current?.focus()}>Focus review date</Button></div>
    </form><p data-date-changes>Changes: {changes}</p><p role="status">{status}</p>
  </div></UIProvider>;
}