Skip to content

feat: semantic support content、body #922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,10 @@ function RangePicker<DateType extends object = any>(
generateConfig,
button: components.button,
input: components.input,
styles,
classNames,
}),
[prefixCls, locale, generateConfig, components.button, components.input],
[prefixCls, locale, generateConfig, components.button, components.input, classNames, styles],
);

// ======================== Effect ========================
Expand Down
4 changes: 3 additions & 1 deletion src/PickerInput/SinglePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,10 @@ function Picker<DateType extends object = any>(
generateConfig,
button: components.button,
input: components.input,
styles,
classNames,
}),
[prefixCls, locale, generateConfig, components.button, components.input],
[prefixCls, locale, generateConfig, components.button, components.input, styles, classNames],
);

// ======================== Effect ========================
Expand Down
5 changes: 3 additions & 2 deletions src/PickerInput/context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import type { GenerateConfig } from '../generate';
import type { Components, Locale } from '../interface';
import type { Components, Locale, SemanticStructure } from '../interface';

export interface PickerContextProps<DateType = any> {
prefixCls: string;
Expand All @@ -9,7 +9,8 @@ export interface PickerContextProps<DateType = any> {
/** Customize button component */
button?: Components['button'];
input?: Components['input'];

styles?: Partial<Record<SemanticStructure, React.CSSProperties>>;
classNames?: Partial<Record<SemanticStructure, string>>;
}

const PickerContext = React.createContext<PickerContextProps>(null!);
Expand Down
16 changes: 13 additions & 3 deletions src/PickerPanel/PanelBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export default function PanelBody<DateType extends object = any>(props: PanelBod
const cellPrefixCls = `${prefixCls}-cell`;

// ============================= Context ==============================
const { onCellDblClick } = React.useContext(PickerHackContext);
const {
onCellDblClick,
classNames: pickerClassNames,
styles,
} = React.useContext(PickerHackContext);

// ============================== Value ===============================
const matchValues = (date: DateType) =>
Expand Down Expand Up @@ -181,8 +185,14 @@ export default function PanelBody<DateType extends object = any>(props: PanelBod

// ============================== Render ==============================
return (
<div className={`${prefixCls}-body`}>
<table className={`${prefixCls}-content`}>
<div
className={classNames(`${prefixCls}-body`, pickerClassNames?.popupBody)}
style={styles?.popupBody}
>
<table
className={classNames(`${prefixCls}-content`, pickerClassNames?.popupContent)}
style={styles?.popupContent}
>
{headerCells && (
<thead>
<tr>{headerCells}</tr>
Expand Down
4 changes: 3 additions & 1 deletion src/PickerPanel/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import type { PanelMode, SharedPanelProps } from '../interface';
import type { PanelMode, SemanticStructure, SharedPanelProps } from '../interface';

export interface PanelContextProps<DateType extends object = any>
extends Pick<
Expand Down Expand Up @@ -106,6 +106,8 @@ export interface PickerHackContextProps {
hideNext?: boolean;
hideHeader?: boolean;
onCellDblClick?: () => void;
styles?: Partial<Record<SemanticStructure, React.CSSProperties>>;
classNames?: Partial<Record<SemanticStructure, string>>;
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/PickerPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,15 @@ function PickerPanel<DateType extends object = any>(
hideHeader,
} = props;

const mergedPrefixCls = React.useContext(PickerContext)?.prefixCls || prefixCls || 'rc-picker';
// ======================== Context ========================
const {
prefixCls: contextPrefixCls,
classNames: pickerClassNames,
styles,
} = React.useContext(PickerContext) || {};

// ======================== prefixCls ========================
const mergedPrefixCls = contextPrefixCls || prefixCls || 'rc-picker';

// ========================== Refs ==========================
const rootRef = React.useRef<HTMLDivElement>();
Expand Down Expand Up @@ -366,8 +374,8 @@ function PickerPanel<DateType extends object = any>(
// ======================== Context =========================
const parentHackContext = React.useContext(PickerHackContext);
const pickerPanelContext = React.useMemo(
() => ({ ...parentHackContext, hideHeader }),
[parentHackContext, hideHeader],
() => ({ ...parentHackContext, hideHeader, classNames: pickerClassNames, styles }),
[parentHackContext, hideHeader, pickerClassNames, styles],
);

// ======================== Warnings ========================
Expand Down
2 changes: 1 addition & 1 deletion src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export type Components<DateType extends object = any> = Partial<
>;

// ========================= Picker =========================
export type SemanticStructure = 'popup';
export type SemanticStructure = 'popup' | 'popupBody' | 'popupContent';
Copy link
Member

Choose a reason for hiding this comment

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

Header 和 Footer 要不要一起加了,另外还有个问题是 DateTime 这种组合是怎么处理的?
截屏2025-03-19 14 21 46

Copy link
Contributor Author

Choose a reason for hiding this comment

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

calendar 的header是内部自定义的,好像只需要content、body 就足够完成 calendar 的语义化了。 当时看到 日历组件比较复杂,还没想到比较好的解法,想着也先往后放放,先把好做的都做掉先。
https://github.com/ant-design/ant-design/pull/53159/files#diff-841e97dfb23daba72a34ee15810c8b36da6464eba16e120739ec6c9636f94fb2L287


export type CustomFormat<DateType> = (value: DateType) => string;

Expand Down
32 changes: 21 additions & 11 deletions tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1349,17 +1349,27 @@ describe('Picker.Basic', () => {
);
});

it('classNames.popup', () => {
render(
<DayPicker
classNames={{
popup: 'bamboo',
}}
open
/>,
);

expect(document.querySelector('.rc-picker-dropdown')).toHaveClass('bamboo');
it('support classNames and styles', () => {
const customClassNames = {
popup: 'custom-popup',
popupBody: 'custom-body',
popupContent: 'custom-content',
};
const customStyles = {
popup: { color: 'red' },
popupBody: { color: 'green' },
popupContent: { color: 'blue' },
};
render(<DayPicker classNames={customClassNames} styles={customStyles} open />);

expect(document.querySelector('.rc-picker-dropdown')).toHaveClass(customClassNames.popup);
expect(document.querySelector('.rc-picker-dropdown')).toHaveStyle(customStyles.popup);
const content = document.querySelector('.rc-picker-content');
const body = document.querySelector('.rc-picker-body');
expect(content).toHaveClass(customClassNames.popupContent);
expect(content).toHaveStyle(customStyles.popupContent);
expect(body).toHaveClass(customClassNames.popupBody);
expect(body).toHaveStyle(customStyles.popupBody);
});

it('showTime config should have format', () => {
Expand Down