Files
Genarrative/packages/shared/src/components/PlatformToggleRow.tsx
T
kdletters c5200c7d45
Project CI / Backend tests (push) Successful in 8m31s
Project CI / Repository checks (push) Successful in 14m14s
Project CI / Frontend tests (push) Successful in 13m2s
Project CI / Native shell tests (push) Successful in 21m30s
补齐共享 UI 组件库与展示页交互 (#202)
## 变更内容

- 新增基于 shadcn open-code 模式的共享 UI canonical 组件、样式与导出。
- 新增 `/components` 共享组件展示页,并覆盖平台组件、Token、状态和移动端布局。
- 补齐平台展示页的筛选、排序、上传预览、标签、开关和异步状态交互。
- 修复排序导致预览主题变化、筛选弹窗误关闭和入口状态不更新的问题。
- 同步网站与客户端构建别名、依赖、路由测试和项目文档。

## 验证

- `npm run test -- src/components/shared-components/SharedComponentsShowcasePage.test.tsx`
- `npm run typecheck`
- `npm run check:encoding`
- `npm run build:raw`
- `git diff --check`
- pre-push 门禁通过

Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/202
Co-authored-by: kdletters <kdletters@qq.com>
Co-committed-by: kdletters <kdletters@qq.com>
2026-08-31 14:10:44 +08:00

135 lines
3.0 KiB
TypeScript

import type { ChangeEvent, ReactNode } from 'react';
import { getPlatformPillBadgeClassName } from './platformPillBadgeModel';
export type PlatformToggleRowSurface = 'soft' | 'plain';
export type PlatformToggleRowMode = 'checkbox' | 'status';
export type PlatformToggleRowProps = {
label: ReactNode;
checked: boolean;
onChange?: (checked: boolean) => void;
disabled?: boolean;
mode?: PlatformToggleRowMode;
icon?: ReactNode;
onLabel?: ReactNode;
offLabel?: ReactNode;
onClick?: () => void;
className?: string;
labelClassName?: string;
surface?: PlatformToggleRowSurface;
};
const PLATFORM_TOGGLE_ROW_SURFACE_CLASS: Record<
PlatformToggleRowSurface,
string
> = {
soft: 'bg-white/74',
plain: 'bg-white/78',
};
function renderToggleStatus({
checked,
offLabel,
onLabel,
}: {
checked: boolean;
offLabel: ReactNode;
onLabel: ReactNode;
}) {
return (
<span
className={getPlatformPillBadgeClassName({
tone: 'neutralSolid',
size: 'sm',
})}
>
{checked ? onLabel : offLabel}
</span>
);
}
/**
* 平台整行开关。
* 统一承接设置面板和结果页配置里的白底 label + checkbox / 状态行。
*/
export function PlatformToggleRow({
label,
checked,
onChange,
disabled = false,
mode = 'checkbox',
icon,
onLabel = '开启',
offLabel = '关闭',
onClick,
className,
labelClassName,
surface = 'soft',
}: PlatformToggleRowProps) {
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
onChange?.(event.target.checked);
};
const rowClassName = [
'flex min-h-12 items-center justify-between gap-3 rounded-[1rem] border border-[var(--platform-subpanel-border)] px-3',
PLATFORM_TOGGLE_ROW_SURFACE_CLASS[surface],
disabled ? 'opacity-60' : null,
className,
]
.filter(Boolean)
.join(' ');
const labelNode = (
<span
className={[
'flex min-w-0 items-center gap-2 text-sm font-semibold text-[var(--platform-text-strong)]',
labelClassName,
]
.filter(Boolean)
.join(' ')}
>
{icon ? <span className="shrink-0">{icon}</span> : null}
<span>{label}</span>
</span>
);
if (mode === 'status') {
if (onClick) {
return (
<button
type="button"
onClick={onClick}
disabled={disabled}
className={[
rowClassName,
'w-full text-left transition hover:bg-white disabled:cursor-not-allowed',
].join(' ')}
>
{labelNode}
{renderToggleStatus({ checked, onLabel, offLabel })}
</button>
);
}
return (
<div className={rowClassName}>
{labelNode}
{renderToggleStatus({ checked, onLabel, offLabel })}
</div>
);
}
return (
<label className={rowClassName}>
{labelNode}
<input
type="checkbox"
checked={checked}
disabled={disabled}
onChange={handleChange}
className="h-4 w-4 rounded border-[var(--platform-subpanel-border)]"
/>
</label>
);
}