Skip to content

feat: support semantic for time panel #926

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 1 commit into from
Apr 16, 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
10 changes: 10 additions & 0 deletions docs/examples/time.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import zhCN from '../../src/locale/zh_CN';
import '../../assets/index.less';

const defaultValue = moment('2019-11-28 01:02:03');
const testClassNames = {
input: 'test-input',
prefix: 'test-prefix',
suffix: 'test-suffix',
popupContent: 'test-popup-content',
popupItem: 'test-popup-item',
}

export default () => {
return (
Expand All @@ -24,6 +31,9 @@ export default () => {

<h3>TimePicker</h3>
<Picker
classNames={testClassNames}
prefix="prefix"
suffixIcon="suffix"
defaultValue={defaultValue}
picker="time"
locale={zhCN}
Expand Down
10 changes: 7 additions & 3 deletions src/PickerInput/Selector/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import PickerContext from '../context';
import classNames from 'classnames';

export interface IconProps extends React.HtmlHTMLAttributes<HTMLElement> {
icon?: React.ReactNode;
Expand All @@ -8,11 +9,14 @@ export interface IconProps extends React.HtmlHTMLAttributes<HTMLElement> {

export default function Icon(props: IconProps) {
const { icon, type, ...restProps } = props;

const { prefixCls } = React.useContext(PickerContext);
const { prefixCls, classNames: iconClassNames, styles } = React.useContext(PickerContext);

return icon ? (
<span className={`${prefixCls}-${type}`} {...restProps}>
<span
className={classNames(`${prefixCls}-${type}`, iconClassNames?.suffix)}
style={styles?.suffix}
{...restProps}
>
{icon}
</span>
) : null;
Expand Down
9 changes: 8 additions & 1 deletion src/PickerInput/Selector/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
} = props;
const { value, onFocus, onBlur, onMouseUp } = props;

const { prefixCls, input: Component = 'input' } = React.useContext(PickerContext);
const {
prefixCls,
input: Component = 'input',
classNames: inputClassNames,
styles,
} = React.useContext(PickerContext);
const inputPrefixCls = `${prefixCls}-input`;

// ======================== Value =========================
Expand Down Expand Up @@ -380,12 +385,14 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
ref={holderRef}
className={classNames(
inputPrefixCls,
inputClassNames?.input,
{
[`${inputPrefixCls}-active`]: active && showActiveCls,
[`${inputPrefixCls}-placeholder`]: helped,
},
className,
)}
style={styles?.input}
>
<Component
ref={inputRef}
Expand Down
11 changes: 9 additions & 2 deletions src/PickerInput/Selector/RangeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function RangeSelector<DateType extends object = any>(
const rtl = direction === 'rtl';

// ======================== Prefix ========================
const { prefixCls } = React.useContext(PickerContext);
const { prefixCls, classNames: selectorClassNames, styles } = React.useContext(PickerContext);

// ========================== Id ==========================
const ids = React.useMemo(() => {
Expand Down Expand Up @@ -238,7 +238,14 @@ function RangeSelector<DateType extends object = any>(
onMouseDown?.(e);
}}
>
{prefix && <div className={`${prefixCls}-prefix`}>{prefix}</div>}
{prefix && (
<div
className={classNames(`${prefixCls}-prefix`, selectorClassNames?.prefix)}
style={styles?.prefix}
>
{prefix}
</div>
)}
<Input
ref={inputStartRef}
{...getInputProps(0)}
Expand Down
11 changes: 9 additions & 2 deletions src/PickerInput/Selector/SingleSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function SingleSelector<DateType extends object = any>(
const rtl = direction === 'rtl';

// ======================== Prefix ========================
const { prefixCls } = React.useContext(PickerContext);
const { prefixCls, classNames: selectorClassNames, styles } = React.useContext(PickerContext);

// ========================= Refs =========================
const rootRef = React.useRef<HTMLDivElement>();
Expand Down Expand Up @@ -225,7 +225,14 @@ function SingleSelector<DateType extends object = any>(
onMouseDown?.(e);
}}
>
{prefix && <div className={`${prefixCls}-prefix`}>{prefix}</div>}
{prefix && (
<div
className={classNames(`${prefixCls}-prefix`, selectorClassNames?.prefix)}
style={styles?.prefix}
>
{prefix}
</div>
)}
{selectorNode}
</div>
);
Expand Down
6 changes: 4 additions & 2 deletions src/PickerPanel/TimePanel/TimePanelBody/TimeColumn.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import classNames from 'classnames';
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
import * as React from 'react';
import { usePanelContext } from '../../context';
import { PickerHackContext, usePanelContext } from '../../context';
import useScrollTo from './useScrollTo';

const SCROLL_DELAY = 300;
Expand Down Expand Up @@ -33,6 +33,7 @@ export default function TimeColumn<DateType extends object>(props: TimeUnitColum
props;

const { prefixCls, cellRender, now, locale } = usePanelContext<DateType>();
const { classNames: pickerClassNames, styles } = React.useContext(PickerHackContext);

const panelPrefixCls = `${prefixCls}-time-panel`;
const cellPrefixCls = `${prefixCls}-time-panel-cell`;
Expand Down Expand Up @@ -103,7 +104,8 @@ export default function TimeColumn<DateType extends object>(props: TimeUnitColum
return (
<li
key={unitValue}
className={classNames(cellPrefixCls, {
style={styles?.popupItem}
className={classNames(cellPrefixCls, pickerClassNames?.popupItem, {
[`${cellPrefixCls}-selected`]: value === unitValue,
[`${cellPrefixCls}-disabled`]: disabled,
})}
Expand Down
12 changes: 10 additions & 2 deletions src/PickerPanel/TimePanel/TimePanelBody/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { SharedPanelProps, SharedTimeProps } from '../../../interface';
import { formatValue } from '../../../utils/dateUtil';
import { PickerHackContext, usePanelContext } from '../../context';
import TimeColumn, { type Unit } from './TimeColumn';
import classNames from 'classnames';

function isAM(hour: number) {
return hour < 12;
Expand Down Expand Up @@ -38,7 +39,11 @@ export default function TimePanelBody<DateType extends object = any>(

const value = values?.[0] || null;

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

// ========================== Info ==========================
const [getValidTime, rowHourUnits, getMinuteUnits, getSecondUnits, getMillisecondUnits] =
Expand Down Expand Up @@ -268,7 +273,10 @@ export default function TimePanelBody<DateType extends object = any>(
};

return (
<div className={`${prefixCls}-content`}>
<div
className={classNames(`${prefixCls}-content`, pickerClassNames?.popupContent)}
style={styles?.popupContent}
>
{showHour && (
<TimeColumn
units={hourUnits}
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' | 'popupBody' | 'popupContent' | 'popupItem';
export type SemanticStructure = 'popup' | 'popupBody' | 'popupContent' | 'popupItem' | 'suffix' | 'prefix' | 'input';

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

Expand Down
58 changes: 57 additions & 1 deletion tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import KeyCode from '@rc-component/util/lib/KeyCode';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import { resetWarned } from '@rc-component/util/lib/warning';
import React from 'react';
import { PickerPanel, type PickerRef } from '../src';
import Picker, { PickerPanel, type PickerRef } from '../src';
import type { PanelMode, PickerMode } from '../src/interface';
import momentGenerateConfig from '../src/generate/moment';
import enUS from '../src/locale/en_US';
Expand Down Expand Up @@ -1406,6 +1406,62 @@ describe('Picker.Basic', () => {
expect(item).toHaveClass(customClassNames.popupItem);
expect(item).toHaveStyle(customStyles.popupItem);
});
it('classNames and styles should support time panel', async () => {
const testClassNames = {
input: 'test-input',
prefix: 'test-prefix',
suffix: 'test-suffix',
popupContent: 'custom-content',
popupItem: 'custom-item',
};
const testStyles = {
input: { color: 'red' },
prefix: { color: 'green' },
suffix: { color: 'blue' },
popupContent: { color: 'blue' },
popupItem: { color: 'yellow' },
};
const defaultValue = moment('2019-11-28 01:02:03');
const { container } = render(
<Picker
classNames={testClassNames}
styles={testStyles}
prefix="prefix"
suffixIcon="suffix"
defaultValue={defaultValue}
picker="time"
locale={zhCN}
disabledTime={(now) => ({
disabledHours: () => [now.hours()],
})}
generateConfig={momentGenerateConfig}
/>,
);
const input = container.querySelectorAll('.rc-picker-input')[0];
const prefix = container.querySelector('.rc-picker-prefix');
const suffix = container.querySelector('.rc-picker-suffix');
expect(input).toHaveClass(testClassNames.input);
expect(input).toHaveStyle(testStyles.input);
expect(prefix).toHaveClass(testClassNames.prefix);
expect(prefix).toHaveStyle(testStyles.prefix);
expect(suffix).toHaveClass(testClassNames.suffix);
expect(suffix).toHaveStyle(testStyles.suffix);
const { container: panel } = render(
<PickerPanel
classNames={testClassNames}
styles={testStyles}
picker="time"
locale={enUS}
generateConfig={momentGenerateConfig}
/>,
);
const content = panel.querySelector('.rc-picker-content');
const item = panel.querySelector('.rc-picker-time-panel-cell');
expect(content).toHaveClass(testClassNames.popupContent);
expect(content).toHaveStyle(testStyles.popupContent);
expect(item).toHaveClass(testClassNames.popupItem);
expect(item).toHaveStyle(testStyles.popupItem);
});
it('showTime config should have format', () => {
render(
<DayPicker
Expand Down