-
Notifications
You must be signed in to change notification settings - Fork 3
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
Filter fields #286
Filter fields #286
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import classNames from 'classnames' | ||
import { forwardRef } from 'react' | ||
import { FormVariant, SearchField, SearchFieldProps } from '../form' | ||
import { FilterOptions, useFilter } from './useFilter' | ||
|
||
export type SearchFilterFieldProps = SearchFieldProps & { | ||
filter: { | ||
value: HTMLInputElement['value'] | ||
setValue: (v: HTMLInputElement['value']) => void | ||
options?: FilterOptions | ||
} | ||
} | ||
|
||
export const SearchFilterField = forwardRef< | ||
HTMLInputElement, | ||
SearchFilterFieldProps | ||
>(function SearchFilterField({ filter, className, ...props }, ref) { | ||
const filterProps = useFilter<HTMLInputElement>()( | ||
filter.value, | ||
filter.setValue, | ||
{ debounce: true, ...filter.options }, | ||
) | ||
|
||
return ( | ||
<SearchField | ||
{...filterProps} | ||
variant={FormVariant.Soft} | ||
className={classNames('w-full grow md:w-auto', className)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if it is a good idea to hardcode the classes here. What's your take on this @lukasvice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is what we always use according to our latest design specification There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but why not use the theme? Note that Tailwind is configured to look for class names only in theme files and not in component files! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad. I didn't understand the comment. Of course I will put them in the theme file |
||
{...props} | ||
ref={ref} | ||
/> | ||
) | ||
}) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we create a story for these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the comment above |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useMatchMediaQuery } from '@aboutbits/react-toolbox' | ||
import classNames from 'classnames' | ||
import { ForwardedRef, forwardRef } from 'react' | ||
import { SelectFieldProps, SelectField, FormVariant } from '../form' | ||
import { LoadingInput } from '../loading' | ||
import { useTheme } from '../../framework' | ||
import { FilterOptions, useFilter } from './useFilter' | ||
|
||
export type HTMLElementWithValue = HTMLElement & { | ||
value: unknown | ||
} | ||
|
||
export type FilterProps< | ||
TElement extends HTMLElementWithValue, | ||
TValue extends TElement['value'], | ||
> = { | ||
filter: { | ||
value: TValue | ||
setValue: (v: TValue) => void | ||
options?: FilterOptions | ||
} | ||
} | ||
|
||
export type SelectFilterFieldProps<TValue extends HTMLSelectElement['value']> = | ||
SelectFieldProps & FilterProps<HTMLSelectElement, TValue> | ||
|
||
function SelectFilterFieldComponent<TValue extends HTMLSelectElement['value']>( | ||
{ filter, className, ...props }: SelectFilterFieldProps<TValue>, | ||
ref: ForwardedRef<HTMLSelectElement>, | ||
) { | ||
const isScreenMedium = useMatchMediaQuery('(min-width: 768px)') | ||
|
||
const filterProps = useFilter<HTMLSelectElement>()( | ||
filter.value, | ||
filter.setValue, | ||
filter.options, | ||
) | ||
|
||
const { | ||
filter: { select: theme }, | ||
} = useTheme() | ||
|
||
return ( | ||
<SelectField | ||
{...filterProps} | ||
variant={isScreenMedium ? FormVariant.Transparent : FormVariant.Soft} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like this we have no change to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only the default. The variant can be overwritten to your liking |
||
className={classNames(theme.normal, className)} | ||
{...props} | ||
ref={ref} | ||
/> | ||
) | ||
} | ||
|
||
/** | ||
* A SelectField configured to be used for filtering a list. | ||
*/ | ||
export const SelectFilterField = forwardRef(SelectFilterFieldComponent) as < | ||
TValue extends HTMLSelectElement['value'], | ||
>( | ||
props: SelectFilterFieldProps<TValue> & { | ||
ref?: ForwardedRef<HTMLSelectElement> | ||
}, | ||
) => ReturnType<typeof SelectFilterFieldComponent> | ||
|
||
export function LoadingSelectFilterField() { | ||
const { | ||
filter: { select: theme }, | ||
} = useTheme() | ||
|
||
return <LoadingInput withLabel={false} className={theme.loading} /> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const className = { | ||
select: { | ||
normal: 'w-full md:w-auto', | ||
loading: 'w-full md:w-40', | ||
}, | ||
} | ||
|
||
export default className |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we create a story for these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are already shown in the List examples: https://286.react-ui.aboutbits.dev/?path=/story/examples-list--list-with-filter