From c6fe5e8b9d632453c02eac93c181c5d92276e528 Mon Sep 17 00:00:00 2001 From: joaoGabriel55 Date: Tue, 26 Jul 2022 11:04:41 -0300 Subject: [PATCH 1/2] feat(input): implement floating input label style --- src/components/ui/input/input.component.tsx | 43 +++++++++++++++++++ src/components/ui/input/input.spec.tsx | 8 ++++ .../input/inputFloatingUsage.component.tsx | 15 +++++++ 3 files changed, 66 insertions(+) create mode 100644 src/showcases/components/input/inputFloatingUsage.component.tsx diff --git a/src/components/ui/input/input.component.tsx b/src/components/ui/input/input.component.tsx index 06306a31c..feaa13e32 100644 --- a/src/components/ui/input/input.component.tsx +++ b/src/components/ui/input/input.component.tsx @@ -6,6 +6,7 @@ import React from 'react'; import { + Animated, ImageProps, NativeSyntheticEvent, Platform, @@ -51,6 +52,7 @@ export interface InputProps extends TextInputProps, InputStyledProps { size?: EvaSize; disabled?: boolean; label?: RenderProp | React.ReactText; + floatingLabel?: RenderProp | React.ReactText; caption?: RenderProp | React.ReactText; accessoryLeft?: RenderProp>; accessoryRight?: RenderProp>; @@ -81,6 +83,9 @@ export type InputElement = React.ReactElement; * to render above the input field. * If it is a function, expected to return a Text. * + * @property {ReactElement | ReactText | (TextProps) => ReactElement} floatingLabel - Function component to render floating Input view. + * Expected to return View. + * * @property {ReactElement | ReactText | (TextProps) => ReactElement} caption - Function component to render below Input view. * Expected to return View. * @@ -142,6 +147,8 @@ export class Input extends React.Component implements WebEventRespon private textInputRef = React.createRef(); private webEventResponder: WebEventResponderInstance = WebEventResponder.create(this); + private animatedIsFocused = new Animated.Value(this.props.value === '' ? 0 : 1); + public focus = (): void => { this.textInputRef.current?.focus(); @@ -219,6 +226,7 @@ export class Input extends React.Component implements WebEventRespon fontSize: textFontSize, fontWeight: textFontWeight, color: textColor, + paddingTop: 12 }, placeholder: { color: placeholderColor, @@ -236,6 +244,27 @@ export class Input extends React.Component implements WebEventRespon fontWeight: labelFontWeight, fontFamily: labelFontFamily, }, + floatingLabel: { + zIndex: 1, + textAlign: 'left', + color: labelColor, + marginBottom: labelMarginBottom, + fontFamily: labelFontFamily, + position: 'absolute', + left: 16, + fontWeight:this.animatedIsFocused.interpolate({ + inputRange: [0, 1], + outputRange: [10, labelFontWeight], + }), + top: this.animatedIsFocused.interpolate({ + inputRange: [0, 1.5], + outputRange: [12, 0], + }), + fontSize: this.animatedIsFocused.interpolate({ + inputRange: [0, 1], + outputRange: [15, labelFontSize], + }), + }, captionLabel: { fontSize: captionFontSize, fontWeight: captionFontWeight, @@ -245,11 +274,22 @@ export class Input extends React.Component implements WebEventRespon }; }; + componentDidUpdate() { + const value = (this.isFocused() || this.props.value !== ''); + + Animated.timing(this.animatedIsFocused, { + toValue: value ? 1 : 0, + useNativeDriver: value, + duration: 200, + }).start(); + }; + public render(): React.ReactElement { const { eva, textStyle, label, + floatingLabel, caption, accessoryLeft, accessoryRight, @@ -269,6 +309,9 @@ export class Input extends React.Component implements WebEventRespon style={[evaStyle.label, styles.label]} component={label} /> + + {floatingLabel} + { expect(component.queryByText('I love Babel')).toBeTruthy(); }); + it('should render text passed to floatingLabel prop', () => { + const component = render( + , + ); + + expect(component.queryByText('Floating Label')).toBeTruthy(); + }); + it('should render component passed to caption prop', () => { const Caption = (props): React.ReactElement => ( { + + const [value, setValue] = React.useState(''); + + return ( + setValue(nextValue)} + /> + ); +}; From 2fed43f616889a0e32d99aa59a4a6f60f92e579e Mon Sep 17 00:00:00 2001 From: joaoGabriel55 Date: Tue, 26 Jul 2022 11:13:17 -0300 Subject: [PATCH 2/2] fix(input): minor fix floating input label style --- src/components/ui/input/input.component.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ui/input/input.component.tsx b/src/components/ui/input/input.component.tsx index feaa13e32..6c5591ddc 100644 --- a/src/components/ui/input/input.component.tsx +++ b/src/components/ui/input/input.component.tsx @@ -226,7 +226,7 @@ export class Input extends React.Component implements WebEventRespon fontSize: textFontSize, fontWeight: textFontWeight, color: textColor, - paddingTop: 12 + ...(this.props.floatingLabel ? { paddingTop: 12 }: {}) }, placeholder: { color: placeholderColor,