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

add defaultOpen prop to Disclosure component #447

Merged
merged 2 commits into from
Apr 26, 2021
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add `disabled` prop to `RadioGroup` and `RadioGroup.Option` ([#401](https://github.com/tailwindlabs/headlessui/pull/401))
- Add `defaultOpen` prop to the `Disclosure` component ([#447](https://github.com/tailwindlabs/headlessui/pull/447))

## [Unreleased - Vue]

Expand All @@ -29,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add `disabled` prop to `RadioGroup` and `RadioGroupOption` ([#401](https://github.com/tailwindlabs/headlessui/pull/401))
- Add `defaultOpen` prop to the `Disclosure` component ([#447](https://github.com/tailwindlabs/headlessui/pull/447))

## [@headlessui/react@v1.0.0] - 2021-04-14

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ describe('Rendering', () => {
assertDisclosurePanel({ state: DisclosureState.Visible, textContent: 'Panel is: open' })
})
)

it('should be possible to render a Disclosure in an open state by default', async () => {
render(
<Disclosure defaultOpen>
{({ open }) => (
<>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>Panel is: {open ? 'open' : 'closed'}</Disclosure.Panel>
</>
)}
</Disclosure>
)

assertDisclosureButton({
state: DisclosureState.Visible,
attributes: { id: 'headlessui-disclosure-button-1' },
})
assertDisclosurePanel({ state: DisclosureState.Visible, textContent: 'Panel is: open' })

await click(getDisclosureButton())

assertDisclosureButton({ state: DisclosureState.InvisibleUnmounted })
})
})

describe('Disclosure.Button', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,16 @@ interface DisclosureRenderPropArg {
}

export function Disclosure<TTag extends ElementType = typeof DEFAULT_DISCLOSURE_TAG>(
props: Props<TTag, DisclosureRenderPropArg>
props: Props<TTag, DisclosureRenderPropArg> & {
defaultOpen?: boolean
}
) {
let { defaultOpen = false, ...passthroughProps } = props
let buttonId = `headlessui-disclosure-button-${useId()}`
let panelId = `headlessui-disclosure-panel-${useId()}`

let reducerBag = useReducer(stateReducer, {
disclosureState: DisclosureStates.Closed,
disclosureState: defaultOpen ? DisclosureStates.Open : DisclosureStates.Closed,
linkedPanel: false,
buttonId,
panelId,
Expand All @@ -135,7 +138,7 @@ export function Disclosure<TTag extends ElementType = typeof DEFAULT_DISCLOSURE_
return (
<DisclosureContext.Provider value={reducerBag}>
{render({
props,
props: passthroughProps,
slot,
defaultTag: DEFAULT_DISCLOSURE_TAG,
name: 'Disclosure',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ describe('Rendering', () => {
assertDisclosurePanel({ state: DisclosureState.Visible, textContent: 'Panel is: open' })
})
)

it('should be possible to render a Disclosure in an open state by default', async () => {
renderTemplate(
html`
<Disclosure v-slot="{ open }" defaultOpen>
<DisclosureButton>Trigger</DisclosureButton>
<DisclosurePanel>Panel is: {{open ? 'open' : 'closed'}}</DisclosurePanel>
</Disclosure>
`
)

await new Promise<void>(nextTick)

assertDisclosureButton({
state: DisclosureState.Visible,
attributes: { id: 'headlessui-disclosure-button-1' },
})
assertDisclosurePanel({ state: DisclosureState.Visible, textContent: 'Panel is: open' })

await click(getDisclosureButton())

assertDisclosureButton({ state: DisclosureState.InvisibleUnmounted })
})
})

describe('DisclosureButton', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ export let Disclosure = defineComponent({
name: 'Disclosure',
props: {
as: { type: [Object, String], default: 'template' },
defaultOpen: { type: [Boolean], default: false },
},
setup(props, { slots, attrs }) {
let { ...passThroughProps } = props

let disclosureState = ref<StateDefinition['disclosureState']['value']>(DisclosureStates.Closed)
let disclosureState = ref<StateDefinition['disclosureState']['value']>(
props.defaultOpen ? DisclosureStates.Open : DisclosureStates.Closed
)
let panelRef = ref<StateDefinition['panelRef']['value']>(null)

let api = {
Expand All @@ -62,6 +63,7 @@ export let Disclosure = defineComponent({
provide(DisclosureContext, api)

return () => {
let { defaultOpen: _, ...passThroughProps } = props
let slot = { open: disclosureState.value === DisclosureStates.Open }
return render({ props: passThroughProps, slot, slots, attrs, name: 'Disclosure' })
}
Expand Down