-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from sonalake/feature/text-input
Feature/text input
- Loading branch information
Showing
10 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './input.model'; | ||
export * from './input.util'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export type InputSize = 'md' | 'sm'; | ||
|
||
export type BaseInputProps = { | ||
size?: InputSize; | ||
disabled?: boolean; | ||
readOnly?: boolean; | ||
error?: boolean; | ||
className?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import clsx from 'clsx'; | ||
|
||
import { InputSize } from './input.model'; | ||
|
||
type TextBasedInputClassProps = { | ||
disabled?: boolean; | ||
readOnly?: boolean; | ||
error?: boolean; | ||
size?: InputSize; | ||
}; | ||
|
||
export const getTextBasedInputClasses = ({ | ||
disabled, | ||
readOnly, | ||
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', | ||
'border-primary-150 focus:shadow-primary-100': !error, | ||
'border-error-100 focus:shadow-error-100': error, | ||
'pointer-events-none': readOnly, | ||
'text-neutral-200': disabled, | ||
'text-black': !disabled, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './text-input.component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { ComponentPropsWithRef, forwardRef } from 'react'; | ||
import clsx from 'clsx'; | ||
|
||
import { BaseInputProps, getTextBasedInputClasses } from '../input/'; | ||
|
||
type TextInputProps = Omit<ComponentPropsWithRef<'input'>, 'size'> & | ||
BaseInputProps; | ||
|
||
export const TextInput = forwardRef<HTMLInputElement, TextInputProps>( | ||
({ disabled, readOnly, error, size, className, ...rest }, ref) => ( | ||
<input | ||
ref={ref} | ||
type="text" | ||
disabled={disabled} | ||
readOnly={readOnly} | ||
aria-invalid={error || undefined} | ||
className={clsx( | ||
getTextBasedInputClasses({ | ||
disabled, | ||
readOnly, | ||
error, | ||
size, | ||
}), | ||
className | ||
)} | ||
{...rest} | ||
/> | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { axe } from 'jest-axe'; | ||
|
||
import { TextInput } from './text-input.component'; | ||
|
||
describe('Text-Input', () => { | ||
test('renders an accessible text-input', async () => { | ||
const { container } = render( | ||
<TextInput defaultValue="Test Value" aria-label="test-input" /> | ||
); | ||
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument(); | ||
expect(await axe(container)).toHaveNoViolations(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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>; | ||
|
||
const Template: ComponentStory<typeof TextInput> = (args) => ( | ||
<TextInput {...args} /> | ||
); | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
defaultValue: 'Default', | ||
}; | ||
|
||
export const WithPlaceholder = Template.bind({}); | ||
WithPlaceholder.args = { | ||
placeholder: 'Placeholder', | ||
}; | ||
|
||
export const Small = Template.bind({}); | ||
Small.args = { | ||
size: 'sm', | ||
defaultValue: 'Small', | ||
}; | ||
|
||
export const WithError = Template.bind({}); | ||
WithError.args = { | ||
error: true, | ||
defaultValue: 'Error', | ||
}; | ||
|
||
export const Disabled = Template.bind({}); | ||
Disabled.args = { | ||
disabled: true, | ||
defaultValue: 'Disabled', | ||
}; | ||
|
||
export const Readonly = Template.bind({}); | ||
Readonly.args = { | ||
readOnly: true, | ||
defaultValue: 'Readonly', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters