From 35c52e968eb95fd6adaf9757b18ebc7187f8e400 Mon Sep 17 00:00:00 2001 From: Martin Malfertheiner Date: Fri, 5 Nov 2021 13:54:24 +0100 Subject: [PATCH] add documentation of Section Filter --- .../sectionFilter/SectionFilter.stories.mdx | 134 ++++++++++++++++++ .../sectionFilter/SectionFilter.tsx | 15 +- 2 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 src/components/sectionFilter/SectionFilter.stories.mdx diff --git a/src/components/sectionFilter/SectionFilter.stories.mdx b/src/components/sectionFilter/SectionFilter.stories.mdx new file mode 100644 index 00000000..f9b33c64 --- /dev/null +++ b/src/components/sectionFilter/SectionFilter.stories.mdx @@ -0,0 +1,134 @@ +import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs' +import { useState } from 'react' +import { action } from '@storybook/addon-actions' +import { + Section, + SectionContentList, + SectionHeader, + SectionListItemButton, + SectionTitle, +} from '../section' +import { Select } from '../form' +import { SectionFilter } from './SectionFilter' +import { SectionFilterAction } from './SectionFilterAction' + + ( +
+ +
+ ), + ]} +/> + +# SectionFilter + +You can use the SectionFilter component to create a filter on a specific Section. +It supports two modes out off the box. The popup mode shows the filter in a popup, which is very convenient on mobile devices, whereas the drop under mode shows the filter in a collapsible fashion. + +This component is based on two libraries that you need to install in your project as well: + +``` +npm i @reach/dialog @aboutbits/react-toolbox +``` + +export const Template = () => { + const [filter, setFilter] = useState({ role: '', department: '' }) + const [filterShow, setFilterShow] = useState(false) + const content = Array.from(Array(10).keys()) + .map((item, index) => ({ + name: `User ${item + 1}`, + role: index % 8 === 0 ? 'ADMIN' : 'USER', + department: index % 3 === 0 ? 'HR' : 'SALES', + })) + .filter((item) => { + return ( + (filter.role === '' || item.role === filter.role) && + (filter.department === '' || item.department === filter.department) + ) + }) + return ( + <> + + Users + { + setFilterShow(!filterShow) + }} + isFiltering={filter.department !== '' || filter.role !== ''} + /> + + {filterShow && ( + { + console.log({ values }) + setFilter(values) + }} + onClose={() => setFilterShow(false)} + confirmationButtonContent="Apply filter" + > + + + + )} + + {content.map((item) => ( + action('clicked')} + > + {`${item.name} (${item.role} - ${item.department})`} + + ))} + + + ) +} + + + {Template.bind({})} + + +### Props + + + +### Theme + +- `section.filter.trigger.base`: styles of the indicator of applied filters +- `section.filter.container.base`: base styles for the pop under form container +- `section.filter.container.normal`: color styles for the pop under form container +- `section.filter.popup.base`: base styles for the dialog form container +- `section.filter.popup.normal`: color styles for the dialog form container diff --git a/src/components/sectionFilter/SectionFilter.tsx b/src/components/sectionFilter/SectionFilter.tsx index 9eb97c1e..520f14d1 100644 --- a/src/components/sectionFilter/SectionFilter.tsx +++ b/src/components/sectionFilter/SectionFilter.tsx @@ -1,6 +1,12 @@ import classNames from 'classnames' import { Form, Formik, useFormikContext } from 'formik' -import { ReactChildren, ReactElement, ReactNode, useEffect } from 'react' +import { + ReactChildren, + ReactElement, + ReactNode, + useEffect, + useRef, +} from 'react' import { useMatchMediaQuery } from '@aboutbits/react-toolbox' import { useTheme } from '../../framework' import { ClassNameProps } from '../types' @@ -40,13 +46,16 @@ type Props = ClassNameProps & { function SubmitOnChange(): null { const formik = useFormikContext() + const initialRender = useRef(true) useEffect(() => { - if (formik.isValid && formik.dirty) { + if (initialRender.current) { + initialRender.current = false + } else if (formik.isValid) { formik.submitForm() } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [formik.isValid, formik.dirty, formik.values]) + }, [formik.isValid, formik.values]) return null }