Skip to content

Commit 390b66d

Browse files
committed
feat: onFocus onBlur
1 parent 78f3b82 commit 390b66d

7 files changed

Lines changed: 104 additions & 23 deletions

File tree

apps/example-web/src/App.tsx

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import {
1414
type OnSubmitEditing,
1515
type OnChangeMentionEvent,
1616
type OnMentionDetected,
17-
EnrichedText,
1817
} from 'react-native-enriched-html';
1918
import { WEB_DEFAULT_HTML_STYLE } from './defaultHtmlStyle';
20-
import type { NativeSyntheticEvent, TextStyle } from 'react-native';
19+
import type { NativeSyntheticEvent } from 'react-native';
2120
import { EditorActions } from './components/EditorActions';
2221
import { SetValueModal } from './components/SetValueModal';
2322
import { ImageModal } from './components/ImageModal';
@@ -28,6 +27,7 @@ import { Toolbar } from './components/Toolbar';
2827
import { MentionPopup, type MentionItem } from './components/MentionPopup';
2928
import { useUserMention } from './hooks/useUserMention';
3029
import { useChannelMention } from './hooks/useChannelMention';
30+
import { TextRenderer } from './components/TextRenderer';
3131

3232
const DEFAULT_LINK_STATE: OnLinkDetected = {
3333
text: '',
@@ -332,16 +332,7 @@ function App() {
332332

333333
{showHtmlOutput && <HtmlOutputPanel html={currentHtml} />}
334334

335-
<div className="container enriched-text-container">
336-
<h1 className="app-title">Enriched Text</h1>
337-
<EnrichedText
338-
style={enrichedTextStyle}
339-
htmlStyle={WEB_DEFAULT_HTML_STYLE}
340-
useHtmlNormalizer
341-
>
342-
{enrichedTextValue}
343-
</EnrichedText>
344-
</div>
335+
<TextRenderer htmlValue={enrichedTextValue} />
345336

346337
{isSetValueModalOpen && (
347338
<SetValueModal
@@ -383,14 +374,4 @@ const enrichedInputStyle: EnrichedInputStyle = {
383374
fontSize: 18,
384375
};
385376

386-
const enrichedTextStyle: TextStyle = {
387-
backgroundColor: 'gainsboro',
388-
width: '100%',
389-
marginVertical: 12,
390-
paddingVertical: 12,
391-
paddingHorizontal: 14,
392-
borderRadius: 8,
393-
fontSize: 18,
394-
};
395-
396377
export default App;
File renamed without changes.

apps/example-web/src/components/EditorActions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import './EditorActions.css';
1+
import './Actions.css';
22

33
interface EditorActionsProps {
44
showHtmlOutput: boolean;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import './Actions.css';
2+
3+
interface TextActionsProps {
4+
onFocus: () => void;
5+
onBlur: () => void;
6+
}
7+
8+
export function TextActions({ onFocus, onBlur }: TextActionsProps) {
9+
return (
10+
<div className="btn-row" data-testid="text-actions-row">
11+
<button className="btn" data-testid="text-focus-button" onClick={onFocus}>
12+
Focus
13+
</button>
14+
<button className="btn" data-testid="text-blur-button" onClick={onBlur}>
15+
Blur
16+
</button>
17+
</div>
18+
);
19+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { useRef } from 'react';
2+
import type { TextStyle } from 'react-native';
3+
import {
4+
EnrichedText,
5+
type BlurEvent,
6+
type EnrichedTextInstance,
7+
type FocusEvent,
8+
} from 'react-native-enriched-html';
9+
import { WEB_DEFAULT_HTML_STYLE } from '../defaultHtmlStyle';
10+
11+
interface TextRendererProps {
12+
htmlValue: string;
13+
}
14+
15+
export function TextRenderer({ htmlValue }: TextRendererProps) {
16+
const ref = useRef<EnrichedTextInstance>(null);
17+
18+
const handleTextFocus = (e: FocusEvent) => {
19+
console.log('[EnrichedText] onFocus', e.nativeEvent);
20+
};
21+
22+
const handleTextBlur = (e: BlurEvent) => {
23+
console.log('[EnrichedText] onBlur', e.nativeEvent);
24+
};
25+
26+
return (
27+
<div className="container enriched-text-container">
28+
<h1 className="app-title">Enriched Text</h1>
29+
<EnrichedText
30+
ref={ref}
31+
style={enrichedTextStyle}
32+
htmlStyle={WEB_DEFAULT_HTML_STYLE}
33+
useHtmlNormalizer
34+
onFocus={handleTextFocus}
35+
onBlur={handleTextBlur}
36+
>
37+
{htmlValue}
38+
</EnrichedText>
39+
<div className="btn-row" data-testid="editor-actions-row">
40+
<button
41+
className="btn"
42+
data-testid="focus-button"
43+
onClick={() => ref.current?.focus()}
44+
>
45+
Focus
46+
</button>
47+
<button
48+
className="btn"
49+
data-testid="blur-button"
50+
onClick={() => ref.current?.blur()}
51+
>
52+
Blur
53+
</button>
54+
</div>
55+
</div>
56+
);
57+
}
58+
59+
const enrichedTextStyle: TextStyle = {
60+
backgroundColor: 'gainsboro',
61+
width: '100%',
62+
marginVertical: 12,
63+
paddingVertical: 12,
64+
paddingHorizontal: 14,
65+
borderRadius: 8,
66+
fontSize: 18,
67+
};

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type {
1717
FocusEvent,
1818
BlurEvent,
1919
EnrichedTextInputInstance,
20+
EnrichedTextInstance,
2021
ContextMenuItem,
2122
OnChangeMentionEvent,
2223
EnrichedTextHtmlStyle,

src/web/EnrichedText.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { prepareHtmlForWeb } from './normalization/prepareHtmlForWeb';
2020
import { INLINE_IMAGE_CSS_VARIABLES } from './styleConversion/inlineImageCSSVariables';
2121
import { useImageErrorFallback } from './useImageErrorFallback';
2222
import { usePressInteractions } from './usePressInteractions';
23+
import { adaptWebToNativeEvent } from './adaptWebToNativeEvent';
2324

2425
export const EnrichedText = memo(
2526
({
@@ -30,6 +31,8 @@ export const EnrichedText = memo(
3031
selectionColor,
3132
selectable = false,
3233
useHtmlNormalizer = false,
34+
onFocus,
35+
onBlur,
3336
}: EnrichedTextProps) => {
3437
const containerRef = useRef<HTMLDivElement>(null);
3538

@@ -102,6 +105,16 @@ export const EnrichedText = memo(
102105
tabIndex={-1}
103106
style={finalStyle}
104107
className={ENRICHED_TEXT_CLASSNAME}
108+
onFocus={
109+
onFocus
110+
? (event) => onFocus(adaptWebToNativeEvent(event, { target: -1 }))
111+
: undefined
112+
}
113+
onBlur={
114+
onBlur
115+
? (event) => onBlur(adaptWebToNativeEvent(event, { target: -1 }))
116+
: undefined
117+
}
105118
dangerouslySetInnerHTML={{ __html: finalHtml }}
106119
/>
107120
</>

0 commit comments

Comments
 (0)