Skip to content

Commit 07c9443

Browse files
balzssHerrTopi
authored andcommitted
fix(ui-date-time-input,ui-form-field): make DateTimeInput compatible with the new error format
1 parent b52ff30 commit 07c9443

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

docs/guides/form-errors.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,34 @@ const Example = () => {
135135
renderMessages={() => messages}
136136
/>
137137

138+
<DateTimeInput
139+
description={`DateTimeInput (layout="column")`}
140+
datePlaceholder="Choose a date"
141+
dateRenderLabel="Date"
142+
timeRenderLabel="Time"
143+
invalidDateTimeMessage="Invalid date!"
144+
prevMonthLabel="Previous month"
145+
nextMonthLabel="Next month"
146+
defaultValue="2018-01-18T13:30"
147+
layout="columns"
148+
isRequired={isRequired}
149+
messages={messages}
150+
/>
151+
152+
<DateTimeInput
153+
description={`DateTimeInput (layout="stacked")`}
154+
datePlaceholder="Choose a date"
155+
dateRenderLabel="Date"
156+
timeRenderLabel="Time"
157+
invalidDateTimeMessage="Invalid date!"
158+
prevMonthLabel="Previous month"
159+
nextMonthLabel="Next month"
160+
defaultValue="2018-01-18T13:30"
161+
layout="stacked"
162+
isRequired={isRequired}
163+
messages={messages}
164+
/>
165+
138166
</div>
139167
</div>
140168
)

packages/ui-date-time-input/src/DateTimeInput/__new-tests__/DateTimeInput.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ describe('<DateTimeInput />', () => {
173173
invalidDateTimeMessage={invalidDateTimeMessage}
174174
/>
175175
)
176-
const timeInput = screen.getByLabelText('time-input')
176+
const timeInput = screen.getByLabelText('time-input *')
177177

178178
await userEvent.type(timeInput, '1:00 PM')
179179
fireEvent.blur(timeInput)

packages/ui-date-time-input/src/DateTimeInput/index.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
165165
? this.props.invalidDateTimeMessage(parsed.toISOString(true))
166166
: this.props.invalidDateTimeMessage
167167
}
168-
errorMsg = text ? { text, type: 'error' } : undefined
168+
errorMsg = text ? { text, type: 'newError' } : undefined
169169
return {
170170
iso: parsed.clone(),
171171
calendarSelectedDate: parsed.clone(),
@@ -348,7 +348,7 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
348348
? this.props.invalidDateTimeMessage(dateStr ? dateStr : '')
349349
: this.props.invalidDateTimeMessage
350350
// eslint-disable-next-line no-param-reassign
351-
newState.message = { text: text, type: 'error' }
351+
newState.message = { text: text, type: 'newError' }
352352
}
353353
if (this.areDifferentDates(this.state.iso, newState.iso)) {
354354
if (typeof this.props.onChange === 'function') {
@@ -567,6 +567,17 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
567567
allowNonStepInput
568568
} = this.props
569569

570+
const allMessages = [
571+
...(showMessages && this.state.message ? [this.state.message] : []),
572+
...(messages || [])
573+
]
574+
575+
const hasError = allMessages.find((m) => m.type === 'newError')
576+
// 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
577+
const subComponentMessages: FormMessage[] = hasError
578+
? [{ type: 'newError', text: '' }]
579+
: []
580+
570581
return (
571582
<FormFieldGroup
572583
description={description}
@@ -575,6 +586,7 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
575586
colSpacing={colSpacing}
576587
vAlign="top"
577588
elementRef={this.handleRef}
589+
isGroup={false}
578590
messages={[
579591
...(showMessages && this.state.message ? [this.state.message] : []),
580592
...(messages || [])
@@ -601,6 +613,7 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
601613
onRequestRenderNextMonth={this.handleRenderNextMonth}
602614
onRequestRenderPrevMonth={this.handleRenderPrevMonth}
603615
isRequired={isRequired}
616+
messages={subComponentMessages}
604617
interaction={interaction}
605618
renderNavigationLabel={
606619
<span>
@@ -624,6 +637,8 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
624637
inputRef={timeInputRef}
625638
interaction={interaction}
626639
allowNonStepInput={allowNonStepInput}
640+
isRequired={isRequired}
641+
messages={subComponentMessages}
627642
/>
628643
</FormFieldGroup>
629644
)

packages/ui-form-field/src/FormFieldGroup/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class FormFieldGroup extends Component<FormFieldGroupProps> {
133133
}
134134

135135
render() {
136-
const { styles, makeStyles, ...props } = this.props
136+
const { styles, makeStyles, isGroup, ...props } = this.props
137137

138138
return (
139139
<FormFieldLayout
@@ -145,7 +145,7 @@ class FormFieldGroup extends Component<FormFieldGroupProps> {
145145
aria-disabled={props.disabled ? 'true' : undefined}
146146
aria-invalid={this.invalid ? 'true' : undefined}
147147
elementRef={this.handleRef}
148-
isGroup
148+
isGroup={isGroup}
149149
>
150150
{this.renderFields()}
151151
</FormFieldLayout>

packages/ui-form-field/src/FormFieldMessage/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class FormFieldMessage extends Component<FormFieldMessageProps> {
8181

8282
return this.props.variant !== 'screenreader-only' ? (
8383
<span css={{ display: 'flex' }}>
84-
{this.props.variant === 'newError' && (
84+
{this.props.variant === 'newError' && this.props.children && (
8585
<span css={styles?.errorIcon}>
8686
<IconWarningSolid color="error" />
8787
</span>

0 commit comments

Comments
 (0)