Home

Awesome

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

yarn
yarn start // and then select platform / device

Simple example on the web

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

yarn
yarn web // open localhost:3000

What is on it?

Usage

import * as React from "react";
import { UIField } from "./UIField";
import { rules, runner } from "../rules";
import { ThemeProvider, withTheme } from "../Theme";

import { Field, reduxForm } from "redux-form";
import { TextInput } from "react-native";
import { reduxForm } from "redux-form";

const SimpleTheme = {
  TextInput: {
    borderColor: "red"
  },
  ErrorText: {
    color: "red"
  }
};
class MyApp extends React.PureComponent<{}> {
  render() {
    return (
      <ThemeProvider theme={SimpleTheme}>
        <MyFieldWrapped />
      </ThemeProvider>
    );
  }
}

const MyTextField = UIField(SimpleInputTextField);
class MyFieldWrapped extends Component<{}> {
  render() {
    // redux-form field
    return (
      <Field
        name="name"
        component={MyTextField}
        hintText="Name"
        floatingLabelText="Name"
        placeholder="Name"
        autoCapitalize="none"
        autoCorrect={false}
      />
    );
  }
}

const SimpleInputTextField = props => {
  const { theme, errorText, onChange, onBlur, onFocus, value } = props;

  // Injected theme and input props
  return (
    <View>
      <TextInput
        style={[theme.TextInput]}
        underlineColorAndroid="transparent"
        onChangeText={onChange}
        onBlur={onBlur}
        onFocus={onFocus}
        value={value}
      />
      {!!errorText && <Text theme={theme.ErrorText}>{errorText}</Text>}
    </View>
  );
};
// Use reduxForm transparently
export default reduxForm({
  form: "example",
  initialValues: {
    name: "Jane Doe"
  },
  validate: values =>
    runner.run(values, [runner.ruleRunner("name", "Name", rules.required)])
})(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.

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

Normalization

Related cool projects

Contribute

PR, stars ✭ and issue reporting, welcome!

License

MIT