Skip to content
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

refactor(date): updated errorMessage story and modified to import props #3112

Merged
merged 5 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .changeset/spicy-islands-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/date-input": patch
"@nextui-org/date-picker": patch
---

chore(date): update errorMessageFunction story and docs for date libraries
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const App = `import {DateInput} from "@nextui-org/react";
import {CalendarDate, parseDate} from "@internationalized/date";

export default function App() {
return (
<div className="flex w-full flex-wrap md:flex-nowrap gap-4">
<DateInput
label={"Birth date"}
defaultValue={parseDate("2024-04-04")}
placeholderValue={new CalendarDate(1995, 11, 6)}
description={"Thiis is my birth date."}
ryo-manba marked this conversation as resolved.
Show resolved Hide resolved
ryo-manba marked this conversation as resolved.
Show resolved Hide resolved
isInvalid
errorMessage={(value) => {
if (value.isInvalid) {
return "Please enter a valid date.";
}
}}
className="max-w-xs"
/>
</div>
);
}`;

const react = {
"/App.jsx": App,
};

export default {
...react,
};
2 changes: 2 additions & 0 deletions apps/docs/content/components/date-input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import labelPlacements from "./label-placements";
import description from "./description";
import startEndContent from "./start-end-content";
import errorMessage from "./error-message";
import errorMessageFunction from "./error-message-function";
import controlled from "./controlled";
import timeZones from "./time-zones";
import granularity from "./granularity";
Expand All @@ -25,6 +26,7 @@ export const dateInputContent = {
description,
startEndContent,
errorMessage,
errorMessageFunction,
controlled,
timeZones,
granularity,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const App = `import {DatePicker} from "@nextui-org/react";

export default function App() {
return (
<div className="flex w-full flex-wrap md:flex-nowrap gap-4">
<DatePicker
label="Birth date"
className="max-w-[284px]"
isInvalid
errorMessage={(value) => {
if (value.isInvalid) {
return "Please enter a valid date.";
}
}}
/>
</div>
);
}`;

const react = {
"/App.jsx": App,
};

export default {
...react,
};
2 changes: 2 additions & 0 deletions apps/docs/content/components/date-picker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import variants from "./variants";
import labelPlacements from "./label-placements";
import description from "./description";
import errorMessage from "./error-message";
import errorMessageFunction from "./error-message-function";
import withMonthAndYearPickers from "./with-month-and-year-pickers";
import withTimeField from "./with-time-field";
import selectorIcon from "./selector-icon";
Expand All @@ -28,6 +29,7 @@ export const datePickerContent = {
labelPlacements,
description,
errorMessage,
errorMessageFunction,
withMonthAndYearPickers,
withTimeField,
selectorIcon,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const App = `import {DateRangePicker} from "@nextui-org/react";
import {parseDate} from "@internationalized/date";

export default function App() {
return (
<DateRangePicker
isInvalid
label="Stay duration"
variant="bordered"
errorMessage="Please enter your stay duration"
errorMessage={(value) => {
ryo-manba marked this conversation as resolved.
Show resolved Hide resolved
if (value.isInvalid) {
return "Please enter your stay duration";
}
}}
defaultValue={{
start: parseDate("2024-04-01"),
end: parseDate("2024-04-08"),
}}
className="max-w-xs"
/>
);
}`;

const react = {
"/App.jsx": App,
};

export default {
...react,
};
2 changes: 2 additions & 0 deletions apps/docs/content/components/date-range-picker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import variants from "./variants";
import labelPlacements from "./label-placements";
import description from "./description";
import errorMessage from "./error-message";
import errorMessageFunction from "./error-message-function";
import withTimeField from "./with-time-field";
import selectorIcon from "./selector-icon";
import controlled from "./controlled";
Expand All @@ -28,6 +29,7 @@ export const dateRangePickerContent = {
labelPlacements,
description,
errorMessage,
errorMessageFunction,
withTimeField,
selectorIcon,
controlled,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const App = `import {TimeInput} from "@nextui-org/react";

export default function App() {
return (
<TimeInput
label="Event Time"
isInvalid
errorMessage={(value) => {
if (value.isInvalid) {
return "Please enter a valid time";
}
}}
/>
);
}`;

const react = {
"/App.jsx": App,
};

export default {
...react,
};
19 changes: 19 additions & 0 deletions apps/docs/content/components/time-input/error-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const App = `import {TimeInput} from "@nextui-org/react";

export default function App() {
return (
<TimeInput
label="Event Time"
isInvalid
errorMessage="Please enter a valid time"
/>
);
}`;

const react = {
"/App.jsx": App,
};

export default {
...react,
};
Comment on lines +3 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using a function for errorMessage to allow dynamic validation responses.

- errorMessage="Please enter a valid time"
+ errorMessage={(value) => value.isInvalid ? "Please enter a valid time" : ""}

Committable suggestion was skipped due low confidence.

4 changes: 4 additions & 0 deletions apps/docs/content/components/time-input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import disabled from "./disabled";
import readonly from "./read-only";
import withoutLabel from "./without-label";
import withDescription from "./with-description";
import errorMessage from "./error-message";
import errorMessageFunction from "./error-message-function";
import labelPlacement from "./label-placement";
import startContent from "./start-content";
import endContent from "./end-content";
Expand All @@ -23,6 +25,8 @@ export const timeInputContent = {
readonly,
withoutLabel,
withDescription,
errorMessage,
errorMessageFunction,
labelPlacement,
startContent,
endContent,
Expand Down
4 changes: 4 additions & 0 deletions apps/docs/content/docs/components/date-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ You can combine the `isInvalid` and `errorMessage` properties to show an invalid

<CodeDemo title="With Error Message" files={dateInputContent.errorMessage} />

You can also pass an error message as a function. This allows for dynamic error message handling based on the [ValidationResult]((https://github.com/adobe/react-spectrum/blob/1cacbf1d438675feb3859fee54b17e620b458d9c/packages/%40react-types/shared/src/inputs.d.ts#L44-L51)).

<CodeDemo title="With Error Message Function" files={dateInputContent.errorMessageFunction} />

### Controlled

You can use the `value` and `onChange` properties to control the input value.
Expand Down
4 changes: 4 additions & 0 deletions apps/docs/content/docs/components/date-picker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ You can combine the `isInvalid` and `errorMessage` properties to show an invalid

<CodeDemo title="With Error Message" files={datePickerContent.errorMessage} />

You can also pass an error message as a function. This allows for dynamic error message handling based on the [ValidationResult]((https://github.com/adobe/react-spectrum/blob/1cacbf1d438675feb3859fee54b17e620b458d9c/packages/%40react-types/shared/src/inputs.d.ts#L44-L51)).

<CodeDemo title="With Error Message Function" files={datePickerContent.errorMessageFunction} />

### With Month and Year Pickers

<CodeDemo title="With Month and Year Pickers" files={datePickerContent.withMonthAndYearPickers} />
Expand Down
4 changes: 4 additions & 0 deletions apps/docs/content/docs/components/date-range-picker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ You can combine the `isInvalid` and `errorMessage` properties to show an invalid

<CodeDemo title="With Error Message" files={dateRangePickerContent.errorMessage} />

You can also pass an error message as a function. This allows for dynamic error message handling based on the [ValidationResult]((https://github.com/adobe/react-spectrum/blob/1cacbf1d438675feb3859fee54b17e620b458d9c/packages/%40react-types/shared/src/inputs.d.ts#L44-L51)).

<CodeDemo title="With Error Message Function" files={dateRangePickerContent.errorMessageFunction} />

### With Time Fields

DateRangePicker automatically includes time fields when a `CalendarDateTime` or `ZonedDateTime` object is provided as the value.
Expand Down
11 changes: 11 additions & 0 deletions apps/docs/content/docs/components/time-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ A description for the field. Provides a hint such as specific requirements for w

<CodeDemo title="With Description" files={timeInputContent.withDescription} />

### With Error Message

You can combine the `isInvalid` and `errorMessage` properties to show an invalid input.

<CodeDemo title="With Error Message" files={timeInputContent.errorMessage} />

You can also pass an error message as a function. This allows for dynamic error message handling based on the [ValidationResult]((https://github.com/adobe/react-spectrum/blob/1cacbf1d438675feb3859fee54b17e620b458d9c/packages/%40react-types/shared/src/inputs.d.ts#L44-L51)).

<CodeDemo title="With Error Message Function" files={timeInputContent.errorMessageFunction} />


### Label Placement

The label's overall position relative to the element it is labeling.
Expand Down
26 changes: 26 additions & 0 deletions packages/components/date-input/stories/time-input.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ZonedDateTime,
} from "@internationalized/date";
import {useDateFormatter} from "@react-aria/i18n";
import {ValidationResult} from "@react-types/shared";

import {TimeInput, TimeInputProps, TimeInputValue as TimeValue} from "../src";

Expand Down Expand Up @@ -192,6 +193,31 @@ export const WithDescription = {
},
};

export const WithErrorMessage = {
render: Template,

args: {
...defaultProps,
isInvalid: true,
errorMessage: "Please enter a valid time",
},
};

export const WithErrorMessageFunction = {
render: Template,

args: {
...defaultProps,
isInvalid: true,
errorMessage: "Please enter a valid time",
},
errorMessage: (value: ValidationResult) => {
ryo-manba marked this conversation as resolved.
Show resolved Hide resolved
if (value.isInvalid) {
return "Please enter a valid date";
}
},
};

export const LabelPlacement = {
render: LabelPlacementTemplate,

Expand Down
Loading