-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Canvas] Move Templates to be stored as Saved Objects #69438
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { useState, useEffect, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Paginate as Component, PaginateProps, PaginateChildProps } from './paginate'; | ||
|
||
export { PaginateProps, PaginateChildProps }; | ||
export interface InPaginateProps { | ||
perPage?: number; | ||
startPage?: number; | ||
rows: any[]; | ||
children: (props: PaginateChildProps) => React.ReactNode; | ||
} | ||
|
||
export const Paginate: React.FunctionComponent<InPaginateProps> = ({ | ||
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. nit: you can import |
||
perPage = 10, | ||
startPage = 0, | ||
rows, | ||
children, | ||
}) => { | ||
const totalPages = Math.ceil(rows.length / perPage); | ||
const initialCurrentPage = totalPages > 0 ? Math.min(startPage, totalPages - 1) : 0; | ||
const [currentPage, setPage] = useState(initialCurrentPage); | ||
const hasRenderedRef = useRef<boolean>(false); | ||
const maxPage = totalPages - 1; | ||
const start = currentPage * perPage; | ||
const end = currentPage === 0 ? perPage : perPage * (currentPage + 1); | ||
const nextPageEnabled = currentPage < maxPage; | ||
const prevPageEnabled = currentPage > 0; | ||
const partialRows = rows.slice(start, end); | ||
|
||
const nextPage = () => { | ||
if (nextPageEnabled) { | ||
setPage(currentPage + 1); | ||
} | ||
}; | ||
|
||
const prevPage = () => { | ||
if (prevPageEnabled) { | ||
setPage(currentPage - 1); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (!hasRenderedRef.current) { | ||
hasRenderedRef.current = true; | ||
} else { | ||
setPage(0); | ||
} | ||
}, [perPage, hasRenderedRef]); | ||
|
||
return ( | ||
<Component | ||
rows={partialRows} | ||
perPage={perPage} | ||
pageNumber={currentPage} | ||
totalPages={totalPages} | ||
setPage={setPage} | ||
nextPage={nextPage} | ||
prevPage={prevPage} | ||
nextPageEnabled={nextPageEnabled} | ||
prevPageEnabled={prevPageEnabled} | ||
children={children} | ||
/> | ||
); | ||
}; | ||
|
||
Paginate.propTypes = { | ||
rows: PropTypes.array.isRequired, | ||
perPage: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
startPage: PropTypes.number, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
// TODO: We should fully build out this interface for our router | ||
// or switch to a different router that is already typed | ||
interface Router { | ||
navigateTo: ( | ||
name: string, | ||
params: Record<string, number | string>, | ||
state?: Record<string, string> | ||
) => void; | ||
} | ||
|
||
export const RouterContext = React.createContext<Router | undefined>(undefined); |
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.
I'd consider changing this to
childFn
... I was confused about this being a function producing children as opposed to a collection of children.