写一个按钮组件
- User
写一个按钮组件
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
写一个按钮组件
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
我来为你创建一个可复用的按钮组件。这里提供React版本的实现:
import React from 'react';
import './Button.css';
const Button = ({
children,
type = 'primary',
size = 'medium',
disabled = false,
loading = false,
onClick,
className = '',
...props
}) => {
const baseClass = 'btn';
const typeClass = `btn--${type}`;
const sizeClass = `btn--${size}`;
const disabledClass = disabled ? 'btn--disabled' : '';
const loadingClass = loading ? 'btn--loading' : '';
const buttonClass = [
baseClass,
typeClass,
sizeClass,
disabledClass,
loadingClass,
className
].filter(Boolean).join(' ');
const handleClick = (e) => {
if (disabled || loading) return;
onClick && onClick(e);
};
return (
<button
className={buttonClass}
onClick={handleClick}
disabled={disabled || loading}
{...props}
>
{loading && <span className="btn__spinner"></span>}
<span className="btn__content">{children}</span>
</button>
);
};
export default Button;
对应的CSS样式文件:
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
transition: all 0.2s ease;
position: relative;
text-decoration: none;
outline: none;
}
/* 按钮类型 */
.btn--primary {
background-color: #007bff;
color: white;
}
.btn--primary:hover:not(.btn--disabled):not(.btn--loading) {
background-color: #0056b3;
}
.btn--secondary {
background-color: #6c757d;
color: white;
}
.btn--secondary:hover:not(.btn--disabled):not(.btn--loading) {
background-color: #545b62;
}
.btn--success {
background-color: #28a745;
color: white;
}
.btn--danger {
background-color: #dc3545;
color: white;
}
.btn--outline {
background-color: transparent;
color: #007bff;
border: 1px solid #007bff;
}
/* 按钮大小 */
.btn--small {
padding: 6px 12px;
font-size: 12px;
}
.btn--medium {
padding: 8px 16px;
font-size: 14px;
}
.btn--large {
padding: 12px 24px;
font-size: 16px;
}
/* 状态样式 */
.btn--disabled {
opacity: 0.6;
cursor: not-allowed;
}
.btn--loading {
cursor: not-allowed;
}
.btn--loading .btn__content {
opacity: 0.7;
}
/* 加载动画 */
.btn__spinner {
width: 14px;
height: 14px;
border: 2px solid transparent;
border-top: 2px solid currentColor;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-right: 8px;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
使用示例:
function App() {
return (
<div>
{/* 基础按钮 */}
<Button onClick={() => console.log('点击了按钮')}>
点击我
</Button>
{/* 不同类型 */}
<Button type="secondary">次要按钮</Button>
<Button type="success">成功按钮</Button>
<Button type="danger">危险按钮</Button>
<Button type="outline">轮廓按钮</Button>
{/* 不同大小 */}
<Button size="small">小按钮</Button>
<Button size="large">大按钮</Button>
{/* 状态按钮 */}
<Button disabled>禁用按钮</Button>
<Button loading>加载中...</Button>
</div>
);
}
这个按钮组件具有以下特性:
你需要针对特定框架的版本吗?比如Vue或者原生JavaScript?