feat: 新增 node-frontend-react-class 示例(React 19 类组件,Tengine 托管)
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { Component } from 'react';
|
||||
import Home from './views/Home.jsx';
|
||||
import Form from './views/Form.jsx';
|
||||
import About from './views/About.jsx';
|
||||
|
||||
const TABS = [
|
||||
{ key: 'home', label: '首页', view: Home },
|
||||
{ key: 'form', label: '表单', view: Form },
|
||||
{ key: 'about', label: '关于', view: About },
|
||||
];
|
||||
|
||||
// 类组件写法:用 this.state + this.setState 管理页签,等价于函数版的 useState
|
||||
export default class App extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { active: 'home' };
|
||||
this.setActive = this.setActive.bind(this);
|
||||
}
|
||||
|
||||
setActive(key) {
|
||||
this.setState({ active: key });
|
||||
}
|
||||
|
||||
render() {
|
||||
const { active } = this.state;
|
||||
const Current = TABS.find((t) => t.key === active).view;
|
||||
|
||||
return (
|
||||
<div className="app">
|
||||
<header className="header">
|
||||
<span className="logo">node-frontend-react-class</span>
|
||||
<nav className="nav">
|
||||
{TABS.map((t) => (
|
||||
<button
|
||||
key={t.key}
|
||||
type="button"
|
||||
className={t.key === active ? 'nav-item active' : 'nav-item'}
|
||||
onClick={() => this.setActive(t.key)}
|
||||
>
|
||||
{t.label}
|
||||
</button>
|
||||
))}
|
||||
</nav>
|
||||
</header>
|
||||
<main className="main">
|
||||
<Current />
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { StrictMode } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import App from './App.jsx';
|
||||
import './styles.css';
|
||||
|
||||
createRoot(document.getElementById('root')).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
);
|
||||
@@ -0,0 +1,156 @@
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
html,
|
||||
body,
|
||||
#root {
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||
color: #303133;
|
||||
background: #f5f7fa;
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 24px;
|
||||
height: 56px;
|
||||
padding: 0 20px;
|
||||
background: #fff;
|
||||
border-bottom: 1px solid #ebeef5;
|
||||
}
|
||||
|
||||
.logo {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: #409eff;
|
||||
}
|
||||
|
||||
.nav {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.nav-item {
|
||||
padding: 6px 14px;
|
||||
border: 1px solid transparent;
|
||||
border-radius: 4px;
|
||||
background: transparent;
|
||||
color: #303133;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.nav-item:hover {
|
||||
color: #409eff;
|
||||
}
|
||||
|
||||
.nav-item.active {
|
||||
color: #409eff;
|
||||
border-color: #d9ecff;
|
||||
background: #ecf5ff;
|
||||
}
|
||||
|
||||
.main {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: #fff;
|
||||
border: 1px solid #ebeef5;
|
||||
border-radius: 4px;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card + .card {
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
margin: 0 0 16px;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.kv {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.kv th,
|
||||
.kv td {
|
||||
padding: 6px 12px;
|
||||
border: 1px solid #ebeef5;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.kv th {
|
||||
width: 40%;
|
||||
background: #fafafa;
|
||||
}
|
||||
|
||||
.clock {
|
||||
margin: 0 0 12px;
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 6px 14px;
|
||||
border: 1px solid #dcdfe6;
|
||||
border-radius: 4px;
|
||||
background: #fff;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn.primary {
|
||||
color: #fff;
|
||||
border-color: #409eff;
|
||||
background: #409eff;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.field label {
|
||||
width: 80px;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.field input,
|
||||
.field select {
|
||||
flex: 1;
|
||||
max-width: 320px;
|
||||
padding: 6px 10px;
|
||||
border: 1px solid #dcdfe6;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.hint {
|
||||
margin-top: 12px;
|
||||
padding: 8px 12px;
|
||||
border-radius: 4px;
|
||||
background: #f0f9eb;
|
||||
color: #67c23a;
|
||||
}
|
||||
|
||||
.error {
|
||||
margin-left: 8px;
|
||||
color: #f56c6c;
|
||||
font-size: 12px;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Component } from 'react';
|
||||
|
||||
// 静态说明页:类组件写法,无状态
|
||||
export default class About extends Component {
|
||||
render() {
|
||||
return (
|
||||
<div className="card">
|
||||
<h3 className="card-title">关于</h3>
|
||||
<table className="kv">
|
||||
<tbody>
|
||||
<tr><th>框架</th><td>React 19.3.1(class 组件)</td></tr>
|
||||
<tr><th>写法</th><td>继承 Component,状态在 this.state,生命周期用 componentDidMount/WillUnmount</td></tr>
|
||||
<tr><th>构建</th><td>Vite 7 + @vitejs/plugin-react 5</td></tr>
|
||||
<tr><th>部署</th><td>Tengine 3.1.0(proxy-tengine3 镜像)托管 dist/</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import { Component } from 'react';
|
||||
|
||||
const EMPTY = { name: '', type: '', enabled: true, date: '' };
|
||||
|
||||
// 类组件受控表单:字段统一走 this.state.form,错误与结果各占一个 state 字段
|
||||
export default class Form extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { form: { ...EMPTY }, errors: {}, result: '' };
|
||||
this.update = this.update.bind(this);
|
||||
this.submit = this.submit.bind(this);
|
||||
this.reset = this.reset.bind(this);
|
||||
}
|
||||
|
||||
update(key, value) {
|
||||
this.setState((s) => ({ form: { ...s.form, [key]: value } }));
|
||||
}
|
||||
|
||||
validate(form) {
|
||||
const next = {};
|
||||
if (!form.name.trim()) next.name = '请输入名称';
|
||||
if (!form.type) next.type = '请选择类型';
|
||||
this.setState({ errors: next });
|
||||
return Object.keys(next).length === 0;
|
||||
}
|
||||
|
||||
submit(e) {
|
||||
e.preventDefault();
|
||||
const { form } = this.state;
|
||||
if (!this.validate(form)) return;
|
||||
this.setState({ result: `已提交:${JSON.stringify(form)}` });
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.setState({ form: { ...EMPTY }, errors: {}, result: '' });
|
||||
}
|
||||
|
||||
render() {
|
||||
const { form, errors, result } = this.state;
|
||||
return (
|
||||
<div className="card">
|
||||
<h3 className="card-title">React 类组件受控表单</h3>
|
||||
<form onSubmit={this.submit} noValidate>
|
||||
<div className="field">
|
||||
<label htmlFor="name">名称</label>
|
||||
<input id="name" value={form.name} onChange={(e) => this.update('name', e.target.value)} placeholder="请输入名称" />
|
||||
{errors.name && <span className="error">{errors.name}</span>}
|
||||
</div>
|
||||
<div className="field">
|
||||
<label htmlFor="type">类型</label>
|
||||
<select id="type" value={form.type} onChange={(e) => this.update('type', e.target.value)}>
|
||||
<option value="">请选择</option>
|
||||
<option value="frontend">前端</option>
|
||||
<option value="backend">后端</option>
|
||||
</select>
|
||||
{errors.type && <span className="error">{errors.type}</span>}
|
||||
</div>
|
||||
<div className="field">
|
||||
<label htmlFor="enabled">启用</label>
|
||||
<input id="enabled" type="checkbox" checked={form.enabled} onChange={(e) => this.update('enabled', e.target.checked)} />
|
||||
</div>
|
||||
<div className="field">
|
||||
<label htmlFor="date">日期</label>
|
||||
<input id="date" type="date" value={form.date} onChange={(e) => this.update('date', e.target.value)} />
|
||||
</div>
|
||||
<div className="field">
|
||||
<label />
|
||||
<button type="submit" className="btn primary">提交</button>{' '}
|
||||
<button type="button" className="btn" onClick={this.reset}>重置</button>
|
||||
</div>
|
||||
</form>
|
||||
{result && <div className="hint">{result}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import { Component, version as reactVersion } from 'react';
|
||||
|
||||
function format(d) {
|
||||
const p = (n) => String(n).padStart(2, '0');
|
||||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
||||
}
|
||||
|
||||
// 类组件:状态放 this.state,用 componentDidMount / componentWillUnmount 管理定时器
|
||||
export default class Home extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
count: 0,
|
||||
clock: format(new Date()),
|
||||
keyword: '',
|
||||
};
|
||||
this.timer = null;
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.timer = setInterval(() => {
|
||||
this.setState({ clock: format(new Date()) });
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
}
|
||||
|
||||
render() {
|
||||
const { count, clock, keyword } = this.state;
|
||||
return (
|
||||
<>
|
||||
<div className="grid">
|
||||
<div className="card">
|
||||
<h3 className="card-title">运行时</h3>
|
||||
<table className="kv">
|
||||
<tbody>
|
||||
<tr><th>React</th><td>{reactVersion}</td></tr>
|
||||
<tr><th>写法</th><td>class(继承 Component)</td></tr>
|
||||
<tr><th>路由</th><td>示例用 this.state 页签,不引入路由库</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<h3 className="card-title">计数器(this.state)</h3>
|
||||
<p className="clock">{count}</p>
|
||||
<button type="button" className="btn" onClick={() => this.setState((s) => ({ count: s.count - 1 }))}>-1</button>{' '}
|
||||
<button type="button" className="btn primary" onClick={() => this.setState((s) => ({ count: s.count + 1 }))}>+1</button>{' '}
|
||||
<button type="button" className="btn" onClick={() => this.setState({ count: 0 })}>重置</button>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<h3 className="card-title">当前时间(componentDidMount)</h3>
|
||||
<p className="clock">{clock}</p>
|
||||
<span className="hint">前端本地时间,不请求后端</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="card">
|
||||
<h3 className="card-title">受控输入</h3>
|
||||
<div className="field">
|
||||
<label htmlFor="kw">关键字</label>
|
||||
<input
|
||||
id="kw"
|
||||
value={keyword}
|
||||
onChange={(e) => this.setState({ keyword: e.target.value })}
|
||||
placeholder="输入关键字过滤,纯前端"
|
||||
/>
|
||||
</div>
|
||||
{keyword && <div className="hint">已输入:{keyword}</div>}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user