5

This is actually less a question than it is me sharing my technique for trimming field values using this library. I'd read every single posting I could find on the matter on Stack Exchange and elsewhere, and could not find a complete working solution. It took me too much time and some trial and error to figure out.

With redux-form you must use the change action creator to reset the field value to the trimmed value. The hard part was finding the right place in the code under the right conditions to do the trim and fire the action. I tried the normalize method included with redux-form. No. I tried trimming in custom onChange and onBlur handlers on the fields. No. Most attempted solutions resulted in an inability to type a space in the field. I thought I could get it done in a custom onBlur handler, but if I dispatched a redux-form change action from within that handler then the change action updated the state first, then the pending blur action followed and would overwrite the trimmed value with the original value in the state.

Anyway, here's my solution.

1) Import the redux-form change action and add custom method to your props to dispatch it.

import { connect } from 'react-redux';
import { change } from 'redux-form';

const form = 'record-form';

function mapDispatchToProps(dispatch) {
    return {
        trimFieldValue: (field, value) => {
            dispatch(change(form, field, value.trim()));
        },
    };
}
const enhance = connect(null, mapDispatchToProps);
export default enhance(MyComponent);

2) Keep track of field focus in the local state, add onFocus and onBlur handlers to fields that need to be trimmed.

in the render method...

    const handleBlur = (event) => {
        this.setState({
            hasFocus: null
        });
    }

    const handleFocus = (event) => {
        this.setState({
            hasFocus: event.target.name
        });
    }

in the JSX...

<LongTextInput source="name" onBlur={handleBlur} onFocus={handleFocus} />
<LongTextInput source="description" onBlur={handleBlur} onFocus={handleFocus} />

3) Inspect for the need to trim values componentDidUpdate() and dispatch the action to do so if necessary.

componentDidUpdate = (prevProps, prevState) => {
    if (this.state && this.state.name && this.state.hasFocus !== 'name' && (this.state.name !== this.state.name.trim())) {
        this.props.trimFieldValue('name', this.state.name);
    }
    if (this.state && this.state.description && this.state.hasFocus !== 'description' && (this.state.description !== this.state.description.trim())) {
        this.props.trimFieldValue('description', this.state.description);
    }
}

My componentDidUpdate method is a little ugly, but it works for now. The gist of it is that for each field you're testing to trim, you need to do nothing if that field has focus. If the field does not have focus, and it should be trimmed, then fire off the action to update that field in the redux form using your custom dispatcher in the props.

Maybe it should not have taken me so long, but I found this solution not an easy one to figure out. Maybe this approach will save someone else some time. I'm also open to feedback along the lines of "uh, why didn't you just do this!" if there's something I missed.

  • 1
    It's good that you document your findings! What you could do is to rephrase it as a question, then paste your solution as an answer to your own question. https://stackoverflow.com/help/self-answer – jonahe Sep 11 '17 at 21:13
  • Forgot to add that if you follow the above steps you can even accept your own answer. – jonahe Sep 11 '17 at 23:07
  • Awesome post @Michael, I also needed custom value to be set from my custom input. The only thing I'm missing is `const form = 'record-form';` - where it comes 'record-form' comes from? It's standard AOR name of the form for the current record? – suricactus Dec 07 '17 at 03:11
  • Yes. 'record-form' is the standard used by redux form. – Michael Westermann Dec 11 '17 at 23:28
  • Or we could also trim on the dataProvider side, just before the form values are to be submitted to the server. What do you think of this? – user2078023 Jun 03 '19 at 16:26

0 Answers0