-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add all the things * polish components & introduce types * types: add property descriptions * re-add deprecated flag * improve story according to feedback/best practices * `Radio`: re-add `@deprecated` flag * fix story options * radio: pass on `props` to `Button` explicitly * add changelog entry * types: fix typo Co-authored-by: Marin Atanasov <8436925+tyxla@users.noreply.github.com> * types: fix wording for `label` Co-authored-by: Marco Ciampini <marco.ciampo@gmail.com> * apply current practices to story --------- Co-authored-by: Markus Dobmann <m.do@posteo.net> Co-authored-by: Marin Atanasov <8436925+tyxla@users.noreply.github.com>
- Loading branch information
1 parent
203610f
commit 9a6ab20
Showing
10 changed files
with
268 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import type * as Ariakit from '@ariakit/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
export const RadioGroupContext = createContext< { | ||
store?: Ariakit.RadioStore; | ||
disabled?: boolean; | ||
} >( { | ||
store: undefined, | ||
disabled: undefined, | ||
} ); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import * as Ariakit from '@ariakit/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, forwardRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ButtonGroup from '../button-group'; | ||
import type { WordPressComponentProps } from '../context'; | ||
import { RadioGroupContext } from './context'; | ||
import type { RadioGroupProps } from './types'; | ||
|
||
function UnforwardedRadioGroup( | ||
{ | ||
label, | ||
checked, | ||
defaultChecked, | ||
disabled, | ||
onChange, | ||
children, | ||
...props | ||
}: WordPressComponentProps< RadioGroupProps, 'div', false >, | ||
ref: React.ForwardedRef< any > | ||
) { | ||
const radioStore = Ariakit.useRadioStore( { | ||
value: checked, | ||
defaultValue: defaultChecked, | ||
setValue: ( newValue ) => { | ||
onChange?.( newValue ?? undefined ); | ||
}, | ||
} ); | ||
|
||
const contextValue = useMemo( | ||
() => ( { | ||
store: radioStore, | ||
disabled, | ||
} ), | ||
[ radioStore, disabled ] | ||
); | ||
|
||
return ( | ||
<RadioGroupContext.Provider value={ contextValue }> | ||
<Ariakit.RadioGroup | ||
store={ radioStore } | ||
render={ <ButtonGroup>{ children }</ButtonGroup> } | ||
aria-label={ label } | ||
ref={ ref } | ||
{ ...props } | ||
/> | ||
</RadioGroupContext.Provider> | ||
); | ||
} | ||
|
||
/** | ||
* @deprecated Use `RadioControl` or `ToggleGroupControl` instead. | ||
*/ | ||
export const RadioGroup = forwardRef( UnforwardedRadioGroup ); | ||
export default RadioGroup; |
11 changes: 0 additions & 11 deletions
11
packages/components/src/radio-group/radio-context/index.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { forwardRef, useContext } from '@wordpress/element'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import * as Ariakit from '@ariakit/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Button from '../button'; | ||
import { RadioGroupContext } from './context'; | ||
import type { WordPressComponentProps } from '../context'; | ||
import type { RadioProps } from './types'; | ||
|
||
function UnforwardedRadio( | ||
{ | ||
value, | ||
children, | ||
...props | ||
}: WordPressComponentProps< RadioProps, 'button', false >, | ||
ref: React.ForwardedRef< any > | ||
) { | ||
const { store, disabled } = useContext( RadioGroupContext ); | ||
|
||
const selectedValue = store?.useState( 'value' ); | ||
const isChecked = selectedValue !== undefined && selectedValue === value; | ||
|
||
return ( | ||
<Ariakit.Radio | ||
disabled={ disabled } | ||
store={ store } | ||
ref={ ref } | ||
value={ value } | ||
render={ | ||
<Button | ||
variant={ isChecked ? 'primary' : 'secondary' } | ||
{ ...props } | ||
/> | ||
} | ||
> | ||
{ children || value } | ||
</Ariakit.Radio> | ||
); | ||
} | ||
|
||
/** | ||
* @deprecated Use `RadioControl` or `ToggleGroupControl` instead. | ||
*/ | ||
export const Radio = forwardRef( UnforwardedRadio ); | ||
export default Radio; |
This file was deleted.
Oops, something went wrong.
83 changes: 0 additions & 83 deletions
83
packages/components/src/radio-group/stories/index.story.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.