From e1fc8ea5904de90f94d3f63287555c75067846ac Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 14 Mar 2021 03:03:12 +0300 Subject: feat: add Page component --- src/components/Button.tsx | 11 +++++++++-- src/components/DataTable.tsx | 30 ------------------------------ src/components/ListTable.tsx | 5 +++-- src/containers/Home.tsx | 6 +++--- src/containers/Page.tsx | 25 +++++++++++++++++++++++++ src/containers/Products.tsx | 14 +++++++++----- 6 files changed, 49 insertions(+), 42 deletions(-) delete mode 100644 src/components/DataTable.tsx create mode 100644 src/containers/Page.tsx diff --git a/src/components/Button.tsx b/src/components/Button.tsx index 8f2398b..5724b40 100644 --- a/src/components/Button.tsx +++ b/src/components/Button.tsx @@ -1,6 +1,9 @@ -import React from 'react'; +import React, { useCallback } from 'react'; +import { useHistory } from 'react-router-dom'; interface Props { + onClick?: () => void; + route?: string; variant?: 'contained' | 'outlined'; size?: 'sm' | 'md' | 'lg'; children: string; @@ -18,10 +21,14 @@ const sizes = { sm: 'p-3', }; -const Button: React.FC = ({ variant = 'contained', size = 'md', children }) => { +const Button: React.FC = ({ onClick, route, variant = 'contained', size = 'md', children }) => { + const history = useHistory(); + const navigateRoute = useCallback(() => history.push(route || '/'), [route, history]); + return ( - - - {items.length === 0 &&
No data
} - - ); -}; - -export default DataTable; diff --git a/src/components/ListTable.tsx b/src/components/ListTable.tsx index 9315a27..85b86aa 100644 --- a/src/components/ListTable.tsx +++ b/src/components/ListTable.tsx @@ -6,13 +6,14 @@ interface Field { } interface Props { - items: any[]; + items?: any[]; fields: Field[]; handleRowClick?: (index: number) => void; } -const ListTable: React.FC = ({ items, fields, handleRowClick = () => {} }) => { +const ListTable: React.FC = ({ items = [], fields, handleRowClick = () => {} }) => { + if (!items.length) return
No data
; return ( diff --git a/src/containers/Home.tsx b/src/containers/Home.tsx index 7f62186..f39861a 100644 --- a/src/containers/Home.tsx +++ b/src/containers/Home.tsx @@ -1,15 +1,15 @@ import React from 'react'; -import Paper from '../components/Paper'; +import Page from '../containers/Page'; import Button from '../components/Button'; const Home: React.FC = () => ( - +

Привет, мир!

-
+ ); export default Home; diff --git a/src/containers/Page.tsx b/src/containers/Page.tsx new file mode 100644 index 0000000..0c8269e --- /dev/null +++ b/src/containers/Page.tsx @@ -0,0 +1,25 @@ +import React from 'react'; +import Paper from '../components/Paper'; +import Button from '../components/Button'; + +interface Action { + name: string; + route: string; +} + +interface Props { + title: string; + actions?: Action[]; +} + +const Page: React.FC = ({ title, actions, children }) => ( + +
+ {title} + {actions?.map(action => ())} +
+ {children} +
+); + +export default Page; diff --git a/src/containers/Products.tsx b/src/containers/Products.tsx index 8cd301a..3542df1 100644 --- a/src/containers/Products.tsx +++ b/src/containers/Products.tsx @@ -1,6 +1,6 @@ import React from 'react'; -import Paper from '../components/Paper'; -import DataTable from '../components/DataTable'; +import Page from '../containers/Page'; +import ListTable from '../components/ListTable'; import { useProducts } from '../hooks/useAPIClient'; const fields = [ @@ -9,13 +9,17 @@ const fields = [ { key: 'type', label: 'Тип' }, ]; +const actions = [ + { name: 'Добавить', route: 'products/add' } +]; + const Home: React.FC = () => { const { data: products } = useProducts(); return ( - - - + + + ); }; -- cgit v1.2.3