I don't wish to save parsed values in state i.e. money like $1,000. I need to store 1000. I don't see a way to add formatted data to the Control from state. Parser property is great for preventing/formatting in the control itself but this also goes back to state this way.
A functionality not unlike parser but formatter on the Control component
There is parser only unless I am missing something I can't find in docs or repo.
The fragile part of this is this: let's say you entered in $1000 and expected $1,000 to be the view value in a standard <input type="text">.
1000 to $1,000$1,000 converts to $1,000$1,000 back to 1000 and send it back to the store.And let's say you press backspace - your parser needs to be smart enough to know that $1,00 should parse to $100 so that your formatter can pick it up again. I can go on but this gets into some tricky logic where, if it's not perfectly in sync, can end up in an infinite loop.
I highly recommend creating a simple custom component for this instead, something like this:
function parseCurrency(value) { /* ... */ }
function formatCurrency(value) { /* ... */ }
const Currency = (props) => (
<input
type="text"
value={formatCurrency(props.value)}
onChange={e => props.onChange(parseCurrency(e.target.value))}
/>
);
// rendered anywhere:
<Control.text component={Currency} model="invoice.total" />
The great thing about the above example is that it's more explicit and clear than using parse and (a nonexistent) format. But I'm curious to see how a format prop would look like to you - if you can make a compelling case (with good examples), I'd be more than happy to add it in.
I hear what you’re saying. It does seem to be a common need in form development though. Show one way store another. I will try the custom component wrapper and see how it fares. I’ll think on the format property and get back.
I was thinking the formatter was for controlling how the data looks like in the input and the parser is for storage in the store. It is a little different of a concept of what you are using a parser for.
Angular has a standard way of doing this on its ngModel. See the section for parsers/formatters on this link. https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
From: David Khourshid [mailto:[email protected]]
Sent: Tuesday, April 11, 2017 11:06 PM
To: davidkpiano/react-redux-form react-redux-form@noreply.github.com
Cc: Hawk, Greg Greg.Hawk@lendingtree.com; Author author@noreply.github.com
Subject: Re: [davidkpiano/react-redux-form] Formatters versus Parsers missing functionality (#745)
The fragile part of this is this: let's say you entered in $1000 and expected $1,000 to be the view value in a standard .
And let's say you press backspace - your parser needs to be smart enough to know that $1,00 should parse to $100 so that your formatter can pick it up again. I can go on but this gets into some tricky logic where, if it's not perfectly in sync, can end up in an infinite loop.
I highly recommend creating a simple custom component for this instead, something like this:
function parseCurrency(value) { /* ... */ }
function formatCurrency(value) { /* ... */ }
const Currency = (props) => (
type="text"
value={formatCurrency(props.value)}
onChange={e => props.onChange(parseCurrency(e.target.value))}
/>
);
// rendered anywhere:
The great thing about the above example is that it's more explicit and clear than using parse and (a nonexistent) format. But I'm curious to see how a format prop would look like to you - if you can make a compelling case (with good examples), I'd be more than happy to add it in.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/davidkpiano/react-redux-form/issues/745#issuecomment-293459544, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AE2tk90slVwDPgGwCJZhidzbC9O93NlUks5rvD-ngaJpZM4M644c.
Here is a more clear article if you aren’t familiar with angular’s implementation. https://alexperry.io/angularjs/2014/12/10/parsers-and-formatters-angular.html
From: David Khourshid [mailto:[email protected]]
Sent: Tuesday, April 11, 2017 11:06 PM
To: davidkpiano/react-redux-form react-redux-form@noreply.github.com
Cc: Hawk, Greg Greg.Hawk@lendingtree.com; Author author@noreply.github.com
Subject: Re: [davidkpiano/react-redux-form] Formatters versus Parsers missing functionality (#745)
The fragile part of this is this: let's say you entered in $1000 and expected $1,000 to be the view value in a standard .
And let's say you press backspace - your parser needs to be smart enough to know that $1,00 should parse to $100 so that your formatter can pick it up again. I can go on but this gets into some tricky logic where, if it's not perfectly in sync, can end up in an infinite loop.
I highly recommend creating a simple custom component for this instead, something like this:
function parseCurrency(value) { /* ... */ }
function formatCurrency(value) { /* ... */ }
const Currency = (props) => (
type="text"
value={formatCurrency(props.value)}
onChange={e => props.onChange(parseCurrency(e.target.value))}
/>
);
// rendered anywhere:
The great thing about the above example is that it's more explicit and clear than using parse and (a nonexistent) format. But I'm curious to see how a format prop would look like to you - if you can make a compelling case (with good examples), I'd be more than happy to add it in.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/davidkpiano/react-redux-form/issues/745#issuecomment-293459544, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AE2tk90slVwDPgGwCJZhidzbC9O93NlUks5rvD-ngaJpZM4M644c.
@davidkpiano I am trying the custom component but it doesn't seem to do anything with the model now? I am not quite sure how to use the custom component if I lose all the validators, model-state synch etc with this custom control. Is there a production type example following this pattern?
Can you share the code?
@davidkpiano I don't have a small example. Can you tell me how the custom control is meant to work? It seems like it loses all the functionality of the standard controls. I'm at a loss as to how to use one.
I'm actually going to implement formatters soon. I realize it's a necessary feature.
Awesome!!!
From: David Khourshid [mailto:[email protected]]
Sent: Tuesday, May 23, 2017 2:44 PM
To: davidkpiano/react-redux-form react-redux-form@noreply.github.com
Cc: Hawk, Greg Greg.Hawk@lendingtree.com; Author author@noreply.github.com
Subject: Re: [davidkpiano/react-redux-form] Formatters versus Parsers missing functionality (#745)
I'm actually going to implement formatters soon. I realize it's a necessary feature.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/davidkpiano/react-redux-form/issues/745#issuecomment-303494656, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AE2tkw1NyHt9aIJWRIOSY6Tzdw9Mum3vks5r8yj1gaJpZM4M644c.
@davidkpiano any movement on the formatters? I am running up against a wall soon.
@greghawk You can use a custom component as a stop-gap. What exactly is blocking you?
@davidkpiano You're right. Let me try your example of above and see if can get it going. Will that custom control play nice with validations?
Of course! Just add the right hooks to your component to let RRF know when it updates, and keep the view value in your own component's state.
That is, RRF shouldn't care what the view value is. It only should care what the real value is. Even something like react-maskedinput should work, same concept.
@davidkpiano Cool. I am trying but the validation piece is not happy. I'll look at masked input as well to see what they are doing.
This is what I've tried so far but I think it is trying to validate the formatted value even though redux is storing proper value:
const Formatter = (props) => (
type={props.type}
autoComplete={props.autoComplete}
autoFocus={props.autoFocus}
className ={props.className}
placeholder={props.placeholder}
onBlur={props.onBlur}
value={props.formatter(props.value)}
onKeyPress={props.onKeyPress}
onFocus={props.onFocus}
onChange={e => props.onChange(() => {
return (e.target.value).replace(/\D/g, '');
})}
/>
);
const TextControl = ({steps, step, formatter, type}) => {
return (
I was able to hack this together and it seems to work in case it helps anybody prior to 'formatting' being officially released:
import numeral from 'numeral';
import MaskedInput from 'react-text-mask';
import createNumberMask from 'text-mask-addons/dist/createNumberMask';
const numberMask = createNumberMask({
prefix: '',
suffix: ''
})
const MyTextInput = (props) => <MaskedInput mask={numberMask} {...props} />;
<Control.text
autoFocus
model=".people"
className="form-control"
updateOn="blur"
placeholder="Some text"
autoComplete="off"
component={MyTextInput}
validators={{
isNumeric: validator.isNumeric,
required: val => val && val.length,
minLength: val => val && val.length > 3
}}
parser={(val) => { return (val) ? numeral(val).format('0') : val; }}
/>
I think the trick is the parser cleaning it back up for validation.
@davidkpiano - any thoughts on the PR? I'll have some time this weekend to work on any revisions you might see fit.
@brycesenz I'll accept it - if developers aren't careful, it can be a footgun, but I'm willing to give the feature a try!
Most helpful comment
I was able to hack this together and it seems to work in case it helps anybody prior to 'formatting' being officially released:
I think the trick is the parser cleaning it back up for validation.