An open source design system that's fully customizable and agent ready
TypeScript
fix(DateInput, TimeInput, DateTimeInput): value accepts an explicit undefined (#10129)
Ojas Mutrejaf88d572acb26
diff --git a/.changeset/date-time-input-value-accepts-undefined.md b/.changeset/date-time-input-value-accepts-undefined.mdnew file mode 100644index 0000000000..7b82b02a6a--- /dev/null+++ b/.changeset/date-time-input-value-accepts-undefined.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] DateInput, TimeInput, DateTimeInput: `value` now accepts an explicit `undefined`. In a project with `exactOptionalPropertyTypes`, `value={state}` failed to compile when `state` held what `onChange` gave, because `onChange` sends `undefined` for a cleared field and an optional property no longer took one. A controlled field no longer needs a cast there. (#8961)++@HelloOjasMutrejadiff --git a/packages/core/src/DateInput/DateInput.tsx b/packages/core/src/DateInput/DateInput.tsxindex e4ece00680..a72c8f5806 100644--- a/packages/core/src/DateInput/DateInput.tsx+++ b/packages/core/src/DateInput/DateInput.tsx@@ -278,7 +278,7 @@ export interface DateInputProps extends Omit</*** The selected date in ISO format (YYYY-MM-DD).*/- value?: ISODateString;+ value?: ISODateString | undefined;/*** Callback fired when the date changes.diff --git a/packages/core/src/DateInput/__tests__/valueUndefined.types.test.ts b/packages/core/src/DateInput/__tests__/valueUndefined.types.test.tsnew file mode 100644index 0000000000..99bd492471--- /dev/null+++ b/packages/core/src/DateInput/__tests__/valueUndefined.types.test.ts@@ -0,0 +1,88 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Regression check for `value` accepting an explicit `undefined`+ * @input Compiles a small consumer file against the DateInput, TimeInput and+ * DateTimeInput sources with `exactOptionalPropertyTypes` on+ * @output Asserts the consumer file has no type errors+ * @position Testing; the repository's own TypeScript settings treat+ * `value?: T` and `value?: T | undefined` as the same type, so only a+ * consumer compiled with `exactOptionalPropertyTypes` notices if the+ * `| undefined` is removed again+ *+ * SYNC: When the `value` prop of any of the three inputs changes, update this+ * fixture to match.+ */++import * as path from 'node:path';+import {fileURLToPath} from 'node:url';+import ts from 'typescript';+import {describe, expect, it} from 'vitest';++const HERE = path.dirname(fileURLToPath(import.meta.url));+const SRC = path.resolve(HERE, '..', '..');+const ROOT_CONFIG = path.resolve(SRC, '..', '..', '..', 'tsconfig.json');+const FIXTURE = path.join(HERE, 'valueUndefined.fixture.tsx');+const SRC_POSIX = SRC.split(path.sep).join('/');++const FIXTURE_SOURCE = `+import {useState} from 'react';+import {DateInput} from '${SRC_POSIX}/DateInput/DateInput';+import {TimeInput} from '${SRC_POSIX}/TimeInput/TimeInput';+import {DateTimeInput} from '${SRC_POSIX}/DateTimeInput/DateTimeInput';++type DateValue = React.ComponentProps<typeof DateInput>['value'];+type TimeValue = React.ComponentProps<typeof TimeInput>['value'];+type DateTimeValue = React.ComponentProps<typeof DateTimeInput>['value'];++export function Fixture() {+ const [date, setDate] = useState<DateValue>();+ const [time, setTime] = useState<TimeValue>();+ const [dateTime, setDateTime] = useState<DateTimeValue>();+ return (+ <>+ <DateInput label="Date" value={date} onChange={setDate} />+ <TimeInput label="Time" value={time} onChange={setTime} />+ <DateTimeInput label="Date and time" value={dateTime} onChange={setDateTime} />+ </>+ );+}+`;++describe('value accepts an explicit undefined', () => {+ it('compiles for a consumer with exactOptionalPropertyTypes on', () => {+ const config = ts.readConfigFile(ROOT_CONFIG, ts.sys.readFile);+ const parsed = ts.parseJsonConfigFileContent(+ config.config,+ ts.sys,+ path.dirname(ROOT_CONFIG),+ );+ const options: ts.CompilerOptions = {+ ...parsed.options,+ noEmit: true,+ skipLibCheck: true,+ exactOptionalPropertyTypes: true,+ };+ const host = ts.createCompilerHost(options);+ const original = host.getSourceFile.bind(host);+ host.getSourceFile = (fileName, languageVersion, ...rest) =>+ path.resolve(fileName) === FIXTURE+ ? ts.createSourceFile(fileName, FIXTURE_SOURCE, languageVersion, true)+ : original(fileName, languageVersion, ...rest);+ const readFile = host.readFile.bind(host);+ host.readFile = fileName =>+ path.resolve(fileName) === FIXTURE ? FIXTURE_SOURCE : readFile(fileName);+ const fileExists = host.fileExists.bind(host);+ host.fileExists = fileName =>+ path.resolve(fileName) === FIXTURE || fileExists(fileName);++ const program = ts.createProgram([FIXTURE], options, host);+ // The sources have their own errors under this setting that are not+ // about `value`; only the consumer file is checked.+ const diagnostics = ts+ .getPreEmitDiagnostics(program, program.getSourceFile(FIXTURE))+ .map(d => ts.flattenDiagnosticMessageText(d.messageText, '\n'));++ expect(diagnostics).toEqual([]);+ }, 120_000);+});diff --git a/packages/core/src/DateTimeInput/DateTimeInput.tsx b/packages/core/src/DateTimeInput/DateTimeInput.tsxindex 1721b29538..3c15675e73 100644--- a/packages/core/src/DateTimeInput/DateTimeInput.tsx+++ b/packages/core/src/DateTimeInput/DateTimeInput.tsx@@ -368,7 +368,7 @@ export interface DateTimeInputProps extends Omit</*** The selected datetime in ISO 8601 format ("YYYY-MM-DDTHH:MM" or "YYYY-MM-DDTHH:MM:SS").*/- value?: ISODateTimeString;+ value?: ISODateTimeString | undefined;/*** Callback fired when the datetime changes.diff --git a/packages/core/src/TimeInput/TimeInput.tsx b/packages/core/src/TimeInput/TimeInput.tsxindex 32d0d067ba..7d55e91081 100644--- a/packages/core/src/TimeInput/TimeInput.tsx+++ b/packages/core/src/TimeInput/TimeInput.tsx@@ -243,7 +243,7 @@ export interface TimeInputProps extends Omit</*** The selected time in ISO format (HH:MM or HH:MM:SS).*/- value?: ISOTimeString;+ value?: ISOTimeString | undefined;/*** Callback fired when the time changes.