Skip to content

Commit

Permalink
docs(checkbox): 📝 refactor stories with better types handling
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Sep 1, 2021
1 parent fc12143 commit 09d84e4
Show file tree
Hide file tree
Showing 124 changed files with 456 additions and 191 deletions.
28 changes: 28 additions & 0 deletions scripts/create-preview-tabs.ts → .storybook/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
/* eslint-disable no-new-func */
import { CodeSandboxTemplate } from "storybook-addon-preview";

type CreateControlsOptions = {
unions?: string[];
ignore?: string[];
allow?: string[];
};

export const createControls = (options?: CreateControlsOptions) => {
try {
const ignoredControls = (options?.ignore || []).reduce((cur, key) => {
return {
...cur,
[key]: { table: { disable: true } },
};
}, {});

const allowedControls = (options?.allow || []).reduce((cur, key) => {
return {
...cur,
[key]: { table: { disable: true } },
};
}, {});

return { ...ignoredControls, ...allowedControls };
} catch (error) {
console.log(error);
}
};

interface Props {
js?: string;
ts?: string;
Expand Down
10 changes: 0 additions & 10 deletions .storybook/webpack.config.js

This file was deleted.

1 change: 0 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default {
moduleNameMapper: {
"\\.(css|less|sass|scss)$": "<rootDir>/src/__mocks__/styleMock.js",
"^@shared(.*)$": "<rootDir>/shared$1",
"^@renderlesskit/react$": "<rootDir>/src",
},
coveragePathIgnorePatterns: [
"node_modules",
Expand Down
23 changes: 19 additions & 4 deletions scripts/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ function isStateReturnDeclaration(node) {
);
}

/**
* @param {import("ts-morph").Node<Node>} node
*/
function isInitialStateDeclaration(node) {
const kindName = node.getKindName();
const escapedName = getEscapedName(node);
return (
kindName === "TypeAliasDeclaration" && /.+InitialState$/.test(escapedName)
);
}

/**
* @param {import("ts-morph").Node<Node>} node
*/
Expand Down Expand Up @@ -296,10 +307,7 @@ function reduceKeys(acc, [moduleName, array]) {

const finalString = `${declaration} = ${value};\n`;

if (!moduleName.endsWith("State")) {
return `${acc}export ${finalString}`;
}
return `${acc}${finalString}`;
return `${acc}export ${finalString}`;
}

/**
Expand Down Expand Up @@ -339,6 +347,13 @@ function makeKeys(rootPath) {
keys[getModuleName(node)] = [...stateKeys, ...props];
}
}
if (isInitialStateDeclaration(node)) {
const literalNode = isOptionsDeclaration(node)
? getLiteralNode(node)
: node;
const props = literalNode ? getPropsNames(literalNode, true) : [];
keys[getModuleName(node)] = props;
}
});
});

Expand Down
34 changes: 27 additions & 7 deletions src/accordion/__keys.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
// Automatically generated
const ACCORDION_BASE_STATE_KEYS = [
export const USE_ACCORDION_BASE_STATE_KEYS = [
"baseId",
"unstable_idCountRef",
"unstable_virtual",
"rtl",
"orientation",
"items",
"groups",
"currentId",
"loop",
"wrap",
"shift",
"unstable_includesBaseElement",
] as const;
export const ACCORDION_BASE_STATE_KEYS = [
...USE_ACCORDION_BASE_STATE_KEYS,
"unstable_idCountRef",
"items",
"groups",
"unstable_moves",
"unstable_hasActiveWidget",
"unstable_includesBaseElement",
"panels",
"setBaseId",
"registerItem",
Expand Down Expand Up @@ -41,7 +44,15 @@ const ACCORDION_BASE_STATE_KEYS = [
"registerPanel",
"unregisterPanel",
] as const;
const ACCORDION_MULTI_STATE_KEYS = [
export const USE_ACCORDION_MULTI_STATE_KEYS = [
...USE_ACCORDION_BASE_STATE_KEYS,
"manual",
"selectedIds",
"defaultSelectedIds",
"onSelectedIdsChange",
"shouldUpdate",
] as const;
export const ACCORDION_MULTI_STATE_KEYS = [
...ACCORDION_BASE_STATE_KEYS,
"selectedIds",
"allowToggle",
Expand All @@ -50,7 +61,16 @@ const ACCORDION_MULTI_STATE_KEYS = [
"setSelectedIds",
"select",
] as const;
const ACCORDION_STATE_KEYS = [
export const USE_ACCORDION_STATE_KEYS = [
...USE_ACCORDION_BASE_STATE_KEYS,
"selectedId",
"manual",
"allowToggle",
"defaultSelectedId",
"onSelectedIdChange",
"shouldUpdate",
] as const;
export const ACCORDION_STATE_KEYS = [
...ACCORDION_BASE_STATE_KEYS,
"selectedId",
"allowToggle",
Expand Down
1 change: 1 addition & 0 deletions src/accordion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./AccordionBaseState";
export * from "./Accordion";
export * from "./AccordionTrigger";
export * from "./AccordionPanel";
export * from "./__keys";
2 changes: 1 addition & 1 deletion src/accordion/stories/AccordionBasic.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AccordionPanel,
AccordionTrigger,
useAccordionState,
} from "@renderlesskit/react";
} from "../index";

export function App(props: any) {
const state = useAccordionState(props);
Expand Down
2 changes: 1 addition & 1 deletion src/accordion/stories/AccordionBasic.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CompositeState } from "reakit/ts";

import { AccordionInitialState } from "../AccordionState";
import { App as Accordion } from "./AccordionBasic.component";
import { createPreviewTabs } from "../../../scripts/create-preview-tabs";
import { createPreviewTabs } from "../../../.storybook/utils";
import { accordionBasicTemplate, accordionBasicTemplateJs } from "./templates";

export const Default: React.FC<
Expand Down
2 changes: 1 addition & 1 deletion src/accordion/stories/AccordionMulti.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AccordionPanel,
AccordionTrigger,
useAccordionMultiState,
} from "@renderlesskit/react";
} from "../index";

export function App(props: any) {
const state = useAccordionMultiState(props);
Expand Down
2 changes: 1 addition & 1 deletion src/accordion/stories/AccordionMulti.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CompositeState } from "reakit/ts";

import { AccordionInitialState } from "../AccordionState";
import { App as Accordion } from "./AccordionMulti.component";
import { createPreviewTabs } from "../../../scripts/create-preview-tabs";
import { createPreviewTabs } from "../../../.storybook/utils";
import { accordionMultiTemplate, accordionMultiTemplateJs } from "./templates";

export const Default: React.FC<
Expand Down
2 changes: 1 addition & 1 deletion src/accordion/stories/AccordionStyled.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
AccordionTrigger,
useAccordionState,
AccordionInitialState,
} from "@renderlesskit/react";
} from "../index";

// Styled based on https://www.w3.org/TR/wai-aria-practices-1.2/examples/accordion/accordion.html
export const App: React.FC<AccordionInitialState> = props => {
Expand Down
2 changes: 1 addition & 1 deletion src/accordion/stories/AccordionStyled.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import "./AccordionStyled.css";
import { AccordionInitialState } from "../AccordionState";
import { App as Accordion } from "./AccordionStyled.component";
import { createPreviewTabs } from "../../../scripts/create-preview-tabs";
import { createPreviewTabs } from "../../../.storybook/utils";

export const Default: React.FC<
Omit<Partial<AccordionInitialState>, keyof CompositeState>
Expand Down
2 changes: 1 addition & 1 deletion src/accordion/stories/AccordionZAuto.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AccordionPanel,
AccordionTrigger,
useAccordionState,
} from "@renderlesskit/react";
} from "../index";

export function App(props: any) {
const initialProps = {
Expand Down
2 changes: 1 addition & 1 deletion src/accordion/stories/AccordionZAuto.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CompositeState } from "reakit/ts";

import { AccordionInitialState } from "../AccordionState";
import { App as Accordion } from "./AccordionZAuto.component";
import { createPreviewTabs } from "../../../scripts/create-preview-tabs";
import { createPreviewTabs } from "../../../.storybook/utils";
import { accordionZAutoTemplate, accordionZAutoTemplateJs } from "./templates";

export const Default: React.FC<
Expand Down
1 change: 1 addition & 0 deletions src/breadcrumbs/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./Breadcrumbs";
export * from "./BreadcrumbLink";
export * from "./__keys";
2 changes: 1 addition & 1 deletion src/breadcrumbs/stories/Breadcrumbs.component.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";

import { Breadcrumbs, BreadcrumbLink } from "@renderlesskit/react";
import { Breadcrumbs, BreadcrumbLink } from "../index";

export const App: React.FC = props => {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/breadcrumbs/stories/Breadcrumbs.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
breadcrumbsTemplateJs,
breadcrumbsCssTemplate,
} from "./templates";
import { createPreviewTabs } from "../../../scripts/create-preview-tabs";
import { createPreviewTabs } from "../../../.storybook/utils";

export default {
component: Breadcrumbs,
Expand Down
16 changes: 14 additions & 2 deletions src/calendar/__keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Automatically generated
const CALENDAR_STATE_KEYS = [
export const USE_CALENDAR_STATE_KEYS = [
"value",
"defaultValue",
"onChange",
"minValue",
"maxValue",
"isDisabled",
"isReadOnly",
"autoFocus",
"id",
] as const;
export const CALENDAR_STATE_KEYS = [
"calendarId",
"dateValue",
"minDate",
Expand Down Expand Up @@ -33,7 +44,8 @@ const CALENDAR_STATE_KEYS = [
"selectFocusedDate",
"selectDate",
] as const;
const RANGE_CALENDAR_STATE_KEYS = [
export const USE_RANGE_CALENDAR_STATE_KEYS = USE_CALENDAR_STATE_KEYS;
export const RANGE_CALENDAR_STATE_KEYS = [
...CALENDAR_STATE_KEYS,
"dateRangeValue",
"anchorDate",
Expand Down
1 change: 1 addition & 0 deletions src/calendar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from "./CalendarButton";
export * from "./CalendarGrid";
export * from "./CalendarHeader";
export * from "./CalendarWeekTitle";
export * from "./__keys";
2 changes: 1 addition & 1 deletion src/calendar/stories/CalendarBase.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
CalendarHeader,
CalendarWeekTitle,
CalendarInitialState,
} from "@renderlesskit/react";
} from "../index";

import {
ChevronLeft,
Expand Down
4 changes: 2 additions & 2 deletions src/calendar/stories/CalendarBase.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
utilsTemplate,
} from "./templates";
import { App as Calendar } from "./CalendarBase.component";
import { createPreviewTabs } from "../../../scripts/create-preview-tabs";
import { createPreviewTabs } from "../../../.storybook/utils";
import { format } from "util";
import { addWeeks } from "@renderlesskit/react/utils";
import { addWeeks } from "../../utils/index";

export default {
component: Calendar,
Expand Down
2 changes: 1 addition & 1 deletion src/calendar/stories/CalendarRange.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
CalendarWeekTitle,
useRangeCalendarState,
RangeCalendarInitialState,
} from "@renderlesskit/react";
} from "../index";

import {
ChevronLeft,
Expand Down
4 changes: 2 additions & 2 deletions src/calendar/stories/CalendarRange.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
utilsTemplate,
} from "./templates";
import { App as RangeCalendar } from "./CalendarRange.component";
import { createPreviewTabs } from "../../../scripts/create-preview-tabs";
import { createPreviewTabs } from "../../../.storybook/utils";
import {
addDays,
addWeeks,
format,
subDays,
subWeeks,
} from "@renderlesskit/react/utils";
} from "../../utils/index";

export default {
title: "Calendar/Range",
Expand Down
3 changes: 1 addition & 2 deletions src/checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ export const useCheckbox = createHook<CheckboxOptions, CheckboxHTMLProps>({

const onChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
console.log("%cevent", "color: #1d5673", event);
const element = event.currentTarget;

if (disabled) {
event.stopPropagation();
event.preventDefault();

return;
}

Expand All @@ -103,7 +103,6 @@ export const useCheckbox = createHook<CheckboxOptions, CheckboxHTMLProps>({
if (!setState) return;

if (typeof value === "undefined") {
console.log("%cchecked", "color: #731d1d", checked);
setState(!checked);
} else {
const stateProp = Array.isArray(state) ? state : [];
Expand Down
7 changes: 2 additions & 5 deletions src/checkbox/CheckboxState.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useControllableState } from "@renderlesskit/react";
import { useControllableState } from "../utils/index";

export type CheckboxState = {
/**
Expand All @@ -11,7 +11,7 @@ export type CheckboxState = {

export type CheckboxActions = {
/**
* Sets `state`.
* Sets `state` for the checkbox.
*/
setState: React.Dispatch<React.SetStateAction<CheckboxState["state"]>>;
};
Expand Down Expand Up @@ -47,14 +47,11 @@ export function useCheckboxState(
onStateChange,
} = props;

console.log("%cstateProp", "color: #00b300", stateProp);

const [state, setState] = useControllableState({
defaultValue: defaultState,
value: stateProp,
onChange: onStateChange,
});
console.log("%cstate", "color: #d90000", state);

return { state, setState };
}
Loading

0 comments on commit 09d84e4

Please sign in to comment.