Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUR-370, SRML-64 - Date Picker component issues and improvements #234

Merged
merged 7 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Version 1.3.4 - 27th December, 2024
- Improvement - Enhanced the UI of the Table and Line chart component for responsive design.
- Improvement - Added option group to the Select component.
- Improvement - Updated the Table component design.
- Improvement - Added support for controlling selected dates through the 'selected' prop in DatePicker component.
- Fixed - DatePicker component crash when navigating through years.

Version 1.3.3 - 20th December, 2024
- Fixed - React `Each child in a list should have a unique "key" prop` console warning.
Expand Down
6 changes: 3 additions & 3 deletions src/components/accordion/accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default {
},
tags: [ 'autodocs' ],
subcomponents: {
Item: Accordion.Item,
Trigger: Accordion.Trigger,
Content: Accordion.Content,
'Accordion.Item': Accordion.Item,
'Accordion.Trigger': Accordion.Trigger,
'Accordion.Content': Accordion.Content,
},
decorators: [
( Story ) => (
Expand Down
10 changes: 2 additions & 8 deletions src/components/datepicker/datepicker-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const DatePickerComponent = ( {
}
};

const handleYearClick = ( { yearValue }: { yearValue: number } ) => {
const handleYearClick = ( yearValue: number ) => {
setSelectedYear( yearValue );
setShowYearSelect( false );
setShowMonthSelect( true );
Expand Down Expand Up @@ -245,13 +245,7 @@ const DatePickerComponent = ( {
<Button
key={ yearValue }
variant="ghost"
onClick={ () =>
handleYearClick(
yearValue as unknown as {
yearValue: number;
}
)
}
onClick={ () => handleYearClick( yearValue ) }
className={ cn(
'h-10 w-full text-center font-normal relative',
yearValue === selectedYear &&
Expand Down
26 changes: 21 additions & 5 deletions src/components/datepicker/datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
subWeeks,
subMonths,
} from 'date-fns';
import { getDefaultSelectedValue } from './utils';

export interface DatePickerProps {
/** Defines the selection selectionType of the date picker: single, range, or multiple dates. */
Expand All @@ -33,6 +34,8 @@ export interface DatePickerProps {
showOutsideDays?: boolean;
/** Show or hide the footer. */
isFooter?: boolean;
/** Selected date value. */
selected?: Date | Date[] | TDateRange | null;
}

const DatePicker = ( {
Expand All @@ -46,17 +49,30 @@ const DatePicker = ( {
cancelButtonText = 'Cancel',
showOutsideDays = true,
isFooter = true,
selected,
...props
}: DatePickerProps ) => {
const [ selectedDates, setSelectedDates ] = useState<
TDateRange | Date | Date[] | null
>( () => {
if ( selectionType === 'multiple' ) {
return [];
} else if ( selectionType === 'range' ) {
return { from: null, to: null };
if ( ! selected ) {
return getDefaultSelectedValue( selectionType );
}
return null;

// Type guards for different selection types
const isValidMultiple =
selectionType === 'multiple' && Array.isArray( selected );
const isValidRange =
selectionType === 'range' && 'from' in selected && 'to' in selected;
const isValidSingle =
selectionType === 'single' && selected instanceof Date;

// Return selected if valid, otherwise return default value
if ( isValidMultiple || isValidRange || isValidSingle ) {
return selected;
}

return getDefaultSelectedValue( selectionType );
} );

const handleSelect = ( selectedDate: Date | Date[] | TDateRange | null ) => {
Expand Down
10 changes: 10 additions & 0 deletions src/components/datepicker/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ export const formatWeekdayName = ( date: Date ) => {
export const generateYearRange = ( start: number, count = 24 ) => {
return Array.from( { length: count }, ( _, i ) => start + i );
};

export const getDefaultSelectedValue = ( type: string ) => {
if ( type === 'multiple' ) {
return [];
}
if ( type === 'range' ) {
return { from: null, to: null };
}
return null;
};
Loading