NestedLink icon indicating copy to clipboard operation
NestedLink copied to clipboard

Do you have any possibility to check for a field to be pristine/touched?

Open ckpinguin opened this issue 7 years ago • 4 comments

Somehow it should be possible to only check validation, if field is touched or onBlur (switch to the next field)?

ckpinguin avatar Apr 20 '17 20:04 ckpinguin

Create your own <Input /> tag which behaves like this. This is the normal way of handling such a situation in the way needed for you.

For the start copy Input implementation from tags.jsx. Then change the way how it handles the validation error which is enclosed in the link. There is no magic at all, the creation of your own controls is straightforward:

https://github.com/Volicon/NestedLink/blob/master/tags.jsx#L35

Your Input must know whenever it's focused or touched and suppress displaying the validation error if it's not. Probably, you will need to add the local state to it to make it remember such a thing.

gaperton avatar Apr 20 '17 22:04 gaperton

You're supposed to create your own controls wrapping all necessary styles and handling error formatting, in the way that your forms will be defined with the semantic markup. That's an overall idea and links makes it practically possible. tags.jsx is just an example and the starting boilerplate.

That's how it is done in userslist example:

const ValidatedInput = ( props ) => (
    <div>
        <Input { ...props } />
        <div className="validation-error">
            { props.valueLink.error || '' }
        </div>
    </div>
);

https://github.com/Volicon/NestedLink/blob/master/example/src/userslist.jsx#L150

You need to do the same thing but use raw <input/> and add the local state to make it remember whenever it was touched. That's it.

gaperton avatar Apr 20 '17 22:04 gaperton

OK, thanks I'll try it :-) Yesterday I „solved“ it using onBlur={() => this.setState({titleTouched: true})} but having more local state in a subcomponent did not feel right.

ckpinguin avatar Apr 21 '17 07:04 ckpinguin

Yesterday I „solved“ it using onBlur={() => this.setState({titleTouched: true})} but having more local state in a subcomponent did not feel right.

It is okay. You need to hide this local state in <Input/> itself once and forget about it. This state is entirely private and there is no reason to expose it outside.

gaperton avatar Apr 21 '17 15:04 gaperton