ButtonIconCycle.tsx 666 B

12345678910111213141516171819202122232425262728
  1. import clsx from "clsx";
  2. export const ButtonIconCycle = <T extends any>({
  3. options,
  4. value,
  5. onChange,
  6. group,
  7. }: {
  8. options: { value: T; text: string; icon: JSX.Element }[];
  9. value: T | null;
  10. onChange: (value: T) => void;
  11. group: string;
  12. }) => {
  13. const current = options.find((op) => op.value === value);
  14. const cycle = () => {
  15. const index = options.indexOf(current!);
  16. const next = (index + 1) % options.length;
  17. onChange(options[next].value);
  18. };
  19. return (
  20. <label key={group} className={clsx({ active: current!.value !== null })}>
  21. <input type="button" name={group} onClick={cycle} />
  22. {current!.icon}
  23. </label>
  24. );
  25. };