Skip to content

Commit

Permalink
Documentation: Remove withState HOC references part 2 (#33222)
Browse files Browse the repository at this point in the history
* Update Popover component docs
* Update FormTokenField component docs
* Update FormToggle component docs
* Update Disabled component docs
* Update TextControl component docs
* Update MenuItem component docs
* Update FontSizePicker component docs
  • Loading branch information
Mamaduka authored and sarayourfriend committed Jul 15, 2021
1 parent 8692185 commit 4e37d4c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 84 deletions.
12 changes: 6 additions & 6 deletions packages/components/src/disabled/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Assuming you have a form component, you can disable all form inputs by wrapping

```jsx
import { Button, Disabled, TextControl } from '@wordpress/components';
import { withState } from '@wordpress/compose';
import { useState } from '@wordpress/element';

const MyDisabled = () => {
const [ isDisabled, setIsDisabled ] = useState( true );

const MyDisabled = withState( {
isDisabled: true,
} )( ( { isDisabled, setState } ) => {
let input = <TextControl label="Input" onChange={ () => {} } />;
if ( isDisabled ) {
input = <Disabled>{ input }</Disabled>;
}

const toggleDisabled = () => {
setState( ( state ) => ( { isDisabled: ! state.isDisabled } ) );
setIsDisabled( ( state ) => ! state );
};

return (
Expand All @@ -30,7 +30,7 @@ const MyDisabled = withState( {
</Button>
</div>
);
} );
};
```

A component can detect if it has been wrapped in a `<Disabled>` by accessing its [context](https://reactjs.org/docs/context.html) using `Disabled.Consumer`.
Expand Down
39 changes: 19 additions & 20 deletions packages/components/src/font-size-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,37 @@ The component renders a user interface that allows the user to select predefined

```jsx
import { FontSizePicker } from '@wordpress/components';
import { withState } from '@wordpress/compose';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

...
const MyFontSizePicker = withState( {
fontSize: 16,
} )( ( { fontSize, setState } ) => {
const fontSizes = [
{
name: __( 'Small' ),
slug: 'small',
size: 12,
},
{
name: __( 'Big' ),
slug: 'big',
size: 26,
},
];
const fallbackFontSize = 16;
const fontSizes = [
{
name: __( 'Small' ),
slug: 'small',
size: 12,
},
{
name: __( 'Big' ),
slug: 'big',
size: 26,
},
];
const fallbackFontSize = 16;

const MyFontSizePicker = () => {
const [ fontSize, setFontSize ] = useState( 12 );

return (
<FontSizePicker
fontSizes={ fontSizes }
value={ fontSize }
fallbackFontSize={ fallbackFontSize }
onChange={ ( newFontSize ) => {
setState( { fontSize: newFontSize } );
setFontSize( newFontSize );
} }
/>
);
} );
};

...

Expand Down
14 changes: 6 additions & 8 deletions packages/components/src/form-toggle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,16 @@ When a user switches a toggle, its corresponding action takes effect immediately

```jsx
import { FormToggle } from '@wordpress/components';
import { withState } from '@wordpress/compose';
import { useState } from '@wordpress/element';

const MyFormToggle = () => {
const [ isChecked, setChecked ] = useState( true );

const MyFormToggle = withState( {
checked: true,
} )( ( { checked, setState } ) => (
<FormToggle
checked={ checked }
onChange={ () =>
setState( ( state ) => ( { checked: ! state.checked } ) )
}
onChange={ () => setChecked( ( state ) => ! state ) }
/>
) );
};
```

### Props
Expand Down
39 changes: 21 additions & 18 deletions packages/components/src/form-token-field/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,26 @@ The `value` property is handled in a manner similar to controlled form component

```jsx
import { FormTokenField } from '@wordpress/components';
import { withState } from '@wordpress/compose';
import { useState } from '@wordpress/element';

const MyFormTokenField = withState( {
tokens: [],
suggestions: [
'Africa',
'America',
'Antarctica',
'Asia',
'Europe',
'Oceania',
],
} )( ( { tokens, suggestions, setState } ) => (
<FormTokenField
value={ tokens }
suggestions={ suggestions }
onChange={ ( tokens ) => setState( { tokens } ) }
/>
) );
const continents = [
'Africa',
'America',
'Antarctica',
'Asia',
'Europe',
'Oceania',
];

const MyFormTokenField = () => {
const [ selectedContinents, setSelectedContinents ] = useState( [] );

return(
<FormTokenField
value={ selectedContinents }
suggestions={ continents }
onChange={ ( tokens ) => setSelectedContinents( tokens ) }
/>
);
};
```
30 changes: 15 additions & 15 deletions packages/components/src/menu-item/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ MenuItem is a component which renders a button intended to be used in combinatio

```jsx
import { MenuItem } from '@wordpress/components';
import { withState } from '@wordpress/compose';

const MyMenuItem = withState( {
isActive: true,
} )( ( { isActive, setState } ) => (
<MenuItem
icon={ isActive ? 'yes' : 'no' }
isSelected={ isActive }
onClick={ () =>
setState( ( state ) => ( { isActive: ! state.isActive } ) )
}
>
Toggle
</MenuItem>
) );
import { useState } from '@wordpress/element';

const MyMenuItem = () => {
const [ isActive, setIsActive ] = useState( true );

return (
<MenuItem
icon={ isActive ? 'yes' : 'no' }
isSelected={ isActive }
onClick={ () => setIsActive( ( state ) => ! state ) }
>
Toggle
</MenuItem>
);
};
```

## Props
Expand Down
12 changes: 6 additions & 6 deletions packages/components/src/popover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ Render a Popover within the parent to which it should anchor:

```jsx
import { Button, Popover } from '@wordpress/components';
import { withState } from '@wordpress/compose';
import { useState } from '@wordpress/element';

const MyPopover = withState( {
isVisible: false,
} )( ( { isVisible, setState } ) => {
const MyPopover = () => {
const [ isVisible, setIsVisible ] = useState( false );
const toggleVisible = () => {
setState( ( state ) => ( { isVisible: ! state.isVisible } ) );
setIsVisible( ( state ) => ! state );
};

return (
<Button variant="secondary" onClick={ toggleVisible }>
Toggle Popover!
{ isVisible && <Popover>Popover is toggled!</Popover> }
</Button>
);
} );
};
```

If a Popover is returned by your component, it will be shown. To hide the popover, simply omit it from your component's render value.
Expand Down
24 changes: 13 additions & 11 deletions packages/components/src/text-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ Render a user interface to input the name of an additional css class.

```js
import { TextControl } from '@wordpress/components';
import { withState } from '@wordpress/compose';

const MyTextControl = withState( {
className: '',
} )( ( { className, setState } ) => (
<TextControl
label="Additional CSS Class"
value={ className }
onChange={ ( className ) => setState( { className } ) }
/>
) );
import { useState } from '@wordpress/element';

const MyTextControl = () => {
const [ className, setClassName ] = useState( '' );

return (
<TextControl
label="Additional CSS Class"
value={ className }
onChange={ ( value ) => setClassName( value ) }
/>
);
};
```

### Props
Expand Down

0 comments on commit 4e37d4c

Please sign in to comment.