简体   繁体   English

React Native AlertIOS占位符

[英]React Native AlertIOS placeholder

I am using AlertIOS with input: 我在输入中使用AlertIOS 在此处输入图片说明

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'
);

I wish to add a placeholder for the TextInput inside that alert. 我希望在该警报内为TextInput添加一个占位符。 Is this possible? 这可能吗? I know there is a default value option, however I wish to have a placeholder instead of it. 我知道有一个默认值选项,但是我希望有一个占位符代替它。

Technically it is possible in iOS, is it possible in react-native? 从技术上讲,这在iOS中是可行的,在react-native中是否可行? The short answer is currently no. 简短的答案是目前没有。

The reason it is not possible is that the placeholder property of the TextField hasn't been exposed to the react-native side. 之所以不可能,是因为TextField的占位符属性尚未公开到本地反应端。 If we look st the code in RCTAlertManager.m we can see that defaults for the placeholder are set for the Password and Login options, but there is no way to set them of the plain-text option 如果我们看一下RCTAlertManager.m的代码,我们可以看到为PasswordLogin选项设置了placeholder默认值,但是没有办法为plain-text选项设置它们。

https://github.com/facebook/react-native/blob/master/React/Modules/RCTAlertManager.m 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;
  }

The only way to make this work is to find a dependency that has the ability to set the placeholder for the TextInput in the Alert . 进行这项工作的唯一方法是找到一个可以在Alert设置TextInput placeholder的依赖项。 Or you could make your own native module based on the code in RCTAlertManager.m that allows you to set the placeholder 或者,您可以基于RCTAlertManager.m中的代码制作自己的本机模块,该代码可让您设置placeholder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM