项目作者: agrcrobles

项目描述 :
Made my own UI for react native and web with redux-form following some best practices
高级语言: JavaScript
项目地址: git://github.com/agrcrobles/redux-form-styled-for-react-native.git
创建时间: 2017-12-12T21:58:52Z
项目社区:https://github.com/agrcrobles/redux-form-styled-for-react-native

开源协议:MIT License

下载


redux-form-styled-for-react-native

A cross platform way to define custom styles to react-native components with redux-form, working on expo and react-native-web.

Simple sample on expo

Scan this QR code with your Expo mobile app to load the project immediately.

https://expo.io/@agrcrobles/redux-form-styled-for-react-native

QR

  1. yarn
  2. yarn start // and then select platform / device

Simple example on the web

http://redux-form-styled-react-native.herokuapp.com/

  1. yarn
  2. yarn web // open localhost:3000

What is on it?

  • Used redux-form transparently to instance layout components and set my own theme through the context efficiently.

  • Used getRenderedComponent() to return reference to the UI component that has been rendered. This is useful for calling instance methods on the UI components. For example, if you wanted to focus any given field

  • Usee errorMessage string to display a message when it is due.

  • Usee a generic ruleRunner to eval my own validation rules.

Usage

  1. import * as React from "react";
  2. import { UIField } from "./UIField";
  3. import { rules, runner } from "../rules";
  4. import { ThemeProvider, withTheme } from "../Theme";
  5. import { Field, reduxForm } from "redux-form";
  6. import { TextInput } from "react-native";
  7. import { reduxForm } from "redux-form";
  8. const SimpleTheme = {
  9. TextInput: {
  10. borderColor: "red"
  11. },
  12. ErrorText: {
  13. color: "red"
  14. }
  15. };
  16. class MyApp extends React.PureComponent<{}> {
  17. render() {
  18. return (
  19. <ThemeProvider theme={SimpleTheme}>
  20. <MyFieldWrapped ></MyFieldWrapped>
  21. </ThemeProvider>
  22. );
  23. }
  24. }
  25. const MyTextField = UIField(SimpleInputTextField);
  26. class MyFieldWrapped extends Component<{}> {
  27. render() {
  28. // redux-form field
  29. return (
  30. <Field
  31. name="name"
  32. component={MyTextField}
  33. hintText="Name"
  34. floatingLabelText="Name"
  35. placeholder="Name"
  36. autoCapitalize="none"
  37. autoCorrect={false}
  38. ></Field>
  39. );
  40. }
  41. }
  42. const SimpleInputTextField = props => {
  43. const { theme, errorText, onChange, onBlur, onFocus, value } = props;
  44. // Injected theme and input props
  45. return (
  46. <View>
  47. <TextInput
  48. style={[theme.TextInput]}
  49. underlineColorAndroid="transparent"
  50. onChangeText={onChange}
  51. onBlur={onBlur}
  52. onFocus={onFocus}
  53. value={value}
  54. ></TextInput>
  55. {!!errorText && <Text theme={theme.ErrorText}>{errorText}</Text>}
  56. </View>
  57. );
  58. };
  59. // Use reduxForm transparently
  60. export default reduxForm({
  61. form: "example",
  62. initialValues: {
  63. name: "Jane Doe"
  64. },
  65. validate: values =>
  66. runner.run(values, [runner.ruleRunner("name", "Name", rules.required)])
  67. })(MyForm);

Theme

It uses react-broadcast to emit a context change notification.

Themes uses React’s context feature to provide available to children
regardless of how deep they are in the tree of Component.

  • <ThemeProvider Theme>

makes Theme available from context.Theme to children of the Provider via
props using withTheme() HOC as in react-router.

While this is powerful it also can be abused and make it hard to manage.

Rules

Using ruleRunner useful pattern taken from:

Available Rules

  • Required

Normalization

  • toPhone

Contribute

PR, stars ✭ and issue reporting, welcome!

License

MIT