Skip to content

Commit 4dd71ee

Browse files
committed
enhance: not allow change for locked
1 parent e1d6848 commit 4dd71ee

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-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, {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react';
2+
3+
export default function useRangeActiveLock(): [
4+
focused: boolean,
5+
triggerFocus: (focused: boolean) => void,
6+
// lastOperation: (type?: OperationType) => OperationType,
7+
// activeIndex: number,
8+
setActiveIndex: (index: number) => void,
9+
// nextActiveIndex: NextActive<DateType>,
10+
// activeList: number[],
11+
] {
12+
const [activeIndex, setActiveIndex] = React.useState<number | null>(null);
13+
const [focused, setFocused] = React.useState<boolean>(false);
14+
const [activeList, setActiveList] = React.useState<number[]>([]);
15+
16+
// ============================= Active =============================
17+
const onActive = (index: number) => {
18+
setActiveIndex(index);
19+
setActiveList([...activeList, index]);
20+
};
21+
22+
return [focused, setFocused, onActive];
23+
}

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
}

0 commit comments

Comments
 (0)