File tree Expand file tree Collapse file tree
packages/features/schedules/components Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -74,6 +74,7 @@ export const ScheduleDay = <TFieldValues extends FieldValues>({
7474} ) => {
7575 const { watch, setValue } = useFormContext ( ) ;
7676 const watchDayRange = watch ( name ) ;
77+ const lastNonEmptyDayRangeRef = useRef < TimeRange [ ] | null > ( null ) ;
7778
7879 return (
7980 < div
@@ -97,7 +98,20 @@ export const ScheduleDay = <TFieldValues extends FieldValues>({
9798 checked = { watchDayRange && ! ! watchDayRange . length }
9899 data-testid = { `${ weekday } -switch` }
99100 onCheckedChange = { ( isChecked ) => {
100- setValue ( name , ( isChecked ? [ DEFAULT_DAY_RANGE ] : [ ] ) as TFieldValues [ typeof name ] ) ;
101+ if ( isChecked ) {
102+ const previousDayRange = lastNonEmptyDayRangeRef . current ;
103+ const newValue =
104+ ( previousDayRange && previousDayRange . length > 0
105+ ? previousDayRange
106+ : [ DEFAULT_DAY_RANGE ] ) as TFieldValues [ typeof name ] ;
107+
108+ setValue ( name , newValue ) ;
109+ } else {
110+ if ( watchDayRange && watchDayRange . length > 0 ) {
111+ lastNonEmptyDayRangeRef . current = watchDayRange as unknown as TimeRange [ ] ;
112+ }
113+ setValue ( name , [ ] as TFieldValues [ typeof name ] ) ;
114+ }
101115 } }
102116 />
103117 </ div >
You can’t perform that action at this time.
0 commit comments