66 lines
5.3 KiB
TypeScript
66 lines
5.3 KiB
TypeScript
import type { Color } from '../adapters/real';
|
|
import { useEffect, useRef, useId, cloneElement, isValidElement, type ReactElement, type ReactNode } from 'react';
|
|
export function Icon({ name }: {
|
|
name: string;
|
|
}) { return <svg className="ico" viewBox="0 0 24 24" aria-hidden="true"><use href={`/assets/icons.svg#i-${name}`}/></svg>; }
|
|
export function Button({ children, onClick, disabled = false, primary = false, small = true }: {
|
|
children: ReactNode;
|
|
onClick?: () => void;
|
|
disabled?: boolean;
|
|
primary?: boolean;
|
|
small?: boolean;
|
|
}) { return <button className={`${primary ? 'btn-primary' : 'btn-ghost'}${small ? ' sm' : ''}`} disabled={disabled} onClick={onClick}>{children}</button>; }
|
|
export function Status({ text, error = false }: {
|
|
text: string;
|
|
error?: boolean;
|
|
}) { return text ? <div role={error ? 'alert' : 'status'} className={`status show ${error ? 'fail' : 'ok'}`}>{text}</div> : null; }
|
|
export function Field({ label, children, hint }: {
|
|
label: string;
|
|
children: ReactNode;
|
|
hint?: string;
|
|
}) { const id = useId(); return <label className="field" htmlFor={id}><span id={`${id}-label`}>{label}</span>{isValidElement(children) ? cloneElement(children as ReactElement<{
|
|
id: string;
|
|
'aria-labelledby': string;
|
|
}>, { id, 'aria-labelledby': `${id}-label` }) : children}{hint && <small>{hint}</small>}</label>; }
|
|
export function Modal({ title, children, onClose, onConfirm, confirm = '确认', danger = false, disabled = false }: {
|
|
title: string;
|
|
children: ReactNode;
|
|
onClose: () => void;
|
|
onConfirm: () => void;
|
|
confirm?: string;
|
|
danger?: boolean;
|
|
disabled?: boolean;
|
|
}) {
|
|
const root = useRef<HTMLDivElement>(null);
|
|
const close = useRef(onClose);
|
|
close.current = onClose;
|
|
useEffect(() => { const prev = document.activeElement as HTMLElement | null; (root.current?.querySelector<HTMLElement>('input,button:not(:disabled)') || root.current)?.focus(); const key = (e: KeyboardEvent) => { if (e.key === 'Escape') {
|
|
e.stopImmediatePropagation();
|
|
close.current();
|
|
} if (e.key === 'Tab') {
|
|
const nodes = root.current?.querySelectorAll<HTMLElement>('button:not(:disabled),input');
|
|
if (nodes?.length) {
|
|
const first = nodes[0], last = nodes[nodes.length - 1];
|
|
if (e.shiftKey && document.activeElement === first) {
|
|
e.preventDefault();
|
|
last.focus();
|
|
}
|
|
else if (!e.shiftKey && document.activeElement === last) {
|
|
e.preventDefault();
|
|
first.focus();
|
|
}
|
|
}
|
|
} }; document.addEventListener('keydown', key, true); return () => { document.removeEventListener('keydown', key, true); prev?.focus(); }; }, []);
|
|
return <><div className="scrim open" onClick={onClose}/><div className={`modal glass open ${danger ? 'danger' : ''}`} ref={root} tabIndex={-1} role="dialog" aria-modal="true" aria-label={title}><div className="mi"><Icon name={danger ? 'warn' : 'sparkle'}/></div><h3>{title}</h3><div className="mb">{children}</div><div className="acts"><Button onClick={onClose}>取消</Button><Button primary disabled={disabled} onClick={onConfirm}>{confirm}</Button></div></div></>;
|
|
}
|
|
export function BaseColor({ color }: { color?: Color } = {}) {
|
|
if (color) return <><div className="card birth-color"><h4><Icon name="lock"/>出生底色 · 真实排盘</h4>
|
|
{color.calendar && <dl className="kv calendar"><dt>公历</dt><dd>{color.calendar.solar}</dd><dt>农历</dt><dd>{color.calendar.lunar}{color.calendar.leap_month && <span className="tag accent">闰月</span>}</dd><dt>出生时辰</dt><dd>{color.calendar.hour} · {color.calendar.hour_range}</dd><dt>排盘时间</dt><dd>北京时间 {color.calendar.calculation_time}</dd></dl>}
|
|
<div className="pillars">{[['year','年柱'], ['month','月柱'], ['day','日柱'], ['time','时柱']].map(([key, label]) => <div key={key}><small>{label}</small><b>{color.pillars[key]}</b></div>)}</div>
|
|
<details className="birth-help"><summary>四柱、日主和农历,分别是什么?</summary><p>四柱是出生年、月、日、时的四组干支,合起来为八个字。日柱的第一个字叫日主,是解释组合的参照。五行描述组合的侧重,十神描述各天干与日主的关系,不是十种好坏标签。</p><p>{color.calendar?.rule || '月柱按节气划分,与农历月份不是同一回事。'}</p></details>
|
|
<p className="d-foot">{color.interpretation || '当前档案尚未提供分层说明,请更新服务后重试。原底色保留。'}</p>
|
|
<div className="color-sections">{color.sections?.map(section => <section key={section.id}><h4>{section.title}</h4><p>{section.text}</p><details><summary>这段说明的依据</summary><p>{section.basis}</p></details></section>)}</div>
|
|
</div><p className="lock"><Icon name="lock"/>确认创建后,生辰与底色只读;后来的成长另存。</p></>;
|
|
return <><div className="card"><h4><Icon name="lock"/>出生底色 · 演示预览</h4><div className="pillars">{['年柱', '月柱', '日柱', '时柱'].map(p => <div key={p}><small>{p}</small><b>待接</b></div>)}</div><p>演示不执行排盘,不用固定八字或性格代替计算。普通入口会显示真实公历、农历、四柱和六层底色说明。</p></div><p className="lock"><Icon name="lock"/>确认创建后,生辰与底色只读,成长另存。</p></>;
|
|
}
|