syncfusion-react-textbox
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseImplementing Syncfusion React TextBox Component
实现Syncfusion React TextBox组件
The TextBox component is a lightweight input control that captures user text input with support for floating labels, validation states, icons, and advanced features. This skill guides you through implementing, configuring, and customizing the TextBox component in React applications.
TextBox组件是一款轻量级输入控件,可采集用户文本输入,支持浮动标签、校验状态、图标等高级特性。本指南将介绍如何在React应用中实现、配置和自定义TextBox组件。
Navigation Guide
导航指南
🛑 Agentic use: Do not execute multiple steps autonomously. Confirm with the user before each action (install, run, file creation).
🛑 Agent使用说明: 请勿自主执行多步操作,每次执行安装、运行、文件创建等操作前都需要先向用户确认。
Getting Started
入门指南
📄 Read: references/getting-started.md
- Vite setup for React development
- Installing package 🛑 STOP — Do not install packages autonomously. Ask the user to run:
@syncfusion/ej2-react-inputs. Pin a specific version (e.g.,npm install @syncfusion/ej2-react-inputs) and verify with@syncfusion/ej2-react-inputs@28.x.xnpm audit - Adding CSS imports and themes
- Creating your first TextBox component
- Adding icons and floating labels
- Running the development server 🛑 STOP — Do not start the dev server autonomously. Ask the user to run:
npm run dev
📄 阅读: references/getting-started.md
- React开发的Vite环境搭建
- 安装包 🛑 停止 — 请勿自主安装包,告知用户运行命令:
@syncfusion/ej2-react-inputs,固定指定版本(例如npm install @syncfusion/ej2-react-inputs)并通过@syncfusion/ej2-react-inputs@28.x.x校验npm audit - 引入CSS和主题
- 创建你的第一个TextBox组件
- 添加图标和浮动标签
- 运行开发服务器 🛑 停止 — 请勿自主启动开发服务器,告知用户运行命令:
npm run dev
Features and Groups
特性与功能组
📄 Read: references/features-and-groups.md
- Floating label behavior (Never, Always, Auto)
- Icons with method (prepend/append)
addIcon() - Clear button with property
showClearButton - Rounded corner with CSS class
e-corner - Disabled state with
enabled={false} - Multi-line textbox creation
- TextBox with clear button and floating label combinations
📄 阅读: references/features-and-groups.md
- 浮动标签行为(永不显示、始终显示、自动)
- 通过方法添加图标(前置/后置)
addIcon() - 通过属性启用清除按钮
showClearButton - 通过CSS类实现圆角效果
e-corner - 通过设置禁用状态
enabled={false} - 创建多行文本框
- 同时支持清除按钮和浮动标签的TextBox组合
Styling and Sizing
样式与尺寸配置
📄 Read: references/styling-and-sizing.md
- Three predefined sizes: Normal, Small (), Large (
e-small)e-bigger - Applying size classes via property
cssClass - Rounded corner with CSS class
e-corner - CSS customization for TextBox wrapper and floating label
- Custom CSS classes and themes
- Responsive design patterns
📄 阅读: references/styling-and-sizing.md
- 三种预设尺寸:普通、小尺寸()、大尺寸(
e-small)e-bigger - 通过属性应用尺寸类
cssClass - 通过CSS类实现圆角效果
e-corner - 自定义TextBox容器和浮动标签的CSS样式
- 自定义CSS类和主题
- 响应式设计模式
Multiline TextBox
多行文本框
📄 Read: references/multiline-textbox.md
- Creating multiline/textarea inputs with
multiline={true} - Floating labels with multiline
- Auto-resizing textboxes
- Disabling resize functionality
- Limiting text length with
htmlAttributes={{ maxlength: '...' }} - Character counting and display
📄 阅读: references/multiline-textbox.md
- 通过创建多行/textarea输入框
multiline={true} - 多行文本框支持浮动标签
- 自适应高度文本框
- 禁用调整尺寸功能
- 通过限制文本长度
htmlAttributes={{ maxlength: '...' }} - 字符计数与展示
Validation and States
校验与状态
📄 Read: references/validation-and-states.md
- Error, warning, and success validation states via
cssClass - Applying validation classes (,
e-error,e-warning)e-success - Disabled state with (not
enabled={false})disabled - Read-only state with
readonly={true} - Differences between disabled and read-only
- Dynamic color changes based on values using event
input
📄 阅读: references/validation-and-states.md
- 通过配置错误、警告、成功校验状态
cssClass - 应用校验类(、
e-error、e-warning)e-success - 通过设置禁用状态(而非
enabled={false})disabled - 通过设置只读状态
readonly={true} - 禁用状态和只读状态的区别
- 通过事件根据输入值动态修改颜色
input
Advanced Features
高级特性
📄 Read: references/advanced-features.md
- Adornments: and
prependTemplatepropertiesappendTemplate - Interactive adornments (password toggle, delete button)
- React functional components with hooks
- ,
useState,useEffect,useRefintegrationuseReducer - Event handling (created, input, change events)
- Form validation patterns
📄 阅读: references/advanced-features.md
- 装饰元素:和
prependTemplate属性appendTemplate - 交互性装饰元素(密码可见性切换、删除按钮)
- 结合Hooks的React函数式组件实现
- 、
useState、useEffect、useRef集成useReducer - 事件处理(created、input、change事件)
- 表单校验模式
Accessibility and Migration
无障碍与迁移
📄 Read: references/accessibility-and-migration.md
- WCAG 2.2, Section 508, and WAI-ARIA compliance
- Screen reader support and ARIA attributes
- Right-to-Left (RTL) support with property
enableRtl - Keyboard navigation support
- Migrating from CSS TextBox to React component
- Before/after code comparison
📄 阅读: references/accessibility-and-migration.md
- 符合WCAG 2.2、Section 508和WAI-ARIA标准
- 屏幕阅读器支持与ARIA属性
- 通过属性支持从右到左(RTL)显示
enableRtl - 键盘导航支持
- 从CSS TextBox迁移到React组件
- 迁移前后代码对比
API Reference
API参考
📄 Read: references/api.md
- All properties: ,
placeholder,floatLabelType,value,type,cssClass,multiline,showClearButton,enabled,readonly,enableRtl,enablePersistence,autocomplete,htmlAttributes,locale,width,prependTemplateappendTemplate - Methods: ,
addIcon,addAttributes,removeAttributes,focusIn,focusOut,destroygetPersistData - Events: ,
created,destroyed,change,input,focusblur
📄 阅读: references/api.md
- 所有属性:、
placeholder、floatLabelType、value、type、cssClass、multiline、showClearButton、enabled、readonly、enableRtl、enablePersistence、autocomplete、htmlAttributes、locale、width、prependTemplateappendTemplate - 方法:、
addIcon、addAttributes、removeAttributes、focusIn、focusOut、destroygetPersistData - 事件:、
created、destroyed、change、input、focusblur
Quick Start
快速开始
Basic TextBox with Floating Label
带浮动标签的基础TextBox
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import './App.css';
export default function App() {
return (
<TextBoxComponent
placeholder="Enter your name"
floatLabelType="Auto"
/>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import './App.css';
export default function App() {
return (
<TextBoxComponent
placeholder="Enter your name"
floatLabelType="Auto"
/>
);
}TextBox with Icon
带图标的TextBox
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { useRef } from 'react';
export default function App() {
const textboxRef = useRef(null);
const handleCreate = () => {
if (textboxRef.current) {
textboxRef.current.addIcon('append', 'e-icons e-input-popup-date');
}
};
return (
<TextBoxComponent
placeholder="Enter date"
floatLabelType="Auto"
ref={textboxRef}
created={handleCreate}
/>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { useRef } from 'react';
export default function App() {
const textboxRef = useRef(null);
const handleCreate = () => {
if (textboxRef.current) {
textboxRef.current.addIcon('append', 'e-icons e-input-popup-date');
}
};
return (
<TextBoxComponent
placeholder="Enter date"
floatLabelType="Auto"
ref={textboxRef}
created={handleCreate}
/>
);
}TextBox with Clear Button
带清除按钮的TextBox
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function App() {
return (
<TextBoxComponent
placeholder="Enter your email"
floatLabelType="Auto"
showClearButton={true}
/>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function App() {
return (
<TextBoxComponent
placeholder="Enter your email"
floatLabelType="Auto"
showClearButton={true}
/>
);
}Multiline TextBox
多行TextBox
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function App() {
return (
<TextBoxComponent
multiline={true}
placeholder="Enter your address"
floatLabelType="Auto"
/>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function App() {
return (
<TextBoxComponent
multiline={true}
placeholder="Enter your address"
floatLabelType="Auto"
/>
);
}Common Patterns
常用场景
Form with Validation States
带校验状态的表单
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { useState } from 'react';
export default function ValidationForm() {
const [cssClass, setCssClass] = useState('');
return (
<div>
<TextBoxComponent
placeholder="Enter username"
cssClass={cssClass}
floatLabelType="Auto"
input={(e: any) => {
if (!e.value) setCssClass('');
else if (e.value.length < 3) setCssClass('e-error');
else if (e.value.length < 6) setCssClass('e-warning');
else setCssClass('e-success');
}}
/>
</div>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { useState } from 'react';
export default function ValidationForm() {
const [cssClass, setCssClass] = useState('');
return (
<div>
<TextBoxComponent
placeholder="Enter username"
cssClass={cssClass}
floatLabelType="Auto"
input={(e: any) => {
if (!e.value) setCssClass('');
else if (e.value.length < 3) setCssClass('e-error');
else if (e.value.length < 6) setCssClass('e-warning');
else setCssClass('e-success');
}}
/>
</div>
);
}Password TextBox with Toggle
带可见性切换的密码输入框
tsx
import * as React from 'react';
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { useRef, useState } from 'react';
export default function PasswordInput() {
const textboxRef = useRef<TextBoxComponent>(null);
const [isVisible, setIsVisible] = useState(false);
const toggleVisibility = () => {
if (textboxRef.current) {
const newVisibility = !isVisible;
textboxRef.current.type = newVisibility ? 'text' : 'password';
setIsVisible(newVisibility);
}
};
function appendTemplate(): JSX.Element {
return (
<>
<span className="e-input-separator"></span>
<span
className={`e-icons ${isVisible ? 'e-eye-slash' : 'e-eye'}`}
onClick={toggleVisibility}
style={{ cursor: 'pointer' }}
></span>
</>
);
}
return (
<TextBoxComponent
ref={textboxRef}
type="password"
placeholder="Enter password"
floatLabelType="Auto"
appendTemplate={appendTemplate}
/>
);
}tsx
import * as React from 'react';
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { useRef, useState } from 'react';
export default function PasswordInput() {
const textboxRef = useRef<TextBoxComponent>(null);
const [isVisible, setIsVisible] = useState(false);
const toggleVisibility = () => {
if (textboxRef.current) {
const newVisibility = !isVisible;
textboxRef.current.type = newVisibility ? 'text' : 'password';
setIsVisible(newVisibility);
}
};
function appendTemplate(): JSX.Element {
return (
<>
<span className="e-input-separator"></span>
<span
className={`e-icons ${isVisible ? 'e-eye-slash' : 'e-eye'}`}
onClick={toggleVisibility}
style={{ cursor: 'pointer' }}
></span>
</>
);
}
return (
<TextBoxComponent
ref={textboxRef}
type="password"
placeholder="Enter password"
floatLabelType="Auto"
appendTemplate={appendTemplate}
/>
);
}Email Input with Unit Label
带单位标签的邮箱输入框
tsx
import * as React from 'react';
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function EmailInput() {
function prependTemplate(): JSX.Element {
return (
<>
<span className="e-icons e-user"></span>
<span className="e-input-separator"></span>
</>
);
}
function appendTemplate(): JSX.Element {
return (
<>
<span className="e-input-separator"></span>
<span>.com</span>
</>
);
}
return (
<TextBoxComponent
type="email"
placeholder="Enter email"
floatLabelType="Auto"
prependTemplate={prependTemplate}
appendTemplate={appendTemplate}
/>
);
}tsx
import * as React from 'react';
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function EmailInput() {
function prependTemplate(): JSX.Element {
return (
<>
<span className="e-icons e-user"></span>
<span className="e-input-separator"></span>
</>
);
}
function appendTemplate(): JSX.Element {
return (
<>
<span className="e-input-separator"></span>
<span>.com</span>
</>
);
}
return (
<TextBoxComponent
type="email"
placeholder="Enter email"
floatLabelType="Auto"
prependTemplate={prependTemplate}
appendTemplate={appendTemplate}
/>
);
}Rounded Corner TextBox
圆角TextBox
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function RoundedCornerTextBox() {
return (
<TextBoxComponent
placeholder="Enter Date"
cssClass="e-corner"
/>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function RoundedCornerTextBox() {
return (
<TextBoxComponent
placeholder="Enter Date"
cssClass="e-corner"
/>
);
}Disabled TextBox
禁用状态TextBox
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function DisabledTextBox() {
return (
<TextBoxComponent
placeholder="Enter Name"
enabled={false}
/>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function DisabledTextBox() {
return (
<TextBoxComponent
placeholder="Enter Name"
enabled={false}
/>
);
}RTL TextBox
RTL TextBox
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function RTLTextBox() {
return (
<TextBoxComponent
placeholder="أدخل اسمك"
floatLabelType="Auto"
enableRtl={true}
/>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
export default function RTLTextBox() {
return (
<TextBoxComponent
placeholder="أدخل اسمك"
floatLabelType="Auto"
enableRtl={true}
/>
);
}Auto-sizing Multiline TextBox
自适应高度多行TextBox
tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { useRef } from 'react';
export default function AutoSizeTextbox() {
const textboxRef = useRef(null);
const handleInput = () => {
if (textboxRef.current) {
const elem = textboxRef.current.respectiveElement;
elem.style.height = 'auto';
elem.style.height = elem.scrollHeight + 'px';
}
};
const handleCreate = () => {
if (textboxRef.current) {
textboxRef.current.addAttributes({ rows: 1 });
}
handleInput();
};
return (
<TextBoxComponent
multiline={true}
placeholder="Enter your message"
floatLabelType="Auto"
ref={textboxRef}
created={handleCreate}
input={handleInput}
/>
);
}tsx
import { TextBoxComponent } from '@syncfusion/ej2-react-inputs';
import { useRef } from 'react';
export default function AutoSizeTextbox() {
const textboxRef = useRef(null);
const handleInput = () => {
if (textboxRef.current) {
const elem = textboxRef.current.respectiveElement;
elem.style.height = 'auto';
elem.style.height = elem.scrollHeight + 'px';
}
};
const handleCreate = () => {
if (textboxRef.current) {
textboxRef.current.addAttributes({ rows: 1 });
}
handleInput();
};
return (
<TextBoxComponent
multiline={true}
placeholder="Enter your message"
floatLabelType="Auto"
ref={textboxRef}
created={handleCreate}
input={handleInput}
/>
);
}Key Properties
核心属性
| Property | Type | Purpose |
|---|---|---|
| | Hint text shown when input is empty |
| | Label animation behavior |
| | Sets the content of the TextBox |
| | Input type ( |
| | Convert to textarea for multi-line input |
| | Display clear button when input has value |
| | Apply CSS classes for sizing/validation/appearance (e.g., |
| | Enable ( |
| | Allow selection but prevent editing |
| | Enable right-to-left rendering |
| | Persist value state between page reloads ⚠️ Stores data in browser storage — enable only with explicit user consent |
| | Control browser autocomplete ( |
| | Pass additional HTML attributes (e.g., |
| | Override global culture/localization value |
| | Set component width |
| | Render element before input |
| | Render element after input |
| 属性 | 类型 | 用途 |
|---|---|---|
| | 输入框为空时显示的提示文本 |
| | 标签动画行为 |
| | 设置TextBox的内容 |
| | 输入框类型( |
| | 转换为textarea实现多行输入 |
| | 输入框有值时显示清除按钮 |
| | 应用CSS类实现尺寸/校验/外观定制(例如 |
| | 启用( |
| | 允许选中但禁止编辑 |
| | 启用从右到左渲染 |
| | 页面重载间保留值状态 ⚠️ 数据存储在浏览器缓存中 — 仅在获得用户明确同意后启用 |
| | 控制浏览器自动填充( |
| | 传递额外HTML属性(例如 |
| | 覆盖全局文化/本地化配置 |
| | 设置组件宽度 |
| | 在输入框前渲染元素 |
| | 在输入框后渲染元素 |
Key Events
核心事件
| Event | Arguments | Purpose |
|---|---|---|
| | Fires after component initialization |
| | Fires when component is destroyed |
| | Fires when value changes on focus-out |
| | Fires on every keystroke |
| | Fires when TextBox gains focus |
| | Fires when TextBox loses focus |
| 事件 | 参数 | 用途 |
|---|---|---|
| | 组件初始化完成后触发 |
| | 组件销毁时触发 |
| | 输入框失焦且值发生变化时触发 |
| | 每次按键输入时触发 |
| | TextBox获得焦点时触发 |
| | TextBox失去焦点时触发 |
Related Documentation
相关文档
ℹ️ External links below are for manual reference only. Do not auto-fetch these URLs in an agentic pipeline without explicit user consent.
- Syncfusion React TextBox Component Demo (external)
- React TextBox API Reference (external)
- Syncfusion React Inputs Package (external — verify before installing)
- React Functional Components
ℹ️ 以下外部链接仅用于手动参考。 未获得用户明确同意前,请勿在Agent流水线中自动抓取这些URL的内容。
- Syncfusion React TextBox组件演示 (外部链接)
- React TextBox API参考 (外部链接)
- Syncfusion React Inputs包 (外部链接 — 安装前请校验)
- React函数式组件