Skip to content

Commit

Permalink
enhance: not allow change for locked
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Nov 12, 2024
1 parent e1d6848 commit 4dd71ee
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
41 changes: 22 additions & 19 deletions docs/examples/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,35 @@ export default () => {
{...sharedLocale}
style={{ width: 400 }}
showTime
// allowEmpty
// disabledDate={(_, info) => {
// console.log('Date:', info);
// return false;
// }}
disabledTime={(date, range, info) => {
// console.log(`Time-${range}`, range, info);
const { from } = info;
// disabledTime={(date, range, info) => {
// // console.log(`Time-${range}`, range, info);
// const { from } = info;

if (from) {
console.log(
`Time-${range}`,
from.format('YYYY-MM-DD HH:mm:ss'),
date.format('YYYY-MM-DD HH:mm:ss'),
);
}
// if (from) {
// console.log(
// `Time-${range}`,
// from.format('YYYY-MM-DD HH:mm:ss'),
// date.format('YYYY-MM-DD HH:mm:ss'),
// );
// }

if (from && from.isSame(date, 'day')) {
return {
disabledHours: () => [from.hour()],
disabledMinutes: () => [0, 1, 2, 3],
disabledSeconds: () => [0, 1, 2, 3],
};
}
return {};
}}
// if (from && from.isSame(date, 'day')) {
// return {
// disabledHours: () => [from.hour()],
// disabledMinutes: () => [0, 1, 2, 3],
// disabledSeconds: () => [0, 1, 2, 3],
// };
// }
// return {};
// }}
/>

<RangePicker {...sharedLocale} style={{ width: 400 }} allowEmpty />
{/* <SinglePicker
{...dateFnsSharedLocale}
style={{ width: 400 }}
Expand Down
20 changes: 19 additions & 1 deletion src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import useRangeDisabledDate from './hooks/useRangeDisabledDate';
import useRangePickerValue from './hooks/useRangePickerValue';
import useRangeValue, { useInnerValue } from './hooks/useRangeValue';
import useShowNow from './hooks/useShowNow';
import Popup, { PopupShowTimeConfig } from './Popup';
import Popup, { type PopupShowTimeConfig } from './Popup';
import RangeSelector, { type SelectorIdType } from './Selector/RangeSelector';

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

// ======================= Selector =======================
const onSelectorFocus: SelectorProps['onFocus'] = (event, index) => {
// Check if `needConfirm` but user not submit yet
const activeListLen = activeIndexList.length;
const lastActiveIndex = activeIndexList[activeListLen - 1];
if (
activeListLen &&
lastActiveIndex !== index &&
needConfirm &&
// Not change index if is not filled
!allowEmpty[lastActiveIndex] &&
!hasSubmitValue(lastActiveIndex) &&
calendarValue[lastActiveIndex]
) {
selectorRef.current.focus({ index: lastActiveIndex });
return;
}

lastOperation('input');

triggerOpen(true, {
Expand Down
23 changes: 23 additions & 0 deletions src/PickerInput/hooks/useRangeActiveLock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';

export default function useRangeActiveLock(): [
focused: boolean,
triggerFocus: (focused: boolean) => void,
// lastOperation: (type?: OperationType) => OperationType,
// activeIndex: number,
setActiveIndex: (index: number) => void,
// nextActiveIndex: NextActive<DateType>,
// activeList: number[],
] {
const [activeIndex, setActiveIndex] = React.useState<number | null>(null);
const [focused, setFocused] = React.useState<boolean>(false);
const [activeList, setActiveList] = React.useState<number[]>([]);

// ============================= Active =============================
const onActive = (index: number) => {
setActiveIndex(index);
setActiveList([...activeList, index]);
};

return [focused, setFocused, onActive];
}
11 changes: 9 additions & 2 deletions src/PickerInput/hooks/useRangeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function orderDates<DateType extends object, DatesType extends DateType[]>(
* Used for internal value management.
* It should always use `mergedValue` in render logic
*/
export function useCalendarValue<MergedValueType extends object[]>(mergedValue: MergedValueType) {
function useCalendarValue<MergedValueType extends object[]>(mergedValue: MergedValueType) {
const [calendarValue, setCalendarValue] = useSyncState(mergedValue);

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

// ============================ Check =============================
function hasSubmitValue(index: number) {
return !!submitValue()[index];
}

// ============================ Return ============================
return [flushSubmit, triggerSubmit];
return [flushSubmit, triggerSubmit, hasSubmitValue];
}

0 comments on commit 4dd71ee

Please sign in to comment.