File size: 6,186 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# Segmented Control
Segmented Control manipulates the content shown following an exclusive or “either/or” pattern.
## Usage
It can be utilized via two different techniques: **child components** or an **options array**. These techniques are available because certain use cases prefer the "selection" logic to be built into `SegmentedControl`, while others prefer to explicitly define that logic elsewhere.
### Child components
The children technique is appropriate when you'd like to define the "selection" logic at the same point where `<SegmentedControl>` is implemented.
A good example for this case is navigation. Sometimes the option that is selected is defined by the route, other times it's a state value, external prop, etc.
```jsx
import { SegmentedControl } from '@automattic/components';
import { Component } from 'react';
export default class extends Component {
// ...
render() {
return (
<SegmentedControl>
<SegmentedControl.Item
selected={ this.state.selected === 'all' }
onClick={ this.handleFilterClick( 'all' ) }
>
All
</SegmentedControl.Item>
<SegmentedControl.Item
selected={ this.state.selected === 'unread' }
onClick={ this.handleFilterClick( 'unread' ) }
>
Unread
</SegmentedControl.Item>
<SegmentedControl.Item
selected={ this.state.selected === 'comments' }
onClick={ this.handleFilterClick( 'comments' ) }
>
Comments
</SegmentedControl.Item>
<SegmentedControl.Item
selected={ this.state.selected === 'follows' }
onClick={ this.handleFilterClick( 'follows' ) }
>
Follows
</SegmentedControl.Item>
<SegmentedControl.Item
selected={ this.state.selected === 'likes' }
onClick={ this.handleFilterClick( 'likes' ) }
>
Likes
</SegmentedControl.Item>
</SegmentedControl>
);
}
handleFilterClick( value ) {
return () => {
// ... (track analytics, add to state, etc.)
this.setState( {
selected: value,
// ...
} );
};
}
}
```
The key here is that it's up to the parent component to explicitly define things such as the selected item, potential `onClick` callbacks, etc.
#### Props
##### Segmented Control
| Name | Type | Default | Description |
| ----------- | -------- | ------- | ----------------------------------------- |
| `className` | `string` | `0` | Class(es) applied to `.segmented-control` |
| `style` | `string` | `0` | Inline styles applied to the main element |
| `compact` | `bool` | `false` | Decreases the size |
##### Control Item
| Name | Type | Default | Description |
| ------------ | -------- | ------- | ------------------------------------------------------- |
| `selected`\* | `bool` | `false` | Determines the selected item |
| `path` | `string` | `null` | URL to navigate to when item is clicked |
| `title` | `string` | `null` | Title to show when hovering over item |
| `onClick` | | `null` | Callback applied when `SegmentedControlItem` is clicked |
### Options array
We also provide `SimplifiedSegmentedControl` which uses an `options` array as a prop. This technique is great for situations where you don't want to explicitly define things like what happens when an item is clicked or which item is currently selected, etc.
A good example for this case is a form element. You don't want to have to write the logic for updating the component when a new selection is made, but you might want to hook into certain events like: when a new selection is made, what was the option?
**NOTE**: _there is still more work here in order to be fully functional as a form element. This is currently experimental._
```jsx
import SimplifiedSegmentedControl from '@automattic/components/segmented-control/simplified';
const options = [
{ value: 'all', label: 'All' },
{ value: 'unread', label: 'Unread' },
{ value: 'comments', label: 'Comments' },
{ value: 'follows', label: 'Follows' },
{ value: 'likes', label: 'Likes' },
];
export default class extends React.Component {
// ...
handleOnSelect = ( option ) => {
console.log( 'selected option:', option ); // full object of selected option
};
render() {
return <SimplifiedSegmentedControl options={ options } onSelect={ this.handleOnSelect } />;
}
}
```
Note that all the "selection" logic will be applied in `SimplifiedSegmentedControl` itself using a simple `selected` value comparison in state. It will update itself when an option has been clicked.
#### Props
| Name | Type | Default | Description |
| ----------------- | -------- | ------- | ------------------------------------------------ |
| `options`\* | `array` | `null` | The main data set for rendering options |
| `initialSelected` | `string` | `null` | Represents the initial selected option's `value` |
| `onSelect` | | `null` | Callback whenever a new item has been clicked |
##### `options` prop example
```js
const options = [
{
value: 'the value', // *required* - (string) tracked by component
label: 'the label', // *required* - (string) displayed to user
path: 'a path', // optional - (string) URL to navigate when clicked
},
// ...
];
```
### General guidelines
- There are two states: selected and non-selected. There must always be only one selected state, no more, no less.
- The primary style is preferred. Use your best judgement if you want to use the non-primary style to remove visual conflict with another primary elements in the view.
- Text should be concise and specific. Use no more than two words.
- A default selection is required. The default selection is the first option in the segmented control.
## Related components
- To group buttons together, use the `ButtonGroup` component.
- To navigate between multiple pages of items, use the `Pagination` component.
- To alternate among related views within the same context with _tabs_, use the `SectionNav` component.
|