Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tail placeholder change event #11

Merged
merged 3 commits into from
Oct 27, 2024
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
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default function App() {
value={textState.formatted}
style={styles.maskedTextInput}
onAdvancedMaskTextChange={onChangeText}
onTailPlaceholderChange={console.log}
primaryMaskFormat="[00]-[$$]-[00]"
autocomplete={false}
allowSuggestions={true}
Expand Down
13 changes: 9 additions & 4 deletions ios/AdvancedTextInputMaskDecoratorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ class AdvancedTextInputMaskDecoratorView: UIView {

@objc var onAdvancedMaskTextChange: RCTDirectEventBlock?

@objc var onAdvancedMaskTextChangedCallback: (_ extracted: String, _ formatted: String) -> Void {
{ [weak self] extracted, formatted in
@objc var onAdvancedMaskTextChangedCallback: (
_ extracted: String,
_ formatted: String,
_ tailPlaceholder: String
) -> Void {
{ [weak self] extracted, formatted, tailPlaceholder in
guard let self = self else { return }

let eventData: [String: String] = [
"extracted": extracted,
"formatted": formatted,
"tailPlaceholder": tailPlaceholder,
]

if NSDictionary(dictionary: eventData).isEqual(to: lastDispatchedEvent) {
Expand Down Expand Up @@ -207,8 +212,8 @@ class AdvancedTextInputMaskDecoratorView: UIView {
affineFormats: affinityFormat,
affinityCalculationStrategy: AffinityCalculationStrategy.forNumber(number: affinityCalculationStrategy),
customNotations: (customNotations as? [[String: Any]])?.compactMap { $0.toNotation() } ?? [],
onMaskedTextChangedCallback: { input, value, _, _ in
self.onAdvancedMaskTextChangedCallback(value, input.allText)
onMaskedTextChangedCallback: { input, value, _, tailPlaceholder in
self.onAdvancedMaskTextChangedCallback(value, input.allText, tailPlaceholder)
},
allowSuggestions: allowSuggestions
)
Expand Down
3 changes: 2 additions & 1 deletion ios/AdvancedtextInputMaskDecoratorView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ - (void)onAdvancedMaskTextChangeWithEventData:(NSDictionary *)eventData

NSString *formatted = eventData[(id) @"formatted"];
NSString *extracted = eventData[(id) @"extracted"];
NSString *tailPlaceholder = eventData[(id) @"tailPlaceholder"];

AdvancedTextInputMaskDecoratorViewEventEmitter::OnAdvancedMaskTextChange event = {
.formatted = std::string([formatted UTF8String]),
.extracted = std::string([extracted UTF8String]),
};
.tailPlaceholder = std::string([tailPlaceholder UTF8String])};

std::dynamic_pointer_cast<const AdvancedTextInputMaskDecoratorViewEventEmitter>(_eventEmitter)
->onAdvancedMaskTextChange(event);
Expand Down
9 changes: 6 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ const MaskedTextInput = memo<MaskedTextInputProps>(
autoCapitalize = 'words',
value,
onAdvancedMaskTextChange,
onTailPlaceholderChange,
...rest
},
ref
) => {
const IS_FABRIC = 'nativeFabricUIManager' in global;

const onAdvancedMaskTextChangeCallback = useCallback(
({ nativeEvent: { extracted, formatted } }) =>
onAdvancedMaskTextChange(extracted, formatted),
[onAdvancedMaskTextChange]
({ nativeEvent: { extracted, formatted, tailPlaceholder } }) => {
onAdvancedMaskTextChange?.(extracted, formatted);
onTailPlaceholderChange?.(tailPlaceholder);
},
[onAdvancedMaskTextChange, onTailPlaceholderChange]
);

return (
Expand Down
16 changes: 8 additions & 8 deletions src/specs/AdvancedTextInputMaskDecoratorViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNati
export type OnAdvancedMaskTextChange = Readonly<{
extracted: string;
formatted: string;
tailPlaceholder: string;
}>;

type CustomTransformation = Readonly<{
Expand All @@ -32,20 +33,19 @@ type Notation = {
};

export interface NativeProps extends ViewProps {
primaryMaskFormat: string;
customNotations?: ReadonlyArray<Notation>;
onAdvancedMaskTextChange?: DirectEventHandler<OnAdvancedMaskTextChange>;
affinityCalculationStrategy?: Int32;
affinityFormat?: ReadonlyArray<string>;
allowSuggestions?: boolean;
autocomplete?: boolean;
autocompleteOnFocus?: boolean;
autoSkip?: boolean;
isRTL?: boolean;
affinityCalculationStrategy?: Int32;
customNotations?: ReadonlyArray<Notation>;
customTransformation?: CustomTransformation;
defaultValue?: string;
isRTL?: boolean;
onAdvancedMaskTextChange?: DirectEventHandler<OnAdvancedMaskTextChange>;
primaryMaskFormat: string;
value?: string;
// IOS only props
allowSuggestions?: boolean;
autocompleteOnFocus?: boolean;
}

export default codegenNativeComponent<NativeProps>(
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ export type Notation = {
export type MaskedTextInputDecoratorViewNativeProps = ViewProps & {
primaryMaskFormat: string;
customNotations?: Notation[];
onAdvancedMaskTextChange: (
onAdvancedMaskTextChange?: (
extractedValue: string,
formattedValue: string
) => void;
onTailPlaceholderChange?: (tailPlaceholder: string) => void;
affinityFormat?: string[];
autocomplete?: boolean;
autoSkip?: boolean;
Expand Down
Loading