-
Notifications
You must be signed in to change notification settings - Fork 147
Fix macOS Alert API issues #355
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
Changes from all commits
685f103
8ccd26c
45d2ee2
1e2c329
408dae3
bdaa8ad
5a67ae0
e5a6df2
4bca23c
d067629
6ed01c8
ae5cc57
caf0975
e3fbee9
a75050c
3a77e37
d10ff74
dd11d73
ba2730b
5da14a9
b1a873b
bca9ae9
7ff5624
1056600
d3422e3
92c66ac
b295454
1bdf8c7
b880837
3c7c7c3
ea43b8d
88bb37e
f165a29
1681c21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React = require('react'); | ||
var ReactNative = require('react-native'); | ||
var {StyleSheet, View, Text, TouchableHighlight, AlertMacOS} = ReactNative; | ||
|
||
var {SimpleAlertExampleBlock} = require('./AlertExample'); | ||
|
||
exports.framework = 'React'; | ||
exports.title = 'AlertMacOS'; | ||
exports.description = 'macOS alerts'; | ||
exports.examples = [ | ||
{ | ||
title: 'Alerts', | ||
render() { | ||
return <SimpleAlertExampleBlock />; | ||
}, | ||
}, | ||
{ | ||
title: 'Prompt Options', | ||
render(): React.Element<any> { | ||
return <PromptOptions />; | ||
}, | ||
}, | ||
{ | ||
title: 'Prompt Types', | ||
render() { | ||
return ( | ||
<View> | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => AlertMacOS.prompt('Plain Text Entry')}> | ||
<View style={styles.button}> | ||
<Text>plain-text</Text> | ||
</View> | ||
</TouchableHighlight> | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt('Secure Text', null, null, 'secure-text') | ||
}> | ||
<View style={styles.button}> | ||
<Text>secure-text</Text> | ||
</View> | ||
</TouchableHighlight> | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt( | ||
'Login & Password', | ||
null, | ||
null, | ||
'login-password', | ||
[ | ||
{default: '', placeholder: 'login'}, | ||
{default: '', placeholder: 'Password'}, | ||
], | ||
) | ||
}> | ||
<View style={styles.button}> | ||
<Text>login-password</Text> | ||
</View> | ||
</TouchableHighlight> | ||
</View> | ||
); | ||
}, | ||
}, | ||
{ | ||
title: 'Prompt Presentation', | ||
render() { | ||
return ( | ||
<View> | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt( | ||
'Default sheet', | ||
null, | ||
null, | ||
'default', | ||
[{default: '', placeholder: ''}], | ||
false, | ||
) | ||
}> | ||
<View style={styles.button}> | ||
<Text>Default sheet</Text> | ||
</View> | ||
</TouchableHighlight> | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt( | ||
'Modal', | ||
null, | ||
null, | ||
'default', | ||
[{default: '', placeholder: ''}], | ||
true, | ||
) | ||
}> | ||
<View style={styles.button}> | ||
<Text>Modal</Text> | ||
</View> | ||
</TouchableHighlight> | ||
</View> | ||
); | ||
}, | ||
}, | ||
{ | ||
title: 'Prompt Style', | ||
render() { | ||
return ( | ||
<View> | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt('Default warning style', null, null, 'default') | ||
}> | ||
<View style={styles.button}> | ||
<Text>Default warning style</Text> | ||
</View> | ||
</TouchableHighlight> | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt( | ||
'Critical', | ||
null, | ||
null, | ||
'default', | ||
[{default: '', placeholder: ''}], | ||
false, | ||
true, | ||
) | ||
}> | ||
<View style={styles.button}> | ||
<Text>Critical</Text> | ||
</View> | ||
</TouchableHighlight> | ||
</View> | ||
); | ||
}, | ||
}, | ||
]; | ||
|
||
class PromptOptions extends React.Component<$FlowFixMeProps, any> { | ||
state: any; | ||
customButtons: Array<Object>; | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
// $FlowFixMe this seems to be a Flow bug, `saveResponse` is defined below | ||
this.saveResponse = this.saveResponse.bind(this); | ||
|
||
this.customButtons = [ | ||
{ | ||
text: 'Custom OK', | ||
onPress: this.saveResponse, | ||
}, | ||
{ | ||
text: 'Custom Cancel', | ||
style: 'cancel', | ||
}, | ||
]; | ||
|
||
this.state = { | ||
promptValue: undefined, | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<View> | ||
<Text style={{marginBottom: 10}}> | ||
<Text style={{fontWeight: 'bold'}}>Prompt value:</Text>{' '} | ||
{this.state.promptValue} | ||
</Text> | ||
|
||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt('Type a value', null, this.saveResponse) | ||
}> | ||
<View style={styles.button}> | ||
<Text>prompt with title & callback</Text> | ||
</View> | ||
</TouchableHighlight> | ||
|
||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt('Type a value', null, this.customButtons) | ||
}> | ||
<View style={styles.button}> | ||
<Text>prompt with title & custom buttons</Text> | ||
</View> | ||
</TouchableHighlight> | ||
|
||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt( | ||
'Type a value', | ||
null, | ||
this.saveResponse, | ||
undefined, | ||
[{default: 'Default value', placeholder: ''}], | ||
) | ||
}> | ||
<View style={styles.button}> | ||
<Text>prompt with title, callback & default inputs</Text> | ||
</View> | ||
</TouchableHighlight> | ||
|
||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={() => | ||
AlertMacOS.prompt( | ||
'Type a value', | ||
null, | ||
this.customButtons, | ||
'login-password', | ||
[ | ||
{default: '[email protected]', placeholder: 'login'}, | ||
{default: '', placeholder: 'password'}, | ||
], | ||
) | ||
}> | ||
<View style={styles.button}> | ||
<Text> | ||
prompt with title, custom buttons, login/password & default inputs | ||
</Text> | ||
</View> | ||
</TouchableHighlight> | ||
</View> | ||
); | ||
} | ||
|
||
saveResponse(promptValue) { | ||
this.setState({promptValue: JSON.stringify(promptValue)}); | ||
} | ||
} | ||
|
||
var styles = StyleSheet.create({ | ||
wrapper: { | ||
borderRadius: 5, | ||
marginBottom: 5, | ||
}, | ||
button: { | ||
backgroundColor: '#eeeeee', | ||
padding: 10, | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,21 +263,21 @@ - (void)invalidate | |
} | ||
|
||
void (^callbacksHandlers)(NSModalResponse response) = ^void(NSModalResponse response) { | ||
|
||
NSString *buttonKey = @"0"; | ||
if (response >= NSAlertFirstButtonReturn) { | ||
NSString *buttonKey = buttons[response - NSAlertFirstButtonReturn].allKeys.firstObject; | ||
NSArray<NSTextField*> *textfields = [accessoryView isKindOfClass:NSTextField.class] ? @[accessoryView] : accessoryView.subviews; | ||
if (textfields.count == 2) { | ||
NSDictionary<NSString *, NSString *> *loginCredentials = @{ | ||
@"login": textfields.firstObject.stringValue, | ||
@"password": textfields.lastObject.stringValue | ||
}; | ||
callback(@[buttonKey, loginCredentials]); | ||
} else if (textfields.count == 1) { | ||
callback(@[buttonKey, textfields.firstObject.stringValue]); | ||
} else { | ||
callback(@[buttonKey]); | ||
} | ||
buttonKey = buttons[response - NSAlertFirstButtonReturn].allKeys.firstObject; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this copied from somewhere? We should probably get rid of the dot notation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same below in this file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When it was ported from the ios above years ago it assumed the same dot notation. |
||
} | ||
NSArray<NSTextField*> *textfields = [accessoryView isKindOfClass:NSTextField.class] ? @[accessoryView] : accessoryView.subviews; | ||
if (textfields.count == 2) { | ||
NSDictionary<NSString *, NSString *> *loginCredentials = @{ | ||
@"login": textfields.firstObject.stringValue, | ||
@"password": textfields.lastObject.stringValue | ||
}; | ||
callback(@[buttonKey, loginCredentials]); | ||
} else if (textfields.count == 1) { | ||
callback(@[buttonKey, textfields.firstObject.stringValue]); | ||
} else { | ||
callback(@[buttonKey]); | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha. That's what it was before it was removed in the .61 merge. This will go away again after issue #354.