|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import React from 'react'; |
| 5 | + |
| 6 | +interface PermutationsViewProps<T> { |
| 7 | + permutations: ReadonlyArray<T>; |
| 8 | + render: (props: T, index?: number) => React.ReactElement; |
| 9 | +} |
| 10 | + |
| 11 | +function formatValue(key: string, value: any) { |
| 12 | + if (typeof value === 'function') { |
| 13 | + return value.toString(); |
| 14 | + } |
| 15 | + if (value && value.$$typeof) { |
| 16 | + // serialize React content to string |
| 17 | + // TODO: Pretty-print original JSX, currently this are bare VDOM nodes |
| 18 | + return JSON.stringify(value); |
| 19 | + } |
| 20 | + return value; |
| 21 | +} |
| 22 | + |
| 23 | +const maximumPermutations = 276; |
| 24 | + |
| 25 | +export default function PermutationsView<T>({ permutations, render }: PermutationsViewProps<T>) { |
| 26 | + if (permutations.length > maximumPermutations) { |
| 27 | + throw new Error(`Too many permutations (${permutations.length}), maximum is ${maximumPermutations}`); |
| 28 | + } |
| 29 | + |
| 30 | + return ( |
| 31 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '16px' }}> |
| 32 | + {permutations.map((permutation, index) => { |
| 33 | + const id = JSON.stringify(permutation, formatValue); |
| 34 | + return ( |
| 35 | + <div key={id} data-permutation={id}> |
| 36 | + {render(permutation, index)} |
| 37 | + </div> |
| 38 | + ); |
| 39 | + })} |
| 40 | + </div> |
| 41 | + ); |
| 42 | +} |
0 commit comments