-
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.
- Loading branch information
1 parent
90c3d4d
commit 65e1f27
Showing
13 changed files
with
208 additions
and
17 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
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,10 @@ | ||
import type { TextInputProps } from '../TextInput'; | ||
|
||
export type SelectProps = Omit< | ||
TextInputProps, | ||
'beforeComponent' | 'afterComponent' | 'type' | ||
> & { | ||
beforeComponent?: | ||
| TextInputProps.InnerComponents.Icon | ||
| TextInputProps.InnerComponents.Avatar; | ||
}; |
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,65 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { Select } from './Select'; | ||
|
||
import { TetDocs } from '@/docs-components/TetDocs'; | ||
|
||
const meta = { | ||
component: Select, | ||
tags: ['autodocs'], | ||
args: { | ||
placeholder: 'Input placeholder', | ||
}, | ||
argTypes: {}, | ||
|
||
parameters: { | ||
docs: { | ||
description: { | ||
component: | ||
'A component that allows users to choose one or more options from a list, typically presented as a dropdown or pop-up menu. Select components are commonly used in forms and settings.', | ||
}, | ||
page: () => ( | ||
<TetDocs docs="https://docs.tetrisly.com/components/list/select" /> | ||
), | ||
}, | ||
}, | ||
} satisfies Meta<typeof Select>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = {}; | ||
|
||
export const Disabled: Story = { | ||
args: { | ||
state: 'disabled', | ||
}, | ||
}; | ||
|
||
export const Alert: Story = { | ||
args: { | ||
state: 'alert', | ||
}, | ||
}; | ||
|
||
export const BeforeIconComponent: Story = { | ||
args: { | ||
beforeComponent: { | ||
type: 'Icon', | ||
props: { | ||
name: '20-bolt', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const BeforeAvatarComponent: Story = { | ||
args: { | ||
beforeComponent: { | ||
type: 'Avatar', | ||
props: { | ||
initials: 'A', | ||
}, | ||
}, | ||
}, | ||
}; |
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,73 @@ | ||
import { vi } from 'vitest'; | ||
|
||
import { Select } from './Select'; | ||
import { fireEvent, render } from '../../tests/render'; | ||
|
||
const handleEventMock = vi.fn(); | ||
|
||
const getSelect = (jsx: JSX.Element) => { | ||
const { getByTestId, queryByTestId } = render(jsx); | ||
|
||
return { | ||
textInput: getByTestId('text-input'), | ||
input: getByTestId('text-input-input') as HTMLInputElement, | ||
beforeComponent: queryByTestId('text-input-before-component'), | ||
}; | ||
}; | ||
|
||
describe('Select', () => { | ||
beforeEach(() => { | ||
handleEventMock.mockClear(); | ||
}); | ||
|
||
it('should render the select', () => { | ||
const { textInput } = getSelect(<Select />); | ||
expect(textInput).toBeInTheDocument(); | ||
}); | ||
|
||
it('should emit onChange', () => { | ||
const { input } = getSelect(<Select value="" onChange={handleEventMock} />); | ||
|
||
if (input) { | ||
fireEvent.change(input, { target: { value: '2020-05-24' } }); | ||
} | ||
|
||
expect(handleEventMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should emit onBlur', () => { | ||
const { input } = getSelect(<Select onBlur={handleEventMock} />); | ||
|
||
if (input) { | ||
fireEvent.blur(input); | ||
} | ||
|
||
expect(handleEventMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should emit onFocus', () => { | ||
const { input } = getSelect(<Select onFocus={handleEventMock} />); | ||
|
||
if (input) { | ||
fireEvent.focus(input); | ||
} | ||
|
||
expect(handleEventMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render beforeComponent', () => { | ||
const { beforeComponent } = getSelect( | ||
<Select beforeComponent={{ type: 'Icon', props: { name: '20-bolt' } }} />, | ||
); | ||
|
||
expect(beforeComponent).toBeInTheDocument(); | ||
}); | ||
|
||
it('should propagate custom prop to text input', () => { | ||
const { textInput } = getSelect( | ||
<Select custom={{ backgroundColor: 'background-negative-subtle' }} />, | ||
); | ||
|
||
expect(textInput).toHaveStyle('background-color: rgb(254, 245, 245)'); | ||
}); | ||
}); |
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,17 @@ | ||
import { FC } from 'react'; | ||
|
||
import { SelectProps } from './Select.props'; | ||
import { TextInput, TextInputProps } from '../TextInput'; | ||
|
||
import { MarginProps } from '@/types'; | ||
|
||
const DROPDOWN_INDICATOR_COMPONENT: TextInputProps['afterComponent'] = { | ||
type: 'IconButton', | ||
props: { | ||
icon: '20-chevron-down-small', | ||
}, | ||
}; | ||
|
||
export const Select: FC<SelectProps & MarginProps> = (props) => ( | ||
<TextInput afterComponent={DROPDOWN_INDICATOR_COMPONENT} {...props} /> | ||
); |
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 { Select } from './Select'; | ||
export type { SelectProps } from './Select.props'; |
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
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