Skip to content

Commit eddb9aa

Browse files
authored
Fix disabled (#160)
* fix: disabledMin & disabledSec with 12hour * test: Add test case
1 parent bff4841 commit eddb9aa

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

src/panels/TimePanel/TimeBody.tsx

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ function TimeBody<DateType>(props: TimeBodyProps<DateType>) {
7878
const columnPrefixCls = `${prefixCls}-time-panel`;
7979

8080
let isPM: boolean | undefined;
81-
let hour = value ? generateConfig.getHour(value) : -1;
81+
const originHour = value ? generateConfig.getHour(value) : -1;
82+
let hour = originHour;
8283
const minute = value ? generateConfig.getMinute(value) : -1;
8384
const second = value ? generateConfig.getSecond(value) : -1;
8485

@@ -147,28 +148,25 @@ function TimeBody<DateType>(props: TimeBodyProps<DateType>) {
147148
});
148149
}, [use12Hours, memorizedRawHours]);
149150

150-
const minutes = generateUnits(0, 59, minuteStep, disabledMinutes && disabledMinutes(hour));
151+
const minutes = generateUnits(0, 59, minuteStep, disabledMinutes && disabledMinutes(originHour));
151152

152153
const seconds = generateUnits(
153154
0,
154155
59,
155156
secondStep,
156-
disabledSeconds && disabledSeconds(hour, minute),
157+
disabledSeconds && disabledSeconds(originHour, minute),
157158
);
158159

159160
// ====================== Operations ======================
160161
operationRef.current = {
161162
onUpDown: diff => {
162163
const column = columns[activeColumnIndex];
163164
if (column) {
164-
const valueIndex = column.units.findIndex(
165-
unit => unit.value === column.value,
166-
);
165+
const valueIndex = column.units.findIndex(unit => unit.value === column.value);
167166

168167
const unitLen = column.units.length;
169168
for (let i = 1; i < unitLen; i += 1) {
170-
const nextUnit =
171-
column.units[(valueIndex + diff * i + unitLen) % unitLen];
169+
const nextUnit = column.units[(valueIndex + diff * i + unitLen) % unitLen];
172170

173171
if (nextUnit.disabled !== true) {
174172
column.onSelect(nextUnit.value);
@@ -210,26 +208,14 @@ function TimeBody<DateType>(props: TimeBodyProps<DateType>) {
210208
});
211209

212210
// Minute
213-
addColumnNode(
214-
showMinute,
215-
<TimeUnitColumn key="minute" />,
216-
minute,
217-
minutes,
218-
num => {
219-
onSelect(setTime(isPM, hour, num, second), 'mouse');
220-
},
221-
);
211+
addColumnNode(showMinute, <TimeUnitColumn key="minute" />, minute, minutes, num => {
212+
onSelect(setTime(isPM, hour, num, second), 'mouse');
213+
});
222214

223215
// Second
224-
addColumnNode(
225-
showSecond,
226-
<TimeUnitColumn key="second" />,
227-
second,
228-
seconds,
229-
num => {
230-
onSelect(setTime(isPM, hour, minute, num), 'mouse');
231-
},
232-
);
216+
addColumnNode(showSecond, <TimeUnitColumn key="second" />, second, seconds, num => {
217+
onSelect(setTime(isPM, hour, minute, num), 'mouse');
218+
});
233219

234220
// 12 Hours
235221
let PMIndex = -1;
@@ -250,9 +236,7 @@ function TimeBody<DateType>(props: TimeBodyProps<DateType>) {
250236
},
251237
);
252238

253-
return (
254-
<div className={contentPrefixCls}>{columns.map(({ node }) => node)}</div>
255-
);
239+
return <div className={contentPrefixCls}>{columns.map(({ node }) => node)}</div>;
256240
}
257241

258242
export default TimeBody;

tests/__snapshots__/panel.spec.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ exports[`Picker.Panel should render correctly in rtl 1`] = `
614614
</div>
615615
`;
616616

617-
exports[`Picker.Panel time disabled columns 1`] = `
617+
exports[`Picker.Panel time disabled columns basic 1`] = `
618618
<div
619619
class="rc-picker-panel"
620620
tabindex="0"

tests/panel.spec.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,17 +421,37 @@ describe('Picker.Panel', () => {
421421
});
422422
});
423423

424-
it('time disabled columns', () => {
425-
const wrapper = mount(
426-
<MomentPickerPanel
427-
mode="time"
428-
disabledHours={() => [0, 1, 2, 3, 4, 5, 6, 7]}
429-
disabledMinutes={() => [2, 4, 6, 8, 10]}
430-
disabledSeconds={() => [10, 20, 30, 40, 50]}
431-
/>,
432-
);
424+
describe('time disabled columns', () => {
425+
it('basic', () => {
426+
const wrapper = mount(
427+
<MomentPickerPanel
428+
mode="time"
429+
disabledHours={() => [0, 1, 2, 3, 4, 5, 6, 7]}
430+
disabledMinutes={() => [2, 4, 6, 8, 10]}
431+
disabledSeconds={() => [10, 20, 30, 40, 50]}
432+
/>,
433+
);
433434

434-
expect(wrapper.render()).toMatchSnapshot();
435+
expect(wrapper.render()).toMatchSnapshot();
436+
});
437+
438+
it('use12Hour', () => {
439+
const disabledMinutes = jest.fn(() => []);
440+
const disabledSeconds = jest.fn(() => []);
441+
442+
mount(
443+
<MomentPickerPanel
444+
mode="time"
445+
use12Hours
446+
value={getMoment('2000-01-01 13:07:04')}
447+
disabledMinutes={disabledMinutes}
448+
disabledSeconds={disabledSeconds}
449+
/>,
450+
);
451+
452+
expect(disabledMinutes).toHaveBeenCalledWith(13);
453+
expect(disabledSeconds).toHaveBeenCalledWith(13, 7);
454+
});
435455
});
436456

437457
it('warning with invalidate value', () => {

0 commit comments

Comments
 (0)