Skip to content

Commit

Permalink
fix: showTime align logic (#724)
Browse files Browse the repository at this point in the history
* test: test driven

* fix: part show time config

* fix: logic

* fix: format logic

* chore: update types
  • Loading branch information
zombieJ authored Jan 4, 2024
1 parent 3a3a41d commit 9590f9b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
10 changes: 6 additions & 4 deletions docs/examples/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ export default () => {
// console.log('Popup!', node);
// return node.parentElement!;
// }}
picker="time"
// picker="time"
showTime={{ showHour: true, showMinute: true }}
defaultPickerValue={dayjs('2000-01-01 03:05:08')}
presets={[
{
Expand Down Expand Up @@ -250,12 +251,13 @@ export default () => {
</button>

<div style={{ display: 'flex', flexWrap: 'wrap', gap: 16 }}>
{/* <PickerPanel
<PickerPanel
generateConfig={dayjsGenerateConfig}
locale={zhCN}
value={value}
// multiple
picker="month"
picker="time"
use12Hours
onChange={setSingleValue}
// onPickerValueChange={(pickerValue) => {
// console.log('🎼 PickerValue Change:', pickerValue);
Expand All @@ -264,7 +266,7 @@ export default () => {
console.error('1');
console.log('🎲 PanelValue Change:', panelValue, mode);
}}
/> */}
/>
{/* <CellPicker
picker="time"
locale={{
Expand Down
16 changes: 8 additions & 8 deletions src/hooks/useTimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ function pickTimeProps<DateType extends object = any>(props: any): SharedTimePro
return timeProps;
}

export function getTimeConfig<Config extends object>(
export function getTimeConfig<DateType extends object>(
componentProps: {
picker?: PickerMode;
showTime?: boolean | Config;
showTime?: boolean | Partial<SharedTimeProps<DateType>>;
} = {},
): Config {
): SharedTimeProps<DateType> {
const { showTime, picker } = componentProps;

if (showTime || picker === 'time') {
const timeConfig =
showTime && typeof showTime === 'object'
? showTime
: (pickTimeProps(componentProps) as Config);
showTime && typeof showTime === 'object' ? showTime : pickTimeProps(componentProps);

const pickedProps = pickProps(timeConfig);

return {
format: 'HH:mm:ss',
...pickProps(timeConfig),
format: pickedProps.use12Hours ? 'HH:mm:ss A' : 'HH:mm:ss',
...pickedProps,
};
}

Expand Down
26 changes: 19 additions & 7 deletions src/hooks/useTimeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ function emptyDisabled<T>(): T[] {
return [];
}

function checkShow(format: string, keywords: string[], propShow?: boolean) {
return propShow ?? keywords.some((keyword) => format.includes(keyword));
function checkShow(
format: string,
keywords: string[],
hasOtherShowConfig: boolean,
show?: boolean,
) {
if (show !== undefined) {
return show;
}
return !hasOtherShowConfig && keywords.some((keyword) => format.includes(keyword));
}

function generateUnits(
Expand Down Expand Up @@ -96,11 +104,15 @@ export default function useTimeInfo<DateType extends object = any>(
}

// ========================== Show ==========================
let mergedShowHour = checkShow(format, ['H', 'h', 'k', 'LT', 'LLL'], showHour);
let mergedShowMinute = checkShow(format, ['m', 'LT', 'LLL'], showMinute);
let mergedShowSecond = checkShow(format, ['s', 'LTS'], showSecond);
const mergedShowMillisecond = checkShow(format, ['SSS'], showMillisecond);
const mergedShowMeridiem = checkShow(format, ['a', 'A', 'LT', 'LLL'], use12Hours);
const hasShowConfig = [showHour, showMinute, showSecond, showMillisecond].some(
(show) => show !== undefined,
);

let mergedShowHour = checkShow(format, ['H', 'h', 'k', 'LT', 'LLL'], hasShowConfig, showHour);
let mergedShowMinute = checkShow(format, ['m', 'LT', 'LLL'], hasShowConfig, showMinute);
let mergedShowSecond = checkShow(format, ['s', 'LTS'], hasShowConfig, showSecond);
const mergedShowMillisecond = checkShow(format, ['SSS'], hasShowConfig, showMillisecond);
const mergedShowMeridiem = checkShow(format, ['a', 'A', 'LT', 'LLL'], hasShowConfig, use12Hours);

// Fallback if all can not see
if (!mergedShowHour && !mergedShowMinute && !mergedShowSecond && !mergedShowMillisecond) {
Expand Down
21 changes: 21 additions & 0 deletions tests/panel.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,25 @@ describe('Picker.Panel', () => {
expect(onChange.mock.calls[0][0].format('HH:mm:ss')).toEqual('03:03:03');
});
});

it('showHour, showMinute, !showSecond', () => {
const { container } = render(
<DayPickerPanel
showTime={{
showHour: true,
showMinute: true,
}}
/>,
);

expect(container.querySelectorAll('.rc-picker-time-panel-column')).toHaveLength(2);
});

it('use12Hours with format', () => {
const { container } = render(
<DayPickerPanel picker="time" use12Hours defaultValue={getDay('2000-01-01 01:02:03')} />,
);

expect(container.querySelector('.rc-picker-header-view').textContent).toEqual('01:02:03 AM');
});
});

1 comment on commit 9590f9b

@vercel
Copy link

@vercel vercel bot commented on 9590f9b Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.