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

Datepicker stories #72

Merged
merged 2 commits into from
Oct 7, 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
2 changes: 1 addition & 1 deletion src/datepicker/DatePicker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createOnKeyDown } from "reakit-utils";
import { BoxHTMLProps, BoxOptions, useBox } from "reakit";
import { createComponent, createHook } from "reakit-system";
import { ariaAttr, callAllHandlers } from "@chakra-ui/utils";
import { ariaAttr, callAllHandlers, dataAttr } from "@chakra-ui/utils";

import { DATE_PICKER_KEYS } from "./__keys";
import { DatePickerStateReturn } from "./DatePickerState";
Expand Down
8 changes: 8 additions & 0 deletions src/datepicker/DatePickerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const useDatePickerState = (props: DatePickerStateInitialProps = {}) => {
isDisabled,
isReadOnly,
isRequired,
autoFocus,
pickerId: pickerIdProp,
dialogId: dialogIdProp,
formatOptions,
Expand Down Expand Up @@ -98,6 +99,13 @@ export const useDatePickerState = (props: DatePickerStateInitialProps = {}) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [popover.visible]);

React.useEffect(() => {
if (autoFocus) {
composite.first();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [autoFocus, composite.first]);

return {
pickerId,
dialogId,
Expand Down
11 changes: 10 additions & 1 deletion src/datepicker/DatePickerTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
} from "reakit";

import { DATE_PICKER_TRIGGER_KEYS } from "./__keys";
import { DatePickerStateReturn } from "./DatePickerState";

export type DatePickerTriggerOptions = PopoverDisclosureOptions;
export type DatePickerTriggerOptions = PopoverDisclosureOptions &
Pick<DatePickerStateReturn, "isDisabled" | "isReadOnly">;

export type DatePickerTriggerHTMLProps = PopoverDisclosureHTMLProps;

Expand All @@ -23,6 +25,13 @@ export const useDatePickerTrigger = createHook<
compose: usePopoverDisclosure,
keys: DATE_PICKER_TRIGGER_KEYS,

useOptions(options, htmlProps) {
return {
disabled: options.isDisabled || options.isReadOnly,
...options,
};
},

useProps(_, { onMouseDown: htmlOnMouseDown, ...htmlProps }) {
const onMouseDown = (e: React.MouseEvent) => {
e.stopPropagation();
Expand Down
5 changes: 4 additions & 1 deletion src/datepicker/DateSegment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export const useDateSegment = createHook<

useOptions(options, htmlProps) {
return {
disabled: options.segment.type === "literal",
disabled:
options.isDisabled ||
options.isReadOnly ||
options.segment.type === "literal",
...options,
};
},
Expand Down
31 changes: 27 additions & 4 deletions src/datepicker/stories/DatePicker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";
import { addDays } from "date-fns";
import { Meta } from "@storybook/react";
import { addDays, addWeeks, subWeeks } from "date-fns";

import "./index.css";
import { DatePickerStateInitialProps, DateValue } from "../index.d";
Expand All @@ -24,10 +24,8 @@ const DatePickerComp: React.FC<DatePickerStateInitialProps> = props => {
...props,
});

console.log("%c state", "color: #f27999", state);

return (
<DatePicker {...state}>
<DatePicker className="datepicker" {...state}>
<div className="datepicker__header">
<DateSegmentField {...state} className="datepicker__field">
{state.segments.map((segment, i) => (
Expand Down Expand Up @@ -78,3 +76,28 @@ export const ControllableState = () => {
</div>
);
};

export const MinMaxDate = () => (
<DatePickerComp minValue={new Date()} maxValue={addWeeks(new Date(), 1)} />
);

export const InValidDate = () => (
<DatePickerComp
defaultValue={addWeeks(new Date(), 2)}
minValue={subWeeks(new Date(), 1)}
maxValue={addWeeks(new Date(), 1)}
/>
);

export const isDisabled = () => (
<DatePickerComp defaultValue={addDays(new Date(), 1)} isDisabled />
);

export const isReadOnly = () => (
<DatePickerComp defaultValue={addDays(new Date(), 1)} isReadOnly />
);

export const autoFocus = () => (
// eslint-disable-next-line jsx-a11y/no-autofocus
<DatePickerComp defaultValue={addDays(new Date(), 1)} autoFocus />
);
9 changes: 9 additions & 0 deletions src/datepicker/stories/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
border: 1px solid #1e65fd;
}

[aria-invalid="true"] > .datepicker__header {
border: 1px solid #c00000;
}

.datepicker__field {
font-family: monospace;
display: flex;
Expand All @@ -34,8 +38,13 @@
padding: 2px;
border-radius: 4px;
}

.datepicker__field--item:focus {
background-color: #1e65fd;
color: white;
outline: none;
}

.datepicker [aria-disabled="true"] {
opacity: 0.5;
}