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

Fixed Input styles for Storybook web #21

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .ondevice/storybook.requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ global.STORIES = [
directory: "./storybook/stories",
files: "**/*.stories.?(ts|tsx|js|jsx)",
importPathMatcher:
"^\\.[\\\\/](?:storybook\\/stories(?:\\/(?!\\.)(?:(?:(?!(?:^|\\/)\\.).)*?)\\/|\\/|$)(?!\\.)(?=.)[^/]*?\\.stories\\.(?:ts|tsx|js|jsx)?)$",
"^\\.[\\\\/](?:storybook[\\\\/]stories(?:[\\\\/](?!\\.)(?:(?:(?!(?:^|[\\\\/])\\.).)*?)[\\\\/]|[\\\\/]|$)(?!\\.)(?=.)[^\\\\/]*?\\.stories\\.(?:ts|tsx|js|jsx)?)$",
},
];

Expand Down Expand Up @@ -49,6 +49,7 @@ const getStories = () => {
return {
"./storybook/stories/Avatar/Avatar.stories.js": require("../storybook/stories/Avatar/Avatar.stories.js"),
"./storybook/stories/CheckBox/CheckBox.stories.js": require("../storybook/stories/CheckBox/CheckBox.stories.js"),
"./storybook/stories/DesignStystem/Colors.stories.js": require("../storybook/stories/DesignStystem/Colors.stories.js"),
"./storybook/stories/Image/Image.stories.js": require("../storybook/stories/Image/Image.stories.js"),
"./storybook/stories/Input/Input.stories.js": require("../storybook/stories/Input/Input.stories.js"),
"./storybook/stories/Loading/Loading.stories.js": require("../storybook/stories/Loading/Loading.stories.js"),
Expand Down
22 changes: 12 additions & 10 deletions src/components/Input/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,29 @@ describe('Input', () => {

expect(mockSetInputState).toHaveBeenCalledWith('focus');
});
it('as it has valid label, placeholder and user focuses out of the input without value', () => {
it('as it has valid label, placeholder and user finishes typing on input without value', () => {
const mockSetInputState = jest.fn();
useStateSpy.mockReturnValueOnce(['', mockSetInputState]);

const InputComp = create(<Input label="Test" placeholder="Test" />).root;

const TextInputComp = InputComp.findByType(TextInput);
const {onBlur} = TextInputComp.props;
const {onEndEditing} = TextInputComp.props;

onBlur();
onEndEditing({nativeEvent: {}});

expect(mockSetInputState).toHaveBeenCalledWith('incomplete');
});
it('as it has valid label, placeholder and user focuses out of the input with value', () => {
it('as it has valid label, placeholder and user finishes typing on input with value', () => {
const mockSetInputState = jest.fn();
useStateSpy.mockReturnValueOnce(['', mockSetInputState]);

const InputComp = create(<Input label="Test" placeholder="Test" value="Test" />).root;

const TextInputComp = InputComp.findByType(TextInput);
const {onBlur} = TextInputComp.props;
const {onEndEditing} = TextInputComp.props;

onBlur();
onEndEditing({nativeEvent: {text: 'valid value'}});

expect(mockSetInputState).toHaveBeenCalledWith('complete');
});
Expand All @@ -83,11 +83,12 @@ describe('Input', () => {
const InputComp = create(<Input label="Test" placeholder="Test" />).root;

const TextInputComp = InputComp.findByType(TextInput);
const {onChange} = TextInputComp.props;
const {onChange, onEndEditing} = TextInputComp.props;

onEndEditing({nativeEvent: {text: 'valid value'}});
onChange();

expect(mockSetInputState).toBeCalledTimes(0);
expect(mockSetInputState).toBeCalledTimes(1);
});
it('as it has valid label, placeholder and user submits', () => {
const mockSetInputState = jest.fn();
Expand All @@ -96,11 +97,12 @@ describe('Input', () => {
const InputComp = create(<Input label="Test" placeholder="Test" />).root;

const TextInputComp = InputComp.findByType(TextInput);
const {onSubmitEditing} = TextInputComp.props;
const {onSubmitEditing, onEndEditing} = TextInputComp.props;

onEndEditing({nativeEvent: {text: 'valid value'}});
onSubmitEditing();

expect(mockSetInputState).toBeCalledTimes(0);
expect(mockSetInputState).toBeCalledTimes(1);
});
});
});
24 changes: 18 additions & 6 deletions src/components/Input/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import React, {LegacyRef, useEffect, useState} from 'react';
import {TextInput, StyleSheet, View, Text, KeyboardType} from 'react-native';
import {
TextInput,
StyleSheet,
View,
Text,
KeyboardType,
NativeSyntheticEvent,
TextInputEndEditingEventData,
} from 'react-native';
import {
Status,
getBorderColor,
Expand Down Expand Up @@ -75,8 +83,12 @@ const Input = React.forwardRef<TextInput, InputProps>(
return onFocus();
};

const onBlurHandler = () => {
if (value) {
const onEndEditingHandler = ({
nativeEvent,
}: NativeSyntheticEvent<TextInputEndEditingEventData>) => {
const {text = ''} = nativeEvent;

if (text) {
setInputState('complete');
return onBlur();
}
Expand All @@ -86,7 +98,7 @@ const Input = React.forwardRef<TextInput, InputProps>(
};

const hasMessage = !!statusMessage;
const isLabelVisible = inputState === 'focus' && !value;
const isLabelVisible = !disabled && !readOnly && (inputState !== 'incomplete' || hasMessage);

const validBorderColor = getBorderColor({inputState, hasMessage, status, inputColor});
const validLabelColor = getLabelColor({
Expand Down Expand Up @@ -115,7 +127,7 @@ const Input = React.forwardRef<TextInput, InputProps>(
letterSpacing: 0,
lineHeight: 19,
position: 'absolute',
bottom: raiseLabel({disabled, hasMessage, inputState}) ? 25 : 5,
bottom: raiseLabel({disabled, hasMessage, inputState}) ? 25 : 0,
},
input: {
color: validInputTextColor,
Expand All @@ -138,7 +150,7 @@ const Input = React.forwardRef<TextInput, InputProps>(
style={styles.input}
ref={ref}
onFocus={onFocusHandler}
onBlur={onBlurHandler}
onEndEditing={onEndEditingHandler}
onChange={onChange}
onSubmitEditing={onSubmitEditing}
placeholder={placeholder}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Input/utils/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ describe('getLabelColor', () => {
expect(result).toBe(inputColor);
});

it('should return palette.primary.main when statusMessage is not empty', () => {
it('should return palette.error.main when statusMessage is not empty', () => {
const disabled = false;
const readOnly = false;
const inputState = 'notFocus';
const result = getLabelColor({disabled, readOnly, inputColor, inputState, statusMessage});
expect(result).toBe(palette.primary.main);
expect(result).toBe(palette.error.main);
});

it('should return palette.grey[500] when statusMessage is empty', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Input/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const getLabelColor = ({
}

if (statusMessage) {
return palette.primary.main;
return palette.error.main;
}

return palette.grey[500];
Expand Down