Skip to content

Commit

Permalink
feat(Date Picker): implement formatDate method to handle single, mult…
Browse files Browse the repository at this point in the history
…i and range date formats
  • Loading branch information
Cata1989 committed May 17, 2024
1 parent 63c5886 commit d1ccf0b
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions packages/beeq/src/components/date-picker/bq-date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, Element, Event, EventEmitter, h, Listen, Method, Prop, State
import { DaysOfWeek } from './scss/bq-date-picker.types';
import { FloatingUIPlacement } from '../../services/interfaces';
import { hasSlotContent, isDefined, isHTMLElement } from '../../shared/utils';
import { TInputValidation, TInputValue } from '../input/bq-input.types';
import { TInputValidation } from '../input/bq-input.types';

/**
* @part base - The component's base wrapper.
Expand Down Expand Up @@ -340,21 +340,16 @@ export class BqDatePicker {
/**
* Processes the focused date value to extract the last date portion.
*
* @param value - The value to be processed, can be a string, number, or string array.
* @param value - The value to be processed, can be a string.
* @returns The extracted last date portion of the value.
*/
private processFocusedDateValue = (value: TInputValue) => {
private processFocusedDateValue = (value: string) => {
const dateLength = 10; // Length of a standard date in the format YYYY-MM-DD

if (typeof value === 'string') {
if (value) {
return value.slice(-dateLength);
}
if (typeof value === 'number') {
return value.toString();
}
if (Array.isArray(value)) {
return value[value.length - 1];
}

return null;
};

Expand Down Expand Up @@ -385,6 +380,29 @@ export class BqDatePicker {
this.open = !!this.multi;
};

private formatDate = (value: string): string | undefined => {
if (!value) return;

const formatOptions: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'short',
year: 'numeric',
};
const dateFormatter = new Intl.DateTimeFormat(this.locale, formatOptions);

if (this.range) {
const [start, end] = value.split('/').map((dateStr) => new Date(dateStr));
return dateFormatter.formatRange(start, end);
}

if (this.multi) {
const dates = value.split(' ').map((dateStr) => new Date(dateStr));
return dates.map((date) => dateFormatter.format(date)).join(', ');
}

return dateFormatter.format(new Date(value));
};

// render() function
// Always the last one in the class.
// ===================================
Expand All @@ -394,6 +412,7 @@ export class BqDatePicker {

const CallyCalendar = this.CalendarType;

console.log('gggg', this.processFocusedDateValue(this.value));
return (
<div class="bq-date-picker" part="base">
{/* Label */}
Expand Down Expand Up @@ -454,7 +473,7 @@ export class BqDatePicker {
required={this.required}
spellcheck={false}
type="text"
value={this.value}
value={this.formatDate(this.value)}
part="input"
// Events
onBlur={this.handleBlur}
Expand Down

0 comments on commit d1ccf0b

Please sign in to comment.