Skip to content

Commit

Permalink
added base input component
Browse files Browse the repository at this point in the history
  • Loading branch information
christian97dd committed Nov 27, 2024
1 parent cc3a75c commit 8d712c1
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .ondevice/storybook.requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const getStories = () => {
return {
'./storybook/stories/Avatar/Avatar.stories.js': require('../storybook/stories/Avatar/Avatar.stories.js'),
'./storybook/stories/Button/Button.stories.js': require('../storybook/stories/Button/Button.stories.js'),
'./storybook/stories/BaseInput/BaseInput.stories.js': require('../storybook/stories/BaseInput/BaseInput.stories.js'),
'./storybook/stories/Carousel/Carousel.stories.js': require('../storybook/stories/Carousel/Carousel.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'),
Expand All @@ -60,11 +61,13 @@ const getStories = () => {
'./storybook/stories/List/List.stories.js': require('../storybook/stories/List/List.stories.js'),
'./storybook/stories/Loading/Loading.stories.js': require('../storybook/stories/Loading/Loading.stories.js'),
'./storybook/stories/LoadingFullScreen/LoadingFullScreen.stories.js': require('../storybook/stories/LoadingFullScreen/LoadingFullScreen.stories.js'),
'./storybook/stories/MainCardList/MainCardList.stories.js': require('../storybook/stories/MainCardList/MainCardList.stories.js'),
'./storybook/stories/ProgressBar/ProgressBar.stories.js': require('../storybook/stories/ProgressBar/ProgressBar.stories.js'),
'./storybook/stories/RadioButton/RadioButton.stories.js': require('../storybook/stories/RadioButton/RadioButton.stories.js'),
'./storybook/stories/Select/Select.stories.js': require('../storybook/stories/Select/Select.stories.js'),
'./storybook/stories/StatusChip/StatusChip.stories.js': require('../storybook/stories/StatusChip/StatusChip.stories.js'),
'./storybook/stories/Svg/Svg.stories.js': require('../storybook/stories/Svg/Svg.stories.js'),
'./storybook/stories/SwipeItemSelectionList/SwipeItemSelectionList.stories.js': require('../storybook/stories/SwipeItemSelectionList/SwipeItemSelectionList.stories.js'),
'./storybook/stories/SwipeList/SwipeList.stories.js': require('../storybook/stories/SwipeList/SwipeList.stories.js'),
'./storybook/stories/SwipeUp/SwipeUp.stories.js': require('../storybook/stories/SwipeUp/SwipeUp.stories.js'),
'./storybook/stories/Tabs/Tabs.stories.js': require('../storybook/stories/Tabs/Tabs.stories.js'),
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.11.0] - 2024-11-27

### Added

- Added base input component

## [1.10.3] - 2024-10-24

### Fixed
Expand Down
44 changes: 44 additions & 0 deletions src/components/atoms/BaseInput/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import {render, fireEvent} from '@testing-library/react-native';
import BaseInput from './index';

describe('BaseInput Component', () => {
it('renders correctly with placeholder and value', () => {
const {getByPlaceholderText} = render(
<BaseInput placeholder="Enter text" value="Test value" />
);

const input = getByPlaceholderText('Enter text');
expect(input.props.value).toBe('Test value');
});

it('calls onChangeText when text changes', () => {
const mockOnChangeText = jest.fn();
const {getByPlaceholderText} = render(
<BaseInput placeholder="Enter text" value="" onChangeText={mockOnChangeText} />
);

const input = getByPlaceholderText('Enter text');
fireEvent.changeText(input, 'New value');
expect(mockOnChangeText).toHaveBeenCalledWith('New value');
});

it('applies custom styles correctly', () => {
const customStyle = {borderColor: 'red'};
const {getByPlaceholderText} = render(
<BaseInput placeholder="Enter text" value="" style={customStyle} />
);

const input = getByPlaceholderText('Enter text');
expect(input.props.style).toEqual(
expect.arrayContaining([expect.objectContaining(customStyle)])
);
});

it('defaults textAlign to center', () => {
const {getByPlaceholderText} = render(<BaseInput placeholder="Enter text" value="" />);

const input = getByPlaceholderText('Enter text');
expect(input.props.textAlign).toBe('center');
});
});
44 changes: 44 additions & 0 deletions src/components/atoms/BaseInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, {forwardRef} from 'react';
import {StyleSheet, TextInput, TextInputProps} from 'react-native';
import {palette} from 'theme/palette';
import {moderateScale, scaledForDevice} from 'scale';

interface BaseInputProps extends TextInputProps {
placeholder?: string;
onChangeText?: (text: string) => void;
style?: any;
textAlign?: 'center' | 'left' | 'right' | undefined;
}

const BaseInput = forwardRef<TextInput, BaseInputProps>(
({value, placeholder, onChangeText, style, textAlign, ...props}, ref) => {
const styles = StyleSheet.create({
input: {
padding: 0,
height: scaledForDevice(70, moderateScale),
borderColor: palette.primary.main,
borderWidth: 2,
borderRadius: 8,
fontSize: scaledForDevice(42, moderateScale),
backgroundColor: palette.white.light,
color: palette.black.main,
},
});

return (
<TextInput
style={[styles.input, style]}
ref={ref}
value={value}
placeholder={placeholder}
textAlign={textAlign || 'center'}
onChangeText={onChangeText}
selectionColor={palette.primary.main}
placeholderTextColor={palette.grey[500]}
{...props}
/>
);
}
);

export default BaseInput;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Svg from 'atoms/Svg';
import SwipeUp from 'atoms/SwipeUp';
import {SwipeUpFlatList, SwipeUpScrollView, SwipeUpView} from 'atoms/SwipeUp/childComponents';
import Text from 'atoms/Text';
import BaseInput from 'atoms/BaseInput';

// Molecules
import Avatar from 'molecules/Avatar';
Expand Down Expand Up @@ -67,4 +68,5 @@ export {
ItemSelectionButton,
SwipeItemSelectionList,
MainCardList,
BaseInput,
};
25 changes: 25 additions & 0 deletions storybook/stories/BaseInput/BaseInput.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, {useState} from 'react';
import BaseInput from 'atoms/BaseInput';

export default {
title: 'Components/BaseInput',
argTypes: {
textAlign: {
options: ['center', 'left', 'right', null],
defaultValue: 'center',
control: {type: 'select'},
},
},
};

export const DefaultProps = (props) => {
const [value, setValue] = useState('');

return <BaseInput value={value} onChangeText={setValue} {...props} />;
};

DefaultProps.storyName = 'Base input with default props';

DefaultProps.args = {
placeholder: 'Janis Commerce',
};

0 comments on commit 8d712c1

Please sign in to comment.