fix(ui): 完善界面交互与开发配置
This commit is contained in:
@@ -17,7 +17,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import React, { forwardRef, useLayoutEffect, useRef, useState } from 'react';
|
||||
import React, {
|
||||
forwardRef,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
import { Table, theme } from 'antd';
|
||||
import { colord } from 'colord';
|
||||
import { isCenterTableScope, prepareCenterColumns } from './centerTablePolicy';
|
||||
@@ -56,8 +62,21 @@ const CenterTable = forwardRef(function CenterTable(
|
||||
const scroller = root.querySelector('.ant-table-content,.ant-table-body');
|
||||
const previousTabIndex = scroller?.getAttribute('tabindex');
|
||||
if (scroller && previousTabIndex === null) scroller.tabIndex = 0;
|
||||
const update = () => setWidth(root.clientWidth);
|
||||
update();
|
||||
const observer = new ResizeObserver(([entry]) => {
|
||||
// ResizeObserver already measured the box; avoid a synchronous layout read.
|
||||
setWidth(entry.contentRect.width);
|
||||
});
|
||||
observer.observe(root);
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
if (scroller && previousTabIndex === null)
|
||||
scroller.removeAttribute('tabindex');
|
||||
};
|
||||
}, [enabled]);
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
const root = tableRef.current?.nativeElement;
|
||||
if (!root) return;
|
||||
const measureActionWidths = () => {
|
||||
const next = {};
|
||||
root.querySelectorAll('th.center-action-cell').forEach((header) => {
|
||||
@@ -69,9 +88,15 @@ const CenterTable = forwardRef(function CenterTable(
|
||||
(element) => element.getBoundingClientRect().width > 0,
|
||||
);
|
||||
const widths = cells.map((cell) => {
|
||||
const controls = [
|
||||
const controlsWithWidths = [
|
||||
...cell.querySelectorAll('button, a, [role="button"]'),
|
||||
].filter((element) => element.getBoundingClientRect().width > 0);
|
||||
]
|
||||
.map((element) => ({
|
||||
element,
|
||||
width: element.getBoundingClientRect().width,
|
||||
}))
|
||||
.filter(({ width }) => width > 0);
|
||||
const controls = controlsWithWidths.map(({ element }) => element);
|
||||
if (!controls.length) return 0;
|
||||
let group = controls[0].parentElement;
|
||||
while (
|
||||
@@ -85,10 +110,7 @@ const CenterTable = forwardRef(function CenterTable(
|
||||
(Number.parseFloat(cellStyle.paddingRight) || 0);
|
||||
const gap = Number.parseFloat(getComputedStyle(group).columnGap) || 8;
|
||||
return (
|
||||
controls.reduce(
|
||||
(total, element) => total + element.getBoundingClientRect().width,
|
||||
0,
|
||||
) +
|
||||
controlsWithWidths.reduce((total, { width }) => total + width, 0) +
|
||||
gap * Math.max(0, controls.length - 1) +
|
||||
padding
|
||||
);
|
||||
@@ -107,13 +129,8 @@ const CenterTable = forwardRef(function CenterTable(
|
||||
});
|
||||
};
|
||||
const frame = requestAnimationFrame(measureActionWidths);
|
||||
const observer = new ResizeObserver(update);
|
||||
observer.observe(root);
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
cancelAnimationFrame(frame);
|
||||
if (scroller && previousTabIndex === null)
|
||||
scroller.removeAttribute('tabindex');
|
||||
};
|
||||
}, [enabled, width, columns, props.dataSource]);
|
||||
return (
|
||||
|
||||
@@ -36,7 +36,10 @@ import {
|
||||
Space as AntSpace,
|
||||
} from 'antd';
|
||||
import dayjs from 'dayjs';
|
||||
import { useConsoleControlSize } from '../console/ConsoleContext';
|
||||
import {
|
||||
useConsoleControlSize,
|
||||
useConsolePage,
|
||||
} from '../console/ConsoleContext';
|
||||
|
||||
export const controlSize = (size) => (size === 'default' ? 'middle' : size);
|
||||
export const placement = (position) =>
|
||||
@@ -53,6 +56,12 @@ export const placement = (position) =>
|
||||
const statusOf = (status) =>
|
||||
['error', 'warning'].includes(status) ? status : undefined;
|
||||
|
||||
function useSettingsInputId(id) {
|
||||
const generatedId = React.useId();
|
||||
const isSettings = useConsolePage()?.page.key === 'setting';
|
||||
return id ?? (isSettings ? generatedId : undefined);
|
||||
}
|
||||
|
||||
// Existing screens consume values; DOM events stay inside this view boundary.
|
||||
export const Input = forwardRef(function Input(
|
||||
{
|
||||
@@ -69,10 +78,20 @@ export const Input = forwardRef(function Input(
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
const isSettings = useConsolePage()?.page.key === 'setting';
|
||||
const Component = mode === 'password' ? AntInput.Password : AntInput;
|
||||
return (
|
||||
<Component
|
||||
{...props}
|
||||
id={useSettingsInputId(props.id)}
|
||||
autoComplete={
|
||||
props.autoComplete ??
|
||||
(isSettings
|
||||
? mode === 'password' || props.type === 'password'
|
||||
? 'new-password'
|
||||
: 'off'
|
||||
: undefined)
|
||||
}
|
||||
ref={ref}
|
||||
size={useConsoleControlSize(size)}
|
||||
readOnly={readonly ?? props.readOnly}
|
||||
@@ -91,6 +110,7 @@ export const TextArea = forwardRef(function TextArea(
|
||||
return (
|
||||
<AntInput.TextArea
|
||||
{...props}
|
||||
id={useSettingsInputId(props.id)}
|
||||
ref={ref}
|
||||
autoSize={autosize}
|
||||
maxLength={props.maxLength ?? maxCount}
|
||||
@@ -152,6 +172,7 @@ export const InputNumber = forwardRef(function InputNumber(
|
||||
return (
|
||||
<AntInputNumber
|
||||
{...props}
|
||||
id={useSettingsInputId(props.id)}
|
||||
ref={ref}
|
||||
size={useConsoleControlSize(size)}
|
||||
controls={!hideButtons}
|
||||
|
||||
+62
-14
@@ -82,8 +82,12 @@ export const Form = forwardRef(function Form(
|
||||
const FormComponent = consolePage ? ConsoleForm : AntForm;
|
||||
const [form] = AntForm.useForm();
|
||||
const [, renderValues] = useReducer((value) => value + 1, 0);
|
||||
const callbacks = useRef({ getFormApi, onValueChange });
|
||||
callbacks.current = { getFormApi, onValueChange };
|
||||
const callbacks = useRef({});
|
||||
callbacks.current = {
|
||||
getFormApi,
|
||||
onValueChange,
|
||||
renderValues: typeof children === 'function' ? renderValues : () => {},
|
||||
};
|
||||
const api = useMemo(
|
||||
() => ({
|
||||
getValues: () => form.getFieldsValue(true),
|
||||
@@ -94,17 +98,17 @@ export const Form = forwardRef(function Form(
|
||||
callbacks.current.onValueChange?.(form.getFieldsValue(true), {
|
||||
[field]: value,
|
||||
});
|
||||
renderValues();
|
||||
callbacks.current.renderValues();
|
||||
},
|
||||
setValues: (next) => {
|
||||
form.setFieldsValue(next);
|
||||
callbacks.current.onValueChange?.(form.getFieldsValue(true), next);
|
||||
renderValues();
|
||||
callbacks.current.renderValues();
|
||||
},
|
||||
reset: (fields) => {
|
||||
form.resetFields(fields?.map?.(fieldPath));
|
||||
callbacks.current.onValueChange?.(form.getFieldsValue(true));
|
||||
renderValues();
|
||||
callbacks.current.renderValues();
|
||||
},
|
||||
validate: (fields) => form.validateFields(fields?.map?.(fieldPath)),
|
||||
submitForm: () => form.submit(),
|
||||
@@ -122,10 +126,13 @@ export const Form = forwardRef(function Form(
|
||||
useImperativeHandle(ref, () => ({ formApi: api, ...api }), [api]);
|
||||
useEffect(() => {
|
||||
callbacks.current.getFormApi?.(api);
|
||||
renderValues();
|
||||
callbacks.current.renderValues();
|
||||
}, [api]);
|
||||
useEffect(() => {
|
||||
if (values) form.setFieldsValue(values);
|
||||
if (values) {
|
||||
form.setFieldsValue(values);
|
||||
callbacks.current.renderValues();
|
||||
}
|
||||
}, [form, values]);
|
||||
return (
|
||||
<FormContext.Provider value={form}>
|
||||
@@ -151,7 +158,13 @@ export const Form = forwardRef(function Form(
|
||||
wrapperCol={
|
||||
props.wrapperCol ?? (labelWidth ? { flex: '1 1 0' } : undefined)
|
||||
}
|
||||
labelAlign={labelAlign}
|
||||
labelAlign={
|
||||
labelAlign === 'left'
|
||||
? 'start'
|
||||
: labelAlign === 'right'
|
||||
? 'end'
|
||||
: labelAlign
|
||||
}
|
||||
onFinish={onSubmit}
|
||||
onFinishFailed={
|
||||
consolePage && onSubmitFail
|
||||
@@ -168,7 +181,7 @@ export const Form = forwardRef(function Form(
|
||||
}
|
||||
onValuesChange={(_, all) => {
|
||||
callbacks.current.onValueChange?.(all);
|
||||
renderValues();
|
||||
callbacks.current.renderValues();
|
||||
}}
|
||||
>
|
||||
{typeof children === 'function' ? (
|
||||
@@ -198,15 +211,41 @@ function labelProps(label) {
|
||||
|
||||
// Legacy Form.RadioGroup reports the selected value; standalone RadioGroup reports an event.
|
||||
const FieldRadioGroup = forwardRef(function FieldRadioGroup(
|
||||
{ onChange, ...props },
|
||||
{ onChange, id, options, children, ...props },
|
||||
ref,
|
||||
) {
|
||||
// A label must target a radio input, rather than the group's non-labelable div.
|
||||
const optionIndex = options?.findIndex((option) => !option?.disabled);
|
||||
let childLabeled = false;
|
||||
return (
|
||||
<Controls.RadioGroup
|
||||
{...props}
|
||||
ref={ref}
|
||||
id={id ? `${id}-group` : undefined}
|
||||
options={options?.map((option, index) =>
|
||||
index === optionIndex && id
|
||||
? {
|
||||
...(typeof option === 'object'
|
||||
? option
|
||||
: { label: option, value: option }),
|
||||
id,
|
||||
}
|
||||
: option,
|
||||
)}
|
||||
onChange={(event) => onChange?.(event.target.value, event)}
|
||||
/>
|
||||
>
|
||||
{React.Children.map(children, (child) => {
|
||||
if (
|
||||
!id ||
|
||||
childLabeled ||
|
||||
!React.isValidElement(child) ||
|
||||
child.props.disabled
|
||||
)
|
||||
return child;
|
||||
childLabeled = true;
|
||||
return React.cloneElement(child, { id });
|
||||
})}
|
||||
</Controls.RadioGroup>
|
||||
);
|
||||
});
|
||||
|
||||
@@ -284,6 +323,9 @@ export function withField(
|
||||
) : (
|
||||
<Component
|
||||
{...props}
|
||||
{...(Component === FieldRadioGroup && fieldLabel.label
|
||||
? { 'aria-label': props['aria-label'] ?? fieldLabel.label }
|
||||
: {})}
|
||||
id={props.id ?? fieldLabel.htmlFor}
|
||||
ref={ref}
|
||||
style={style}
|
||||
@@ -370,13 +412,19 @@ Form.Slot = function Slot({ label, error, children, ...props }) {
|
||||
</AntForm.Item>
|
||||
);
|
||||
};
|
||||
Form.Section = function Section({ text, extraText, children, ...props }) {
|
||||
Form.Section = function Section({
|
||||
text,
|
||||
extraText,
|
||||
children,
|
||||
component: Component = 'section',
|
||||
...props
|
||||
}) {
|
||||
return (
|
||||
<section {...props}>
|
||||
<Component {...props}>
|
||||
<h3 className='ui-form-section'>{text}</h3>
|
||||
{extraText && <p className='ui-field-help'>{extraText}</p>}
|
||||
{children}
|
||||
</section>
|
||||
</Component>
|
||||
);
|
||||
};
|
||||
Form.Label = function Label({ required, children, ...props }) {
|
||||
|
||||
@@ -383,12 +383,14 @@ function CenterPaginatedTable({
|
||||
{...props}
|
||||
pagination={{
|
||||
...centerPaginationOptions(config, isMobile),
|
||||
showSizeChanger: Boolean(
|
||||
local ||
|
||||
config.onPageSizeChange ||
|
||||
config.onShowSizeChange ||
|
||||
config.onChange,
|
||||
),
|
||||
showSizeChanger:
|
||||
config.showSizeChanger ??
|
||||
Boolean(
|
||||
local ||
|
||||
config.onPageSizeChange ||
|
||||
config.onShowSizeChange ||
|
||||
config.onChange,
|
||||
),
|
||||
current,
|
||||
pageSize,
|
||||
onShowSizeChange: undefined,
|
||||
|
||||
Reference in New Issue
Block a user