项目作者: njgaoqing

项目描述 :
修改react-native-gifted-form中的navigation为react-navigation
高级语言: JavaScript
项目地址: git://github.com/njgaoqing/react-native-gifted-form-update.git
创建时间: 2018-03-29T04:26:30Z
项目社区:https://github.com/njgaoqing/react-native-gifted-form-update

开源协议:MIT License

下载


Gifted Form

在原有react-native-gifted-form基础上新增组件 并修改navigation方式

npm downloads
npm version
Latest GitHub tag

Form component for React Native.

screenshot

Example

  1. var { GiftedForm, GiftedFormManager } = require('react-native-gifted-form');
  2. var FormComponent = createReactClass({
  3. render() {
  4. return (
  5. <GiftedForm
  6. formName='signupForm' // GiftedForm instances that use the same name will also share the same states
  7. openModal={(route) => {
  8. navigator.push(route); // The ModalWidget will be opened using this method. Tested with ExNavigator
  9. }}
  10. clearOnClose={false} // delete the values of the form when unmounted
  11. defaults={{
  12. /*
  13. username: 'Farid',
  14. 'gender{M}': true,
  15. password: 'abcdefg',
  16. country: 'FR',
  17. birthday: new Date(((new Date()).getFullYear() - 18)+''),
  18. */
  19. }}
  20. validators={{
  21. fullName: {
  22. title: 'Full name',
  23. validate: [{
  24. validator: 'isLength',
  25. arguments: [1, 23],
  26. message: '{TITLE} must be between {ARGS[0]} and {ARGS[1]} characters'
  27. }]
  28. },
  29. username: {
  30. title: 'Username',
  31. validate: [{
  32. validator: 'isLength',
  33. arguments: [3, 16],
  34. message: '{TITLE} must be between {ARGS[0]} and {ARGS[1]} characters'
  35. },{
  36. validator: 'matches',
  37. arguments: /^[a-zA-Z0-9]*$/,
  38. message: '{TITLE} can contains only alphanumeric characters'
  39. }]
  40. },
  41. password: {
  42. title: 'Password',
  43. validate: [{
  44. validator: 'isLength',
  45. arguments: [6, 16],
  46. message: '{TITLE} must be between {ARGS[0]} and {ARGS[1]} characters'
  47. }]
  48. },
  49. emailAddress: {
  50. title: 'Email address',
  51. validate: [{
  52. validator: 'isLength',
  53. arguments: [6, 255],
  54. },{
  55. validator: 'isEmail',
  56. }]
  57. },
  58. bio: {
  59. title: 'Biography',
  60. validate: [{
  61. validator: 'isLength',
  62. arguments: [0, 512],
  63. message: '{TITLE} must be between {ARGS[0]} and {ARGS[1]} characters'
  64. }]
  65. },
  66. gender: {
  67. title: 'Gender',
  68. validate: [{
  69. validator: (...args) => {
  70. if (args[0] === undefined) {
  71. return false;
  72. }
  73. return true;
  74. },
  75. message: '{TITLE} is required',
  76. }]
  77. },
  78. birthday: {
  79. title: 'Birthday',
  80. validate: [{
  81. validator: 'isBefore',
  82. arguments: [moment().utc().subtract(18, 'years').format('YYYY-MM-DD')],
  83. message: 'You must be at least 18 years old'
  84. }, {
  85. validator: 'isAfter',
  86. arguments: [moment().utc().subtract(100, 'years').format('YYYY-MM-DD')],
  87. message: '{TITLE} is not valid'
  88. }]
  89. },
  90. country: {
  91. title: 'Country',
  92. validate: [{
  93. validator: 'isLength',
  94. arguments: [2],
  95. message: '{TITLE} is required'
  96. }]
  97. },
  98. }}
  99. >
  100. <GiftedForm.SeparatorWidget ></GiftedForm.SeparatorWidget>
  101. <GiftedForm.TextInputWidget
  102. name='fullName' // mandatory
  103. title='Full name'
  104. image={require('../../assets/icons/color/user.png')}
  105. placeholder='Marco Polo'
  106. clearButtonMode='while-editing'
  107. ></GiftedForm.TextInputWidget>
  108. <GiftedForm.TextInputWidget
  109. name='username'
  110. title='Username'
  111. image={require('../../assets/icons/color/contact_card.png')}
  112. placeholder='MarcoPolo'
  113. clearButtonMode='while-editing'
  114. onTextInputFocus={(currentText = '') => {
  115. if (!currentText) {
  116. let fullName = GiftedFormManager.getValue('signupForm', 'fullName');
  117. if (fullName) {
  118. return fullName.replace(/[^a-zA-Z0-9-_]/g, '');
  119. }
  120. }
  121. return currentText;
  122. }}
  123. />
  124. <GiftedForm.TextInputWidget
  125. name='password' // mandatory
  126. title='Password'
  127. placeholder='******'
  128. clearButtonMode='while-editing'
  129. secureTextEntry={true}
  130. image={require('../../assets/icons/color/lock.png')}
  131. ></GiftedForm.TextInputWidget>
  132. <GiftedForm.TextInputWidget
  133. name='emailAddress' // mandatory
  134. title='Email address'
  135. placeholder='example@nomads.ly'
  136. keyboardType='email-address'
  137. clearButtonMode='while-editing'
  138. image={require('../../assets/icons/color/email.png')}
  139. ></GiftedForm.TextInputWidget>
  140. <GiftedForm.SeparatorWidget ></GiftedForm.SeparatorWidget>
  141. <GiftedForm.ModalWidget
  142. title='Gender'
  143. displayValue='gender'
  144. image={require('../../assets/icons/color/gender.png')}
  145. >
  146. <GiftedForm.SeparatorWidget ></GiftedForm.SeparatorWidget>
  147. <GiftedForm.SelectWidget name='gender' title='Gender' multiple={false}>
  148. <GiftedForm.OptionWidget image={require('../../assets/icons/color/female.png')} title='Female' value='F'></GiftedForm.OptionWidget>
  149. <GiftedForm.OptionWidget image={require('../../assets/icons/color/male.png')} title='Male' value='M'></GiftedForm.OptionWidget>
  150. </GiftedForm.SelectWidget>
  151. </GiftedForm.ModalWidget>
  152. <GiftedForm.ModalWidget
  153. title='Birthday'
  154. displayValue='birthday'
  155. image={require('../../assets/icons/color/birthday.png')}
  156. scrollEnabled={false}
  157. >
  158. <GiftedForm.SeparatorWidget></GiftedForm.SeparatorWidget>
  159. <GiftedForm.DatePickerIOSWidget
  160. name='birthday'
  161. mode='date'
  162. getDefaultDate={() => {
  163. return new Date(((new Date()).getFullYear() - 18)+'');
  164. }}
  165. />
  166. </GiftedForm.ModalWidget>
  167. <GiftedForm.ModalWidget
  168. title='Country'
  169. displayValue='country'
  170. image={require('../../assets/icons/color/passport.png')}
  171. scrollEnabled={false}
  172. >
  173. <GiftedForm.SelectCountryWidget
  174. code='alpha2'
  175. name='country'
  176. title='Country'
  177. autoFocus={true}
  178. ></GiftedForm.SelectCountryWidget>
  179. </GiftedForm.ModalWidget>
  180. <GiftedForm.ModalWidget
  181. title='Biography'
  182. displayValue='bio'
  183. image={require('../../assets/icons/color/book.png')}
  184. scrollEnabled={true} // true by default
  185. >
  186. <GiftedForm.SeparatorWidget></GiftedForm.SeparatorWidget>
  187. <GiftedForm.TextAreaWidget
  188. name='bio'
  189. autoFocus={true}
  190. placeholder='Something interesting about yourself'
  191. ></GiftedForm.TextAreaWidget>
  192. </GiftedForm.ModalWidget>
  193. <GiftedForm.ErrorsWidget></GiftedForm.ErrorsWidget>
  194. <GiftedForm.SubmitWidget
  195. title='Sign up'
  196. widgetStyles={{
  197. submitButton: {
  198. backgroundColor: themes.mainColor,
  199. }
  200. }}
  201. onSubmit={(isValid, values, validationResults, postSubmit = null, modalNavigator = null) => {
  202. if (isValid === true) {
  203. // prepare object
  204. values.gender = values.gender[0];
  205. values.birthday = moment(values.birthday).format('YYYY-MM-DD');
  206. /* Implement the request to your server using values variable
  207. ** then you can do:
  208. ** postSubmit(); // disable the loader
  209. ** postSubmit(['An error occurred, please try again']); // disable the loader and display an error message
  210. ** postSubmit(['Username already taken', 'Email already taken']); // disable the loader and display an error message
  211. ** GiftedFormManager.reset('signupForm'); // clear the states of the form manually. 'signupForm' is the formName used
  212. */
  213. }
  214. }}
  215. />
  216. <GiftedForm.NoticeWidget
  217. title='By signing up, you agree to the Terms of Service and Privacy Policity.'
  218. ></GiftedForm.NoticeWidget>
  219. <GiftedForm.HiddenWidget name='tos' value={true} ></GiftedForm.HiddenWidget>
  220. </GiftedForm>
  221. );
  222. }
  223. });

Storing form’s state elsewhere (could be used with Redux) - Beta feature

Pass value prop to your widgets and onValueChange to your GiftedForm to store your state outside of GiftedFormManager’s store.

IMPORTANT: currently only TextInputWidget and HiddenWidget support this feature. PR’s are welcome for the other widgets ;)

  1. import React, { AppRegistry, Component } from 'react-native'
  2. import { GiftedForm, GiftedFormManager } from 'react-native-gifted-form'
  3. class Form extends Component {
  4. constructor(props, context) {
  5. super(props, context)
  6. this.state = {
  7. form: {
  8. fullName: 'Marco Polo',
  9. tos: false,
  10. }
  11. }
  12. }
  13. handleValueChange(values) {
  14. console.log('handleValueChange', values)
  15. this.setState({ form: values })
  16. }
  17. render() {
  18. const { fullName, tos, gender } = this.state.form
  19. console.log('render', this.state.form)
  20. return (
  21. <GiftedForm
  22. formName='signupForm'
  23. openModal={(route) => { this.props.navigator.push(route) }}
  24. onValueChange={this.handleValueChange.bind(this)}
  25. >
  26. <GiftedForm.TextInputWidget
  27. name='fullName'
  28. title='Full name'
  29. placeholder='Marco Polo'
  30. clearButtonMode='while-editing'
  31. value={fullName}
  32. ></GiftedForm.TextInputWidget>
  33. <GiftedForm.HiddenWidget name='tos' value={tos} ></GiftedForm.HiddenWidget>
  34. </GiftedForm>
  35. )
  36. }
  37. }
  38. AppRegistry.registerComponent('Form', () => Form)

Installation

  1. npm install react-native-gifted-form --save
  2. # OR
  3. yarn add react-native-gifted-form

Available widgets

  • TextInputWidget - A text input
  • TextAreaWidget - A text area
  • GooglePlacesWidget - A Google Places picker based on react-native-google-places-autocomplete
  • ModalWidget - A route opener for nested forms
  • GroupWidget - A widgets container with a title
  • HiddenWidget - A non-displayed widget. The value will be passed to SubmitWidget
  • LoadingWidget - A loader
  • RowWidget - A touchable row with title/image
  • RowValueWidget - A touchable row with title/image and a value
  • SelectCountryWidget - A country picker. Flags made by www.IconDrawer.com
  • SelectWidget - A select menu
  • SeparatorWidget - A 10px widgets separator
  • SubmitWidget - A submit button that trigger form validators and error displaying
  • SwitchWidget - A switch
  • DatePickerIOSWidget - Date picker for iOS
  • NoticeWidget - A notice information - PR wanted for onPress handler

See the widget sources for full props details.

Custom widgets

Widgets must implement the mixin GiftedForm.WidgetMixin. See TextAreaWidget for a good example.

Contributing

Pull requests are welcome! The author is very busy at the moment but there are also some contributors who are also helping out.

License

MIT

Feel free to ask me questions on Twitter @FaridSafi!