Skip to content

- Possible fix for Inline validation messages on field lose focus eve… #1233

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

Merged
merged 3 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface CustomDateFieldProps extends PropsWithIsTabledRequiredAttribute {
isPartOfDateTime: boolean;
setFocused: Dispatch<SetStateAction<boolean>>;
onInputChange: (newInput: string) => void;
onDateBlur: () => void;
onSelectDate: (newDateValue: string) => void;
}

Expand All @@ -55,6 +56,7 @@ function CustomDateField(props: CustomDateFieldProps) {
isTabled,
setFocused,
onInputChange,
onDateBlur,
onSelectDate
} = props;

Expand Down Expand Up @@ -83,7 +85,10 @@ function CustomDateField(props: CustomDateFieldProps) {
size="small"
focused={isFocused}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
onBlur={() => {
onDateBlur();
setFocused(false);
}}
slotProps={{
input: {
readOnly: readOnly && readOnlyVisualStyle === 'readonly',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import useDateValidation from '../../../../hooks/useDateValidation';
import CustomDateField from './CustomDateField';
import { useQuestionnaireStore } from '../../../../stores';
import ItemLabel from '../../ItemParts/ItemLabel';
import useShowFeedback from '../../../../hooks/useShowFeedback';

interface CustomDateItemProps extends BaseItemProps {}

Expand All @@ -52,7 +53,6 @@ function CustomDateItem(props: CustomDateItemProps) {
// Init input value
const answerKey = qrItem?.answer?.[0]?.id;
const qrDate = qrItem ?? createEmptyQrItem(qItem, answerKey);

let valueDate: string = '';
if (qrDate.answer) {
if (qrDate.answer[0].valueDate) {
Expand All @@ -70,6 +70,9 @@ function CustomDateItem(props: CustomDateItemProps) {
// Perform validation checks
const errorFeedback = useDateValidation(input, dateParseFail);

// Provides a way to hide the feedback when the user is typing
const { showFeedback, setShowFeedback, hasBlurred, setHasBlurred } = useShowFeedback();

function handleSelectDate(selectedDate: string) {
setInput(selectedDate);
onQrItemChange({
Expand All @@ -81,10 +84,14 @@ function CustomDateItem(props: CustomDateItemProps) {
function handleInputChange(newInput: string) {
setInput(newInput);

// Only suppress feedback once (before first blur)
if (!hasBlurred) {
setShowFeedback(false);
}

if (newInput === '') {
onQrItemChange(createEmptyQrItem(qItem, answerKey));
}

if (!validateDateInput(newInput)) {
return;
}
Expand All @@ -95,14 +102,19 @@ function CustomDateItem(props: CustomDateItemProps) {
});
}

function handleDateBlur() {
setShowFeedback(true);
setHasBlurred(true); // From now on, feedback should stay visible
}

if (isRepeated) {
return (
<CustomDateField
linkId={qItem.linkId}
itemType={qItem.type}
valueDate={displayDate}
input={input}
feedback={errorFeedback ?? ''}
feedback={showFeedback ? (errorFeedback ?? '') : ''}
isFocused={focused}
displayPrompt={displayPrompt}
entryFormat={entryFormat}
Expand All @@ -111,6 +123,7 @@ function CustomDateItem(props: CustomDateItemProps) {
isTabled={isTabled}
setFocused={setFocused}
onInputChange={handleInputChange}
onDateBlur={handleDateBlur}
onSelectDate={handleSelectDate}
/>
);
Expand All @@ -131,7 +144,7 @@ function CustomDateItem(props: CustomDateItemProps) {
itemType={qItem.type}
valueDate={displayDate}
input={input}
feedback={errorFeedback ?? ''}
feedback={showFeedback ? (errorFeedback ?? '') : ''}
isFocused={focused}
displayPrompt={displayPrompt}
entryFormat={entryFormat}
Expand All @@ -140,6 +153,7 @@ function CustomDateItem(props: CustomDateItemProps) {
isTabled={isTabled}
setFocused={setFocused}
onInputChange={handleInputChange}
onDateBlur={handleDateBlur}
onSelectDate={handleSelectDate}
/>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import useTimeValidation from '../../../../hooks/useTimeValidation';
import useDateNonEmptyValidation from '../../../../hooks/useDateTimeNonEmpty';
import DateTimeField from './DateTimeField';
import ItemLabel from '../../ItemParts/ItemLabel';
import useShowFeedback from '../../../../hooks/useShowFeedback';

interface CustomDateTimeItemProps extends BaseItemProps {}

Expand Down Expand Up @@ -97,6 +98,9 @@ function CustomDateTimeItem(props: CustomDateTimeItemProps) {

dateFeedback = useDateNonEmptyValidation(dateInput, timeInput, dateFeedback, timeFeedback);

// Provides a way to hide the feedback when the user is typing
const { showFeedback, setShowFeedback, hasBlurred, setHasBlurred } = useShowFeedback();

function handleSelectDate(selectedDate: string) {
setDateInput(selectedDate);
updateQRDateTime(selectedDate, timeInput, periodInput, is24HourNotation);
Expand All @@ -105,6 +109,11 @@ function CustomDateTimeItem(props: CustomDateTimeItemProps) {
function handleDateInputChange(newDateInput: string) {
setDateInput(newDateInput);

// Only suppress feedback once (before first blur)
if (!hasBlurred) {
setShowFeedback(false);
}

if (newDateInput === '') {
onQrItemChange(createEmptyQrItem(qItem, answerKey));
return;
Expand All @@ -117,10 +126,20 @@ function CustomDateTimeItem(props: CustomDateTimeItemProps) {
updateQRDateTime(newDateInput, timeInput, periodInput, is24HourNotation);
}

function handleDateBlur() {
setShowFeedback(true);
setHasBlurred(true); // From now on, feedback should stay visible
}

function handleTimeInputChange(newTimeInput: string, newPeriodInput: string) {
setTimeInput(newTimeInput);
setPeriodInput(newPeriodInput);

// Only suppress feedback once (before first blur)
if (!hasBlurred) {
setShowFeedback(false);
}

if (newTimeInput === '') {
updateQRDateTime(dateInput, '', '', false);
return;
Expand All @@ -134,6 +153,11 @@ function CustomDateTimeItem(props: CustomDateTimeItemProps) {
updateQRDateTime(dateInput, newTimeInput, newPeriodInput, is24HourNotation);
}

function handleTimeBlur() {
setShowFeedback(true);
setHasBlurred(true); // From now on, feedback should stay visible
}

function updateQRDateTime(
dateInput: string,
timeInput: string,
Expand Down Expand Up @@ -175,8 +199,8 @@ function CustomDateTimeItem(props: CustomDateTimeItemProps) {
timeInput={timeInput}
periodInput={periodInput}
is24HourNotation={is24HourNotation}
dateFeedback={dateFeedback ?? ''}
timeFeedback={timeFeedback ?? ''}
dateFeedback={showFeedback ? (dateFeedback ?? '') : ''}
timeFeedback={showFeedback ? (timeFeedback ?? '') : ''}
dateFocused={dateFocused}
displayPrompt={displayPrompt}
entryFormat={entryFormat}
Expand All @@ -186,6 +210,9 @@ function CustomDateTimeItem(props: CustomDateTimeItemProps) {
onSelectDate={handleSelectDate}
setDateFocused={setDateFocused}
onTimeInputChange={handleTimeInputChange}
onDateBlur={handleDateBlur}
onTimeBlur={handleTimeBlur}
showFeedback={showFeedback}
/>
</Stack>
);
Expand Down Expand Up @@ -220,6 +247,9 @@ function CustomDateTimeItem(props: CustomDateTimeItemProps) {
onSelectDate={handleSelectDate}
setDateFocused={setDateFocused}
onTimeInputChange={handleTimeInputChange}
onDateBlur={handleDateBlur}
onTimeBlur={handleTimeBlur}
showFeedback={showFeedback}
/>
}
dateFeedback={dateFeedback ?? undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface CustomTimeFieldProps extends PropsWithIsTabledRequiredAttribute {
isPartOfDateTime: boolean;
onTimeInputChange: (newInput: string) => void;
onPeriodChange: (newPeriod: string) => void;
onTimeBlur: () => void;
}

function CustomTimeField(props: CustomTimeFieldProps) {
Expand All @@ -54,7 +55,8 @@ function CustomTimeField(props: CustomTimeFieldProps) {
isPartOfDateTime,
isTabled,
onTimeInputChange,
onPeriodChange
onPeriodChange,
onTimeBlur
} = props;

const readOnlyVisualStyle = useRendererStylingStore.use.readOnlyVisualStyle();
Expand Down Expand Up @@ -83,6 +85,7 @@ function CustomTimeField(props: CustomTimeFieldProps) {
fullWidth
sx={{ flex: 1 }}
onChange={(e: ChangeEvent<HTMLInputElement>) => onTimeInputChange(e.target.value)}
onBlur={onTimeBlur}
label={displayPrompt}
placeholder="--:--"
disabled={readOnly && readOnlyVisualStyle === 'disabled'}
Expand All @@ -102,7 +105,8 @@ function CustomTimeField(props: CustomTimeFieldProps) {
displayEmpty
size="small"
sx={{ flex: 1 }}
onChange={(e) => onPeriodChange(e.target.value)}>
onChange={(e) => onPeriodChange(e.target.value)}
onBlur={onTimeBlur}>
<MenuItem value="">
<span style={{ color: grey['500'] }}>{is24HourNotation ? '-' : 'AM/PM'}</span>
</MenuItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ interface DateTimeFieldProps extends PropsWithIsTabledRequiredAttribute {
entryFormat: string;
readOnly: boolean;
onDateInputChange: (newDateInput: string) => void;
onDateBlur: () => void;
onSelectDate: (selectedDate: string) => void;
setDateFocused: Dispatch<SetStateAction<boolean>>;
onTimeInputChange: (newTimeInput: string, newPeriodInput: string) => void;
onTimeBlur: () => void;
showFeedback: boolean;
}

function DateTimeField(props: DateTimeFieldProps) {
Expand All @@ -59,9 +62,12 @@ function DateTimeField(props: DateTimeFieldProps) {
readOnly,
isTabled,
onDateInputChange,
onDateBlur,
onSelectDate,
setDateFocused,
onTimeInputChange
onTimeInputChange,
onTimeBlur,
showFeedback
} = props;

return (
Expand All @@ -71,7 +77,7 @@ function DateTimeField(props: DateTimeFieldProps) {
itemType={itemType}
valueDate={displayDate}
input={dateInput}
feedback={dateFeedback ?? ''}
feedback={showFeedback ? (dateFeedback ?? '') : ''}
isFocused={dateFocused}
displayPrompt={displayPrompt}
entryFormat={entryFormat}
Expand All @@ -80,6 +86,7 @@ function DateTimeField(props: DateTimeFieldProps) {
isTabled={isTabled}
setFocused={setDateFocused}
onInputChange={onDateInputChange}
onDateBlur={onDateBlur}
onSelectDate={onSelectDate}
/>
<CustomTimeField
Expand All @@ -88,13 +95,14 @@ function DateTimeField(props: DateTimeFieldProps) {
timeInput={timeInput}
periodInput={periodInput}
is24HourNotation={is24HourNotation}
feedback={timeFeedback ?? ''}
feedback={showFeedback ? (timeFeedback ?? '') : ''}
displayPrompt={displayPrompt}
readOnly={readOnly}
isPartOfDateTime={true}
isTabled={isTabled}
onTimeInputChange={(newTimeInput) => onTimeInputChange(newTimeInput, periodInput)}
onPeriodChange={(newPeriodInput) => onTimeInputChange(timeInput, newPeriodInput)}
onTimeBlur={onTimeBlur}
/>
</Stack>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface DecimalFieldProps extends PropsWithIsTabledRequiredAttribute {
readOnly: boolean;
calcExpUpdated: boolean;
onInputChange: (value: string) => void;
onBlur: () => void;
}

function DecimalField(props: DecimalFieldProps) {
Expand All @@ -48,7 +49,8 @@ function DecimalField(props: DecimalFieldProps) {
readOnly,
calcExpUpdated,
isTabled,
onInputChange
onInputChange,
onBlur
} = props;

const readOnlyVisualStyle = useRendererStylingStore.use.readOnlyVisualStyle();
Expand All @@ -60,6 +62,7 @@ function DecimalField(props: DecimalFieldProps) {
value={input}
error={!!feedback}
onChange={(event) => onInputChange(event.target.value)}
onBlur={onBlur}
disabled={readOnly && readOnlyVisualStyle === 'disabled'}
label={displayPrompt}
placeholder={entryFormat === '' ? '0.0' : entryFormat}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import useReadOnly from '../../../hooks/useReadOnly';
import { useQuestionnaireStore } from '../../../stores';
import Box from '@mui/material/Box';
import ItemLabel from '../ItemParts/ItemLabel';
import useShowFeedback from '../../../hooks/useShowFeedback';

interface DecimalItemProps extends BaseItemProps {
qItem: QuestionnaireItem;
Expand Down Expand Up @@ -81,6 +82,9 @@ function DecimalItem(props: DecimalItemProps) {
// Perform validation checks - there's no string-based input here
const feedback = useValidationFeedback(qItem, feedbackFromParent, input);

// Provides a way to hide the feedback when the user is typing
const { showFeedback, setShowFeedback, hasBlurred, setHasBlurred } = useShowFeedback();

// Process calculated expressions
const { calcExpUpdated } = useDecimalCalculatedExpression({
qItem: qItem,
Expand Down Expand Up @@ -108,9 +112,20 @@ function DecimalItem(props: DecimalItemProps) {
const parsedNewInput: string = parseDecimalStringWithPrecision(newInput, precision);

setInput(parsedNewInput);

// Only suppress feedback once (before first blur)
if (!hasBlurred) {
setShowFeedback(false);
}

updateQrItemWithDebounce(parsedNewInput);
}

function handleBlur() {
setShowFeedback(true);
setHasBlurred(true); // From now on, feedback should stay visible
}

// eslint-disable-next-line react-hooks/exhaustive-deps
const updateQrItemWithDebounce = useCallback(
debounce((parsedNewInput: string) => {
Expand Down Expand Up @@ -149,6 +164,7 @@ function DecimalItem(props: DecimalItemProps) {
calcExpUpdated={calcExpUpdated}
isTabled={isTabled}
onInputChange={handleInputChange}
onBlur={handleBlur}
/>
</Box>
);
Expand All @@ -168,14 +184,15 @@ function DecimalItem(props: DecimalItemProps) {
linkId={qItem.linkId}
itemType={qItem.type}
input={input}
feedback={feedback}
feedback={showFeedback ? feedback : ''}
displayPrompt={displayPrompt}
displayUnit={displayUnit}
entryFormat={entryFormat}
readOnly={readOnly}
calcExpUpdated={calcExpUpdated}
isTabled={isTabled}
onInputChange={handleInputChange}
onBlur={handleBlur}
/>
}
feedback={feedback}
Expand Down
Loading
Loading