Skip to content

Commit

Permalink
Add components examples in README.md files - Part 1 (#8267)
Browse files Browse the repository at this point in the history
* Components examples: Button

* Components examples: ButtonGroup

* Components examples: Dashicon

* Components examples: Disabled

* Components examples: Draggable

* Components examples: ClipboardButton

* Components examples: CheckboxControl

* Components examples: BaseControl

* Components examples: Autocomplete

* Components examples: Adding imports and using functional components

* Components examples: ColorPalette

* Components examples: DropZone

* Components examples: Dropdown

* Components examples: DropdownMenu

* Components examples: ExternalLink

* Components examples: FocusableIframe

* Components examples: FontSizePicker

* Components examples: FormFileUpload

* Components examples: FormToggle

* Components examples: FormTokenField

* Components examples: ColorIndicator

* Components examples: IconButton

* Components examples: KeyboardShortcuts

* Components examples: MenuGroup and MenuItem

* Components examples: MenuItemsChoice

* Using hashes instead of equality signs for the README main header

* Using https://wordpress.org instead of .com

* Using checked property in the CheckboxControl example

* Change text used in the ClipboardButton example

* Better format in colors array used in the ColorPalette example

* Placing variable in arrow function between parenthesis

* Add a dynamic message to DropZone example

* Code style according to linter in FocusableIframe example

* Add missing semicolons to FontSizePicker example

* Add fallbackFontSize to FontSizePicker example

* Add missing whitespaces in suggestions array

* Use MenuItem in MenuGroup example
  • Loading branch information
mmtr authored and gziolo committed Aug 2, 2018
1 parent 32beb16 commit cce4232
Show file tree
Hide file tree
Showing 26 changed files with 431 additions and 154 deletions.
77 changes: 51 additions & 26 deletions packages/components/src/autocomplete/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Autocomplete
============
# Autocomplete

This component is used to provide autocompletion support for a child input component.

Expand Down Expand Up @@ -105,33 +104,59 @@ Whether to apply debouncing for the autocompleter. Set to true to enable debounc
- Type: `Boolean`
- Required: No

### Examples
## Usage

The following is a contrived completer for fresh fruit.

```jsx
const fruitCompleter = {
name: 'fruit',
// The prefix that triggers this completer
triggerPrefix: '~',
// The option data
options: [
{ visual: '🍎', name: 'Apple' },
{ visual: '🍊', name: 'Orange' },
{ visual: '🍇', name: 'Grapes' },
],
// Returns a label for an option like "🍊 Orange"
getOptionLabel: option => [
<span class="icon">{ option.visual }</span>,
option.name
],
// Declares that options should be matched by their name
getOptionKeywords: option => [ option.name ],
// Declares that the Grapes option is disabled
isOptionDisabled: option => option.name === 'Grapes',
// Declares completions should be inserted as abbreviations
getOptionCompletion: option => (
<abbr title={ option.name }>{ option.visual }</abbr>
),
import { Autocomplete } from '@wordpress/components';

function FreshFruitAutocomplete() {
const autocompleters = [
{
name: 'fruit',
// The prefix that triggers this completer
triggerPrefix: '~',
// The option data
options: [
{ visual: '🍎', name: 'Apple', id: 1 },
{ visual: '🍊', name: 'Orange', id: 2 },
{ visual: '🍇', name: 'Grapes', id: 3 },
],
// Returns a label for an option like "🍊 Orange"
getOptionLabel: option => (
<span>
<span className="icon" >{ option.visual }</span>{ option.name }
</span>
),
// Declares that options should be matched by their name
getOptionKeywords: option => [ option.name ],
// Declares that the Grapes option is disabled
isOptionDisabled: option => option.name === 'Grapes',
// Declares completions should be inserted as abbreviations
getOptionCompletion: option => (
<abbr title={ option.name }>{ option.visual }</abbr>
),
}
];

return (
<div>
<Autocomplete completers={ autocompleters }>
{ ( { isExpanded, listBoxId, activeId } ) => (
<div
contentEditable
suppressContentEditableWarning
aria-autocomplete="list"
aria-expanded={ isExpanded }
aria-owns={ listBoxId }
aria-activedescendant={ activeId }
>
</div>
) }
</Autocomplete>
<p>Type ~ for triggering the autocomplete.</p>
</div>
);
};
```
29 changes: 16 additions & 13 deletions packages/components/src/base-control/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
BaseControl
=======
# BaseControl

BaseControl component is used to generate labels and help text for components handling user inputs.

Expand All @@ -8,17 +7,21 @@ BaseControl component is used to generate labels and help text for components ha

Render a BaseControl for a textarea input:
```jsx
<BaseControl
id="textarea-1"
label="Text"
help="Enter some text"
>
<textarea
id="textarea-1"
onChange={ onChangeValue }
value={ value }
/>
</BaseControl>
import { BaseControl } from '@wordpress/components';

function MyBaseControl() {
return (
<BaseControl
id="textarea-1"
label="Text"
help="Enter some text"
>
<textarea
id="textarea-1"
/>
</BaseControl>
);
}
```

## Props
Expand Down
16 changes: 16 additions & 0 deletions packages/components/src/button-group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ButtonGroup

## Usage

```jsx
import { Button, ButtonGroup } from '@wordpress/components';

function MyButtonGroup() {
return (
<ButtonGroup>
<Button isPrimary>Button 1</Button>
<Button isPrimary>Button 2</Button>
</ButtonGroup>
);
}
```
5 changes: 1 addition & 4 deletions packages/components/src/button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ The presence of a `href` prop determines whether an `anchor` element is rendered
Renders a button with default style.

```jsx
/**
* WordPress dependencies
*/
import { Button } from "@wordpress/components";

export default function ClickMeButton() {
function MyButton() {
return (
<Button isDefault>
Click me!
Expand Down
24 changes: 15 additions & 9 deletions packages/components/src/checkbox-control/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CheckboxControl
=======
# CheckboxControl

CheckboxControl component is used to generate a checkbox input field.

Expand All @@ -8,13 +7,20 @@ CheckboxControl component is used to generate a checkbox input field.

Render an is author checkbox:
```jsx
<CheckboxControl
heading="User"
label="Is author"
help="Is the user a author or not?"
checked={ checked }
onChange={ onChange }
/>
import { CheckboxControl } from '@wordpress/components';
import { withState } from '@wordpress/compose';

withState( {
isChecked: true,
} )( ( { isChecked, setState } ) => (
<CheckboxControl
heading="User"
label="Is author"
help="Is the user a author or not?"
checked={ isChecked }
onChange={ ( isChecked ) => { setState( { isChecked } ) } }
/>
) )
```

## Props
Expand Down
21 changes: 21 additions & 0 deletions packages/components/src/clipboard-button/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ClipboardButton

## Usage

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

withState( {
hasCopied: false,
} )( ( { hasCopied, setState } ) => (
<ClipboardButton
isPrimary
text="Text to be copied."
onCopy={ () => setState( { hasCopied: true } ) }
onFinishCopy={ () => setState( { hasCopied: false } ) }
>
{ hasCopied ? 'Copied!' : 'Copy Text' }
</ClipboardButton>
) )
```
13 changes: 13 additions & 0 deletions packages/components/src/color-indicator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ColorIndicator

## Usage

```jsx
import { ColorIndicator } from '@wordpress/components';

function MyColorIndicator() {
return (
<ColorIndicator colorValue="#f00" />
);
}
```
25 changes: 25 additions & 0 deletions packages/components/src/color-palette/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ColorPalette

## Usage
```jsx
import { ColorPalette } from '@wordpress/components';
import { withState } from '@wordpress/compose';

withState( {
color: '#f00',
} )( ( { color, setState } ) => {
const colors = [
{ name: 'red', color: '#f00' },
{ name: 'white', color: '#fff' },
{ name: 'blue', color: '#00f' },
];

return (
<ColorPalette
colors={ colors }
value={ color }
onChange={ color => setState( { color } ) }
/>
)
} )
```
17 changes: 17 additions & 0 deletions packages/components/src/dashicon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Dashicon

## Usage

```jsx
import { Dashicon } from '@wordpress/components';

function MyDashicons() {
return (
<div>
<Dashicon icon="admin-home" />
<Dashicon icon="products" />
<Dashicon icon="wordpress" />
</div>
);
}
```
3 changes: 1 addition & 2 deletions packages/components/src/date-time/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
DateTimePicker
=======
# DateTimePicker

DateTimePicker is a React component to render a calendar and clock for selecting a date and time. The calendar and clock components can be accessed individually using the `DatePicker` and `TimePicker` components respectively.

Expand Down
31 changes: 16 additions & 15 deletions packages/components/src/disabled/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Disabled
========
# Disabled

Disabled is a component which disables descendant tabbable elements and prevents pointer interaction.

Expand All @@ -8,25 +7,27 @@ Disabled is a component which disables descendant tabbable elements and prevents
Assuming you have a form component, you can disable all form inputs by wrapping the form with `<Disabled>`.

```jsx
const DisableToggleForm = withState( {
isDisabled: true,
} )( ( { isDisabled, setState } ) => {
let form = <form><input /></form>;
import { Button, Disabled, TextControl } from '@wordpress/components';
import { withState } from '@wordpress/compose';

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

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

const toggleDisabled = () => {
setState( ( state ) => ( { isDisabled: ! state.isDisabled } ) );
};
return (
<div>
{ form }
<button onClick={ toggleDisabled }>
{ input }
<Button isPrimary onClick={ toggleDisabled }>
Toggle Disabled
</button>
</Button>
</div>
);
} )
Expand Down
23 changes: 23 additions & 0 deletions packages/components/src/draggable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,26 @@ The function called when dragging ends.
- Type: `Function`
- Required: No
- Default: `noop`

## Usage

```jsx
import { Dashicon, Draggable, Panel, PanelBody } from '@wordpress/components';

function DraggablePanel() {
return (
<div id="draggable-panel">
<Panel header="Draggable panel" >
<PanelBody>
<Draggable
elementId="draggable-panel"
transferData={ { } }
>
<Dashicon icon="move" />
</Draggable>
</PanelBody>
</Panel>
</div>
);
}
```
26 changes: 16 additions & 10 deletions packages/components/src/drop-zone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@

```jsx
import { DropZoneProvider, DropZone } from '@wordpress/components';

function MyComponent() {
return (
<DropZoneProvider>
<div>
<DropZone onDrop={ () => console.log( 'do something' ) } />
</div>
</DropZoneProvider>
);
}
import { withState } from '@wordpress/compose';

withState( {
hasDropped: false,
} )( ( { hasDropped, setState } ) => (
<DropZoneProvider>
<div>
{ hasDropped ? 'Dropped!' : 'Drop something here' }
<DropZone
onFilesDrop={ () => setState( { hasDropped: true } ) }
onHTMLDrop={ () => setState( { hasDropped: true } ) }
onDrop={ () => setState( { hasDropped: true } ) }
/>
</div>
</DropZoneProvider>
) );
```

## Props
Expand Down
Loading

0 comments on commit cce4232

Please sign in to comment.