# Add \`map\` method to \`Component\`s \(\#3210\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/ec0cff0e1a251d7bf3ab9b92c8bd49de375518c5)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: ec0cff0e1a251d7bf3ab9b92c8bd49de375518c5

Requested commit: ec0cff0e1a251d7bf3ab9b92c8bd49de375518c5

Commit: ec0cff0e1a251d7bf3ab9b92c8bd49de375518c5

Tree: 25452cd2e73cd0ab506196ee961b5a4746b9b06b

Author: Marshall Bowers

Committer: GitHub

## Message

````
Add `map` method to `Component`s (#3210)

This PR adds a `map` method to the `Component` trait.

`map` is a fully-generalized form of `when`, as `when` can be expressed
in terms of `map`:

```rs
div().map(|this| if condition { then(this) } else { this })
```

This allows us to take advantage of Rust's pattern matching when
building up conditions:

```rs
// Before
div()
    .when(self.current_side == PanelSide::Left, |this| this.border_r())
    .when(self.current_side == PanelSide::Right, |this| {
        this.border_l()
    })
    .when(self.current_side == PanelSide::Bottom, |this| {
        this.border_b().w_full().h(current_size)
    })

// After
div()
    .map(|this| match self.current_side {
        PanelSide::Left => this.border_r(),
        PanelSide::Right => this.border_l(),
        PanelSide::Bottom => this.border_b().w_full().h(current_size),
    })
```

Release Notes:

- N/A
````

## Parents

- [5e12b48ae00dfda91ac71a921ebda088fef99a88](https://git.cafe/gitcafe/zed/commit/5e12b48ae00dfda91ac71a921ebda088fef99a88?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/ec0cff0e1a251d7bf3ab9b92c8bd49de375518c5?format=markdown)
