Skip to content

Commit

Permalink
Make setFormFieldValue return a promise on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Rad Azzouz committed Oct 10, 2019
1 parent 876fc2d commit a43aaed
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 25 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ class PSPDFKitView extends React.Component {

return promise;
} else if (Platform.OS === "ios") {
NativeModules.PSPDFKitViewManager.setFormFieldValue(
return NativeModules.PSPDFKitViewManager.setFormFieldValue(
value,
fullyQualifiedName,
findNodeHandle(this.refs.pdfView)
Expand Down
2 changes: 1 addition & 1 deletion ios/RCTPSPDFKit/RCTPSPDFKitView.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ NS_ASSUME_NONNULL_BEGIN

/// Forms
- (NSDictionary<NSString *, NSString *> *)getFormFieldValue:(NSString *)fullyQualifiedName;
- (void)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName;
- (BOOL)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName;

// Toolbar buttons customizations
- (void)setLeftBarButtonItems:(nullable NSArray <NSString *> *)items forViewMode:(nullable NSString *) viewMode animated:(BOOL)animated;
Expand Down
16 changes: 12 additions & 4 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.m
Original file line number Diff line number Diff line change
Expand Up @@ -310,35 +310,43 @@ - (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
return @{@"error": @"Failed to get the form field value."};
}

- (void)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName {
- (BOOL)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName {
if (fullyQualifiedName.length == 0) {
NSLog(@"Invalid fully qualified name.");
return;
return NO;
}

PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document)

VALIDATE_DOCUMENT(document, NO)

BOOL success = NO;
for (PSPDFFormElement *formElement in document.formParser.forms) {
if ([formElement.fullyQualifiedFieldName isEqualToString:fullyQualifiedName]) {
if ([formElement isKindOfClass:PSPDFButtonFormElement.class]) {
if ([value isEqualToString:@"selected"]) {
[(PSPDFButtonFormElement *)formElement select];
success = YES;
} else if ([value isEqualToString:@"deselected"]) {
[(PSPDFButtonFormElement *)formElement deselect];
success = YES;
}
} else if ([formElement isKindOfClass:PSPDFChoiceFormElement.class]) {
((PSPDFChoiceFormElement *)formElement).selectedIndices = [NSIndexSet indexSetWithIndex:value.integerValue];
success = YES;
} else if ([formElement isKindOfClass:PSPDFTextFieldFormElement.class]) {
formElement.contents = value;
success = YES;
} else if ([formElement isKindOfClass:PSPDFSignatureFormElement.class]) {
NSLog(@"Signature form elements are not supported.");
success = NO;
} else {
NSLog(@"Unsupported form element.");
success = NO;
}
break;
}
}
return success;
}

#pragma mark - Notifications
Expand Down
9 changes: 7 additions & 2 deletions ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,15 @@ @implementation RCTPSPDFKitViewManager
});
}

RCT_EXPORT_METHOD(setFormFieldValue:(nullable NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName reactTag:(nonnull NSNumber *)reactTag) {
RCT_EXPORT_METHOD(setFormFieldValue:(nullable NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
[component setFormFieldValue:value fullyQualifiedName:fullyQualifiedName];
BOOL success = [component setFormFieldValue:value fullyQualifiedName:fullyQualifiedName];
if (success) {
resolve(@(success));
} else {
reject(@"error", @"Failed to set form field value.", nil);
}
});
}

Expand Down
149 changes: 132 additions & 17 deletions samples/Catalog/Catalog.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,18 @@ class ManualSave extends Component {
<Button
onPress={() => {
// Manual Save
this.refs.pdfView.saveCurrentDocument();
this.refs.pdfView
.saveCurrentDocument()
.then(result => {
if (result) {
alert("Successfully saved current document.");
} else {
alert("Failed to save current document.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
}}
title="Save"
/>
Expand Down Expand Up @@ -835,24 +846,128 @@ class ProgrammaticFormFilling extends Component {
<Button
onPress={() => {
// Fill Text Form Fields.
this.refs.pdfView.setFormFieldValue("Name_Last", "Appleseed");
this.refs.pdfView.setFormFieldValue("Name_First", "John");
this.refs.pdfView.setFormFieldValue(
"Address_1",
"1 Infinite Loop"
);
this.refs.pdfView.setFormFieldValue("City", "Cupertino");
this.refs.pdfView.setFormFieldValue("STATE", "CA");
this.refs.pdfView.setFormFieldValue("SSN", "123456789");
this.refs.pdfView.setFormFieldValue(
"Telephone_Home",
"(123) 456-7890"
);
this.refs.pdfView.setFormFieldValue("Birthdate", "1/1/1983");
this.refs.pdfView
.setFormFieldValue("Name_Last", "Appleseed")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
this.refs.pdfView
.setFormFieldValue("Name_First", "John")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
this.refs.pdfView
.setFormFieldValue("Address_1", "1 Infinite Loop")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
this.refs.pdfView
.setFormFieldValue("City", "Cupertino")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
this.refs.pdfView
.setFormFieldValue("STATE", "CA")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
this.refs.pdfView
.setFormFieldValue("SSN", "123456789")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
this.refs.pdfView
.setFormFieldValue("Telephone_Home", "(123) 456-7890")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
this.refs.pdfView
.setFormFieldValue("Birthdate", "1/1/1983")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});

// Select a button form elements.
this.refs.pdfView.setFormFieldValue("Sex.0", "selected");
this.refs.pdfView.setFormFieldValue("PHD", "selected");
this.refs.pdfView
.setFormFieldValue("Sex.0", "selected")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
this.refs.pdfView
.setFormFieldValue("PHD", "selected")
.then(result => {
if (result) {
console.log("Successfully set the form field value.");
} else {
alert("Failed to set form field value.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
}}
title="Fill Forms"
/>
Expand Down

0 comments on commit a43aaed

Please sign in to comment.