Skip to content

Commit

Permalink
feat(text-input): add component
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztoflukawski committed Feb 23, 2022
1 parent 5d933fd commit 4fde699
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/models/base-input-props.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { InputSize } from '.';

export type BaseInputProps = {
size?: InputSize;
disabled?: boolean;
readOnly?: boolean;
error?: boolean;
className?: string;
};
2 changes: 2 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './queryable-component.model';
export * from './base-input-props.model';
export * from './input-size.model';
1 change: 1 addition & 0 deletions src/models/input-size.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type InputSize = 'md' | 'sm';
1 change: 1 addition & 0 deletions src/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './pagination';
export * from './property-item';
export * from './table';
export * from './typography';
export * from './text-input';
1 change: 1 addition & 0 deletions src/ui/text-input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './text-input.component';
32 changes: 32 additions & 0 deletions src/ui/text-input/text-input.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { ComponentPropsWithRef, forwardRef } from 'react';
import clsx from 'clsx';

import { BaseInputProps } from '../../models';
import { getTextBasedInputClasses } from '../../utils/text-based-input.utils';

type TextInputProps = BaseInputProps & ComponentPropsWithRef<'input'>;

export const TextInput = forwardRef<HTMLInputElement, TextInputProps>(
({ disabled, readOnly, error, size, className, ...rest }, ref) => {
return (
<>
<input
ref={ref}
type="text"
disabled={disabled}
readOnly={readOnly}
aria-invalid={error || undefined}
className={clsx(
getTextBasedInputClasses({
disabled: disabled || readOnly,
error,
size,
}),
className
)}
{...rest}
/>
</>
);
}
);
19 changes: 19 additions & 0 deletions src/ui/text-input/text-input.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { ComponentMeta, ComponentStory } from '@storybook/react';

import { TextInput } from './text-input.component';

export default {
title: 'Atoms/TextInput',
component: TextInput,
} as ComponentMeta<typeof TextInput>;

export const Default: ComponentStory<typeof TextInput> = () => <TextInput />;

export const Disabled: ComponentStory<typeof TextInput> = () => (
<TextInput disabled={true} />
);

export const Readonly: ComponentStory<typeof TextInput> = () => (
<TextInput readOnly={true} />
);
26 changes: 26 additions & 0 deletions src/utils/text-based-input.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import clsx from 'clsx';

import { InputSize } from '../models';

type TextBasedInputClassProps = {
disabled?: boolean;
error?: boolean;
size?: InputSize;
};

export const getTextBasedInputClasses = ({
disabled,
error,
size = 'md',
}: TextBasedInputClassProps) =>
clsx(
'appearance-none border rounded focus:outline-none placeholder-primary-300 bg-slate-100',
{
'py-1 px-2 text-sm': size === 'sm',
'py-3 px-4 text-base': size === 'md',
// 'text-primary-400': disabled,
// 'text-primary-500': !disabled,
// 'border-red-500 focus:ring-1 focus:ring-red-500': error,
'border-primary-150 focus:shadow-primary-100': !error,
}
);

0 comments on commit 4fde699

Please sign in to comment.