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

RadioGroup: migrate from reakit to ariakit #55580

Merged
merged 12 commits into from
Nov 8, 2023
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Migrate `Divider` from `reakit` to `ariakit` ([#55622](https://github.com/WordPress/gutenberg/pull/55622))
- Migrate `DisclosureContent` from `reakit` to `ariakit` and TypeScript ([#55639](https://github.com/WordPress/gutenberg/pull/55639))
- Migrate `RadioGroup` from `reakit` to `ariakit` and TypeScript ([#55580](https://github.com/WordPress/gutenberg/pull/55580))

### Experimental

Expand Down
18 changes: 18 additions & 0 deletions packages/components/src/radio-group/context.tsx
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,
} );
51 changes: 0 additions & 51 deletions packages/components/src/radio-group/index.js

This file was deleted.

65 changes: 65 additions & 0 deletions packages/components/src/radio-group/index.tsx
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 );
flootr marked this conversation as resolved.
Show resolved Hide resolved
},
} );

const contextValue = useMemo(
() => ( {
store: radioStore,
disabled,
} ),
[ radioStore, disabled ]
);

return (
<RadioGroupContext.Provider value={ contextValue }>
<Ariakit.RadioGroup
store={ radioStore }
render={ <ButtonGroup>{ children }</ButtonGroup> }
ciampo marked this conversation as resolved.
Show resolved Hide resolved
aria-label={ label }
ref={ ref }
{ ...props }
/>
</RadioGroupContext.Provider>
);
}

/**
* @deprecated Use `RadioControl` or `ToggleGroupControl` instead.
*/
export const RadioGroup = forwardRef( UnforwardedRadioGroup );
flootr marked this conversation as resolved.
Show resolved Hide resolved
export default RadioGroup;
11 changes: 0 additions & 11 deletions packages/components/src/radio-group/radio-context/index.js

This file was deleted.

55 changes: 55 additions & 0 deletions packages/components/src/radio-group/radio.tsx
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 );
flootr marked this conversation as resolved.
Show resolved Hide resolved
export default Radio;
40 changes: 0 additions & 40 deletions packages/components/src/radio-group/radio/index.js

This file was deleted.

83 changes: 0 additions & 83 deletions packages/components/src/radio-group/stories/index.story.js

This file was deleted.

Loading
Loading