Skip to content

Commit

Permalink
fix(timepicker): formatValue
Browse files Browse the repository at this point in the history
  • Loading branch information
aesteves60 authored and dpellier committed Jul 29, 2024
1 parent fb5246c commit b8353ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export class OdsTimepicker {
}

@Watch('withSeconds')
@Watch('value')
onValueChange(): void {
formatValue(): void {
const value = formatValue(this.odsInput?.value as string, this.withSeconds);

if (value) {
Expand All @@ -85,8 +84,8 @@ export class OdsTimepicker {
this.value = this.defaultValue ?? null;
}
this.initTimezones();
this.formatValue();
this.defaultCurrentTimezone = this.currentTimezone;
setFormValue(this.internals, this.value);
}

async formResetCallback(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import { ODS_TIMEZONES_PRESET } from '../constant/timezone-preset';
import { type ODS_TIMEZONE, ODS_TIMEZONES, type OdsTimezone } from '../constant/timezones';

function formatValue(value?: string, withSeconds?: boolean): string {
if (!withSeconds && value?.match(/:/g)?.length === 2) {
const inputValue = value.split(':');
return `${ inputValue[0] }:${ inputValue[1] }`;
const numberOfTwoPoint = value?.match(/:/g)?.length;
if (!withSeconds && numberOfTwoPoint) {
if (numberOfTwoPoint === 1) {
return value;
}
if (numberOfTwoPoint === 2) {
const inputValue = value.split(':');
return `${ inputValue[0] }:${ inputValue[1] }`;
}
}

if (withSeconds) {
if (value?.match(/:/g)?.length === 1) {
if (withSeconds && numberOfTwoPoint) {
if (numberOfTwoPoint === 1) {
return `${ value }:00`;
}
if (value?.match(/:/g)?.length === 2) {
if (numberOfTwoPoint === 2) {
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ describe('ods-timepicker controller', () => {
expect(formatValue('12:34:56', false)).toBe('12:34');
});

it('should format value without seconds correctly', () => {
expect(formatValue('12:34', false)).toBe('12:34');
});

it('should format value with seconds correctly', () => {
expect(formatValue('12:34', true)).toBe('12:34:00');
});
Expand Down

0 comments on commit b8353ca

Please sign in to comment.