taiga-family / maskito

Collection of libraries to create an input mask which ensures that user types value according to predefined format.
https://maskito.dev
Apache License 2.0
567 stars 19 forks source link

🚀 - React Native support #656

Open nsbarsukov opened 7 months ago

nsbarsukov commented 7 months ago

Which package(s) are relevant/related to the feature request?

@maskito/react

Description

Problem

Investigate if it is possible to add React Native support.

The following code

import {TextInput, View} from 'react-native';
import {useMaskito} from "@maskito/react";

// ...

const options = {
  mask: /^\d+$/,
}

export default function App() {
  const maskedInputRef = useMaskito({options});

  return (
    <View style={styles.container}>
      <TextInput ref={maskedInputRef}></TextInput>
    </View>
  );
}

throws

TypeError: e.querySelector is not a function (it is undefined)

This error is located at:
    in App (created by withDevTools(App))
    in withDevTools(App)
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in main(RootComponent), js engine: hermes

Why? In React Native el.querySelector does not work!


Proposed solution

Try to create adapter for React Native. Something like:


class MaskitoReactNativeElement extends MaskitoElement {
    constructor(host) {
        this.host = host;
    }

    get value() {
        return this.host.value || '';
    }

    set value(value) {
        this.host.value = value;
    }

    get selectionStart() {
        return this.host.selection?.start;
    }

    get selectionEnd() {
        return this.host.selection?.end;
    }
}

const elementPredicate = el => new MaskitoReactNativeElement(el);

const maskedInputRef = useMaskito({options, elementPredicate});
wesleyguirra commented 1 month ago

how to use with react-native?

nsbarsukov commented 1 month ago

how to use with react-native?

This issue is not solved yet