Files
tap/components/demand/form.tsx
T

55 lines
9.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { useState } from 'react';
import { X, ArrowLeft, ArrowRight, Save, Send, Info, Check, CircleAlert, FilePenLine } from 'lucide-react';
import { Dialog, DialogContent, DialogTitle, DialogDescription } from '@/components/ui/dialog';
import { emptyFields, missingFields, type Fields, type Demand } from '@/lib/demand';
export type SaveInput={action:string;id?:string;revision?:number;fields?:Fields;mode?:string;[key:string]:unknown};
export function DemandForm({initial,onSave,onClose,busy,error,userName,existing}:{initial?:Demand;onSave:(body:SaveInput)=>Promise<void>;onClose:()=>void;busy:boolean;error:string;userName:string;existing:Demand[]}){
const [f,setF]=useState<Fields>(initial?.fields??{...emptyFields,proposer:userName,owner:userName});const [step,setStep]=useState(0);const [invalid,setInvalid]=useState<string[]>([]);const [exit,setExit]=useState(false);
const set=(k:keyof Fields,v:string|boolean)=>setF(prev=>({...prev,[k]:v}));
const duplicate=existing.find(d=>d.id!==initial?.id&&f.title.trim().length>3&&d.fields.title.includes(f.title.trim()));
const field=(k:keyof Fields,label:string,placeholder:string,opts:{large?:boolean;full?:boolean;required?:boolean;type?:string;hint?:string;max?:number}={})=><label className={'form-field '+(opts.full?'span-two':'')} key={k}><span>{label}{opts.required&&<em>*</em>}</span>{opts.large?<textarea value={String(f[k])} onChange={e=>set(k,e.target.value)} placeholder={placeholder} rows={3} maxLength={opts.max??3000}/>:<input type={opts.type??'text'} value={String(f[k])} onChange={e=>set(k,e.target.value)} placeholder={placeholder} maxLength={opts.max??100}/>} {opts.hint&&<small>{opts.hint}</small>}</label>;
async function save(mode:string){const m=mode==='draft'?(f.title.trim()?[]:['需求标题']):missingFields(f,mode==='review');setInvalid(m);if(m.length){setStep(mode==='review'?step:m.some(x=>['需求标题','所属项目','业务模块','提出人','所属部门','需求来源','需求负责人'].includes(x))?0:1);return;}await onSave({action:initial&&!initial.demo?'update':'create',id:initial?.demo?undefined:initial?.id,revision:initial?.revision,fields:f,mode});}
return <Dialog open onOpenChange={open=>{if(!open&&!busy)setExit(true)}}><DialogContent className="demand-modal form-modal" showCloseButton={false} onInteractOutside={e=>e.preventDefault()}><div className="modal-heading"><div className="modal-icon"><FilePenLine size={23}/></div><div><DialogTitle className="modal-title">{initial?'完善需求信息':'登记新需求'}</DialogTitle><DialogDescription className="modal-subtitle">一个独立业务目标,对应一条需求。先登记,再逐步完善。</DialogDescription></div><button className="icon-button" aria-label="关闭登记窗口" onClick={()=>setExit(true)} disabled={busy}><X size={20}/></button></div>
<div className="form-steps">{['基本信息','业务背景与目标','范围与验收','数据与参考材料'].map((s,i)=><button key={s} className={i===step?'current':i<step?'done':''} onClick={()=>setStep(i)}><span>{i<step?<Check size={14}/>:i+1}</span><b>{s}</b></button>)}</div>
<div className="modal-scroll"><div className="form-content">
{initial?.demo&&<div className="info-banner"><Info size={17}/>你正在参考示例填写,保存将创建一条属于你的新需求。</div>}
{exit&&<div className="exit-banner"><div><strong>本次编辑尚未保存</strong><p>你可以继续填写,或保存为草稿后离开。</p></div><button className="btn" onClick={()=>setExit(false)}>继续填写</button><button className="text-button danger-text" onClick={onClose}>放弃本次编辑</button></div>}
{(error||invalid.length>0)&&<div className="error-banner" role="alert"><CircleAlert size={18}/><span>{error||'请补充:'+invalid.join('、')}</span></div>}
<div className="form-section-heading"><h2>{['这是什么需求?','希望解决什么业务问题?','怎样才算做好了?','开发前,还需要哪些信息?'][step]}</h2><p>{['基本信息用于明确需求归属和联系人员。','用熟悉的业务语言描述,不需要填写技术实现方案。','这一部分可在登记后补充,提交评审前需填写完整。','材料可通过链接关联;简单需求可直接使用本表单作为需求正文。'][step]}</p></div>
<div className="form-grid">
{step===0&&<>
{field('title','需求标题','例如:订单管理—支持按订单状态导出',{full:true,required:true,hint:'建议使用“业务模块 + 动作或能力”,避免“页面优化”等笼统标题。'})}
{duplicate&&<div className="span-two info-banner"><Info size={16}/>已有相似需求「{duplicate.fields.title}」,请核对是否为同一目标。</div>}
{field('project','所属项目','例如:企业业务平台',{required:true})}{field('module','业务模块','例如:订单管理',{required:true})}
<label className="form-field"><span>需求类型<em>*</em></span><select value={f.type} onChange={e=>set('type',e.target.value)}>{['新增功能','功能优化','缺陷修复','配置/数据变更'].map(t=><option key={t}>{t}</option>)}</select></label>
{field('department','所属部门','例如:销售部',{required:true})}{field('proposer','提出人','填写实际提出人',{required:true})}{field('owner','需求负责人','负责确认范围和业务验收的人',{required:true})}{field('source','需求来源','例如:销售周会、客户反馈、日常操作问题',{full:true,required:true,max:200})}
</>}
{step===1&&<>
{field('background','业务背景与现有处理方式','现在怎么做?具体遇到了什么问题?请补充实际案例或数据依据。',{large:true,full:true,required:true,max:6000})}
{field('impact','影响对象与业务依据','哪些用户或岗位受影响?影响范围有多大?依据是什么?',{large:true,full:true,required:true})}
{field('goal','预期目标','完成后希望达到什么结果?有量化依据时,请说明目标值与统计口径。',{large:true,full:true,required:true})}
<label className="check-field span-two"><input type="checkbox" checked={f.noFixedDate} onChange={e=>set('noFixedDate',e.target.checked)}/>没有固定的上线日期</label>
{!f.noFixedDate&&<>{field('expectedDate','期望上线日期','',{type:'date',required:true})}{field('dateReason','期望日期依据','例如:9 月经营分析会前使用',{required:true,max:1000})}</>}
<div className="info-banner span-two"><Info size={17}/>这是业务期望时间,正式交付计划由需求负责人和开发负责人评审确认。</div>
</>}
{step===2&&<>
{field('scope','实施范围与功能说明','F-01:本次需要支持的功能、操作步骤和业务规则。\nF-02:另一项相关功能。',{large:true,full:true,required:true,max:6000})}
{field('outOfScope','范围外事项','明确这次不做什么,无相关事项可填写“无”。',{large:true,full:true,required:true})}
{field('criteria','验收标准','AC-01 / F-01:在什么场景下执行什么操作,应得到什么结果。\n请覆盖正常流程、空数据、非法输入等关键异常。',{large:true,full:true,required:true,max:6000,hint:'每一项都应能实际验证,避免仅写“使用正常”“体验良好”。'})}
<label className="form-field"><span>建议优先级<em>*</em></span><select value={f.priority} onChange={e=>set('priority',e.target.value)}><option value="P2">P2 · 常规优化或储备</option><option value="P1">P1 · 近期业务目标</option><option value="P0">P0 · 业务阻塞或关键节点</option></select><small>最终优先级以评审结论为准。</small></label>
{field('priorityReason','优先级依据','说明业务影响、时间要求及依赖',{required:true,max:1000})}
</>}
{step===3&&<>
<label className="check-field span-two"><input type="checkbox" checked={f.noData} onChange={e=>set('noData',e.target.checked)}/>本需求不涉及数据来源</label>
{!f.noData&&<>{field('dataSource','数据来自哪里','平时从哪个系统、页面、报表或文件获取?需要哪些内容、多久更新?多个来源不一致时以谁为准?',{large:true,full:true,required:true})}{field('dataContact','由谁提供数据','提供人、部门及联系方式',{full:true,required:true,max:300})}</>}
{field('risk','依赖与风险','需要哪些系统或人员配合?有无风险及应对方式?无则填“无”。',{large:true,full:true,required:true})}
{field('pending','待确认事项','待确认问题 + 责任人 + 计划确认时间;无则填“无”。',{large:true,full:true,required:true})}
{field('version','需求文档版本','例如:v1.0',{required:true})}
{field('references','参考材料与链接','可填写 PRD、原型、截图或业务规则的受限共享链接,每行一条。',{large:true,full:true,max:5000,hint:'复杂需求请关联独立 PRD;账号密码等敏感信息通过约定受限渠道提供。'})}
</>}
</div></div></div>
<footer className="modal-footer"><div className="footer-left">{(!initial||initial.status==='草稿'||initial.demo)&&<button className="btn" onClick={()=>save('draft')} disabled={busy}><Save size={16}/>保存草稿</button>}{step>0&&<button className="text-button back-button" onClick={()=>setStep(step-1)} disabled={busy}><ArrowLeft size={15}/>上一步</button>}</div><div className="footer-right">{step>=1&&<button className="btn" onClick={()=>save('register')} disabled={busy}>{busy?'保存中…':'先登记,稍后补充'}</button>}{step<3?<button className="btn primary" onClick={()=>setStep(step+1)} disabled={busy}>下一步<ArrowRight size={16}/></button>:<button className="btn primary" onClick={()=>save('review')} disabled={busy}><Send size={16}/>{busy?'提交中…':'提交评审'}</button>}</div></footer>
</DialogContent></Dialog>
}