diff --git a/packages/genesys-spark-components/src/components/stable/gux-time-picker/gux-time-picker.scss b/packages/genesys-spark-components/src/components/stable/gux-time-picker/gux-time-picker.scss index b9727d7b7..7b85b4bcc 100644 --- a/packages/genesys-spark-components/src/components/stable/gux-time-picker/gux-time-picker.scss +++ b/packages/genesys-spark-components/src/components/stable/gux-time-picker/gux-time-picker.scss @@ -1,5 +1,8 @@ @use '~genesys-spark/dist/scss/mixins.scss'; @use '~genesys-spark/dist/scss/focus.scss'; +@use '../gux-form-field/functional-components/gux-form-field-error/gux-form-field-error.scss'; + +@include gux-form-field-error.Style; .gux-time-picker { position: relative; diff --git a/packages/genesys-spark-components/src/components/stable/gux-time-picker/gux-time-picker.tsx b/packages/genesys-spark-components/src/components/stable/gux-time-picker/gux-time-picker.tsx index ad925dfbe..e829f759f 100644 --- a/packages/genesys-spark-components/src/components/stable/gux-time-picker/gux-time-picker.tsx +++ b/packages/genesys-spark-components/src/components/stable/gux-time-picker/gux-time-picker.tsx @@ -9,6 +9,9 @@ import { trackComponent } from '@utils/tracking/usage'; import translationResources from './i18n/en.json'; +import { GuxFormFieldError } from '../gux-form-field/functional-components/functional-components'; +import { randomHTMLId } from '@utils/dom/random-html-id'; + import { GuxClockType, GuxISOHourMinute, @@ -42,6 +45,7 @@ export class GuxTimePicker { private minuteInputElement: HTMLInputElement; private i18n: GetI18nValue; private valueLastChange: GuxISOHourMinute; + private errorMessageId: string = randomHTMLId('gux-time-picker-eror'); @Element() private root: HTMLElement; @@ -70,6 +74,9 @@ export class GuxTimePicker { @State() expanded: boolean = false; + @State() + hasInputError: boolean = false; + @Listen('focus') onFocus() { this.valueLastChange = this.value; @@ -78,6 +85,26 @@ export class GuxTimePicker { @Listen('blur') onBlur() { if (this.valueLastChange !== this.value) { + // Format input time to match format found in the popup time list (e.g. "01:30" -> "1:30", "00:30" -> "12:30", etc) + const split = this.value.split(':'); + const hourParsed = parseInt(split[0], 10); + let hour = hourParsed === 0 ? 12 : hourParsed; + hour = hour > 12 ? hour % 12 : hour; + const minutes = split[1]; + const valueFormatted = `${hour}:${minutes}`; + + // Check if the input value is in the popup time list + const valueIsValid = getTimeDisplayValues( + this.interval, + this.clockType + ).find(displayValue => displayValue === valueFormatted); + + if (!valueIsValid) { + this.hasInputError = true; + } else { + this.hasInputError = false; + } + simulateNativeEvent(this.root, 'change'); } } @@ -267,6 +294,7 @@ export class GuxTimePicker { return (
{this.i18n('time-separator')} + {error} + + ) as JSX.Element; + } + render(): JSX.Element { return ( - - {this.renderTarget()} - {this.renderPopup()} - +
+ + {this.renderTarget()} + {this.renderPopup()} + + {this.maybeRenderInputError()} +
) as JSX.Element; } }