React Native AlertIOS占位符

时间:2019-01-28 02:25:47

标签: javascript react-native

我在输入中使用AlertIOSenter image description here

AlertIOS.prompt(
  'Reset password',
  'Please enter your email in order to reset your password.',
  [
    {
      text: 'Cancel',
      onPress: () => console.log('Cancel Pressed'),
      style: 'cancel',
    },
    {
      text: 'Continue',
      onPress: (password) => console.log('OK Pressed, password: ' + password),
    },
  ],
  'plain-text',
  '',
  'email-address'
);

我希望在该警报内为TextInput添加一个占位符。这可能吗?我知道有一个默认值选项,但是我希望有一个占位符代替它。

1 个答案:

答案 0 :(得分:0)

从技术上讲,这在iOS中是可行的,在react-native中是否可行?简短的答案是目前没有。

之所以不可能,是因为TextField的占位符属性尚未公开到反应本地。如果我们看一下RCTAlertManager.m中的代码,我们可以看到placeholderPassword选项设置了Login的默认值,但是无法设置它们的默认值plain-text选项

https://github.com/facebook/react-native/blob/master/React/Modules/RCTAlertManager.m

switch (type) {
    case RCTAlertViewStylePlainTextInput: {
      [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.secureTextEntry = NO;
        textField.text = defaultValue;
        textField.keyboardType = keyboardType;
      }];
      break;
    }
    case RCTAlertViewStyleSecureTextInput: {
      [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = RCTUIKitLocalizedString(@"Password");
        textField.secureTextEntry = YES;
        textField.text = defaultValue;
        textField.keyboardType = keyboardType;
      }];
      break;
    }
    case RCTAlertViewStyleLoginAndPasswordInput: {
      [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = RCTUIKitLocalizedString(@"Login");
        textField.text = defaultValue;
        textField.keyboardType = keyboardType;
      }];
      [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = RCTUIKitLocalizedString(@"Password");
        textField.secureTextEntry = YES;
      }];
      break;
    }
    case RCTAlertViewStyleDefault:
      break;
  }

完成这项工作的唯一方法是找到一个可以为placeholder中的TextInput设置Alert的依赖项。或者,您可以根据RCTAlertManager.m中的代码制作自己的本机模块,该代码可让您设置placeholder