Created
January 9, 2020 10:26
-
-
Save shirakaba/09808e52086717e853fbbc66f9ac090d to your computer and use it in GitHub Desktop.
nativescript-IQKeyboardManager: TextViewWithHint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as console from "react-nativescript/dist/shared/Logger"; | |
import * as React from "react"; | |
import { PropsWithoutForwardedRef } from "react-nativescript/dist/shared/NativeScriptComponentTypings"; | |
import { TextViewWithHint as NativeScriptTextViewWithHint } from "@nativescript/core"; | |
import { _TextView, TextViewComponentProps } from "react-nativescript/dist/components/TextView"; | |
import { RNSFriendly } from "react-nativescript/dist/components/TextBase"; | |
import { updateListener } from "react-nativescript/dist/client/EventHandling"; | |
import { Container, HostContext, Instance } from "react-nativescript/dist/shared/HostConfigTypes"; | |
import { register } from "react-nativescript/dist/client/ElementRegistry"; | |
import { ContentViewProps, TextViewWithHintProps } from "react-nativescript/dist/shared/NativeScriptComponentTypings"; | |
import { ViewComponentProps, RCTView } from "react-nativescript/dist/components/View"; | |
import { CustomNodeHierarchyManager, Instance, TextInstance } from "react-nativescript/dist/shared/HostConfigTypes"; | |
import { FormattedString } from "tns-core-modules/text/formatted-string"; | |
/* No changes needed, so I'll just import as-is from "react-nativescript/dist/components/TextBase" | |
* However, I'll leave this comment in case I'm wrong. */ | |
// export function RNSFriendly<TBase extends Constructor<NativeScriptTextViewWithHint>>(Base: TBase) { | |
// return class extends Base implements CustomNodeHierarchyManager<NativeScriptTextViewWithHint> { | |
// __ImplementsCustomNodeHierarchyManager__: true = true; | |
// constructor(...args: any[]) { | |
// super(...args); | |
// // This constructor call is needed for some reason; they must be doing something odd with the constructor. | |
// } | |
// __customHostConfigAppendChild(parent: NativeScriptTextViewWithHint, child: Instance | TextInstance): boolean { | |
// if (child instanceof FormattedString) { | |
// parent.formattedText = child; | |
// return true; | |
// } | |
// // i.e. defer to Host Config. | |
// return false; | |
// } | |
// __customHostConfigRemoveChild(parent: NativeScriptTextViewWithHint, child: Instance | TextInstance): boolean { | |
// if (child instanceof FormattedString) { | |
// parent.formattedText = null; | |
// return true; | |
// } | |
// // i.e. defer to Host Config. | |
// return false; | |
// } | |
// __customHostConfigInsertBefore( | |
// parent: NativeScriptTextViewWithHint, | |
// child: Instance | TextInstance, | |
// beforeChild: Instance | TextInstance | |
// ): boolean { | |
// if (child instanceof FormattedString) { | |
// parent.formattedText = child; | |
// return true; | |
// } | |
// // i.e. defer to Host Config. | |
// return false; | |
// } | |
// }; | |
// } | |
export const RNSFriendlyTextViewWithHint = RNSFriendly(NativeScriptTextViewWithHint); | |
const elementKey: string = "textViewWithHint"; | |
register( | |
elementKey, | |
( | |
props: Props, | |
rootContainerInstance: Container, | |
hostContext: HostContext, | |
) => { | |
return new RNSFriendlyTextViewWithHint() as Instance; | |
} | |
); | |
type Constructor<T = {}> = new (...args: any[]) => T; | |
interface Props {} | |
export type TextViewWithHintComponentProps< | |
E extends NativeScriptTextViewWithHint = NativeScriptTextViewWithHint | |
> = Props /* & typeof TextViewWithHint.defaultProps */ & Partial<TextViewWithHintProps> & ViewComponentProps<E>; | |
export class TextViewWithHint< | |
P extends TextViewWithHintComponentProps<E>, | |
S extends {}, | |
E extends NativeScriptTextViewWithHint | |
> extends _TextView<P, S, E> { | |
render() { | |
const { | |
forwardedRef, | |
onBlur, | |
onFocus, | |
onTextChange, | |
onLoaded, | |
onUnloaded, | |
onAndroidBackPressed, | |
onShowingModally, | |
onShownModally, | |
onTap, | |
onDoubleTap, | |
onPinch, | |
onPan, | |
onSwipe, | |
onRotation, | |
onLongPress, | |
onTouch, | |
onPropertyChange, | |
text, | |
formattedText, | |
children, | |
...rest | |
} = this.props; | |
if (text && formattedText) { | |
console.warn(`Both text and formattedText provided; shall use formattedText.`); | |
} | |
const textContent = { | |
[formattedText ? "formattedText" : "text"]: formattedText || text, | |
}; | |
return React.createElement( | |
elementKey, | |
{ | |
...rest, | |
...textContent, | |
ref: forwardedRef || this.myRef, | |
}, | |
children // Weird that a TextView may contain children, but what do I know. | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment