| 12 | } |
| 13 | |
| 14 | function ErrorOverlayInternal(props: { |
| 15 | render: s.JSX.Element | ((props: HeadlessErrorOverlayRenderProps) => s.JSX.Element) |
| 16 | errors: unknown[] |
| 17 | resetError: () => void |
| 18 | }): s.JSX.Element { |
| 19 | const [currentPage, setCurrentPage] = s.createSignal(1) |
| 20 | |
| 21 | const length = s.createMemo(() => props.errors.length) |
| 22 | const currentError = s.createMemo(() => props.errors[currentPage() - 1]) |
| 23 | |
| 24 | s.createComputed((currentLength: number) => { |
| 25 | const newLength = length() |
| 26 | if (currentLength < newLength) { |
| 27 | setCurrentPage(current => current + 1) |
| 28 | } |
| 29 | return newLength |
| 30 | }, length()) |
| 31 | |
| 32 | function goPrev(): void { |
| 33 | setCurrentPage(c => { |
| 34 | if (c > 1) { |
| 35 | return c - 1 |
| 36 | } |
| 37 | return length() |
| 38 | }) |
| 39 | } |
| 40 | function goNext(): void { |
| 41 | setCurrentPage(c => { |
| 42 | if (c < length()) { |
| 43 | return c + 1 |
| 44 | } |
| 45 | return 1 |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | const {render} = props |
| 50 | |
| 51 | if (typeof render === 'function') { |
| 52 | return s.untrack(() => |
| 53 | render({ |
| 54 | goPrev, |
| 55 | goNext, |
| 56 | resetError: props.resetError, |
| 57 | get currentCount() { |
| 58 | return currentPage() |
| 59 | }, |
| 60 | get maxCount() { |
| 61 | return length() |
| 62 | }, |
| 63 | get error() { |
| 64 | return currentError() |
| 65 | }, |
| 66 | }), |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | return render |
| 71 | } |