src/pages/pg001.tsx
import { NextPage } from 'next';
import Head from 'next/head';
import { InputHTMLAttributes, useState } from 'react';
interface IProps {
}
/**
* 组件示例
*/
const page: NextPage<IProps> = () => {
return (
<>
<Head>
<title>组件示例</title>
</Head>
<C001 title='name'></C001>
<C001 title='age'></C001>
</>
);
};
export default page;
function C001({ title, id, placeholder, ...rest }: InputHTMLAttributes<HTMLInputElement>) {
const [inputid] = useState(id || Math.random().toString());
const plchdr = placeholder || 'type here...';
return <>
<div>
<label htmlFor={inputid}>{title}</label>
<br />
<input id={inputid} placeholder={plchdr} {...rest} />
</div>
<style jsx>{`
label{
border: 0;
}
`}</style>
</>;
}
新增组件 src/components/c003.tsx
import { InputHTMLAttributes, useState } from 'react';
export default function Input({ title, id, placeholder, ...rest }: InputHTMLAttributes<HTMLInputElement>) {
const [inputid] = useState(id || Math.random().toString());
const plchdr = placeholder || 'type here...';
return <>
<div>
<label htmlFor={inputid}>{title}</label>
<br />
<input id={inputid} placeholder={plchdr} {...rest} />
</div>
<style jsx>{`
label{
border: 0;
}
`}</style>
</>;
}
导入该组件后,原页面代码src/pages/pg001.tsx
import { NextPage } from 'next';
import Head from 'next/head';
import Input from '../components/c005';
interface IProps {
}
/**
* 组件示例
*/
const page: NextPage<IProps> = () => {
return (
<>
<Head>
<title>组件示例</title>
</Head>
<Input title='name' />
<Input title='age' />
</>
);
};
export default page;