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

chore: simplify examples, extract duplicate code #178

Merged
merged 3 commits into from
Dec 4, 2020
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
33 changes: 32 additions & 1 deletion docs-templates/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,38 @@ keyboard navigaiton & focus management.

### Range Calendar

<!-- IMPORT_EXAMPLE src/calendar/stories/__js/CalendarRange.component.jsx -->
Converting a normal calendar to a range calendar is as easy as just swaping out
the hook to range calendar hook.

You'll need to import the `useRangeCalendarState` hook from the
`@renderlesskit/react` first

```diff
- const state = useCalendarState(props);
+ const state = useRangeCalendarState(props);
```

Also we can customize and style the ranges with CSS attribute selectors

```css
[data-is-range-selection] > span {
/* styles for any cells between start-end (inclusive) */
}
[data-is-selection-start] > span {
/* styles for first selected range cell */
}
[data-is-selection-end] > span {
/* styles for end selected range cell */
}

/* only applied if cell date is first or last of the month*/
[data-is-range-start] > span {
/**/
}
[data-is-range-end] > span {
/**/
}
```

<!-- CODESANDBOX
link_title: Calendar - Open On Sandbox
Expand Down
222 changes: 49 additions & 173 deletions docs/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ import {
CalendarWeekTitle,
} from "@renderlesskit/react";

import {
ChevronLeft,
ChevronRight,
DoubleChevronLeft,
DoubleChevronRight,
} from "./Utils.component";

export const App = props => {
const state = useCalendarState(props);

Expand Down Expand Up @@ -97,177 +104,46 @@ export const App = props => {
};

export default App;

const DoubleChevronLeft = props => {
return (
<svg
{...props}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M15 19l-7-7 7-7"
/>
</svg>
);
};

const ChevronLeft = props => {
return (
<svg
{...props}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M11 19l-7-7 7-7m8 14l-7-7 7-7"
/>
</svg>
);
};

const ChevronRight = props => (
<ChevronLeft style={{ transform: "rotate(180deg)" }} {...props} />
);

const DoubleChevronRight = props => (
<DoubleChevronLeft style={{ transform: "rotate(180deg)" }} {...props} />
);
```

### Range Calendar

```js
import React from "react";
Converting a normal calendar to a range calendar is as easy as just swaping out
the hook to range calendar hook.

import {
Calendar,
CalendarCell,
CalendarGrid,
CalendarHeader,
CalendarButton,
CalendarCellButton,
CalendarWeekTitle,
useRangeCalendarState,
} from "@renderlesskit/react";
You'll need to import the `useRangeCalendarState` hook from the
`@renderlesskit/react` first

export const App = props => {
const state = useRangeCalendarState(props);

return (
<Calendar {...state} className="calendar-range">
<div className="header">
<CalendarButton {...state} goto="previousYear" className="prev-year">
<DoubleChevronLeft />
</CalendarButton>
<CalendarButton {...state} goto="previousMonth" className="prev-month">
<ChevronLeft />
</CalendarButton>
<CalendarHeader {...state} />
<CalendarButton {...state} goto="nextMonth" className="next-month">
<ChevronRight />
</CalendarButton>
<CalendarButton {...state} goto="nextYear" className="next-year">
<DoubleChevronRight />
</CalendarButton>
</div>

<CalendarGrid {...state} as="table" className="dates">
<thead>
<tr>
{state.weekDays.map((day, dayIndex) => {
return (
<CalendarWeekTitle
{...state}
as="th"
scope="col"
key={dayIndex}
dayIndex={dayIndex}
>
<abbr title={day.title}>{day.abbr}</abbr>
</CalendarWeekTitle>
);
})}
</tr>
</thead>
<tbody>
{state.daysInMonth.map((week, weekIndex) => (
<tr key={weekIndex}>
{week.map((day, dayIndex) => (
<CalendarCell {...state} as="td" key={dayIndex} date={day}>
<CalendarCellButton {...state} date={day} />
</CalendarCell>
))}
</tr>
))}
</tbody>
</CalendarGrid>
</Calendar>
);
};

export default App;

const DoubleChevronLeft = props => {
return (
<svg
{...props}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M15 19l-7-7 7-7"
/>
</svg>
);
};

const ChevronLeft = props => {
return (
<svg
{...props}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M11 19l-7-7 7-7m8 14l-7-7 7-7"
/>
</svg>
);
};

const ChevronRight = props => (
<ChevronLeft style={{ transform: "rotate(180deg)" }} {...props} />
);
```diff
- const state = useCalendarState(props);
+ const state = useRangeCalendarState(props);
```

const DoubleChevronRight = props => (
<DoubleChevronLeft style={{ transform: "rotate(180deg)" }} {...props} />
);
Also we can customize and style the ranges with CSS attribute selectors

```css
[data-is-range-selection] > span {
/* styles for any cells between start-end (inclusive) */
}
[data-is-selection-start] > span {
/* styles for first selected range cell */
}
[data-is-selection-end] > span {
/* styles for end selected range cell */
}

/* only applied if cell date is first or last of the month*/
[data-is-range-start] > span {
/**/
}
[data-is-range-end] > span {
/**/
}
```

[Calendar - Open On Sandbox](https://codesandbox.io/s/u3w1h)
[Calendar - Open On Sandbox](https://codesandbox.io/s/lf1ol)

[RangeCalendar - Open On Sandbox](https://codesandbox.io/s/hy4ow)
[RangeCalendar - Open On Sandbox](https://codesandbox.io/s/o4s64)

## Composition

Expand All @@ -283,13 +159,13 @@ const DoubleChevronRight = props => (

### `useCalendarState`

- **`value`** <code>T | undefined</code> The current value (controlled).
- **`defaultValue`** <code>T | undefined</code> The default value
- **`value`** <code>string | undefined</code> The current value (controlled).
- **`defaultValue`** <code>string | undefined</code> The default value
(uncontrolled).
- **`onChange`** <code>((value: T) =&#62; void) | undefined</code> Handler that
is called when the value changes.
- **`minValue`** <code>T | undefined</code> The smallest value allowed.
- **`maxValue`** <code>T | undefined</code> The largest value allowed.
- **`onChange`** <code>((value: string) =&#62; void) | undefined</code> Handler
that is called when the value changes.
- **`minValue`** <code>string | undefined</code> The smallest value allowed.
- **`maxValue`** <code>string | undefined</code> The largest value allowed.
- **`isDisabled`** <code>boolean | undefined</code> Whether the input is
disabled.
- **`isReadOnly`** <code>boolean | undefined</code> Whether the input can be
Expand All @@ -300,13 +176,13 @@ const DoubleChevronRight = props => (

### `useRangeCalendarState`

- **`value`** <code>T | undefined</code> The current value (controlled).
- **`defaultValue`** <code>T | undefined</code> The default value
- **`value`** <code>Range | undefined</code> The current value (controlled).
- **`defaultValue`** <code>Range | undefined</code> The default value
(uncontrolled).
- **`onChange`** <code>((value: T) =&#62; void) | undefined</code> Handler that
is called when the value changes.
- **`minValue`** <code>T | undefined</code> The smallest value allowed.
- **`maxValue`** <code>T | undefined</code> The largest value allowed.
- **`onChange`** <code>((value: Range) =&#62; void) | undefined</code> Handler
that is called when the value changes.
- **`minValue`** <code>string | undefined</code> The smallest value allowed.
- **`maxValue`** <code>string | undefined</code> The largest value allowed.
- **`isDisabled`** <code>boolean | undefined</code> Whether the input is
disabled.
- **`isReadOnly`** <code>boolean | undefined</code> Whether the input can be
Expand Down
Loading