ts-essentials#UnionToIntersection TypeScript Examples

The following examples show how to use ts-essentials#UnionToIntersection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: makeTable.tsx    From ke with MIT License 5 votes vote down vote up
export function makeTable<Components extends TableComponents, Plugin extends TablePlugin>(
  tableComponents: Components,
  plugins: Plugin[] = []
): <T>(
  props: TableProps<
    T,
    TableComponentProps<Components> & { table: UnionToIntersection<PluginProps<Plugin>> },
    { [K in ExtraKey]: UnionToIntersection<PluginExtra<Plugin>[K]> }
  >
) => JSX.Element {
  const {
    table: TableComponent,
    thead: THeadComponent,
    tbody: TBodyComponent,
    headerRow: HeaderRowComponent,
    dataRow: DataRowComponent,
    headerCell: HeaderComponent,
    dataCell: CellComponent,
  } = tableComponents

  const pluginsBefore = compact(plugins.map(({ before }) => before))
  const pluginsAfter = compact(plugins.map(({ after }) => after))

  return (props) => {
    let procProps: TableProps = props
    const allPluginExtraGenerators: ExtraArgGenerators[] = []
    for (let i = 0; i < pluginsBefore.length; i++) {
      const [pluginProps, pluginExtraGenerators] = pluginsBefore[i](procProps)
      procProps = pluginProps
      if (pluginExtraGenerators) {
        allPluginExtraGenerators.push(pluginExtraGenerators)
      }
    }

    const generatorKeys: ExtraKey[] = ['headerRow', 'headerCell', 'dataRow', 'dataCell']
    const compactGenerators = generatorKeys.map(
      (generatorKey) => [generatorKey, compact(byKey(allPluginExtraGenerators, generatorKey))] as const
    )

    const mergedGenerators = Object.fromEntries(
      // TODO: нужно исправить, но пока непонятно как. Очевидно, что необходимо уточнить тип ExtraArgs, так как тм в полях могут быть только словари,
      //  но это аффектит возвращаемы тип makeTable.
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      compactGenerators.map(([key, callbacks]) => [key, partial(arrInvokeAndMerge, callbacks as any)])
    ) as Required<ExtraArgGenerators>

    let procNormalizedProps = normalizeProps(procProps, mergedGenerators)

    for (let i = 0; i < pluginsAfter.length; i++) {
      procNormalizedProps = pluginsAfter[i](procNormalizedProps)
    }

    const { table, thead, tbody } = procNormalizedProps

    return (
      <TableComponent {...table}>
        <THeadComponent>
          <HeaderRowComponent {...thead.row}>
            {thead.cells.map(([key, headerProps]) => (
              <HeaderComponent key={key} {...headerProps} />
            ))}
          </HeaderRowComponent>
        </THeadComponent>
        <TBodyComponent>
          {tbody.map(([rowKey, { row: rowProps, cells }]) => (
            <DataRowComponent key={rowKey} {...rowProps}>
              {cells.map(([cellKey, cellProps]) => (
                <CellComponent key={cellKey} {...cellProps} />
              ))}
            </DataRowComponent>
          ))}
        </TBodyComponent>
      </TableComponent>
    )
  }
}