Skip to content

refactor: Semantic structure #927

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 10 commits into from
Apr 18, 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
11 changes: 10 additions & 1 deletion docs/examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ export default () => {
<div style={{ display: 'flex', flexWrap: 'wrap' }}>
<div style={{ margin: '0 8px' }}>
<h3>Basic</h3>
<Picker<Moment> {...sharedProps} locale={zhCN} suffixIcon="SUFFIX" />
<Picker<Moment>
{...sharedProps}
locale={zhCN}
suffixIcon="SUFFIX"
rootClassName="bamboo"
className="little"
classNames={{
root: 'light',
}}
/>
<Picker<Moment> {...sharedProps} locale={enUS} />
</div>
<div style={{ margin: '0 8px' }}>
Expand Down
17 changes: 13 additions & 4 deletions src/PickerInput/Popup/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import classNames from 'classnames';
import cls from 'classnames';
import * as React from 'react';
import type { GenerateConfig } from '../../generate';
import useTimeInfo from '../../hooks/useTimeInfo';
Expand Down Expand Up @@ -42,7 +42,13 @@ export default function Footer(props: FooterProps) {
disabledDate,
} = props;

const { prefixCls, locale, button: Button = 'button' } = React.useContext(PickerContext);
const {
prefixCls,
locale,
button: Button = 'button',
classNames,
styles,
} = React.useContext(PickerContext);

// >>> Now
const now = generateConfig.getNow();
Expand Down Expand Up @@ -70,7 +76,7 @@ export default function Footer(props: FooterProps) {
const presetNode = showNow && (
<li className={nowPrefixCls}>
<a
className={classNames(nowBtnPrefixCls, nowDisabled && `${nowBtnPrefixCls}-disabled`)}
className={cls(nowBtnPrefixCls, nowDisabled && `${nowBtnPrefixCls}-disabled`)}
aria-disabled={nowDisabled}
onClick={onInternalNow}
>
Expand Down Expand Up @@ -101,7 +107,10 @@ export default function Footer(props: FooterProps) {
}

return (
<div className={`${prefixCls}-footer`}>
<div
className={cls(`${prefixCls}-footer`, classNames.popup.footer)}
style={styles.popup.footer}
>
{extraNode && <div className={`${prefixCls}-footer-extra`}>{extraNode}</div>}
{rangeNode}
</div>
Expand Down
36 changes: 29 additions & 7 deletions src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEvent, useMergedState } from '@rc-component/util';
import cls from 'classnames';
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
import omit from '@rc-component/util/lib/omit';
import pickAttrs from '@rc-component/util/lib/pickAttrs';
Expand Down Expand Up @@ -35,6 +36,7 @@ import useRangeValue, { useInnerValue } from './hooks/useRangeValue';
import useShowNow from './hooks/useShowNow';
import Popup, { type PopupShowTimeConfig } from './Popup';
import RangeSelector, { type SelectorIdType } from './Selector/RangeSelector';
import useSemantic from '../hooks/useSemantic';

function separateConfig<T>(config: T | [T, T] | null | undefined, defaultConfig: T): [T, T] {
const singleConfig = config ?? defaultConfig;
Expand Down Expand Up @@ -158,8 +160,9 @@ function RangePicker<DateType extends object = any>(
const {
// Style
prefixCls,
styles,
classNames,
rootClassName,
styles: propStyles,
classNames: propClassNames,

// Value
defaultValue,
Expand Down Expand Up @@ -224,6 +227,9 @@ function RangePicker<DateType extends object = any>(
// ========================= Refs =========================
const selectorRef = usePickerRef(ref);

// ======================= Semantic =======================
const [mergedClassNames, mergedStyles] = useSemantic(propClassNames, propStyles);

// ========================= Open =========================
const [mergedOpen, setMergeOpen] = useOpen(open, defaultOpen, disabled, onOpenChange);

Expand Down Expand Up @@ -562,6 +568,8 @@ function RangePicker<DateType extends object = any>(
'className',
'onPanelChange',
'disabledTime',
'classNames',
'styles',
]);
return restProps;
}, [filledProps]);
Expand Down Expand Up @@ -693,10 +701,18 @@ function RangePicker<DateType extends object = any>(
generateConfig,
button: components.button,
input: components.input,
styles,
classNames,
classNames: mergedClassNames,
styles: mergedStyles,
}),
[prefixCls, locale, generateConfig, components.button, components.input, classNames, styles],
[
prefixCls,
locale,
generateConfig,
components.button,
components.input,
mergedClassNames,
mergedStyles,
],
);

// ======================== Effect ========================
Expand Down Expand Up @@ -755,8 +771,8 @@ function RangePicker<DateType extends object = any>(
<PickerTrigger
{...pickTriggerProps(filledProps)}
popupElement={panel}
popupStyle={styles.popup}
popupClassName={classNames.popup}
popupStyle={mergedStyles.popup.root}
popupClassName={cls(rootClassName, mergedClassNames.popup.root)}
// Visible
visible={mergedOpen}
onClose={onPopupClose}
Expand All @@ -768,6 +784,12 @@ function RangePicker<DateType extends object = any>(
{...filledProps}
// Ref
ref={selectorRef}
// Style
className={cls(filledProps.className, rootClassName, mergedClassNames.root)}
style={{
...mergedStyles.root,
...filledProps.style,
}}
// Icon
suffixIcon={suffixIcon}
// Active
Expand Down
8 changes: 4 additions & 4 deletions src/PickerInput/Selector/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import PickerContext from '../context';
import classNames from 'classnames';
import cls from 'classnames';

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

export default function Icon(props: IconProps) {
const { icon, type, ...restProps } = props;
const { prefixCls, classNames: iconClassNames, styles } = React.useContext(PickerContext);
const { prefixCls, classNames, styles } = React.useContext(PickerContext);

return icon ? (
<span
className={classNames(`${prefixCls}-${type}`, iconClassNames?.suffix)}
style={styles?.suffix}
className={cls(`${prefixCls}-${type}`, classNames.suffix)}
style={styles.suffix}
{...restProps}
>
{icon}
Expand Down
10 changes: 5 additions & 5 deletions src/PickerInput/Selector/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import classNames from 'classnames';
import cls from 'classnames';
import { useEvent } from '@rc-component/util';
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
import raf from '@rc-component/util/lib/raf';
Expand Down Expand Up @@ -75,7 +75,7 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
const {
prefixCls,
input: Component = 'input',
classNames: inputClassNames,
classNames,
styles,
} = React.useContext(PickerContext);
const inputPrefixCls = `${prefixCls}-input`;
Expand Down Expand Up @@ -383,16 +383,14 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
return (
<div
ref={holderRef}
className={classNames(
className={cls(
inputPrefixCls,
inputClassNames?.input,
{
[`${inputPrefixCls}-active`]: active && showActiveCls,
[`${inputPrefixCls}-placeholder`]: helped,
},
className,
)}
style={styles?.input}
>
<Component
ref={inputRef}
Expand All @@ -406,6 +404,8 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
// Value
value={inputValue}
onChange={onInternalChange}
className={classNames.input}
style={styles.input}
/>
<Icon type="suffix" icon={suffixIcon} />
{clearIcon}
Expand Down
11 changes: 4 additions & 7 deletions src/PickerInput/Selector/RangeSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import classNames from 'classnames';
import cls from 'classnames';
import ResizeObserver from '@rc-component/resize-observer';
import { useEvent } from '@rc-component/util';
import * as React from 'react';
Expand Down Expand Up @@ -121,7 +121,7 @@ function RangeSelector<DateType extends object = any>(
const rtl = direction === 'rtl';

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

// ========================== Id ==========================
const ids = React.useMemo(() => {
Expand Down Expand Up @@ -211,7 +211,7 @@ function RangeSelector<DateType extends object = any>(
<ResizeObserver onResize={syncActiveOffset}>
<div
{...rootProps}
className={classNames(
className={cls(
prefixCls,
`${prefixCls}-range`,
{
Expand Down Expand Up @@ -239,10 +239,7 @@ function RangeSelector<DateType extends object = any>(
}}
>
{prefix && (
<div
className={classNames(`${prefixCls}-prefix`, selectorClassNames?.prefix)}
style={styles?.prefix}
>
<div className={cls(`${prefixCls}-prefix`, classNames.prefix)} style={styles.prefix}>
{prefix}
</div>
)}
Expand Down
11 changes: 4 additions & 7 deletions src/PickerInput/Selector/SingleSelector/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import classNames from 'classnames';
import cls from 'classnames';
import * as React from 'react';
import type { InternalMode, PickerRef, SelectorProps } from '../../../interface';
import { isSame } from '../../../utils/dateUtil';
Expand Down Expand Up @@ -110,7 +110,7 @@ function SingleSelector<DateType extends object = any>(
const rtl = direction === 'rtl';

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

// ========================= Refs =========================
const rootRef = React.useRef<HTMLDivElement>();
Expand Down Expand Up @@ -201,7 +201,7 @@ function SingleSelector<DateType extends object = any>(
return (
<div
{...rootProps}
className={classNames(
className={cls(
prefixCls,
{
[`${prefixCls}-multiple`]: multiple,
Expand All @@ -226,10 +226,7 @@ function SingleSelector<DateType extends object = any>(
}}
>
{prefix && (
<div
className={classNames(`${prefixCls}-prefix`, selectorClassNames?.prefix)}
style={styles?.prefix}
>
<div className={cls(`${prefixCls}-prefix`, classNames.prefix)} style={styles.prefix}>
{prefix}
</div>
)}
Expand Down
36 changes: 29 additions & 7 deletions src/PickerInput/SinglePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEvent, useMergedState } from '@rc-component/util';
import cls from 'classnames';
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
import omit from '@rc-component/util/lib/omit';
import pickAttrs from '@rc-component/util/lib/pickAttrs';
Expand Down Expand Up @@ -31,6 +32,7 @@ import useRangeValue, { useInnerValue } from './hooks/useRangeValue';
import useShowNow from './hooks/useShowNow';
import Popup from './Popup';
import SingleSelector from './Selector/SingleSelector';
import useSemantic from '../hooks/useSemantic';

// TODO: isInvalidateDate with showTime.disabledTime should not provide `range` prop

Expand Down Expand Up @@ -122,8 +124,9 @@ function Picker<DateType extends object = any>(
const {
// Style
prefixCls,
styles,
classNames,
rootClassName,
styles: propStyles,
classNames: propClassNames,

// Value
order,
Expand Down Expand Up @@ -202,6 +205,9 @@ function Picker<DateType extends object = any>(

const toggleDates = useToggleDates(generateConfig, locale, internalPicker);

// ======================= Semantic =======================
const [mergedClassNames, mergedStyles] = useSemantic(propClassNames, propStyles);

// ========================= Open =========================
const [mergedOpen, triggerOpen] = useOpen(open, defaultOpen, [disabled], onOpenChange);

Expand Down Expand Up @@ -478,6 +484,8 @@ function Picker<DateType extends object = any>(
'style',
'className',
'onPanelChange',
'classNames',
'styles',
]);
return {
...restProps,
Expand Down Expand Up @@ -577,10 +585,18 @@ function Picker<DateType extends object = any>(
generateConfig,
button: components.button,
input: components.input,
styles,
classNames,
classNames: mergedClassNames,
styles: mergedStyles,
}),
[prefixCls, locale, generateConfig, components.button, components.input, styles, classNames],
[
prefixCls,
locale,
generateConfig,
components.button,
components.input,
mergedClassNames,
mergedStyles,
],
);

// ======================== Effect ========================
Expand Down Expand Up @@ -615,8 +631,8 @@ function Picker<DateType extends object = any>(
<PickerTrigger
{...pickTriggerProps(filledProps)}
popupElement={panel}
popupStyle={styles.popup}
popupClassName={classNames.popup}
popupStyle={mergedStyles.popup.root}
popupClassName={cls(rootClassName, mergedClassNames.popup.root)}
// Visible
visible={mergedOpen}
onClose={onPopupClose}
Expand All @@ -626,6 +642,12 @@ function Picker<DateType extends object = any>(
{...filledProps}
// Ref
ref={selectorRef}
// Style
className={cls(filledProps.className, rootClassName, mergedClassNames.root)}
style={{
...mergedStyles.root,
...filledProps.style,
}}
// Icon
suffixIcon={suffixIcon}
removeIcon={removeIcon}
Expand Down
7 changes: 4 additions & 3 deletions src/PickerInput/context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import type { GenerateConfig } from '../generate';
import type { Components, Locale, SemanticStructure } from '../interface';
import type { Components, Locale } from '../interface';
import type { FilledClassNames, FilledStyles } from '../hooks/useSemantic';

export interface PickerContextProps<DateType = any> {
prefixCls: string;
Expand All @@ -9,8 +10,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>>;
classNames: FilledClassNames;
styles: FilledStyles;
}

const PickerContext = React.createContext<PickerContextProps>(null!);
Expand Down
Loading