MCPcopy Create free account
hub / github.com/PatoSala/react-native-blocks / useTextInput

Function useTextInput

packages/core/src/hooks/useTextInput.ts:15–265  ·  view source on GitHub ↗
(blockId: string)

Source from the content-addressed store, hash-verified

13import { useBlockRegistrationContext } from "../components/BlockRegistration";
14
15export function useTextInput(blockId: string) {
16 const {
17 blocks,
18 blocksOrder,
19 focusedBlockId,
20 setFocusedBlockId,
21 updateBlock,
22 mergeBlock,
23 splitBlock,
24 removeBlock,
25 getBlockSnapshot
26 } = useBlocksContext();
27 const {
28 registerRef,
29 showSoftInputOnFocus,
30 inputRefs
31 } = useTextBlocksContext();
32 const { isScrolling } = useScrollContext();
33 const { isDragging } = useBlocksMeasuresContext();
34 const { textBasedBlocks, defaultBlockType } = useBlockRegistrationContext();
35
36 const block = getBlockSnapshot(blockId);
37 const title = block.properties.title;
38 const inputRef = React.useRef<TextInput>(null);
39 const selectionRef = React.useRef({ start: title.length, end: title.length });
40 const valueRef = React.useRef(title);
41 const isFocused = focusedBlockId === blockId;
42 const isEditable = isScrolling === false && isDragging.value === false
43 ? true
44 : focusedBlockId === blockId // Keep focusd block editable whn scrolling.
45 ? true
46 : false;
47
48 const api = {
49 current: {
50 getText: () => valueRef.current,
51 setText: (text: string) => {
52 valueRef.current = text;
53 inputRef.current?.setNativeProps({ text: valueRef.current });
54 },
55 focus: () => {
56 inputRef.current?.focus();
57 },
58 blur: () => {
59 inputRef.current?.blur();
60 },
61 setSelection: (selection: { start: number; end: number }) => {
62 inputRef.current?.setSelection(selection.start, selection.end);
63 selectionRef.current = selection;
64 },
65 focusWithSelection: (selection: { start: number; end: number }, text?: string) => {
66 /** Find a better way to sync value on block update */
67 if (text !== undefined) {
68 inputRef.current?.setNativeProps({ text });
69 /* valueRef.current = text; */
70 }
71
72 inputRef.current?.setSelection(selection.start, selection.end); // Sync native input with selection state

Callers 10

CheckboxBlockFunction · 0.90
BulletBlockFunction · 0.90
CalloutBlockFunction · 0.90
CodeBlockFunction · 0.90
TextBlockFunction · 0.90
PageBlockFunction · 0.90
HeaderBlockFunction · 0.90
SubSubHeaderBlockFunction · 0.90
SubHeaderBlockFunction · 0.90
QuoteBlockFunction · 0.90

Calls 6

useBlocksContextFunction · 0.90
useTextBlocksContextFunction · 0.90
useScrollContextFunction · 0.90
useBlocksMeasuresContextFunction · 0.90
registerRefFunction · 0.85

Tested by

no test coverage detected