Skip to content

Commit

Permalink
fix: stories for calendar validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Oct 4, 2024
1 parent 2f77711 commit 389c0f6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
17 changes: 4 additions & 13 deletions components/calendar/src/calendar-input/calendar-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const CalendarInput = ({
const ref = useRef()
const [open, setOpen] = useState(false)
const [partialDate, setPartialDate] = useState(date)
const [isIconDisplayed, setIsIconDisplayed] = useState(false)

const excludeRef = useRef(null)

Expand All @@ -67,9 +66,7 @@ export const CalendarInput = ({
maxDateString: maxDate,
strictValidation,
})
setIsIconDisplayed(
validated.errorMessage || validated.warningMessage
)

setOpen(false)
parentOnDateSelect?.({
calendarDateString: result.calendarDateString,
Expand Down Expand Up @@ -97,7 +94,6 @@ export const CalendarInput = ({
maxDateString: maxDate,
strictValidation,
})
setIsIconDisplayed(validated.errorMessage || validated.warningMessage)
parentOnDateSelect?.({ calendarDateString: partialDate, ...validated })

if (
Expand Down Expand Up @@ -143,12 +139,6 @@ export const CalendarInput = ({
value={partialDate}
onChange={handleChange}
onBlur={handleBlur}
validationText={
pickerResults.errorMessage ||
pickerResults.warningMessage
}
error={!!pickerResults.errorMessage}
warning={!!pickerResults.warningMessage}
inputWidth={inputWidth}
/>
{clearable && (
Expand All @@ -168,7 +158,6 @@ export const CalendarInput = ({
small
onClick={() => {
parentOnDateSelect?.(null)
setIsIconDisplayed(false)
}}
type="button"
>
Expand Down Expand Up @@ -207,7 +196,9 @@ export const CalendarInput = ({
}
.calendar-clear-button {
position: absolute;
inset-inline-end: ${isIconDisplayed ? '36px' : '6px'};
inset-inline-end: ${rest.error || rest.warning
? '36px'
: '6px'};
inset-block-start: 27px;
}
.calendar-clear-button.with-icon {
Expand Down
49 changes: 28 additions & 21 deletions components/calendar/src/stories/calendar-input.prod.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ export default {

const buildCalendar =
({ date, locale, calendar }) =>
() =>
(
<CalendarStoryWrapper
component={CalendarInput}
dir="ltr"
timeZone="Africa/Khartoum"
weekDayFormat="short"
date={date}
locale={locale}
calendar={calendar}
onDateSelect={() => {}}
/>
)
() => (
<CalendarStoryWrapper
component={CalendarInput}
dir="ltr"
timeZone="Africa/Khartoum"
weekDayFormat="short"
date={date}
locale={locale}
calendar={calendar}
onDateSelect={() => {}}
/>
)

export const EthiopicWithAmharic = buildCalendar({
calendar: 'ethiopic',
Expand Down Expand Up @@ -142,9 +141,13 @@ export function CalendarWithEditiableInput() {

export function CalendarWithEditiableInputReactForm() {
const [calendarError, setCalendarError] = useState(undefined)
const [calendarWarning, setCalendarWarning] = useState(undefined)

const errored = () => {
return { calendar: calendarError }
if (!calendarError && !calendarWarning) {
return { calendar: undefined }
}
return { calendar: calendarError || calendarWarning }
}

return (
Expand All @@ -155,7 +158,7 @@ export function CalendarWithEditiableInputReactForm() {
initialValues={{ calendar: '2022-10-12' }}
validate={errored}
>
{({ handleSubmit }) => {
{({ handleSubmit, invalid }) => {
return (
<form onSubmit={handleSubmit}>
<Field name="calendar">
Expand All @@ -167,12 +170,16 @@ export function CalendarWithEditiableInputReactForm() {
editable
date={props.input.value}
calendar="gregory"
validationText={
calendarError || calendarWarning
}
error={!!calendarError}
warning={!!calendarWarning}
strictValidation={true}
minDate="2022-11-01"
onDateSelect={(date) => {
if (!date.isValid) {
setCalendarError(date.errorMessage)
} else {
setCalendarError(undefined)
}
setCalendarError(date.errorMessage)
setCalendarWarning(date.warningMessage)
props.input.onChange(
date ? date?.calendarDateString : ''
)
Expand All @@ -184,7 +191,7 @@ export function CalendarWithEditiableInputReactForm() {

<button
type="submit"
disabled={false}
disabled={invalid}
onClick={handleSubmit}
>
Submit
Expand Down
9 changes: 9 additions & 0 deletions components/calendar/src/stories/calendar-story-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const CalendarStoryWrapper = (props) => {
const [selectedDirection, setSelectedDirection] = useState(dir)
const [selectedWeekFormat, setWeekDayFormat] = useState(weekDayFormat)

const [calendarError, setCalendarError] = useState(undefined)
const [calendarWarning, setCalendarWarning] = useState(undefined)

const [selectedLocale, setLocale] = useState(locale)
const [selectedDate, setSelectedDate] = useState({
calendarDateString: date,
Expand Down Expand Up @@ -112,7 +115,13 @@ export const CalendarStoryWrapper = (props) => {
dir={selectedDirection}
locale={selectedLocale}
date={selectedDate.calendarDateString}
validationText={calendarError || calendarWarning}
error={!!calendarError}
warning={!!calendarWarning}
onDateSelect={(date) => {
setCalendarError(date.errorMessage)
setCalendarWarning(date.warningMessage)

setSelectedDate(date)
}}
timeZone={timeZone}
Expand Down

0 comments on commit 389c0f6

Please sign in to comment.