Files
Genarrative/packages/shared/src/components/PlatformTextField.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

185 lines
4.8 KiB
TypeScript

import type {
InputHTMLAttributes,
SelectHTMLAttributes,
TextareaHTMLAttributes,
} from 'react';
export type PlatformTextFieldSize = 'xs' | 'sm' | 'md' | 'lg';
export type PlatformTextFieldDensity = 'default' | 'compact' | 'roomy';
export type PlatformTextFieldTone = 'warm' | 'rose' | 'emerald' | 'sky';
export type PlatformTextFieldSurface = 'platform' | 'editorDark';
type PlatformTextFieldBaseProps = {
size?: PlatformTextFieldSize;
density?: PlatformTextFieldDensity;
tone?: PlatformTextFieldTone;
surface?: PlatformTextFieldSurface;
};
type PlatformTextFieldInputProps = Omit<
InputHTMLAttributes<HTMLInputElement>,
'size'
> &
PlatformTextFieldBaseProps & {
variant?: 'input';
};
type PlatformTextFieldTextareaProps =
TextareaHTMLAttributes<HTMLTextAreaElement> &
PlatformTextFieldBaseProps & {
variant: 'textarea';
};
export type PlatformTextFieldProps =
| PlatformTextFieldInputProps
| PlatformTextFieldTextareaProps;
export type PlatformSelectFieldProps = Omit<
SelectHTMLAttributes<HTMLSelectElement>,
'size'
> &
PlatformTextFieldBaseProps;
const PLATFORM_TEXT_FIELD_SIZE_CLASS: Record<PlatformTextFieldSize, string> = {
xs: 'text-sm leading-5',
sm: 'text-sm font-semibold',
md: 'text-sm leading-6',
lg: 'text-base font-semibold',
};
const PLATFORM_TEXT_FIELD_PLATFORM_DENSITY_CLASS: Record<
PlatformTextFieldDensity,
string
> = {
default: 'rounded-[1rem] bg-white/86 px-3 py-3',
compact: 'rounded-[0.85rem] bg-white/90 px-3 py-2',
roomy: 'rounded-[1rem] bg-white/90 px-4 py-3',
};
const PLATFORM_TEXT_FIELD_EDITOR_DARK_DENSITY_CLASS: Record<
PlatformTextFieldDensity,
string
> = {
default: 'rounded-[1rem] bg-black/30 px-3 py-3',
compact: 'rounded-[0.85rem] bg-black/30 px-3 py-2',
roomy: 'rounded-[1rem] bg-black/30 px-4 py-3',
};
const PLATFORM_TEXT_FIELD_PLATFORM_TONE_CLASS: Record<
PlatformTextFieldTone,
string
> = {
warm: 'focus:border-[var(--platform-surface-hover-border)] focus:ring-[var(--platform-warm-border)]',
rose: 'focus:border-rose-200 focus:ring-rose-100',
emerald: 'focus:border-emerald-200 focus:ring-emerald-100',
sky: 'focus:border-sky-200 focus:ring-sky-100',
};
const PLATFORM_TEXT_FIELD_EDITOR_DARK_TONE_CLASS: Record<
PlatformTextFieldTone,
string
> = {
warm: 'focus:border-amber-300/40',
rose: 'focus:border-rose-300/40',
emerald: 'focus:border-emerald-400/40',
sky: 'focus:border-sky-400/40',
};
function buildPlatformTextFieldClassName({
surface,
size,
density,
tone,
multiline,
className,
}: {
surface: PlatformTextFieldSurface;
size: PlatformTextFieldSize;
density: PlatformTextFieldDensity;
tone: PlatformTextFieldTone;
multiline?: boolean;
className?: string;
}) {
const isEditorDark = surface === 'editorDark';
return [
'w-full platform-text-field outline-none transition disabled:cursor-not-allowed disabled:opacity-60',
isEditorDark
? 'platform-text-field--editor-dark border border-white/10 text-white'
: 'border border-[var(--platform-subpanel-border)] text-[var(--platform-text-strong)] focus:bg-white focus:ring-2',
isEditorDark
? PLATFORM_TEXT_FIELD_EDITOR_DARK_DENSITY_CLASS[density]
: PLATFORM_TEXT_FIELD_PLATFORM_DENSITY_CLASS[density],
PLATFORM_TEXT_FIELD_SIZE_CLASS[size],
isEditorDark
? PLATFORM_TEXT_FIELD_EDITOR_DARK_TONE_CLASS[tone]
: PLATFORM_TEXT_FIELD_PLATFORM_TONE_CLASS[tone],
multiline ? 'resize-none' : null,
className,
]
.filter(Boolean)
.join(' ');
}
/**
* 平台白底表单输入框。
* 统一承接结果页 / 工作台白底圆角输入框和文本域的基础 chrome。
*/
export function PlatformTextField({
variant = 'input',
size = 'sm',
density = 'default',
tone = 'warm',
surface = 'platform',
className,
...fieldProps
}: PlatformTextFieldProps) {
const fieldClassName = buildPlatformTextFieldClassName({
surface,
size,
density,
tone,
multiline: variant === 'textarea',
className,
});
if (variant === 'textarea') {
return (
<textarea
{...(fieldProps as TextareaHTMLAttributes<HTMLTextAreaElement>)}
className={fieldClassName}
/>
);
}
return (
<input
{...(fieldProps as InputHTMLAttributes<HTMLInputElement>)}
className={fieldClassName}
/>
);
}
/**
* 平台白底下拉框。
* 与 PlatformTextField 共享输入 chrome,避免业务页为同一组表单控件重复拼样式。
*/
export function PlatformSelectField({
size = 'sm',
density = 'default',
tone = 'warm',
surface = 'platform',
className,
...selectProps
}: PlatformSelectFieldProps) {
const fieldClassName = buildPlatformTextFieldClassName({
surface,
size,
density,
tone,
className,
});
return <select {...selectProps} className={fieldClassName} />;
}