Skip to content

Commit 62cf191

Browse files
authored
enhance: Lock switch field if needConfirm (#892)
* enhance: not allow change for locked * test: add test case * chore: clean up
1 parent e1d6848 commit 62cf191

File tree

4 files changed

+67
-22
lines changed

4 files changed

+67
-22
lines changed

docs/examples/debug.tsx

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,35 @@ export default () => {
5555
{...sharedLocale}
5656
style={{ width: 400 }}
5757
showTime
58+
// allowEmpty
5859
// disabledDate={(_, info) => {
5960
// console.log('Date:', info);
6061
// return false;
6162
// }}
62-
disabledTime={(date, range, info) => {
63-
// console.log(`Time-${range}`, range, info);
64-
const { from } = info;
63+
// disabledTime={(date, range, info) => {
64+
// // console.log(`Time-${range}`, range, info);
65+
// const { from } = info;
6566

66-
if (from) {
67-
console.log(
68-
`Time-${range}`,
69-
from.format('YYYY-MM-DD HH:mm:ss'),
70-
date.format('YYYY-MM-DD HH:mm:ss'),
71-
);
72-
}
67+
// if (from) {
68+
// console.log(
69+
// `Time-${range}`,
70+
// from.format('YYYY-MM-DD HH:mm:ss'),
71+
// date.format('YYYY-MM-DD HH:mm:ss'),
72+
// );
73+
// }
7374

74-
if (from && from.isSame(date, 'day')) {
75-
return {
76-
disabledHours: () => [from.hour()],
77-
disabledMinutes: () => [0, 1, 2, 3],
78-
disabledSeconds: () => [0, 1, 2, 3],
79-
};
80-
}
81-
return {};
82-
}}
75+
// if (from && from.isSame(date, 'day')) {
76+
// return {
77+
// disabledHours: () => [from.hour()],
78+
// disabledMinutes: () => [0, 1, 2, 3],
79+
// disabledSeconds: () => [0, 1, 2, 3],
80+
// };
81+
// }
82+
// return {};
83+
// }}
8384
/>
85+
86+
<RangePicker {...sharedLocale} style={{ width: 400 }} allowEmpty />
8487
{/* <SinglePicker
8588
{...dateFnsSharedLocale}
8689
style={{ width: 400 }}

src/PickerInput/RangePicker.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import useRangeDisabledDate from './hooks/useRangeDisabledDate';
3333
import useRangePickerValue from './hooks/useRangePickerValue';
3434
import useRangeValue, { useInnerValue } from './hooks/useRangeValue';
3535
import useShowNow from './hooks/useShowNow';
36-
import Popup, { PopupShowTimeConfig } from './Popup';
36+
import Popup, { type PopupShowTimeConfig } from './Popup';
3737
import RangeSelector, { type SelectorIdType } from './Selector/RangeSelector';
3838

3939
function separateConfig<T>(config: T | [T, T] | null | undefined, defaultConfig: T): [T, T] {
@@ -325,6 +325,8 @@ function RangePicker<DateType extends object = any>(
325325
flushSubmit,
326326
/** Trigger `onChange` directly without check `disabledDate` */
327327
triggerSubmitChange,
328+
/** Tell `index` has filled value in it */
329+
hasSubmitValue,
328330
] = useRangeValue<RangeValueType<DateType>, DateType>(
329331
filledProps,
330332
mergedValue,
@@ -630,6 +632,22 @@ function RangePicker<DateType extends object = any>(
630632

631633
// ======================= Selector =======================
632634
const onSelectorFocus: SelectorProps['onFocus'] = (event, index) => {
635+
// Check if `needConfirm` but user not submit yet
636+
const activeListLen = activeIndexList.length;
637+
const lastActiveIndex = activeIndexList[activeListLen - 1];
638+
if (
639+
activeListLen &&
640+
lastActiveIndex !== index &&
641+
needConfirm &&
642+
// Not change index if is not filled
643+
!allowEmpty[lastActiveIndex] &&
644+
!hasSubmitValue(lastActiveIndex) &&
645+
calendarValue[lastActiveIndex]
646+
) {
647+
selectorRef.current.focus({ index: lastActiveIndex });
648+
return;
649+
}
650+
633651
lastOperation('input');
634652

635653
triggerOpen(true, {

src/PickerInput/hooks/useRangeValue.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function orderDates<DateType extends object, DatesType extends DateType[]>(
7676
* Used for internal value management.
7777
* It should always use `mergedValue` in render logic
7878
*/
79-
export function useCalendarValue<MergedValueType extends object[]>(mergedValue: MergedValueType) {
79+
function useCalendarValue<MergedValueType extends object[]>(mergedValue: MergedValueType) {
8080
const [calendarValue, setCalendarValue] = useSyncState(mergedValue);
8181

8282
/** Sync calendarValue & submitValue back with value */
@@ -186,6 +186,8 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
186186
flushSubmit: (index: number, needTriggerChange: boolean) => void,
187187
/** Trigger `onChange` directly without check `disabledDate` */
188188
triggerSubmitChange: (value: ValueType) => boolean,
189+
/** Tell `index` has filled value in it */
190+
hasSubmitValue: (index: number) => boolean,
189191
] {
190192
const {
191193
// MISC
@@ -331,6 +333,11 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
331333
2,
332334
);
333335

336+
// ============================ Check =============================
337+
function hasSubmitValue(index: number) {
338+
return !!submitValue()[index];
339+
}
340+
334341
// ============================ Return ============================
335-
return [flushSubmit, triggerSubmit];
342+
return [flushSubmit, triggerSubmit, hasSubmitValue];
336343
}

tests/range.spec.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe('Picker.Range', () => {
4949
resetWarned();
5050
global.scrollCalled = false;
5151
jest.useFakeTimers().setSystemTime(getDay('1990-09-03 00:00:00').valueOf());
52+
document.body.innerHTML = '';
5253
});
5354

5455
afterEach(() => {
@@ -2054,4 +2055,20 @@ describe('Picker.Range', () => {
20542055
'rc-picker-input-active',
20552056
);
20562057
});
2058+
2059+
it('should not click to focus on next field if first field is not confirm', () => {
2060+
const onCalendarChange = jest.fn();
2061+
const { container } = render(
2062+
<DayRangePicker onCalendarChange={onCalendarChange} showTime needConfirm />,
2063+
);
2064+
2065+
// Select first field
2066+
openPicker(container, 0);
2067+
selectCell(11);
2068+
expect(onCalendarChange).toHaveBeenCalled();
2069+
2070+
// Not click confirm and click next field
2071+
openPicker(container, 1);
2072+
expect(container.querySelectorAll('.rc-picker-input')[0]).toHaveClass('rc-picker-input-active');
2073+
});
20572074
});

0 commit comments

Comments
 (0)