Skip to content

Commit 036465f

Browse files
authored
feat: support semantic for panel (#923)
* feat: support semantic for pannel * add test
1 parent 001190e commit 036465f

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/PickerPanel/index.tsx

+16-3
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,26 @@ export interface SinglePickerPanelProps<DateType extends object = any>
127127
onChange?: (date: DateType) => void;
128128
}
129129

130+
type PanelSemanticName = 'popupBody' | 'popupContent';
130131
export type PickerPanelProps<DateType extends object = any> = BasePickerPanelProps<DateType> & {
131132
/** multiple selection. Not support time or datetime picker */
132133
multiple?: boolean;
133134

134135
defaultValue?: DateType | DateType[] | null;
135136
value?: DateType | DateType[] | null;
136137
onChange?: (date: DateType | DateType[]) => void;
138+
styles?: Partial<Record<PanelSemanticName, React.CSSProperties>>;
139+
classNames?: Partial<Record<PanelSemanticName, string>>;
137140
};
138141

139142
function PickerPanel<DateType extends object = any>(
140143
props: PickerPanelProps<DateType>,
141144
ref: React.Ref<PickerPanelRef>,
142145
) {
143146
const {
147+
classNames: panelClassNames,
148+
styles: panelStyles,
149+
144150
locale,
145151
generateConfig,
146152

@@ -187,7 +193,7 @@ function PickerPanel<DateType extends object = any>(
187193
const {
188194
prefixCls: contextPrefixCls,
189195
classNames: pickerClassNames,
190-
styles,
196+
styles: pickerStyles,
191197
} = React.useContext(PickerContext) || {};
192198

193199
// ======================== prefixCls ========================
@@ -372,10 +378,17 @@ function PickerPanel<DateType extends object = any>(
372378
DatePanel) as typeof DatePanel;
373379

374380
// ======================== Context =========================
381+
const mergedStyles = pickerStyles ?? panelStyles;
382+
const mergedClassNames = pickerClassNames ?? panelClassNames;
375383
const parentHackContext = React.useContext(PickerHackContext);
376384
const pickerPanelContext = React.useMemo(
377-
() => ({ ...parentHackContext, hideHeader, classNames: pickerClassNames, styles }),
378-
[parentHackContext, hideHeader, pickerClassNames, styles],
385+
() => ({
386+
...parentHackContext,
387+
hideHeader,
388+
classNames: mergedClassNames,
389+
styles: mergedStyles,
390+
}),
391+
[parentHackContext, hideHeader, mergedClassNames, mergedStyles],
379392
);
380393

381394
// ======================== Warnings ========================

tests/picker.spec.tsx

+26-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import KeyCode from '@rc-component/util/lib/KeyCode';
88
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
99
import { resetWarned } from '@rc-component/util/lib/warning';
1010
import React from 'react';
11-
import type { PickerRef } from '../src';
11+
import { PickerPanel, type PickerRef } from '../src';
1212
import type { PanelMode, PickerMode } from '../src/interface';
13+
import momentGenerateConfig from '../src/generate/moment';
1314
import enUS from '../src/locale/en_US';
1415
import zhCN from '../src/locale/zh_CN';
1516
import {
@@ -1371,7 +1372,30 @@ describe('Picker.Basic', () => {
13711372
expect(body).toHaveClass(customClassNames.popupBody);
13721373
expect(body).toHaveStyle(customStyles.popupBody);
13731374
});
1374-
1375+
it('support classNames and styles for panel', () => {
1376+
const customClassNames = {
1377+
popupBody: 'custom-body',
1378+
popupContent: 'custom-content',
1379+
};
1380+
const customStyles = {
1381+
popupBody: { color: 'green' },
1382+
popupContent: { color: 'blue' },
1383+
};
1384+
render(
1385+
<PickerPanel
1386+
classNames={customClassNames}
1387+
styles={customStyles}
1388+
locale={enUS}
1389+
generateConfig={momentGenerateConfig}
1390+
/>,
1391+
);
1392+
const content = document.querySelector('.rc-picker-content');
1393+
const body = document.querySelector('.rc-picker-body');
1394+
expect(content).toHaveClass(customClassNames.popupContent);
1395+
expect(content).toHaveStyle(customStyles.popupContent);
1396+
expect(body).toHaveClass(customClassNames.popupBody);
1397+
expect(body).toHaveStyle(customStyles.popupBody);
1398+
});
13751399
it('showTime config should have format', () => {
13761400
render(
13771401
<DayPicker

0 commit comments

Comments
 (0)