generated from RedHatInsights/frontend-starter-app
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #961 from dlabrecq/select
Replace deprecated PatternFly select component
- Loading branch information
Showing
13 changed files
with
276 additions
and
113 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
export { default as SelectWrapper, SelectWrapperOption } from './selectWrapper'; |
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,8 @@ | ||
import global_spacer_sm from '@patternfly/react-tokens/dist/js/global_spacer_sm'; | ||
import type React from 'react'; | ||
|
||
export const styles = { | ||
badge: { | ||
marginLeft: global_spacer_sm.var, | ||
}, | ||
} as { [className: string]: React.CSSProperties }; |
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,11 @@ | ||
@import url("~@patternfly/patternfly/base/patternfly-variables.css"); | ||
|
||
// Workaround for missing "position" property | ||
.selectWrapper { | ||
.pf-v5-c-menu-toggle { | ||
max-width: unset; | ||
} | ||
.pf-v5-c-menu-toggle__text { | ||
width: max-content; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/routes/components/selectWrapper/selectWrapper.test.tsx
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,31 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import React from 'react'; | ||
|
||
import { SelectWrapper } from './index'; | ||
|
||
test('primary selector', async () => { | ||
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime }); | ||
const handleOnSelect = jest.fn(); | ||
const selectOptions = [ | ||
{ toString: () => 'CPU', value: 'cpu' }, | ||
{ toString: () => 'Memory', value: 'memory' }, | ||
{ toString: () => 'Storage', value: 'storage' }, | ||
]; | ||
render( | ||
<SelectWrapper | ||
onSelect={(_evt, select) => handleOnSelect(select.value)} | ||
placeholder={'Resources'} | ||
selection={selectOptions[0]} | ||
selectOptions={selectOptions} | ||
/> | ||
); | ||
expect(screen.queryAllByText('CPU').length).toBe(1); | ||
expect(screen.queryAllByText('Memory').length).toBe(0); | ||
const button = screen.getByRole('button'); | ||
await user.click(button); | ||
const options = screen.getAllByRole('option'); | ||
expect(options.length).toBe(3); | ||
await user.click(options[1]); | ||
expect(handleOnSelect.mock.calls).toEqual([['memory']]); | ||
}); |
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,108 @@ | ||
import './selectWrapper.scss'; | ||
|
||
import { Icon, MenuToggle, Select, SelectList, SelectOption } from '@patternfly/react-core'; | ||
import React, { useState } from 'react'; | ||
|
||
export interface SelectWrapperOption { | ||
description?: string; // Option description | ||
compareTo?: (option: SelectWrapperOption) => boolean; | ||
isDisabled?: boolean; | ||
toString?: () => string; // Option label | ||
value?: string; // Option value | ||
} | ||
|
||
interface SelectWrapperOwnProps { | ||
ariaLabel?: string; | ||
className?: string; | ||
id?: string; | ||
isDisabled?: boolean; | ||
onSelect?: (event, value: SelectWrapperOption) => void; | ||
placeholder?: string; | ||
position?: 'right' | 'left' | 'center' | 'start' | 'end'; | ||
selection?: string | SelectWrapperOption; | ||
selectOptions?: SelectWrapperOption[]; | ||
toggleIcon?: React.ReactNode; | ||
} | ||
|
||
type SelectWrapperProps = SelectWrapperOwnProps; | ||
|
||
const SelectWrapper: React.FC<SelectWrapperProps> = ({ | ||
ariaLabel, | ||
className, | ||
id, | ||
isDisabled, | ||
onSelect = () => {}, | ||
placeholder = null, | ||
position, | ||
selection, | ||
selectOptions, | ||
toggleIcon, | ||
}) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
const getSelectOption = (option, index) => { | ||
const isSelected = option.value === (typeof selection === 'string' ? selection : selection?.value); | ||
|
||
return ( | ||
<SelectOption | ||
description={option.description} | ||
isDisabled={option.isDisabled} | ||
isSelected={isSelected} | ||
key={`${option.value}-${index}`} | ||
value={option} | ||
> | ||
{option.toString()} | ||
</SelectOption> | ||
); | ||
}; | ||
|
||
const getPlaceholder = () => { | ||
const label = typeof selection === 'string' ? selection : selection?.toString(); | ||
return label ? label : placeholder; | ||
}; | ||
|
||
const handleOnSelect = (evt, value) => { | ||
onSelect(evt, value); | ||
setIsOpen(false); | ||
}; | ||
|
||
const handleOnToggle = () => { | ||
setIsOpen(!isOpen); | ||
}; | ||
|
||
const toggle = toggleRef => ( | ||
<MenuToggle | ||
icon={toggleIcon && <Icon>{toggleIcon}</Icon>} | ||
isDisabled={isDisabled} | ||
isExpanded={isOpen} | ||
onClick={handleOnToggle} | ||
ref={toggleRef} | ||
> | ||
{getPlaceholder()} | ||
</MenuToggle> | ||
); | ||
|
||
return ( | ||
<div className={className ? `selectWrapper ${className}` : 'selectWrapper'}> | ||
<Select | ||
id={id} | ||
onOpenChange={isExpanded => setIsOpen(isExpanded)} | ||
onSelect={handleOnSelect} | ||
isOpen={isOpen} | ||
popperProps={ | ||
position && { | ||
position, | ||
} | ||
} | ||
selected={selection} | ||
toggle={toggle} | ||
> | ||
<SelectList aria-label={ariaLabel}> | ||
{selectOptions.map((option, index) => getSelectOption(option, index))} | ||
</SelectList> | ||
</Select> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SelectWrapper; |
Oops, something went wrong.