Skip to content

Commit

Permalink
enhance: Lock switch field if needConfirm (#892)
Browse files Browse the repository at this point in the history
* enhance: not allow change for locked

* test: add test case

* chore: clean up
  • Loading branch information
zombieJ authored Nov 12, 2024
1 parent e1d6848 commit 62cf191
Show file tree
Hide file tree
Showing 4 changed files with 67 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
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];
}
17 changes: 17 additions & 0 deletions tests/range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('Picker.Range', () => {
resetWarned();
global.scrollCalled = false;
jest.useFakeTimers().setSystemTime(getDay('1990-09-03 00:00:00').valueOf());
document.body.innerHTML = '';
});

afterEach(() => {
Expand Down Expand Up @@ -2054,4 +2055,20 @@ describe('Picker.Range', () => {
'rc-picker-input-active',
);
});

it('should not click to focus on next field if first field is not confirm', () => {
const onCalendarChange = jest.fn();
const { container } = render(
<DayRangePicker onCalendarChange={onCalendarChange} showTime needConfirm />,
);

// Select first field
openPicker(container, 0);
selectCell(11);
expect(onCalendarChange).toHaveBeenCalled();

// Not click confirm and click next field
openPicker(container, 1);
expect(container.querySelectorAll('.rc-picker-input')[0]).toHaveClass('rc-picker-input-active');
});
});

0 comments on commit 62cf191

Please sign in to comment.