Skip to content

Instantly share code, notes, and snippets.

@SanichKotikov
Created November 27, 2020 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SanichKotikov/339d421ff194e24a6da6bb0192944418 to your computer and use it in GitHub Desktop.
Save SanichKotikov/339d421ff194e24a6da6bb0192944418 to your computer and use it in GitHub Desktop.
import React, {useEffect, useState, useCallback} from 'react';
import Sticky from 'react-stickynode';
type TFilter = {
name: string;
value: string;
component: React.ReactElement;
};
interface IProps {
items: TFilter[];
fetch: () => Promise<void>;
change: (name: string, value: string) => void;
}
export const StickyFilters = ({items, fetch, change}: IProps) => {
useEffect(() => {
fetch();
});
const [count, updCount] = useState(0);
const onChange = useCallback((name: string, value: string) => {
updCount(count + 1);
change(name, value);
}, [count]);
return (
<Sticky top={0} enableTransforms={false}>
<section>
<h3>Filters</h3>
<div>
{items.sort((a, b) => a.name.localeCompare(b.name)).map((item, idx) => {
const Comp = item.component;
return (
<div key={idx}>
<label>{item.name}</label>
<Comp data={item} onChange={onChange} />
</div>
);
})}
</div>
<div>{count}</div>
</section>
</Sticky>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment