@material-ui/core#TabsActions TypeScript Examples

The following examples show how to use @material-ui/core#TabsActions. 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: Tabs.tsx    From Teyvat.moe with GNU General Public License v3.0 2 votes vote down vote up
TabBar: FunctionComponent<TabBarProps> = ({
  displayed = true,
  scrollButtons = 'auto',
  icons = false,
  tabs,
  value,
  onChange,
  ...other
}) => {
  const classes = useStyles();

  const [currentValue, setCurrentValue] = useDebouncedState<TabValue>(value, onChange);
  const onValueChange = useCallback(
    (_event, newValue) => setCurrentValue(newValue),
    [setCurrentValue]
  );

  // Fix indicator breaking when tabs dynamically change.
  const theme = useTheme();
  const actionReference = useRef<TabsActions | null>(null);
  useEffect(() => {
    const timeout = setTimeout(() => {
      if (actionReference.current) {
        actionReference.current.updateIndicator();
      }
    }, theme.transitions.duration.enteringScreen);

    return () => {
      clearTimeout(timeout);
    };
  }, [tabs, theme]);

  const renderTab = useCallback(
    (tab) => {
      const label = icons ? <Tooltip title={tab.label}>{tab.icon}</Tooltip> : tab.label;

      return (
        <MaterialTab
          wrapped
          key={tab.value}
          label={label}
          value={tab.value}
          classes={{ root: classes.tab }}
        />
      );
    },
    [icons]
  );

  const renderedTabs = useMemo(() => {
    const sortedTabs = sortByOrder(tabs);

    const result = _.map(sortedTabs, (tab) => {
      if (!tab.enabled) return null;
      return renderTab(tab);
    });

    return result;
  }, [renderTab, tabs]);

  if (!displayed) return null;

  return (
    <MaterialTabs
      action={actionReference}
      value={currentValue}
      onChange={onValueChange}
      centered
      variant="fullWidth"
      scrollButtons={scrollButtons}
      indicatorColor="primary"
      textColor="primary"
      {...other}
    >
      {renderedTabs}
    </MaterialTabs>
  );
}