question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Demo integrating form components with ReduxForm

See original GitHub issue

It would be super helpful to have a few canonical examples of how to integrate the form components with ReduxForm.

Additionally, it may be worth considering adding some ReduxForm HOC to wrap the components:

// Wrapped component
export function ReduxFormTextInput(props) {
  const {
    input: {onChange, value},
    placeholder,
    disabled,
    ...otherProps
  } = props;
  return (
    <TextInput
      onChange={onChange}
      value={value}
      placeholder={placeholder}
      disabled={disabled}
      {...otherProps}
    />
  );
}

// Example use:
<Field
  component={ReduxFormTextInput}
  placeholder="placeholder"
  name="fieldName"
/>

If BaseUI doesn’t expose these wrappers, I think that each project that wants to integrate with ReduxForm will need to reimplement this boilerplate.

I wanted to kick off a discussion of where these examples/wrappers belong. Maybe it makes more sense to create a separate baseui-reduxform repo to prevent bleeding concerns?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
caseyhebebrandcommented, Jan 14, 2019

Here is an example of BaseUI’s stateful input wrapper that we use with redux-forms. This wrapper accepts all redux-form Field props and any props that the StatefulInput is expecting.

type Props = {
  disabled?: boolean,
  value?: string,
  placeholder?: string,
  label?: string,
  onChange?: (e: SyntheticInputEvent<HTMLInputElement>) => void,
  $style?: { [key: string]: any },
};

function ReduxFormTextInput(props: ReduxFormInputProps) {
  const {
    input: { value, onChange },
    meta: { touched, error },
    label,
    placeholder,
    disabled,
    $style,
    ...otherProps
  } = props;
  const shouldShowError = touched && error;

  return (
    <Main $style={$style}>
      <Label>{label}</Label>
      <Container>
        <StatefulInput
          disabled={disabled}
          placeholder={placeholder}
          value={value}
          onChange={onChange}
          error={shouldShowError}
          {...otherProps}
        />
      </Container>
      {shouldShowError && <Error>{error}</Error>}
    </Main>
  );
}

This is how we would utilize the above ReduxFormTextInput component in a redux-form Field:

<Field
      name={'callbackName'}
       label={'Callback name'}
       component={ReduxFormTextInput}
       required={false}
       placeholder={'Enter a name'}
        inputRef={el => (this.callbackNameRef = el)}
        $style={{
             marginLeft: layout.padding.large,
         }}
       validate=[validateCallbackName]
 />

You can see that we pass any of the BaseUI props (overrides, placeholder, etc.) directly into the Field. The wrapper layer utilizes meta for error handling, and directly utilizes redux-form value and onChange.

If you want your field to populate with data, all you need to do is:

  1. Pass initialValues prop into your form component… Each key should correlate to the name of your field. (Ex: initialValues: { callbackName: 'Casey' }).
  2. Determine which prop in BaseUI accepts the initial value, and pass value from the wrapper component to that prop.
  const {
    input: { value, onChange },
  } = props;

<ReduxFormTextInput initialState={{ value: 'Casey' }}/>
0reactions
leyanlocommented, May 8, 2019

@Warbring3r’s solution was super helpful, but we ran into issues around redux-form’s default value being an empty string: https://redux-form.com/8.2.0/docs/api/field.md/#-code-input-value-any-code-

This ended up being incompatible with the Base Select component, and we would not see the placeholder (#1252). We also removed the inline function declaration to avoid memory leaks. For those who might find this helpful, our HOC ended up looking like this:

// @flow
import * as React from 'react';
import {Select} from 'baseui/select';
import type {FieldProps} from 'redux-form';
import type {PropsT} from 'baseui/select';

type ReduxFormSelectProps = FieldProps & PropsT;
export class ReduxFormSelect extends React.PureComponent<ReduxFormSelectProps> {
  onChange = ({
    value: v,
  }: {
    value: $PropertyType<$PropertyType<FieldProps, 'input'>, 'value'>,
  }) => this.props.input.onChange(v);

  render() {
    const {
      input: {value},
      ...restProps
    } = this.props;
    return (
      <Select
        onChange={this.onChange}
        {...(value !== '' ? {value} : {})}
        {...restProps}
      />
    );
  }
}

And our usage looks like:

<Field
  component={ReduxFormSelect}
  name="equipment"
  options={[
    {id: 'van', label: 'Dry Van'},
    {id: 'reefer', label: 'Reefer'},
  ]}
/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Demo integrating form components with ReduxForm #150
Demo integrating form components with ReduxForm #150 ... This wrapper accepts all redux-form Field props and any props that the ...
Read more >
Redux Form - Getting Started
To connect your React form components to your Redux store you'll need the following pieces from the redux-form package: Redux Reducer: formReducer ,;...
Read more >
How to Integrate Redux Form in React Native Application
In this step-by-step guide for beginners, with the help of the redux form example, we will learn how to integrate the redux form...
Read more >
react-semantic-redux-form
Integration of Semantic UI React with Redux Form. Available Components. Components with Field suffix are Form.Field(A field is a form element ...
Read more >
Integrate Redux Form in your React App - Part 1
This is a simple demonstration of how to connect all the standard HTML form elements to redux-form. create src/SimpleForm.js as follow:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found