-
Notifications
You must be signed in to change notification settings - Fork 2
Use MXUI Select component for property type manual account #355
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0428035
feat: use MXUI Select component for property type manual account
7209cff
added the test
ec53fdd
added a test for property type
d52e8c7
add the build step in ci
0d6dd66
using TextField for select
cf3e5e4
test if selecting an option works
8c7b371
add required error validation test
a834c84
add required prop to select
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,43 @@ | ||
| import React from 'react' | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { render, screen, waitFor } from 'src/utilities/testingLibrary' | ||
| import userEvent from '@testing-library/user-event' | ||
| import { Select } from 'src/components/Select' | ||
|
|
||
| const options = [ | ||
| { label: 'Checking', value: 'checking' }, | ||
| { label: 'Savings', value: 'savings' }, | ||
| ] | ||
|
|
||
| const defaultProps = { | ||
| label: 'Account type', | ||
| name: 'account_type', | ||
| onChange: vi.fn(), | ||
| options, | ||
| value: '', | ||
| } | ||
|
|
||
| describe('Select', () => { | ||
| it('renders with default props', () => { | ||
| render(<Select {...defaultProps} />) | ||
|
|
||
| expect(screen.getByLabelText('Account type')).toBeInTheDocument() | ||
| expect(screen.getByText('Select a value')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('calls onChange with the selected value', async () => { | ||
| const user = userEvent.setup() | ||
| const handleChange = vi.fn() | ||
|
|
||
| render(<Select {...defaultProps} onChange={handleChange} />) | ||
|
|
||
| await user.click(screen.getByRole('combobox')) | ||
| await user.click(screen.getByRole('option', { name: 'Savings' })) | ||
|
|
||
| await waitFor(() => | ||
| expect(handleChange).toHaveBeenCalledWith({ | ||
| target: { name: 'account_type', value: 'savings' }, | ||
| }), | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or 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 React from 'react' | ||
| import FormControl from '@mui/material/FormControl' | ||
| import FormHelperText from '@mui/material/FormHelperText' | ||
| import InputLabel from '@mui/material/InputLabel' | ||
| import MenuItem from '@mui/material/MenuItem' | ||
| import MuiSelect, { SelectChangeEvent } from '@mui/material/Select' | ||
|
|
||
| export interface SelectOption { | ||
| label: string | ||
| value: string | ||
| } | ||
|
|
||
| export interface SelectProps { | ||
| 'data-test'?: string | ||
| error?: boolean | ||
| fullWidth?: boolean | ||
| helperText?: string | ||
| label: string | ||
| name: string | ||
| onChange: (e: { target: { name: string; value: string } }) => void | ||
| options?: SelectOption[] | ||
| placeholder?: string | ||
| value: string | ||
| } | ||
|
|
||
| export const Select = ({ | ||
| 'data-test': dataTest, | ||
| error, | ||
| fullWidth = true, | ||
| helperText, | ||
| label, | ||
| name, | ||
| onChange, | ||
| options = [], | ||
| placeholder = 'Select a value', | ||
| value, | ||
| }: SelectProps) => { | ||
| const handleChange = (e: SelectChangeEvent) => { | ||
| onChange({ target: { name, value: e.target.value as string } }) | ||
| } | ||
|
|
||
| return ( | ||
| <FormControl error={error} fullWidth={fullWidth}> | ||
| <InputLabel id={`${name}-label`}>{label}</InputLabel> | ||
| <MuiSelect | ||
| displayEmpty={true} | ||
| inputProps={{ 'data-test': dataTest }} | ||
| label={label} | ||
| labelId={`${name}-label`} | ||
| name={name} | ||
| onChange={handleChange} | ||
| renderValue={(selected) => { | ||
| if (selected === '') { | ||
| return placeholder | ||
| } | ||
| const option = options.find((opt) => opt.value === selected) | ||
| return option ? option.label : selected | ||
| }} | ||
| value={value} | ||
| > | ||
| <MenuItem disabled={true} value=""> | ||
| {placeholder} | ||
| </MenuItem> | ||
| {options.map((option) => ( | ||
| <MenuItem key={option.value} value={option.value}> | ||
| {option.label} | ||
| </MenuItem> | ||
| ))} | ||
| </MuiSelect> | ||
| {helperText && <FormHelperText>{helperText}</FormHelperText>} | ||
| </FormControl> | ||
| ) | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.