-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fuselage, regression): Select filter and anchor overhaul (#654)
- Loading branch information
1 parent
874604a
commit 10ad4f1
Showing
4 changed files
with
128 additions
and
75 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
17 changes: 17 additions & 0 deletions
17
packages/fuselage/src/components/Select/SelectAnchorParams.ts
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 type { | ||
FocusEventHandler, | ||
KeyboardEventHandler, | ||
MouseEventHandler, | ||
ReactNode, | ||
Ref, | ||
} from 'react'; | ||
|
||
export type SelectAnchorParams = { | ||
ref: Ref<Element>; | ||
children: ReactNode; | ||
disabled: boolean; | ||
onClick: MouseEventHandler; | ||
onBlur: FocusEventHandler; | ||
onKeyUp: KeyboardEventHandler; | ||
onKeyDown: KeyboardEventHandler; | ||
}; |
92 changes: 29 additions & 63 deletions
92
packages/fuselage/src/components/Select/SelectFiltered.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 |
---|---|---|
@@ -1,74 +1,40 @@ | ||
import { useMutableCallback } from '@rocket.chat/fuselage-hooks'; | ||
import type { | ||
ComponentProps, | ||
Dispatch, | ||
FormEvent, | ||
Ref, | ||
SetStateAction, | ||
} from 'react'; | ||
import React, { forwardRef, useCallback, useState } from 'react'; | ||
import type { ComponentProps, Dispatch, SetStateAction } from 'react'; | ||
import React, { useState } from 'react'; | ||
|
||
import { Select } from '.'; | ||
import type { Icon } from '..'; | ||
import { InputBox } from '../InputBox'; | ||
import type { SelectAnchorParams } from './SelectAnchorParams'; | ||
import SelectFilteredAnchor from './SelectFilteredAnchor'; | ||
|
||
export type SelectFilteredProps = ComponentProps<typeof Select> & { | ||
filter?: string; | ||
setFilter?: Dispatch<SetStateAction<string>>; | ||
addonIcon?: ComponentProps<typeof Icon>['name']; | ||
}; | ||
|
||
export const SelectFiltered = forwardRef( | ||
( | ||
{ | ||
options, | ||
placeholder, | ||
filter: propFilter, | ||
setFilter: propSetFilter, | ||
...props | ||
}: SelectFilteredProps, | ||
ref: Ref<HTMLInputElement> | ||
) => { | ||
const [filter, setFilter] = useState(''); | ||
const anchor = useCallback( | ||
forwardRef( | ||
( | ||
{ | ||
children: _children, | ||
filter, | ||
...props | ||
}: ComponentProps<typeof InputBox>, | ||
ref: Ref<HTMLInputElement> | ||
) => ( | ||
<InputBox.Input | ||
mi='x4' | ||
flexGrow={1} | ||
className='rcx-select__focus' | ||
ref={ref} | ||
placeholder={placeholder} | ||
value={propFilter || filter} | ||
onChange={useMutableCallback((e: FormEvent<HTMLInputElement>) => | ||
propSetFilter | ||
? propSetFilter(e.currentTarget.value) | ||
: setFilter(e.currentTarget.value) | ||
)} | ||
{...props} | ||
rcx-input-box--undecorated | ||
/> | ||
) | ||
), | ||
[] | ||
); | ||
export const SelectFiltered = ({ | ||
options, | ||
placeholder, | ||
filter: propFilter, | ||
setFilter: propSetFilter, | ||
...props | ||
}: SelectFilteredProps) => { | ||
const [filter, setFilter] = useState(''); | ||
|
||
return ( | ||
<Select | ||
ref={ref} | ||
placeholder={placeholder} | ||
filter={filter} | ||
options={options} | ||
{...props} | ||
anchor={anchor} | ||
/> | ||
); | ||
} | ||
); | ||
return ( | ||
<Select | ||
placeholder={placeholder} | ||
filter={propFilter || filter} | ||
options={options} | ||
{...props} | ||
anchor={(params: SelectAnchorParams) => ( | ||
<SelectFilteredAnchor | ||
placeholder={placeholder} | ||
filter={propFilter || filter} | ||
onChangeFilter={propSetFilter || setFilter} | ||
{...params} | ||
/> | ||
)} | ||
/> | ||
); | ||
}; |
52 changes: 52 additions & 0 deletions
52
packages/fuselage/src/components/Select/SelectFilteredAnchor.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,52 @@ | ||
import type { | ||
FocusEventHandler, | ||
FormEvent, | ||
KeyboardEventHandler, | ||
MouseEventHandler, | ||
ReactNode, | ||
Ref, | ||
} from 'react'; | ||
import React, { forwardRef } from 'react'; | ||
|
||
import { InputBox } from '../InputBox'; | ||
|
||
type SelectFilteredAnchorProps = { | ||
children: ReactNode; | ||
disabled: boolean; | ||
filter: string; | ||
onChangeFilter: (filter: string) => void; | ||
placeholder?: string; | ||
onClick: MouseEventHandler; | ||
onBlur: FocusEventHandler; | ||
onKeyUp: KeyboardEventHandler; | ||
onKeyDown: KeyboardEventHandler; | ||
}; | ||
|
||
const SelectFilteredAnchor = forwardRef(function SelectFilteredAnchor( | ||
{ | ||
children: _children, | ||
filter, | ||
onChangeFilter, | ||
placeholder, | ||
...props | ||
}: SelectFilteredAnchorProps, | ||
ref: Ref<Element> | ||
) { | ||
return ( | ||
<InputBox.Input | ||
mi='x4' | ||
flexGrow={1} | ||
className='rcx-select__focus' | ||
ref={ref} | ||
placeholder={placeholder} | ||
value={filter} | ||
onInput={(e: FormEvent<HTMLInputElement>) => | ||
onChangeFilter(e.currentTarget.value) | ||
} | ||
{...props} | ||
rcx-input-box--undecorated | ||
/> | ||
); | ||
}); | ||
|
||
export default SelectFilteredAnchor; |