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

react-aria: mergeARIADisabled #19765

Merged
merged 5 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Generalize mergeARIADisabled",
"packageName": "@fluentui/react-aria",
"email": "[email protected]",
"dependentChangeType": "patch"
}
9 changes: 9 additions & 0 deletions packages/react-aria/etc/react-aria.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
```ts

import type { IntrinsicShorthandProps } from '@fluentui/react-utilities';
import { ObjectShorthandProps } from '@fluentui/react-utilities';
import * as React_2 from 'react';
import type { ResolveShorthandOptions } from '@fluentui/react-utilities';
import type { ShorthandProps } from '@fluentui/react-utilities';

// @public (undocumented)
export type ARIAButtonShorthandProps = IntrinsicShorthandProps<'button', 'div' | 'span' | 'a'>;

// @public
export function mergeARIADisabled(shorthand: ObjectShorthandProps<{
disabled?: boolean;
'aria-disabled'?: string | boolean;
children?: React_2.ReactNode;
}>): boolean;

// @public
export function useARIAButton<Required extends boolean = false>(value: ShorthandProps<ARIAButtonShorthandProps>, options?: ResolveShorthandOptions<ARIAButtonShorthandProps, Required>): Required extends false ? ARIAButtonShorthandProps | undefined : ARIAButtonShorthandProps;

Expand Down
17 changes: 4 additions & 13 deletions packages/react-aria/src/hooks/useARIAButton.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { Enter, Space } from '@fluentui/keyboard-keys';
import { resolveShorthand, useEventCallback } from '@fluentui/react-utilities';
import { ObjectShorthandProps, resolveShorthand, useEventCallback } from '@fluentui/react-utilities';
import type { IntrinsicShorthandProps, ResolveShorthandOptions, ShorthandProps } from '@fluentui/react-utilities';

function mergeARIADisabled(disabled?: boolean | 'false' | 'true'): boolean {
if (typeof disabled === 'string') {
return disabled === 'false' ? false : true;
}
return disabled ?? false;
}
import { mergeARIADisabled } from '../utils/index';

export type ARIAButtonShorthandProps = IntrinsicShorthandProps<'button', 'div' | 'span' | 'a'>;

Expand All @@ -22,12 +16,9 @@ export function useARIAButton<Required extends boolean = false>(
): Required extends false ? ARIAButtonShorthandProps | undefined : ARIAButtonShorthandProps {
const shorthand = resolveShorthand(value, options);

const { onClick, onKeyDown, onKeyUp, ['aria-disabled']: ariaDisabled } = (shorthand ||
{}) as ARIAButtonShorthandProps;
const { onClick, onKeyDown, onKeyUp } = (shorthand || {}) as ARIAButtonShorthandProps;

const disabled = mergeARIADisabled(
(shorthand && shorthand.as === 'button' ? shorthand.disabled : undefined) ?? ariaDisabled,
);
const disabled = mergeARIADisabled(shorthand as ObjectShorthandProps<ARIAButtonShorthandProps>);

const onClickHandler: ARIAButtonShorthandProps['onClick'] = useEventCallback(ev => {
if (disabled) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-aria/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './hooks/index';
export * from './utils/index';
1 change: 1 addition & 0 deletions packages/react-aria/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mergeARIADisabled';
19 changes: 19 additions & 0 deletions packages/react-aria/src/utils/mergeARIADisabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as React from 'react';
import { ObjectShorthandProps } from '@fluentui/react-utilities';

/**
* Merges disabled declaration with `aria-disabled`
*/
export function mergeARIADisabled(
shorthand: ObjectShorthandProps<{
disabled?: boolean;
'aria-disabled'?: string | boolean;
children?: React.ReactNode;
}>,
) {
const disabled = shorthand.disabled || shorthand['aria-disabled'] || undefined;
if (typeof disabled === 'string') {
bsunderhus marked this conversation as resolved.
Show resolved Hide resolved
return disabled === 'false' ? false : true;
}
return disabled ?? false;
}