This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 833
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Device manager - updated dropdown style in filtered device list (PSG-…
…689) (#9226) * add FilterDropdown wrapper on Dropdown for filter styles * test and fix strict errors * fix comment
- Loading branch information
Kerry
authored
Aug 30, 2022
1 parent
825a0af
commit 50f6986
Showing
10 changed files
with
389 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
.mx_FilterDropdown { | ||
.mx_Dropdown_menu { | ||
margin-top: $spacing-4; | ||
left: unset; | ||
right: -$spacing-12; | ||
width: 232px; | ||
|
||
border: 1px solid $quinary-content; | ||
border-radius: 8px; | ||
box-shadow: 0px 1px 3px rgba(23, 25, 28, 0.05); | ||
|
||
.mx_Dropdown_option_highlight { | ||
background-color: $system; | ||
} | ||
} | ||
|
||
.mx_Dropdown_input { | ||
height: 24px; | ||
background-color: $quinary-content; | ||
border-color: $quinary-content; | ||
color: $secondary-content; | ||
border-radius: 4px; | ||
|
||
&:focus { | ||
border-color: $quinary-content; | ||
} | ||
} | ||
|
||
.mx_Dropdown_arrow { | ||
background: $secondary-content; | ||
} | ||
} | ||
|
||
.mx_FilterDropdown_option { | ||
position: relative; | ||
width: 100%; | ||
box-sizing: border-box; | ||
padding: $spacing-8 0 $spacing-8 $spacing-20; | ||
|
||
font-size: $font-12px; | ||
line-height: $font-15px; | ||
color: $primary-content; | ||
} | ||
|
||
.mx_FilterDropdown_optionSelectedIcon { | ||
height: 14px; | ||
width: 14px; | ||
position: absolute; | ||
top: $spacing-8; | ||
left: 0; | ||
} | ||
|
||
.mx_FilterDropdown_optionLabel { | ||
font-weight: $font-semi-bold; | ||
display: block; | ||
} | ||
|
||
.mx_FilterDropdown_optionDescription { | ||
color: $secondary-content; | ||
margin-top: $spacing-4; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,86 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import { Icon as CheckmarkIcon } from '../../../../res/img/element-icons/roomlist/checkmark.svg'; | ||
import Dropdown, { DropdownProps } from './Dropdown'; | ||
|
||
export type FilterDropdownOption<FilterKeysType extends string> = { | ||
id: FilterKeysType; | ||
label: string; | ||
description?: string; | ||
}; | ||
type FilterDropdownProps<FilterKeysType extends string> = Omit<DropdownProps, 'children'> & { | ||
value: FilterKeysType; | ||
options: FilterDropdownOption<FilterKeysType>[]; | ||
// A label displayed before the selected value | ||
// in the dropdown input | ||
selectedLabel?: string; | ||
}; | ||
|
||
const getSelectedFilterOptionComponent = <FilterKeysType extends string>( | ||
options: FilterDropdownOption<FilterKeysType>[], selectedLabel?: string, | ||
) => (filterKey: FilterKeysType) => { | ||
const option = options.find(({ id }) => id === filterKey); | ||
if (!option) { | ||
return null; | ||
} | ||
if (selectedLabel) { | ||
return `${selectedLabel}: ${option.label}`; | ||
} | ||
return option.label; | ||
}; | ||
|
||
/** | ||
* Dropdown styled for list filtering | ||
*/ | ||
export const FilterDropdown = <FilterKeysType extends string = string>( | ||
{ | ||
value, | ||
options, | ||
selectedLabel, | ||
className, | ||
...restProps | ||
}: FilterDropdownProps<FilterKeysType>, | ||
): React.ReactElement<FilterDropdownProps<FilterKeysType>> => { | ||
return <Dropdown | ||
{...restProps} | ||
value={value} | ||
className={classNames('mx_FilterDropdown', className)} | ||
getShortOption={getSelectedFilterOptionComponent<FilterKeysType>(options, selectedLabel)} | ||
> | ||
{ options.map(({ id, label, description }) => | ||
<div | ||
className='mx_FilterDropdown_option' | ||
data-testid={`filter-option-${id}`} | ||
key={id} | ||
> | ||
{ id === value && <CheckmarkIcon className='mx_FilterDropdown_optionSelectedIcon' /> } | ||
<span className='mx_FilterDropdown_optionLabel'> | ||
{ label } | ||
</span> | ||
{ | ||
!!description | ||
&& <span | ||
className='mx_FilterDropdown_optionDescription' | ||
>{ description }</span> | ||
} | ||
</div>, | ||
) } | ||
</Dropdown>; | ||
}; |
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,68 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { act, fireEvent, render } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { FilterDropdown } from '../../../../src/components/views/elements/FilterDropdown'; | ||
import { flushPromises, mockPlatformPeg } from '../../../test-utils'; | ||
|
||
mockPlatformPeg(); | ||
|
||
describe('<FilterDropdown />', () => { | ||
const options = [ | ||
{ id: 'one', label: 'Option one' }, | ||
{ id: 'two', label: 'Option two', description: 'with description' }, | ||
]; | ||
const defaultProps = { | ||
className: 'test', | ||
value: 'one', | ||
options, | ||
id: 'test', | ||
label: 'test label', | ||
onOptionChange: jest.fn(), | ||
}; | ||
const getComponent = (props = {}): JSX.Element => | ||
(<FilterDropdown {...defaultProps} {...props} />); | ||
|
||
const openDropdown = async (container: HTMLElement): Promise<void> => await act(async () => { | ||
const button = container.querySelector('[role="button"]'); | ||
expect(button).toBeTruthy(); | ||
fireEvent.click(button as Element); | ||
await flushPromises(); | ||
}); | ||
|
||
it('renders selected option', () => { | ||
const { container } = render(getComponent()); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders when selected option is not in options', () => { | ||
const { container } = render(getComponent({ value: 'oops' })); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders selected option with selectedLabel', () => { | ||
const { container } = render(getComponent({ selectedLabel: 'Show' })); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders dropdown options in menu', async () => { | ||
const { container } = render(getComponent()); | ||
await openDropdown(container); | ||
expect(container.querySelector('.mx_Dropdown_menu')).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.