From 07c9443116dc53b1d2da317e42f314ce3e9b9c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20S=C3=A1ros?= Date: Thu, 12 Dec 2024 16:42:36 +0100 Subject: [PATCH] fix(ui-date-time-input,ui-form-field): make DateTimeInput compatible with the new error format --- docs/guides/form-errors.md | 28 +++++++++++++++++++ .../__new-tests__/DateTimeInput.test.tsx | 2 +- .../src/DateTimeInput/index.tsx | 19 +++++++++++-- .../src/FormFieldGroup/index.tsx | 4 +-- .../src/FormFieldMessage/index.tsx | 2 +- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/docs/guides/form-errors.md b/docs/guides/form-errors.md index baed5bc11b..bcef0a474d 100644 --- a/docs/guides/form-errors.md +++ b/docs/guides/form-errors.md @@ -135,6 +135,34 @@ const Example = () => { renderMessages={() => messages} /> + + + + ) diff --git a/packages/ui-date-time-input/src/DateTimeInput/__new-tests__/DateTimeInput.test.tsx b/packages/ui-date-time-input/src/DateTimeInput/__new-tests__/DateTimeInput.test.tsx index 10f767e30d..fe56011c7e 100644 --- a/packages/ui-date-time-input/src/DateTimeInput/__new-tests__/DateTimeInput.test.tsx +++ b/packages/ui-date-time-input/src/DateTimeInput/__new-tests__/DateTimeInput.test.tsx @@ -173,7 +173,7 @@ describe('', () => { invalidDateTimeMessage={invalidDateTimeMessage} /> ) - const timeInput = screen.getByLabelText('time-input') + const timeInput = screen.getByLabelText('time-input *') await userEvent.type(timeInput, '1:00 PM') fireEvent.blur(timeInput) diff --git a/packages/ui-date-time-input/src/DateTimeInput/index.tsx b/packages/ui-date-time-input/src/DateTimeInput/index.tsx index 06e82ab77e..0ad30c1a58 100644 --- a/packages/ui-date-time-input/src/DateTimeInput/index.tsx +++ b/packages/ui-date-time-input/src/DateTimeInput/index.tsx @@ -165,7 +165,7 @@ class DateTimeInput extends Component { ? this.props.invalidDateTimeMessage(parsed.toISOString(true)) : this.props.invalidDateTimeMessage } - errorMsg = text ? { text, type: 'error' } : undefined + errorMsg = text ? { text, type: 'newError' } : undefined return { iso: parsed.clone(), calendarSelectedDate: parsed.clone(), @@ -348,7 +348,7 @@ class DateTimeInput extends Component { ? this.props.invalidDateTimeMessage(dateStr ? dateStr : '') : this.props.invalidDateTimeMessage // eslint-disable-next-line no-param-reassign - newState.message = { text: text, type: 'error' } + newState.message = { text: text, type: 'newError' } } if (this.areDifferentDates(this.state.iso, newState.iso)) { if (typeof this.props.onChange === 'function') { @@ -567,6 +567,17 @@ class DateTimeInput extends Component { allowNonStepInput } = this.props + const allMessages = [ + ...(showMessages && this.state.message ? [this.state.message] : []), + ...(messages || []) + ] + + const hasError = allMessages.find((m) => m.type === 'newError') + // if the component is in error state, create an empty error message to pass down to the subcomponents (DateInput and TimeInput) so they get a red outline and red required asterisk + const subComponentMessages: FormMessage[] = hasError + ? [{ type: 'newError', text: '' }] + : [] + return ( { colSpacing={colSpacing} vAlign="top" elementRef={this.handleRef} + isGroup={false} messages={[ ...(showMessages && this.state.message ? [this.state.message] : []), ...(messages || []) @@ -601,6 +613,7 @@ class DateTimeInput extends Component { onRequestRenderNextMonth={this.handleRenderNextMonth} onRequestRenderPrevMonth={this.handleRenderPrevMonth} isRequired={isRequired} + messages={subComponentMessages} interaction={interaction} renderNavigationLabel={ @@ -624,6 +637,8 @@ class DateTimeInput extends Component { inputRef={timeInputRef} interaction={interaction} allowNonStepInput={allowNonStepInput} + isRequired={isRequired} + messages={subComponentMessages} /> ) diff --git a/packages/ui-form-field/src/FormFieldGroup/index.tsx b/packages/ui-form-field/src/FormFieldGroup/index.tsx index 340a1ddd54..16138eb518 100644 --- a/packages/ui-form-field/src/FormFieldGroup/index.tsx +++ b/packages/ui-form-field/src/FormFieldGroup/index.tsx @@ -133,7 +133,7 @@ class FormFieldGroup extends Component { } render() { - const { styles, makeStyles, ...props } = this.props + const { styles, makeStyles, isGroup, ...props } = this.props return ( { aria-disabled={props.disabled ? 'true' : undefined} aria-invalid={this.invalid ? 'true' : undefined} elementRef={this.handleRef} - isGroup + isGroup={isGroup} > {this.renderFields()} diff --git a/packages/ui-form-field/src/FormFieldMessage/index.tsx b/packages/ui-form-field/src/FormFieldMessage/index.tsx index 37d0a5f2e6..62d00dd1fe 100644 --- a/packages/ui-form-field/src/FormFieldMessage/index.tsx +++ b/packages/ui-form-field/src/FormFieldMessage/index.tsx @@ -81,7 +81,7 @@ class FormFieldMessage extends Component { return this.props.variant !== 'screenreader-only' ? ( - {this.props.variant === 'newError' && ( + {this.props.variant === 'newError' && this.props.children && (