我在创造什么?

  • 我正在创建一个可水平滚动的选项卡组件,用于 Select 一个月的日期
  • 我用的是MUI library美元

问题

  • 网站上没有显示14th Sun年前的日期.(如下图所示)
  • 第一个标签应该是每月的第一天.相反,它是14th Sun
  • 在浏览器元素选项卡中,所有选项卡都在那里.但他们并没有出现在屏幕上

移动视区(不能在第14天之前滚动;从那里开始)

mobile viewport

桌面视窗(第7天之前不能滚动)

desktop viewport

元素选项卡

元素选项卡

我认为部分代码就是问题所在

export default function DayScrollView() {
  const [activeDate, setActiveDate] = useContext(dateContext)
  const [dates, setDates] = useState(getDatesInMonth(activeDate))
  return (
    <Box sx={
      {
        overflowX: 'scroll',
        scrollbarWidth: 'none',
        '-ms-overflow-style': 'none',
        '&::-webkit-scrollbar': {
          display: 'none',
        },
      }
    }>
      <Tabs defaultValue={0}>
        <StyledTabsList>
          {dates.map((date, index) => {
            console.log(`${date.getDate()} ${date.toLocaleString('default', { weekday: 'short' })}`)
            return (
              <StyledTab key={date.toDateString()} value={date.toDateString()} onClick={() => setActiveDate(date)}>
                <Typography variant='subtitle1'>{date.toLocaleString('default', { weekday: 'short' })}</Typography>
                <Typography variant='h5'>{date.getDate()}</Typography>
              </StyledTab>
            )
          })}
        </StyledTabsList>
      </Tabs>
      <Box mr={10} sx={{ width: 20 }} />
    </Box>
  );
}

完整组件代码

import { useContext, useState } from 'react';
import * as React from 'react';
import { styled } from '@mui/system';
import Tabs from '@mui/base/Tabs';
import TabsList from '@mui/base/TabsList';
import TabPanel from '@mui/base/TabPanel';
import { buttonClasses } from '@mui/base/Button';
import Tab, { tabClasses } from '@mui/base/Tab';
import { Box, Typography } from '@mui/material';
import getDatesInMonth from '@/utils/getDatesInMonth';
import { dateContext } from '@/pages/mob';

export default function DayScrollView() {
  const [activeDate, setActiveDate] = useContext(dateContext)
  const [dates, setDates] = useState(getDatesInMonth(activeDate))
  return (
    <Box sx={
      {
        overflowX: 'scroll',
        scrollbarWidth: 'none',
        '-ms-overflow-style': 'none',
        '&::-webkit-scrollbar': {
          display: 'none',
        },
      }
    }>
      <Tabs defaultValue={0}>
        <StyledTabsList>
          {dates.map((date, index) => {
            console.log(date)
            return (
              <StyledTab key={index} value={index} onClick={() => setActiveDate(date)}>
                <Typography variant='subtitle1'>{date.toLocaleString('default', { weekday: 'short' })}</Typography>
                <Typography variant='h5'>{date.getDate()}</Typography>
              </StyledTab>
            )
          })}
        </StyledTabsList>
      </Tabs>
      <Box mr={10} sx={{ width: 20 }} />
    </Box>
  );
}

const StyledTab = styled(Tab)`
.... some css styles
`;

const StyledTabPanel = styled(TabPanel)(
  ({ theme }) => `
  width: 100%;
  ... more styles
  `,
);

const StyledTabsList = styled(TabsList)(
  ({ theme }) => `
  padding-right: 1rem;
  background: transparent;
  display: flex;
  flex-grow: 1;
  align-items: center;
  justify-content: center;
  align-content: space-between;
`,
);

GetDatesInMonth()

function getDatesInMonth(date) {
    const year = date.getFullYear();
    const month = date.getMonth();
    const numDays = new Date(year, month + 1, 0).getDate();
    const dates = [];
    for (let i = 1; i <= numDays; i++) {
        dates.push(new Date(year, month, i));
    }
    return dates;
}
  • 如果需要更多信息,请在 comments 中写下.
  • 如果你认识可以帮助你的人,请投上赞成票并分享这个问题

推荐答案

以下是一个关于css弹性框的问题.

try 以下操作:

const StyledTabsList = styled(TabsList)(
  ({ theme }) => `
  padding-right: 1rem;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  align-content: center;
  `,
);

已更改:

  • 更改了对齐-内容:居中对齐-内容:弹性开始;

Css相关问答推荐

以百分比形式输入的css宽度属性不会生效

当具有外部高度的网格项溢出时,为什么溢出滚动端口填充不应用于网格容器?

使用高图的背景大小渐变

媒体查询在生产中没有响应:React 18 应用程序

:not() 适用于特定类中的所有 html 标签

如何为图像堆叠效果下方的图层实现与磨砂玻璃类似的视觉效果

SVG样式中的CSS类名是否是全局的?

我如何使用 CSS 在我的应用程序中每隔 3 位用逗号分隔数字?

基于高度的容器查询不起作用

在 dbc.Container 上方包含横幅 - Dash

在嵌套的弹性框中创建弹性项目之间的空间

如何在 nth-child 中正确传递 CSS 变量?

内联块在 Internet Explorer 7、6 中不起作用

css 单行或多行垂直对齐

IE划掉伪元素CSS?

CSS Select /样式第一个词

我应该如何组织我的 CSS 文件的内容?

单选按钮和标签显示在同一行

为什么 CSS 中的光标:指针效果不起作用

如何应用 CSS 分页符来打印包含大量行的表格?