Skip to content

Commit

Permalink
disclosure: support as prop + fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance Strickland committed Nov 18, 2020
1 parent 22e60d5 commit 4ac5ef7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/disclosure/__tests__/disclosure.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import * as React from "react";
import { render, act, fireEvent } from "$test/utils";
import { axe } from "jest-axe";
import {
Expand Down
4 changes: 2 additions & 2 deletions packages/disclosure/examples/basic-ts.example.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
import * as React from "react";
import {
Disclosure,
DisclosureButton,
DisclosurePanel
DisclosurePanel,
} from "@reach/disclosure";

let name = "Basic (TS)";
Expand Down
4 changes: 2 additions & 2 deletions packages/disclosure/examples/basic.example.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
import * as React from "react";
import {
Disclosure,
DisclosureButton,
DisclosurePanel
DisclosurePanel,
} from "@reach/disclosure";

let name = "Basic";
Expand Down
12 changes: 6 additions & 6 deletions packages/disclosure/examples/controlled-ts.example.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React, { useState, useEffect } from "react";
import * as React from "react";
import {
Disclosure,
DisclosureButton,
DisclosurePanel
DisclosurePanel,
} from "@reach/disclosure";

let name = "Controlled (TS)";

function Example() {
const [value, setValue] = useState("");
const [open, setOpen] = useState(false);
useEffect(() => {
const [value, setValue] = React.useState("");
const [open, setOpen] = React.useState(false);
React.useEffect(() => {
if (value.toLowerCase().trim() === "open") {
setOpen(true);
} else if (value.toLowerCase().trim() === "close") {
Expand All @@ -26,7 +26,7 @@ function Example() {
style={{ display: "block", margin: "10px 0" }}
type="text"
value={value}
onChange={event => setValue(event.target.value)}
onChange={(event) => setValue(event.target.value)}
/>
</label>
<Disclosure open={open} onChange={() => setOpen(!open)}>
Expand Down
12 changes: 6 additions & 6 deletions packages/disclosure/examples/controlled.example.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React, { useState, useEffect } from "react";
import * as React from "react";
import {
Disclosure,
DisclosureButton,
DisclosurePanel
DisclosurePanel,
} from "@reach/disclosure";

let name = "Controlled";

function Example() {
const [value, setValue] = useState("");
const [open, setOpen] = useState(false);
useEffect(() => {
const [value, setValue] = React.useState("");
const [open, setOpen] = React.useState(false);
React.useEffect(() => {
if (value.toLowerCase().trim() === "open") {
setOpen(true);
} else if (value.toLowerCase().trim() === "close") {
Expand All @@ -26,7 +26,7 @@ function Example() {
style={{ display: "block", margin: "10px 0" }}
type="text"
value={value}
onChange={event => setValue(event.target.value)}
onChange={(event) => setValue(event.target.value)}
/>
</label>
<Disclosure open={open} onChange={() => setOpen(!open)}>
Expand Down
19 changes: 16 additions & 3 deletions packages/disclosure/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const Disclosure: React.FC<DisclosureProps> = ({
);
};

export type DisclosureProps = {
export type DisclosureOwnProps = {
/**
* `Disclosure` expects to receive accept `DisclosureButton` and
* `DisclosurePanel` components as children. It can also accept wrapper
Expand Down Expand Up @@ -163,6 +163,7 @@ export type DisclosureProps = {
*/
open?: boolean;
};
export type DisclosureProps = DisclosureOwnProps;

if (__DEV__) {
Disclosure.displayName = "Disclosure";
Expand Down Expand Up @@ -233,10 +234,14 @@ export const DisclosureButton = forwardRefWithAs<
);
});

type DisclosureButtonDOMProps = Omit<
React.ComponentProps<"button">,
keyof DisclosureButtonOwnProps
>;
/**
* @see Docs https://reach.tech/disclosure#disclosurebutton-props
*/
export type DisclosureButtonProps = {
export type DisclosureButtonOwnProps = {
/**
* Typically a text string that serves as a label for the disclosure button,
* though nested DOM nodes can be passed as well so long as they are valid
Expand All @@ -247,6 +252,8 @@ export type DisclosureButtonProps = {
*/
children: React.ReactNode;
};
export type DisclosureButtonProps = DisclosureButtonDOMProps &
DisclosureButtonOwnProps;

if (__DEV__) {
DisclosureButton.displayName = "DisclosureButton";
Expand Down Expand Up @@ -297,14 +304,20 @@ if (__DEV__) {
/**
* @see Docs https://reach.tech/disclosure#disclosurepanel-props
*/
export type DisclosurePanelProps = React.HTMLAttributes<HTMLDivElement> & {
type DisclosurePanelDOMProps = Omit<
React.ComponentProps<"div">,
keyof DisclosurePanelOwnProps
>;
export type DisclosurePanelOwnProps = {
/**
* Inner collapsible content for the disclosure item.
*
* @see Docs https://reach.tech/disclosure#disclosurepanel-children
*/
children: React.ReactNode;
};
export type DisclosurePanelProps = DisclosurePanelDOMProps &
DisclosurePanelOwnProps;

////////////////////////////////////////////////////////////////////////////////

Expand Down
4 changes: 2 additions & 2 deletions website/src/pages/disclosure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ It is important to note that the `Disclosure` component doesn't actually render
If you want to control the disclosure's value, you can do so by passing [`open`](#disclosure-open) and [`onChange`](#disclosure-onchange) props.

```jsx
const [isOpen, setOpen] = useState(false);
const [isOpen, setOpen] = React.useState(false);
return (
<Disclosure open={isOpen} onChange={() => setOpen(!isOpen)}>
<DisclosureButton />
Expand Down Expand Up @@ -174,7 +174,7 @@ The callback that is fired when a disclosure's open state is changed.
The controlled open state of the disclosure. The `open` prop should be used along with `onChange` to create controlled disclosure components.

```jsx
const [open, setOpen] = useState(false);
const [open, setOpen] = React.useState(false);
return (
<Disclosure open={open} onChange={() => setOpen(!open)}>
<DisclosureButton>I have a secret</DisclosureButton>
Expand Down

0 comments on commit 4ac5ef7

Please sign in to comment.