React Native: Creating an Animated Input Field Using the Animated API
Hello!Today we are sharing with you cognitive material, a translation of which was prepared specifically for students of the course "ReactJS / React Native-developer" .
So, let's begin.
We all saw these input fields:
The caption is large and looks like a placeholder until you focus on the input. It will become smaller and rise up.
It looks great. Smooth. Impeccably.
It also seems that only an experienced developer can do this, doesn’t it?
Well, perhaps this was so before the advent of React Native, in those days when people lived in caves and created all kinds of game. But this is a thing of the past.
You can watch the video or continue reading. It all depends on what you prefer.
What are we trying to achieve?
There are two options for what we are dealing with.
The first is when there is no focus on the input field.
An inscription appears inside the input field, and its size is equal to the size of the text field. The color is dull. This is very similar to the placeholder
property.
And the second option, when there is focus on the input field.
The inscription appears above the input field, its size is smaller, and the color is different from the color of the entered text.
Simplest implementation
Finally we can get to work. So far without any animations.
As it turns out, we have two UI states:
There is no focus on the field and the inscription inside the field.
There is focus on the field, an inscription above the input field.
In fact, we could store the state of whether there is focus on the field or not. Then, depending on this state, we could choose where to place the inscription and what styles to apply to it.
Since the inscription should be in different places, and we do not want it to affect the placement of the components, we will absolutely position it. To make sure that there is enough space for it, you will have to add an indentation from the top to the wrapping view.
And this is a good starting point. So far we have no animations, but we can already change the location of the inscription depending on where the focus is.
Why not use a placeholder
?
Of course, using the TextInput
property of the placeholder seems tempting. However, this will not work, because we want to control how, when and where the inscription is displayed.
Instead, we want the label to be inside the text box when focus is on it. And we want it to move up when focus appears on the input field, and this can only be achieved if we work with the same element.
How about the animation?
Actually the easiest part remained.
Since we have two states in which there may be an inscription, and we choose one, depending on the focus, and the animation of this transition between the states itself is quite trivial.
will mean whether there is focus on the field (1) or not (0);
We will gradually change the number to (1) when focusing and to (0) otherwise;
In the form of this number, we will reflect the style of the inscription: in (0) and (1) we will define styles, and React Native will automatically calculate and apply intermediate styles. And even the colors.
To implement all this is not difficult.
Animated.Value
we will need to initialize in componentWillMount
.
componentWillMount() { this._animatedIsFocused = new Animated.Value(0); }
Then, since the value of this number should be based on whether there is focus on the input field or not, and since we already have this bit of status information, we can add the componentDidUpdate
function, which will change this number depending on this.state