Calendar in React not displaying correctly with Tailwind CSS.

huangapple go评论62阅读模式
英文:

Calendar in React not displaying correctly with Tailwind CSS

问题

Description: 你好,

我正在尝试使用React和Tailwind CSS创建一个日历,但是日历组件在网页上显示不正确。我已经检查了控制台,并验证了行和列的生成正确,但它们在网页上垂直堆叠。

以下是相关的代码片段:

import React, { useState } from 'react';
import './App.css';
import { getMonth } from './util';
import CalendarHeader from './components/CalendarHeader';
import Sidebar from './components/Sidebar';
import Month from './components/Month';

function App() {
  const [currentMonth, setCurrentMonth] = useState(getMonth());
  return (
    <React.Fragment>
      <div className="h-screen flex flex-columns">
        <CalendarHeader/>
        <div className="flex flex-1">
          <Sidebar/>
          <Month month={currentMonth}/>
        </div>
      </div>
    </React.Fragment>
  );
}

export default App;

import dayjs from 'dayjs';

export function getMonth(month = dayjs().month()){
    const year =dayjs().year()
    const ayinIlkGunu = dayjs(new Date(year, month, 1)).day()
    let currentAySayac = 0 - ayinIlkGunu

    const daysMatrix= new Array(5).fill([]).map(() => {
        return new Array(7).fill(null).map(() => {
            currentAySayac++
            return dayjs(new Date(year, month, currentAySayac))
        })
    })
    return daysMatrix;
}

import React from 'react';
import Day from './Day';

export default function Month({ month }) {
  return (
    <div className="flex-1 grid grid-cols-7 grid-rows-5">
        {month.map((row, i) => (
            <React.Fragment key={i}>
                {row.map((day, idx) => (
                    <Day day={day} key={idx} />   
                ))}
            </React.Fragment>
        ))}
    </div>
  );
}

import React from 'react';

export default function Day({ day }) {
  return (
    <div className="bg-gray-200 p-2">
        {day.format()}
    </div>
  );
}

日历的 Month 组件设计为具有7列和5行的网格结构。然而,在网页上,日历垂直堆叠。

我正在使用Tailwind CSS进行样式设计,并在 Day 组件中添加了 bg-gray-200 p-2 类来为每个日期元素设置样式。

我该如何解决这个问题?提供的代码中是否有任何错误或遗漏的部分?期待您的帮助。

谢谢!

英文:

Description: Hello,

I'm trying to create a calendar in React using Tailwind CSS, but the calendar component is not displaying correctly on the web page. I have checked the console and verified that the rows and columns are generated correctly, but they are stacking vertically on the web page.

Here are the relevant code snippets:

import React, { useState } from &#39;react&#39;;
import &#39;./App.css&#39;;
import { getMonth } from &#39;./util&#39;;
import CalendarHeader from &#39;./components/CalendarHeader&#39;;
import Sidebar from &#39;./components/Sidebar&#39;;
import Month from &#39;./components/Month&#39;;
function App() {
const [currentMonth, setCurrentMonth] = useState(getMonth());
return (
&lt;React.Fragment&gt;
&lt;div className=&quot;h-screen flex flex-columns&quot;&gt;
&lt;CalendarHeader/&gt;
&lt;div className=&quot;flex flex-1&quot;&gt;
&lt;Sidebar/&gt;
&lt;Month month={currentMonth}/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/React.Fragment&gt;
);
}
export default App;
import dayjs from &#39;dayjs&#39;;
export function getMonth(month = dayjs().month()){
const year =dayjs().year()
const ayinIlkGunu = dayjs(new Date(year, month, 1)).day()
let currentAySayac = 0 - ayinIlkGunu
const daysMatrix= new Array(5).fill([]).map(() =&gt; {
return new Array(7).fill(null).map(() =&gt; {
currentAySayac++
return dayjs(new Date(year, month, currentAySayac))
})
})
return daysMatrix;
}
import React from &#39;react&#39;;
import Day from &#39;./Day&#39;;
export default function Month({ month }) {
return (
&lt;div className=&quot;flex-1 grid grid-cols-7 grid-rows-5&quot;&gt;
{month.map((row, i) =&gt; (
&lt;React.Fragment key={i}&gt;
{row.map((day, idx) =&gt; (
&lt;Day day={day} key={idx} /&gt;   
))}
&lt;/React.Fragment&gt;
))}
&lt;/div&gt;
);
}
import React from &#39;react&#39;;
export default function Day({ day }) {
return (
&lt;div className=&quot;bg-gray-200 p-2&quot;&gt;
{day.format()}
&lt;/div&gt;
);
}

The Month component of the calendar is designed to have a grid structure with 7 columns and 5 rows. However, on the web page, the calendar is stacking vertically.

I'm using Tailwind CSS for styling, and I have added the bg-gray-200 p-2 classes to the Day component to style each day element.

How can I resolve this issue? Is there any error or missing piece in the provided code? I'm looking forward to your assistance.

Thank you!

答案1

得分: 0

It's really hard to make Tailwind work in the CodeSandBox it seems... You were missing to import the css in the index.js:

Index.js

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";

import "./styles.css"; // 🔍 Import here

import App from "./App";

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>
);

And create this file on root, beside package.json:

tailwind.config.js

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./src/**/*.{js,jsx,ts,tsx}"],
  theme: {
    extend: {}
  },
  plugins: []
};

In the sandbox it's not gonna work, but if you export to ZIP and run local it will:

Calendar in React not displaying correctly with Tailwind CSS.

英文:

It's really hard to make Tailwind work in the CodeSandBox it seems... You were missing to import the css in the index.js:

Index.js

import { StrictMode } from &quot;react&quot;;
import { createRoot } from &quot;react-dom/client&quot;;
import &quot;./styles.css&quot;; // &#128997; Import here
import App from &quot;./App&quot;;
const rootElement = document.getElementById(&quot;root&quot;);
const root = createRoot(rootElement);
root.render(
&lt;StrictMode&gt;
&lt;App /&gt;
&lt;/StrictMode&gt;
);

And create this file on root, beside package.json:

tailwind.config.js

/** @type {import(&#39;tailwindcss&#39;).Config} */
module.exports = {
content: [&quot;./src/**/*.{js,jsx,ts,tsx}&quot;],
theme: {
extend: {}
},
plugins: []
};

In the sandbox it's not gonna work, but if you export to ZIP and run local it will:

https://codesandbox.io/s/compassionate-bash-lqdd3w?file=/src/index.js

Calendar in React not displaying correctly with Tailwind CSS.

huangapple
  • 本文由 发表于 2023年6月12日 01:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定