Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/floating label text input #1637

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/components/ui/input/input.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import React from 'react';
import {
Animated,
ImageProps,
NativeSyntheticEvent,
Platform,
Expand Down Expand Up @@ -51,6 +52,7 @@ export interface InputProps extends TextInputProps, InputStyledProps {
size?: EvaSize;
disabled?: boolean;
label?: RenderProp<TextProps> | React.ReactText;
floatingLabel?: RenderProp<TextProps> | React.ReactText;
caption?: RenderProp<TextProps> | React.ReactText;
accessoryLeft?: RenderProp<Partial<ImageProps>>;
accessoryRight?: RenderProp<Partial<ImageProps>>;
Expand Down Expand Up @@ -81,6 +83,9 @@ export type InputElement = React.ReactElement<InputProps>;
* 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.
*
Expand Down Expand Up @@ -142,6 +147,8 @@ export class Input extends React.Component<InputProps> implements WebEventRespon

private textInputRef = React.createRef<TextInput>();
private webEventResponder: WebEventResponderInstance = WebEventResponder.create(this);
private animatedIsFocused = new Animated.Value(this.props.value === '' ? 0 : 1);


public focus = (): void => {
this.textInputRef.current?.focus();
Expand Down Expand Up @@ -219,6 +226,7 @@ export class Input extends React.Component<InputProps> implements WebEventRespon
fontSize: textFontSize,
fontWeight: textFontWeight,
color: textColor,
...(this.props.floatingLabel ? { paddingTop: 12 }: {})
},
placeholder: {
color: placeholderColor,
Expand All @@ -236,6 +244,27 @@ export class Input extends React.Component<InputProps> 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,
Expand All @@ -245,11 +274,22 @@ export class Input extends React.Component<InputProps> 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<ViewProps> {
const {
eva,
textStyle,
label,
floatingLabel,
caption,
accessoryLeft,
accessoryRight,
Expand All @@ -269,6 +309,9 @@ export class Input extends React.Component<InputProps> implements WebEventRespon
style={[evaStyle.label, styles.label]}
component={label}
/>
<Animated.Text style={evaStyle.floatingLabel}>
{floatingLabel}
</Animated.Text>
<View style={[evaStyle.inputContainer, styles.inputContainer]}>
<FalsyFC
style={evaStyle.icon}
Expand Down
8 changes: 8 additions & 0 deletions src/components/ui/input/input.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ describe('@input: component checks', () => {
expect(component.queryByText('I love Babel')).toBeTruthy();
});

it('should render text passed to floatingLabel prop', () => {
const component = render(
<TestInput floatingLabel='Floating Label' />,
);

expect(component.queryByText('Floating Label')).toBeTruthy();
});

it('should render component passed to caption prop', () => {
const Caption = (props): React.ReactElement<ImageProps> => (
<Image
Expand Down
15 changes: 15 additions & 0 deletions src/showcases/components/input/inputFloatingUsage.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { Input } from '@ui-kitten/components';

export const InputFloatingUsageShowcase = () => {

const [value, setValue] = React.useState('');

return (
<Input
floatingLabel='Floating Label'
value={value}
onChangeText={nextValue => setValue(nextValue)}
/>
);
};