第6项:统一业务控件、主题与移动布局,保留原生输入和焦点契约
This commit is contained in:
1 parent
ff8b05973a
commit
c6ad648d7e
74 files changed
+3564
-2443
No files matched your search
@@ -0,0 +1,198 @@
|
||||
import { Button, Input, Table, type InputRef } from 'antd';
|
||||
import {
|
||||
createContext,
|
||||
useContext,
|
||||
useCallback,
|
||||
useImperativeHandle,
|
||||
useLayoutEffect,
|
||||
useState,
|
||||
type ChangeEventHandler,
|
||||
useRef,
|
||||
type Key,
|
||||
type ReactNode,
|
||||
type ComponentPropsWithRef,
|
||||
type ComponentPropsWithoutRef,
|
||||
} from 'react';
|
||||
import type { TextAreaRef } from 'antd/es/input/TextArea';
|
||||
|
||||
function classes(base: string, className?: string) {
|
||||
return className ? base + ' ' + className : base;
|
||||
}
|
||||
|
||||
export function LedgerButton({
|
||||
type = 'submit',
|
||||
className,
|
||||
...props
|
||||
}: Omit<ComponentPropsWithoutRef<'button'>, 'color'>) {
|
||||
const tokens = className?.split(/\s+/);
|
||||
return (
|
||||
<Button
|
||||
{...props}
|
||||
htmlType={type}
|
||||
type={tokens?.includes('primary') ? 'primary' : 'default'}
|
||||
danger={tokens?.includes('danger')}
|
||||
className={classes('ledger-button', className)}
|
||||
autoInsertSpace={false}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
type TextControl = HTMLInputElement | HTMLTextAreaElement;
|
||||
type FieldValue = ComponentPropsWithoutRef<'input'>['value'];
|
||||
|
||||
// Ant Input 在内部跟踪值。非受控业务表单仍须能通过原生 reset 还原其初始值。
|
||||
function useFormValue<T extends TextControl>(
|
||||
value: FieldValue,
|
||||
defaultValue: FieldValue,
|
||||
onChange: ChangeEventHandler<T> | undefined,
|
||||
control: () => T | null,
|
||||
) {
|
||||
const initial = String(defaultValue ?? '');
|
||||
const [edited, setEdited] = useState(initial);
|
||||
const uncontrolled = value === undefined;
|
||||
useLayoutEffect(() => {
|
||||
if (!uncontrolled) return;
|
||||
const field = control(),
|
||||
form = field?.form;
|
||||
if (!field || !form) return;
|
||||
const reset = (event: Event) => {
|
||||
// 默认动作在事件分发结束后执行。取消重置时保留原输入。
|
||||
field.defaultValue = initial;
|
||||
queueMicrotask(() => {
|
||||
if (!event.defaultPrevented) setEdited(initial);
|
||||
});
|
||||
};
|
||||
form.addEventListener('reset', reset);
|
||||
return () => form.removeEventListener('reset', reset);
|
||||
}, [control, initial, uncontrolled]);
|
||||
const change: ChangeEventHandler<T> = event => {
|
||||
if (uncontrolled) setEdited(event.target.value);
|
||||
onChange?.(event);
|
||||
};
|
||||
return { value: uncontrolled ? edited : value, onChange: change };
|
||||
}
|
||||
|
||||
function AntLedgerInput({
|
||||
size,
|
||||
defaultValue,
|
||||
value,
|
||||
onChange,
|
||||
...props
|
||||
}: Omit<ComponentPropsWithoutRef<'input'>, 'prefix'>) {
|
||||
const editor = useRef<InputRef>(null);
|
||||
const input = useCallback(() => editor.current?.input ?? null, []);
|
||||
const field = useFormValue(value, defaultValue, onChange, input);
|
||||
return <Input {...props} {...field} htmlSize={size} ref={editor} />;
|
||||
}
|
||||
|
||||
export function LedgerInput({ type, className, ...props }: ComponentPropsWithoutRef<'input'>) {
|
||||
const style = classes('ledger-input', className);
|
||||
if (type === 'checkbox' || type === 'radio' || type === 'date' || type === 'hidden' || type === 'file')
|
||||
return <input {...props} type={type} className={style} />;
|
||||
return <AntLedgerInput {...props} type={type} className={style} />;
|
||||
}
|
||||
|
||||
export function LedgerTextArea({
|
||||
ref,
|
||||
className,
|
||||
value,
|
||||
defaultValue,
|
||||
onChange,
|
||||
...props
|
||||
}: ComponentPropsWithRef<'textarea'>) {
|
||||
const editor = useRef<TextAreaRef>(null);
|
||||
const input = useCallback(() => editor.current?.resizableTextArea?.textArea ?? null, []);
|
||||
const field = useFormValue(value, defaultValue, onChange, input);
|
||||
useImperativeHandle(ref, () => editor.current!.resizableTextArea!.textArea);
|
||||
return <Input.TextArea {...props} {...field} ref={editor} className={classes('ledger-textarea', className)} />;
|
||||
}
|
||||
|
||||
export function LedgerSelect({ className, ...props }: ComponentPropsWithRef<'select'>) {
|
||||
return <select {...props} className={classes('ledger-select', className)} />;
|
||||
}
|
||||
|
||||
export function LedgerTable({ className, ...props }: ComponentPropsWithRef<'table'>) {
|
||||
return <table {...props} className={classes('ledger-table', className)} />;
|
||||
}
|
||||
|
||||
export function LedgerDialog({
|
||||
className,
|
||||
initialFocusSelector,
|
||||
...props
|
||||
}: ComponentPropsWithoutRef<'dialog'> & { initialFocusSelector?: string }) {
|
||||
const ref = useRef<HTMLDialogElement>(null);
|
||||
useLayoutEffect(() => {
|
||||
const dialog = ref.current!;
|
||||
dialog.showModal();
|
||||
if (initialFocusSelector) dialog.querySelector<HTMLElement>(initialFocusSelector)?.focus();
|
||||
// 原生 close() 在 DOM 移除前归还焦点给打开弹窗的按钮。
|
||||
return () => dialog.close();
|
||||
}, [initialFocusSelector]);
|
||||
return <dialog {...props} ref={ref} className={classes('ledger-dialog', className)} />;
|
||||
}
|
||||
|
||||
interface Column {
|
||||
title: ReactNode;
|
||||
headerClassName?: string;
|
||||
rowScope?: 'row';
|
||||
}
|
||||
interface TableRow {
|
||||
key: Key;
|
||||
cells: ReactNode[];
|
||||
attributes?: ComponentPropsWithoutRef<'tr'> & { [key: `data-${string}`]: unknown };
|
||||
cellClasses?: (string | undefined)[];
|
||||
}
|
||||
type TableAttributes = Omit<ComponentPropsWithoutRef<'table'>, 'children'>;
|
||||
const TableAttributesContext = createContext<TableAttributes>({});
|
||||
function DataTableElement(props: ComponentPropsWithoutRef<'table'>) {
|
||||
const attributes = useContext(TableAttributesContext);
|
||||
return <LedgerTable {...props} {...attributes} />;
|
||||
}
|
||||
const tableComponents = { table: DataTableElement };
|
||||
|
||||
export function LedgerDataTable({
|
||||
columns,
|
||||
rows,
|
||||
...attributes
|
||||
}: TableAttributes & {
|
||||
columns: Column[];
|
||||
rows: TableRow[];
|
||||
}) {
|
||||
if (!rows.length) {
|
||||
// 原表在无记录时保留空表体,页面自行展示对应的空状态。
|
||||
return (
|
||||
<LedgerTable {...attributes}>
|
||||
<thead>
|
||||
<tr>
|
||||
{columns.map((column, i) => (
|
||||
<th key={i} className={column.headerClassName}>
|
||||
{column.title}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody />
|
||||
</LedgerTable>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<TableAttributesContext.Provider value={attributes}>
|
||||
<Table<TableRow>
|
||||
size="small"
|
||||
rowKey="key"
|
||||
dataSource={rows}
|
||||
pagination={false}
|
||||
components={tableComponents}
|
||||
onRow={row => row.attributes || {}}
|
||||
columns={columns.map(({ title, headerClassName, rowScope }, index) => ({
|
||||
key: index,
|
||||
title,
|
||||
rowScope,
|
||||
onHeaderCell: () => ({ className: headerClassName }),
|
||||
onCell: row => ({ className: row.cellClasses?.[index] }),
|
||||
render: (_value, row) => row.cells[index],
|
||||
}))}
|
||||
/>
|
||||
</TableAttributesContext.Provider>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user