Overview
Flipkart product listings often require rich descriptions to highlight features and benefits. This problem simulates building a simplified text editor component that allows sellers to easily format their product descriptions using common styling options. The goal is to provide a basic, intuitive interface for content creation.
What you need to do / Task
Your task is to build a React component that functions as a basic text editor with markdown-like formatting capabilities. Specifically, you need to:
- Text Input Area: Create a primary input area (e.g., a
textarea) where users can type and select text.
- Formatting Buttons: Implement three buttons in a toolbar above the input area: 'Bold', 'Italic', and 'Underline'.
- Apply Formatting: When a user selects text in the input area and clicks one of the formatting buttons, apply the corresponding markdown-like syntax:
- 'Bold' should wrap selected text with double asterisks:
**selected text**
- 'Italic' should wrap selected text with single asterisks:
*selected text*
- 'Underline' should wrap selected text with underscores:
_selected text_
- Toggle Formatting: If the selected text (or the text immediately surrounding it) is already formatted with the clicked button's style, clicking the button again should remove that formatting.
- Live Preview: Display a live preview of the content below the editor. This preview should render the markdown-like syntax as actual HTML (e.g.,
**text** becomes <strong>text</strong>).
UI/UX expectations
- The editor should clearly separate the input area, the toolbar, and the preview area.
- Buttons in the toolbar should be easily clickable and visually distinct.
- The input area should allow standard text input and selection.
- The preview area should dynamically update as the user types or applies formatting.
- After applying/removing formatting, the editor should ideally maintain the user's cursor position or selection range for a smooth editing experience.
Acceptance criteria
- User can type text into the editor.
- Selecting text and clicking 'Bold' correctly applies
** formatting.
- Selecting text and clicking 'Italic' correctly applies
* formatting.
- Selecting text and clicking 'Underline' correctly applies
_ formatting.
- Clicking a formatting button on already formatted selected text removes the corresponding formatting.
- The preview area correctly renders
**text** as bold, *text* as italic, and _text_ as underlined HTML.
- The editor's state (text content, formatting) is managed efficiently using React's capabilities.