InputAccessoryView.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow
  8. * @format
  9. */
  10. 'use strict';
  11. const Platform = require('../../Utilities/Platform');
  12. const React = require('react');
  13. const StyleSheet = require('../../StyleSheet/StyleSheet');
  14. import RCTInputAccessoryViewNativeComponent from './RCTInputAccessoryViewNativeComponent';
  15. import type {ViewStyleProp} from '../../StyleSheet/StyleSheet';
  16. import type {ColorValue} from '../../StyleSheet/StyleSheetTypes';
  17. /**
  18. * Note: iOS only
  19. *
  20. * A component which enables customization of the keyboard input accessory view.
  21. * The input accessory view is displayed above the keyboard whenever a TextInput
  22. * has focus. This component can be used to create custom toolbars.
  23. *
  24. * To use this component wrap your custom toolbar with the
  25. * InputAccessoryView component, and set a nativeID. Then, pass that nativeID
  26. * as the inputAccessoryViewID of whatever TextInput you desire. A simple
  27. * example:
  28. *
  29. * ```ReactNativeWebPlayer
  30. * import React, { Component } from 'react';
  31. * import { AppRegistry, TextInput, InputAccessoryView, Button } from 'react-native';
  32. *
  33. * export default class UselessTextInput extends Component {
  34. * constructor(props) {
  35. * super(props);
  36. * this.state = {text: 'Placeholder Text'};
  37. * }
  38. *
  39. * render() {
  40. * const inputAccessoryViewID = "uniqueID";
  41. * return (
  42. * <View>
  43. * <ScrollView keyboardDismissMode="interactive">
  44. * <TextInput
  45. * style={{
  46. * padding: 10,
  47. * paddingTop: 50,
  48. * }}
  49. * inputAccessoryViewID=inputAccessoryViewID
  50. * onChangeText={text => this.setState({text})}
  51. * value={this.state.text}
  52. * />
  53. * </ScrollView>
  54. * <InputAccessoryView nativeID=inputAccessoryViewID>
  55. * <Button
  56. * onPress={() => this.setState({text: 'Placeholder Text'})}
  57. * title="Reset Text"
  58. * />
  59. * </InputAccessoryView>
  60. * </View>
  61. * );
  62. * }
  63. * }
  64. *
  65. * // skip this line if using Create React Native App
  66. * AppRegistry.registerComponent('AwesomeProject', () => UselessTextInput);
  67. * ```
  68. *
  69. * This component can also be used to create sticky text inputs (text inputs
  70. * which are anchored to the top of the keyboard). To do this, wrap a
  71. * TextInput with the InputAccessoryView component, and don't set a nativeID.
  72. * For an example, look at InputAccessoryViewExample.js in RNTester.
  73. */
  74. type Props = $ReadOnly<{|
  75. +children: React.Node,
  76. /**
  77. * An ID which is used to associate this `InputAccessoryView` to
  78. * specified TextInput(s).
  79. */
  80. nativeID?: ?string,
  81. style?: ?ViewStyleProp,
  82. backgroundColor?: ?ColorValue,
  83. |}>;
  84. class InputAccessoryView extends React.Component<Props> {
  85. render(): React.Node {
  86. if (Platform.OS !== 'ios') {
  87. console.warn('<InputAccessoryView> is only supported on iOS.');
  88. }
  89. if (React.Children.count(this.props.children) === 0) {
  90. return null;
  91. }
  92. return (
  93. <RCTInputAccessoryViewNativeComponent
  94. style={[this.props.style, styles.container]}
  95. nativeID={this.props.nativeID}
  96. backgroundColor={this.props.backgroundColor}>
  97. {this.props.children}
  98. </RCTInputAccessoryViewNativeComponent>
  99. );
  100. }
  101. }
  102. const styles = StyleSheet.create({
  103. container: {
  104. position: 'absolute',
  105. },
  106. });
  107. module.exports = InputAccessoryView;