Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible styling #47

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/DisplayMonths.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const DisplayMonths = ({
);
});
};
return <Wrapper jalali={jalali}>{renderMonths()}</Wrapper>;
return <Wrapper className="tp-calendar-month-select" jalali={jalali}>{renderMonths()}</Wrapper>;
};

type StyleProps = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/DisplayYears.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const DisplayYears = ({ setDisplayYears, setSource }: Props) => {
);
}

return <Wrapper ref={ref}> {years} </Wrapper>;
return <Wrapper className="tp-calendar-year-select" ref={ref}> {years} </Wrapper>;
};

const Wrapper = styled.div`
Expand Down
1 change: 1 addition & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const Header = ({

return (
<Wrapper
className="tp-calendar-header"
numberOfMonths={numberOfMonths}
jalali={jalali}
displayMonths={displayMonths}
Expand Down
2 changes: 1 addition & 1 deletion src/components/TitleOfWeek.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const TitleOfWeek: React.FunctionComponent<Props> = ({
}

return (
<Wrapper jalali={jalali} startOfWeek={startOfWeek}>
<Wrapper className="tp-calendar-week-titles" jalali={jalali} startOfWeek={startOfWeek}>
{titles.map(item => (
<p key={item}>{item}</p>
))}
Expand Down
2 changes: 2 additions & 0 deletions src/constant/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DefaultTheme } from "styled-components";
import { ElementType, ReactNode } from "react";
import { Dayjs } from "dayjs";

type HeaderIconsPosition = {
right: ReactNode;
Expand Down Expand Up @@ -35,6 +36,7 @@ export interface InitialProps {
numberOfMonths?: number;
initialMonthAndYear?: string;
onRangeDateInScreen?: DatePickerWindowUpdated;
dayClasses?: (day: Dayjs) => string[];
Copy link
Owner

@samsam-ahmadi samsam-ahmadi Aug 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great, @coling , Thank you so much for your great feature.

Before we are going to merge it, can we have a specific type for dayClasses and export it and use it in all places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout. Will get that sorted at some point soon (for various values of "soon" 😛 )

}

type DatePickerWindow = { start: string; end: string };
Expand Down
2 changes: 2 additions & 0 deletions src/datePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DatePicker = ({
onChange,
initialMonthAndYear,
onRangeDateInScreen,
dayClasses,
}: DatePickerProps) => {
const [selectedDays, setSelectedDays] = useState(selectedDaysProps || []);
const [numberOfMonths, setNumberOfMonths] = useState(numberOfMonthsProps);
Expand Down Expand Up @@ -129,6 +130,7 @@ export const DatePicker = ({
disabledBeforeDate={disabledBeforeDate}
disabledBeforeToday={disabledBeforeToday}
numberOfSelectableDays={numberOfSelectableDays}
dayClasses={dayClasses}
/>
)}
</ThemeProvider>
Expand Down
8 changes: 7 additions & 1 deletion src/datePicker/Day.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Props = {
components?: DatePickerComponents;
onChange: DatePickerOnChange;
setSelectedDays: Dispatch<SetStateAction<string[]>>;
dayClasses?: (day: Dayjs) => string[];
};

export const Day: React.FC<Props> = ({
Expand All @@ -40,6 +41,7 @@ export const Day: React.FC<Props> = ({
numberOfSelectableDays,
disabledBeforeDate,
disabledAfterDate,
dayClasses,
}) => {
if (disabledBeforeToday) {
const today = dayjs().format(FORMAT_DATE);
Expand Down Expand Up @@ -134,6 +136,10 @@ export const Day: React.FC<Props> = ({
};

const DayComponent = components?.days;
let extraDayClasses = "";
if (dayClasses) {
extraDayClasses = dayClasses(day).join(" ");
}
return (
<Wrapper
data-test={day.format(FORMAT_DATE)}
Expand All @@ -147,7 +153,7 @@ export const Day: React.FC<Props> = ({
dayjs()
.calendar(jalali ? "jalali" : "gregory")
.format(FORMAT_DATE) === day.format(FORMAT_DATE),
})}
}, extraDayClasses, "tp-calendar-day")}
>
{DayComponent && (
<DayComponent day={day.format(FORMAT_DATE)} jalali={jalali} />
Expand Down
7 changes: 6 additions & 1 deletion src/datePicker/Months.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface Props {
components?: DatePickerComponents;
setSource: Dispatch<SetStateAction<Dayjs>>;
setSelectedDays: Dispatch<SetStateAction<string[]>>;
dayClasses?: (day: Dayjs) => string[];
}

export const Months = ({
Expand All @@ -41,6 +42,7 @@ export const Months = ({
disabled,
onChange,
source: sourceProp,
dayClasses,
}: Props) => {
const renderMonths = () => {
let months = [];
Expand All @@ -55,6 +57,7 @@ export const Months = ({
);
months.push(
<Month
className="tp-calendar-month"
numberOfMonths={numberOfMonths || 1}
key={dayjs().set("month", 1).set("day", 1).diff(source, "d")}
data-test={dayjs().set("month", 1).set("day", 1).diff(source, "d")}
Expand All @@ -66,6 +69,7 @@ export const Months = ({
/>
{weeksDays.map(week => (
<Weeks
className="tp-calendar-week"
jalali={jalali}
data-test={dayjs()
.set("month", 1)
Expand All @@ -90,6 +94,7 @@ export const Months = ({
disabledBeforeDate={disabledBeforeDate}
disabledAfterDate={disabledAfterDate}
numberOfSelectableDays={numberOfSelectableDays}
dayClasses={dayClasses}
/>
))}
</Weeks>
Expand All @@ -99,5 +104,5 @@ export const Months = ({
}
return months;
};
return <Wrapper jalali={jalali}>{renderMonths()}</Wrapper>;
return <Wrapper className="tp-calendar-months" jalali={jalali}>{renderMonths()}</Wrapper>;
};
8 changes: 7 additions & 1 deletion src/rangePicker/Day.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface Props {
setSelectedDays: Dispatch<
SetStateAction<RangePickerSelectedDays | undefined>
>;
dayClasses?: (day: Dayjs) => string[];
}

export const Day = ({
Expand All @@ -52,6 +53,7 @@ export const Day = ({
disabledBeforeToday,
disabledBeforeDate,
disabledAfterDate,
dayClasses,
}: Props) => {
if (disabledBeforeToday) {
const today = dayjs().format(FORMAT_DATE);
Expand Down Expand Up @@ -199,6 +201,10 @@ export const Day = ({
};

const DayComponent = components?.days;
let extraDayClasses = "";
if (dayClasses) {
extraDayClasses = dayClasses(day).join(" ");
}
return (
<Wrapper
data-test={day.format(FORMAT_DATE)}
Expand Down Expand Up @@ -228,7 +234,7 @@ export const Day = ({
dayjs()
.calendar(jalali ? "jalali" : "gregory")
.format(FORMAT_DATE) === day.format(FORMAT_DATE),
})}
}, extraDayClasses, "tp-calendar-day")}
>
{DayComponent && (
<DayComponent day={day.format(FORMAT_DATE)} jalali={jalali} />
Expand Down
7 changes: 6 additions & 1 deletion src/rangePicker/Months.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface Props {
setSelectedDays: Dispatch<
SetStateAction<RangePickerSelectedDays | undefined>
>;
dayClasses?: (day: Dayjs) => string[];
}

export const Months = ({
Expand All @@ -52,6 +53,7 @@ export const Months = ({
disabled,
onChange,
source: sourceProp,
dayClasses,
}: Props) => {
const renderMonths = () => {
let months = [];
Expand All @@ -66,6 +68,7 @@ export const Months = ({
);
months.push(
<Month
className="tp-calendar-month"
numberOfMonths={numberOfMonths || 1}
key={dayjs().set("month", 1).set("day", 1).diff(source, "d")}
data-test={dayjs().set("month", 1).set("day", 1).diff(source, "d")}
Expand All @@ -77,6 +80,7 @@ export const Months = ({
/>
{weeksDays.map(week => (
<Weeks
className="tp-calendar-week"
jalali={jalali}
data-test={dayjs()
.set("month", 1)
Expand All @@ -103,6 +107,7 @@ export const Months = ({
allowDisabledDaysSpan={allowDisabledDaysSpan}
disabledBeforeDate={disabledBeforeDate}
disabledAfterDate={disabledAfterDate}
dayClasses={dayClasses}
/>
))}
</Weeks>
Expand All @@ -113,5 +118,5 @@ export const Months = ({
return months;
};

return <Wrapper jalali={jalali}>{renderMonths()}</Wrapper>;
return <Wrapper className="tp-calendar-months" jalali={jalali}>{renderMonths()}</Wrapper>;
};
2 changes: 2 additions & 0 deletions src/rangePicker/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const RangePicker = ({
onChange,
initialMonthAndYear,
onRangeDateInScreen,
dayClasses,
}: RangePickerProps) => {
const [selectedDays, setSelectedDays] = useState(selectedDaysProps);
const [hoverDay, setHoverDay] = useState<string>();
Expand Down Expand Up @@ -130,6 +131,7 @@ export const RangePicker = ({
disabledBeforeDate={disabledBeforeDate}
disabledAfterDate={disabledAfterDate}
allowDisabledDaysSpan={allowDisabledDaysSpan}
dayClasses={dayClasses}
/>
)}
</ThemeProvider>
Expand Down
17 changes: 17 additions & 0 deletions src/stories/DatePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ stories.add("Selected Days", () => {
);
});

stories.add("Custom Day Classes", () => {
return (
<>
<style type="text/css">{`
.odd { transform: rotate(-10deg); } .even { transform: rotate(10deg); }
`}</style>
<DatePicker
numberOfMonths={1}
autoResponsive={false}
initialMonthAndYear="2021-08"
onChange={dates => window.console.log(dates)}
dayClasses={day => [parseInt(day.format('D')) % 2 == 1 ? 'odd' : 'even' ]}
/>
</>
);
});

stories.add("Number of Selectable Days", () => {
return (
<DatePicker
Expand Down
17 changes: 17 additions & 0 deletions src/stories/RangePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ stories.add("Allow Disabled Days Span", () => {
);
});

stories.add("Custom Day Classes", () => {
return (
<>
<style type="text/css">{`
.odd { transform: rotate(-10deg); } .even { transform: rotate(10deg); }
`}</style>
<RangePicker
numberOfMonths={1}
autoResponsive={false}
initialMonthAndYear="2021-08"
onChange={dates => window.console.log(dates)}
dayClasses={day => [parseInt(day.format('D')) % 2 == 1 ? 'odd' : 'even' ]}
/>
</>
);
});

stories.add("Custom components - Title of weeks Component", () => {
return (
<RangePicker
Expand Down