Skip to content

Commit 9590f9b

Browse files
authored
fix: showTime align logic (#724)
* test: test driven * fix: part show time config * fix: logic * fix: format logic * chore: update types
1 parent 3a3a41d commit 9590f9b

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

docs/examples/debug.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ export default () => {
168168
// console.log('Popup!', node);
169169
// return node.parentElement!;
170170
// }}
171-
picker="time"
171+
// picker="time"
172+
showTime={{ showHour: true, showMinute: true }}
172173
defaultPickerValue={dayjs('2000-01-01 03:05:08')}
173174
presets={[
174175
{
@@ -250,12 +251,13 @@ export default () => {
250251
</button>
251252

252253
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 16 }}>
253-
{/* <PickerPanel
254+
<PickerPanel
254255
generateConfig={dayjsGenerateConfig}
255256
locale={zhCN}
256257
value={value}
257258
// multiple
258-
picker="month"
259+
picker="time"
260+
use12Hours
259261
onChange={setSingleValue}
260262
// onPickerValueChange={(pickerValue) => {
261263
// console.log('🎼 PickerValue Change:', pickerValue);
@@ -264,7 +266,7 @@ export default () => {
264266
console.error('1');
265267
console.log('🎲 PanelValue Change:', panelValue, mode);
266268
}}
267-
/> */}
269+
/>
268270
{/* <CellPicker
269271
picker="time"
270272
locale={{

src/hooks/useTimeConfig.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ function pickTimeProps<DateType extends object = any>(props: any): SharedTimePro
4040
return timeProps;
4141
}
4242

43-
export function getTimeConfig<Config extends object>(
43+
export function getTimeConfig<DateType extends object>(
4444
componentProps: {
4545
picker?: PickerMode;
46-
showTime?: boolean | Config;
46+
showTime?: boolean | Partial<SharedTimeProps<DateType>>;
4747
} = {},
48-
): Config {
48+
): SharedTimeProps<DateType> {
4949
const { showTime, picker } = componentProps;
5050

5151
if (showTime || picker === 'time') {
5252
const timeConfig =
53-
showTime && typeof showTime === 'object'
54-
? showTime
55-
: (pickTimeProps(componentProps) as Config);
53+
showTime && typeof showTime === 'object' ? showTime : pickTimeProps(componentProps);
54+
55+
const pickedProps = pickProps(timeConfig);
5656

5757
return {
58-
format: 'HH:mm:ss',
59-
...pickProps(timeConfig),
58+
format: pickedProps.use12Hours ? 'HH:mm:ss A' : 'HH:mm:ss',
59+
...pickedProps,
6060
};
6161
}
6262

src/hooks/useTimeInfo.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ function emptyDisabled<T>(): T[] {
1515
return [];
1616
}
1717

18-
function checkShow(format: string, keywords: string[], propShow?: boolean) {
19-
return propShow ?? keywords.some((keyword) => format.includes(keyword));
18+
function checkShow(
19+
format: string,
20+
keywords: string[],
21+
hasOtherShowConfig: boolean,
22+
show?: boolean,
23+
) {
24+
if (show !== undefined) {
25+
return show;
26+
}
27+
return !hasOtherShowConfig && keywords.some((keyword) => format.includes(keyword));
2028
}
2129

2230
function generateUnits(
@@ -96,11 +104,15 @@ export default function useTimeInfo<DateType extends object = any>(
96104
}
97105

98106
// ========================== Show ==========================
99-
let mergedShowHour = checkShow(format, ['H', 'h', 'k', 'LT', 'LLL'], showHour);
100-
let mergedShowMinute = checkShow(format, ['m', 'LT', 'LLL'], showMinute);
101-
let mergedShowSecond = checkShow(format, ['s', 'LTS'], showSecond);
102-
const mergedShowMillisecond = checkShow(format, ['SSS'], showMillisecond);
103-
const mergedShowMeridiem = checkShow(format, ['a', 'A', 'LT', 'LLL'], use12Hours);
107+
const hasShowConfig = [showHour, showMinute, showSecond, showMillisecond].some(
108+
(show) => show !== undefined,
109+
);
110+
111+
let mergedShowHour = checkShow(format, ['H', 'h', 'k', 'LT', 'LLL'], hasShowConfig, showHour);
112+
let mergedShowMinute = checkShow(format, ['m', 'LT', 'LLL'], hasShowConfig, showMinute);
113+
let mergedShowSecond = checkShow(format, ['s', 'LTS'], hasShowConfig, showSecond);
114+
const mergedShowMillisecond = checkShow(format, ['SSS'], hasShowConfig, showMillisecond);
115+
const mergedShowMeridiem = checkShow(format, ['a', 'A', 'LT', 'LLL'], hasShowConfig, use12Hours);
104116

105117
// Fallback if all can not see
106118
if (!mergedShowHour && !mergedShowMinute && !mergedShowSecond && !mergedShowMillisecond) {

tests/panel.spec.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,4 +688,25 @@ describe('Picker.Panel', () => {
688688
expect(onChange.mock.calls[0][0].format('HH:mm:ss')).toEqual('03:03:03');
689689
});
690690
});
691+
692+
it('showHour, showMinute, !showSecond', () => {
693+
const { container } = render(
694+
<DayPickerPanel
695+
showTime={{
696+
showHour: true,
697+
showMinute: true,
698+
}}
699+
/>,
700+
);
701+
702+
expect(container.querySelectorAll('.rc-picker-time-panel-column')).toHaveLength(2);
703+
});
704+
705+
it('use12Hours with format', () => {
706+
const { container } = render(
707+
<DayPickerPanel picker="time" use12Hours defaultValue={getDay('2000-01-01 01:02:03')} />,
708+
);
709+
710+
expect(container.querySelector('.rc-picker-header-view').textContent).toEqual('01:02:03 AM');
711+
});
691712
});

0 commit comments

Comments
 (0)