Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/TransformerTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,34 @@ export type TransformerTextInputProps = Omit<TextInputProps, 'value'> & {

export const TransformerTextInput = forwardRef(
(
{ transformer, onChangeText, ...others }: TransformerTextInputProps,
{
transformer,
onChangeText,
defaultValue,
...others
}: TransformerTextInputProps,
forwardedRef: Ref<TransformerTextInputInstance>,
) => {
const transformerId = useMemo(() => {
return registerTransformer(transformer);
}, [transformer]);

// Pre-transform defaultValue on the JS thread so Yoga measures the correct
// text from the start. Without this the native-side transformation happens
// after layout and doesn't trigger a remeasure.
const transformedDefaultValue = useMemo(() => {
if (defaultValue == null) {
return undefined;
}
const result = transformer.worklet({
value: defaultValue,
previousValue: defaultValue,
selection: { start: defaultValue.length, end: defaultValue.length },
previousSelection: { start: 0, end: 0 },
});
return result?.value ?? defaultValue;
}, [defaultValue, transformer]);

useEffect(() => {
return () => {
unregisterTransformer(transformerId);
Expand Down Expand Up @@ -135,6 +156,7 @@ export const TransformerTextInput = forwardRef(
// @ts-expect-error
ref={inputRef}
onChangeText={handleChangeText}
defaultValue={transformedDefaultValue}
{...others}
/>
</TransformerTextInputDecoratorViewNativeComponent>
Expand Down
7 changes: 4 additions & 3 deletions src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runOnUI } from 'react-native-worklets';
import { runOnUI, executeOnUIRuntimeSync } from 'react-native-worklets';
import NativeTransformerTextInputModule from './NativeTransformerTextInputModule';
import { type Selection, type Transformer } from './Transformer';
import { computeUncontrolledSelection, validateSelection } from './selection';
Expand Down Expand Up @@ -29,8 +29,9 @@ function initializeIfNeeded() {
return;
}

// Important that `runOnUI` is called first to make sure the UI runtime is initialized.
runOnUI(() => {
// Set up registry on UI runtime synchronously so it is guaranteed to exist
// when native code accesses it after install().
executeOnUIRuntimeSync(() => {
'worklet';

const transformersMap = new Map<number, TransformerWrapper>();
Expand Down
Loading