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

Disabled: migrate to TypeScript #42708

Merged
merged 37 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c0a13bd
refactor storybook
torounit Jul 26, 2022
9a1e9d5
convert to typescript
torounit Jul 26, 2022
9afae37
fix ComponentProps
torounit Jul 26, 2022
fb80d10
add changelog.
torounit Aug 3, 2022
f4f1499
fix imports order
torounit Aug 4, 2022
3401779
use WordPressComponentProps.
torounit Aug 4, 2022
956cadf
add example
torounit Aug 4, 2022
f901aa6
Update packages/components/src/disabled/types.ts
torounit Aug 8, 2022
a401459
Update packages/components/src/disabled/index.tsx
torounit Aug 8, 2022
8e12cf8
Update packages/components/src/disabled/index.tsx
torounit Aug 17, 2022
2f21b98
Update packages/components/src/disabled/index.tsx
torounit Aug 17, 2022
176b3a3
Remove unused imports.
torounit Aug 17, 2022
a508d2b
Rewrite to Testing-library `will disable all fields`
torounit Aug 17, 2022
9dae208
Rewrite to Testing-library `should cleanly un-disable via reconciliat…
torounit Aug 17, 2022
b701fa2
use rerender
torounit Aug 17, 2022
93c3586
refactor test.
torounit Aug 17, 2022
d86a43f
replace react-dom/test-utils to testing-library.
torounit Aug 17, 2022
6e9a8ba
remove unnecessary MutationObserver stab.
torounit Aug 17, 2022
e409680
Convert to typescript.
torounit Aug 17, 2022
8f86d95
add story for contentEditable
torounit Aug 17, 2022
ea4b31e
add control settings.
torounit Aug 17, 2022
b8de163
fix changelog
torounit Aug 18, 2022
e7b0302
Update packages/components/CHANGELOG.md
torounit Aug 20, 2022
b53ae3e
Omit ref.
torounit Aug 20, 2022
442ec0a
avoid querying
torounit Aug 20, 2022
ae80910
rename div.
torounit Aug 20, 2022
331ccba
test before rerender
torounit Aug 20, 2022
33146e4
Simplify
torounit Aug 20, 2022
02f2039
Update packages/components/src/disabled/test/index.tsx
torounit Aug 20, 2022
6e98823
Merge remote-tracking branch 'origin/refactoring/components/disabled'…
torounit Aug 20, 2022
7909220
test for sneaky DOM manipulation
torounit Aug 20, 2022
28820f0
Fix `isDisabled` so that it keeps its value even if it is changed.
torounit Aug 20, 2022
ee170bb
add default args
torounit Aug 20, 2022
aec7359
Revert "Fix `isDisabled` so that it keeps its value even if it is cha…
torounit Aug 23, 2022
748907c
Merge branch 'trunk' into refactoring/components/disabled
torounit Aug 23, 2022
23d3db8
Update packages/components/src/disabled/test/index.tsx
ciampo Aug 23, 2022
2c44260
Update packages/components/CHANGELOG.md
ciampo Aug 23, 2022
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 packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- `contextConnect`: Refactor away from `_.uniq()` ([#43330](https://github.com/WordPress/gutenberg/pull/43330/)).
- `ColorPalette`: Refactor away from `_.uniq()` ([#43330](https://github.com/WordPress/gutenberg/pull/43330/)).
- `Guide`: Refactor away from `_.times()` ([#43374](https://github.com/WordPress/gutenberg/pull/43374/)).
- `Disabled`: Convert to TypeScript ([#42708](https://github.com/WordPress/gutenberg/pull/42708)).
- `Guide`: Update tests to use `@testing-library/react` ([#43380](https://github.com/WordPress/gutenberg/pull/43380)).
- `Modal`: use `KeyboardEvent.code` instead of deprecated `KeyboardEvent.keyCode`. improve unit tests ([#43429](https://github.com/WordPress/gutenberg/pull/43429/)).
- `FocalPointPicker`: use `KeyboardEvent.code`, partially refactor tests to modern RTL and `user-event` ([#43441](https://github.com/WordPress/gutenberg/pull/43441/)).
Expand All @@ -48,6 +49,7 @@
### Experimental
- `FormTokenField`: add `__experimentalAutoSelectFirstMatch` prop to auto select the first matching suggestion on typing ([#42527](https://github.com/WordPress/gutenberg/pull/42527/)).


ciampo marked this conversation as resolved.
Show resolved Hide resolved
## 19.17.0 (2022-08-10)

### Bug Fix
Expand Down
55 changes: 0 additions & 55 deletions packages/components/src/disabled/index.js

This file was deleted.

80 changes: 80 additions & 0 deletions packages/components/src/disabled/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { useDisabled } from '@wordpress/compose';
import { createContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import { StyledWrapper } from './styles/disabled-styles';
import type { DisabledProps } from './types';
import type { WordPressComponentProps } from '../ui/context';

const Context = createContext< boolean >( false );
const { Consumer, Provider } = Context;

/**
* `Disabled` is a component which disables descendant tabbable elements and prevents pointer interaction.
*
* ```jsx
* import { Button, Disabled, TextControl } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MyDisabled = () => {
* const [ isDisabled, setIsDisabled ] = useState( true );
*
* let input = <TextControl label="Input" onChange={ () => {} } />;
* if ( isDisabled ) {
* input = <Disabled>{ input }</Disabled>;
* }
*
* const toggleDisabled = () => {
* setIsDisabled( ( state ) => ! state );
* };
*
* return (
* <div>
* { input }
* <Button variant="primary" onClick={ toggleDisabled }>
* Toggle Disabled
* </Button>
* </div>
* );
* };
* ```
*/
function Disabled( {
walbo marked this conversation as resolved.
Show resolved Hide resolved
className,
children,
isDisabled = true,
...props
}: Omit< WordPressComponentProps< DisabledProps, 'div' >, 'ref' > ) {
const ref = useDisabled();

if ( ! isDisabled ) {
return <Provider value={ false }>{ children }</Provider>;
}

return (
<Provider value={ true }>
<StyledWrapper
ref={ ref }
className={ classnames( className, 'components-disabled' ) }
{ ...props }
ciampo marked this conversation as resolved.
Show resolved Hide resolved
>
{ children }
</StyledWrapper>
</Provider>
);
}

Disabled.Context = Context;
Disabled.Consumer = Consumer;

export default Disabled;
61 changes: 0 additions & 61 deletions packages/components/src/disabled/stories/index.js

This file was deleted.

87 changes: 87 additions & 0 deletions packages/components/src/disabled/stories/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import Disabled from '../';
import SelectControl from '../../select-control/';
import TextControl from '../../text-control/';
import TextareaControl from '../../textarea-control/';

const meta: ComponentMeta< typeof Disabled > = {
title: 'Components/Disabled',
component: Disabled,
argTypes: {
as: { control: { type: null } },
children: { control: { type: null } },
},
parameters: {
torounit marked this conversation as resolved.
Show resolved Hide resolved
controls: {
expanded: true,
},
docs: { source: { state: 'open' } },
},
};

export default meta;

const Form = () => {
const [ textControlValue, setTextControlValue ] = useState( '' );
const [ textAreaValue, setTextAreaValue ] = useState( '' );
return (
<div>
<TextControl
label="Text Control"
value={ textControlValue }
onChange={ setTextControlValue }
/>
<TextareaControl
label="TextArea Control"
value={ textAreaValue }
onChange={ setTextAreaValue }
/>
<SelectControl
label="Select Control"
onChange={ () => {} }
options={ [
{ value: '', label: 'Select an option', disabled: true },
{ value: 'a', label: 'Option A' },
{ value: 'b', label: 'Option B' },
{ value: 'c', label: 'Option C' },
] }
/>
</div>
);
};

export const Default: ComponentStory< typeof Disabled > = ( args ) => {
return (
<Disabled { ...args }>
<Form />
</Disabled>
);
};
Default.args = {
isDisabled: true,
};

export const ContentEditable: ComponentStory< typeof Disabled > = ( args ) => {
return (
<Disabled { ...args }>
<div contentEditable tabIndex={ 0 }>
contentEditable
</div>
</Disabled>
);
};
ciampo marked this conversation as resolved.
Show resolved Hide resolved
ContentEditable.args = {
isDisabled: true,
};
Loading