36 lines
1.5 KiB
TypeScript
36 lines
1.5 KiB
TypeScript
import React from "react";
|
|
import { Column } from "material-table";
|
|
import { Actions } from "@/components";
|
|
import { Trans } from "react-i18next";
|
|
import { Multilingual } from "@/data";
|
|
|
|
export function actionsColumn<T extends Object>(render: (value: T) => React.ReactNode): Column<T> {
|
|
return {
|
|
title: <Trans i18nKey="management:actions.label" />,
|
|
render: value => <Actions style={{ margin: "-1rem" }} spacing={ 0 }>{ render(value) }</Actions>,
|
|
sorting: false,
|
|
width: 0,
|
|
resizable: false,
|
|
removable: false,
|
|
searchable: false,
|
|
}
|
|
}
|
|
|
|
export function createBoundComponent<T, TBoundProps extends keyof T>(Component: React.ComponentType<T>, bound: Pick<T, TBoundProps>) {
|
|
return (props: Omit<T, TBoundProps>) => <Component { ...bound as any } { ...props } />;
|
|
}
|
|
|
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
export type MultilingualComparator<T> = Comparator<Multilingual<T>>;
|
|
|
|
export function createMultilingualComparator<T>(comparator: Comparator<T>): MultilingualComparator<T> {
|
|
return (a, b) => comparator(a.pl, b.pl);
|
|
}
|
|
|
|
export const multilingualStringComparator = createMultilingualComparator<string>((a, b) => a && b ? a.localeCompare(b) : 0)
|
|
export const multilingualNumberComparator = createMultilingualComparator<number>((a, b) => a - b)
|
|
|
|
export function fieldComparator<T, K extends keyof T>(field: K, comparator: Comparator<T[K]>): Comparator<T> {
|
|
return (a, b) => comparator(a[field], b[field])
|
|
}
|