Canvas: Rating

Rating draws filled ★ and empty ☆ stars. Supports max, color, emptyColor, size.

max:
color:

Grid API

Event Log

Click or hover over Rating cells to see events.

No events yet

Code

import { Grid, createColumnHelper, Rating } from "@ohah/react-wasm-table";

const helper = createColumnHelper<{ score: number }>();

const columns = [
  helper.accessor("score", {
    header: "Rating",
    size: 200,
    cell: (info) => {
      const stars = Math.round((info.getValue() / 100) * 5);
      return <Rating value={stars} max={5} color="#f59e0b" />;
    },
  }),
];

<Grid data={data} columns={columns} width={360} height={460} rowHeight={40} />;

Table API Code

import {
  Table,
  useReactTable,
  flexRender,
  getCoreRowModel,
  Thead,
  Tbody,
  Tr,
  Th,
  Td,
  createColumnHelper,
  Rating,
} from "@ohah/react-wasm-table";

const helper = createColumnHelper<{ score: number }>();

const columns = [
  helper.accessor("score", {
    header: "Rating",
    size: 200,
    cell: (info) => {
      const stars = Math.round((info.getValue() / 100) * 5);
      return <Rating value={stars} max={5} color="#f59e0b" />;
    },
  }),
];

const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel() });

<Table table={table} width={360} height={460} rowHeight={40}>
  <Thead>
    {table.getHeaderGroups().map((hg) => (
      <Tr key={hg.id}>
        {hg.headers.map((h) => (
          <Th key={h.id} colSpan={h.colSpan}>
            {h.isPlaceholder ? null : flexRender(h.column.columnDef.header, h.getContext())}
          </Th>
        ))}
      </Tr>
    ))}
  </Thead>
  <Tbody>
    {table.getRowModel().rows.map((row) => (
      <Tr key={row.id}>
        {row.getVisibleCells().map((cell) => (
          <Td key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</Td>
        ))}
      </Tr>
    ))}
  </Tbody>
</Table>;