Skip to content

Commit 996d995

Browse files
committed
Merge pull request #16 from talyssonoc/html_attributes_at_wrapper_element
Wrapper element can now receive HTML attributes
2 parents c3b3258 + a01b34a commit 996d995

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ With `react-laravel` you'll be able to use [ReactJS](https://facebook.github.io/
99
You just need to add this to your `composer.json`'s `"require"`:
1010

1111
```json
12-
"talyssonoc/react-laravel": "0.3"`
12+
"talyssonoc/react-laravel": "0.4"`
1313
```
1414

1515
Also you got to set the `minimum-stability` of your `composer.json` to `dev`, adding this:
@@ -69,8 +69,9 @@ The `@react_component` directive accepts 3 arguments:
6969
* `componentName`: Is the name of the global variable that holds your component.
7070
* `props`: Associative of the `props` that'll be passed to your component
7171
* `options`: Associative array of options that you can pass to the `react-laravel`:
72-
* `prerender`: Tells react-laravel to render your component server-side too and then just _mount_ it on the client-side. Default to __true__.
72+
* `prerender`: Tells react-laravel to render your component server-side, and then just _mount_ it on the client-side. Default to __true__.
7373
* `tag`: The tag of the element that'll hold your component. Default to __'div'__.
74+
* _html attributes_: Any other valid HTML attribute that will be added to the wrapper element of your component. Example: `'id' => 'my_component'`.
7475

7576
All your components should be inside `public/js/components.js` (you can configure it, see below) and be global.
7677

@@ -108,4 +109,8 @@ Both of then are optional.
108109
* `source`: defaults to `public/vendor/react/laravel/react.js`.
109110
* `components`: defaults to `public/js/components.js`
110111

111-
Your `components.js` file should also be included at your view, and all your components must be at the `window` object.
112+
Your `components.js` file should also be included at your view, and all your components must be at the `window` object.
113+
114+
# Thanks
115+
116+
This package is inspired at [react-rails](https://github.com/reactjs/react-rails).

lib/React.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,16 @@ public function render($component, $props = null, $options = []) {
3737
$markup = '';
3838
}
3939

40-
$props = htmlentities(json_encode($props), ENT_QUOTES);
4140
$tag = $options['tag'];
41+
$props = htmlentities(json_encode($props), ENT_QUOTES);
42+
43+
// Gets all values that aren't used as options and map it as HTML attributes
44+
$htmlAttributes = array_diff_key($options, $this->defaultOptions);
45+
$htmlAttributesString = '';
46+
foreach ($htmlAttributes as $attribute => $value) {
47+
$htmlAttributesString .= "{$attribute}='{$value}'";
48+
}
4249

43-
return "<{$tag} data-react-class='{$component}' data-react-props='{$props}'>{$markup}</{$tag}>";
50+
return "<{$tag} data-react-class='{$component}' data-react-props='{$props}' {$htmlAttributesString}>{$markup}</{$tag}>";
4451
}
4552
}

tests/WrapperTagTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,23 @@ public function testCustomWrapperTagName() {
5656

5757
$this->assertEquals('span', $element->tagName);
5858
}
59+
60+
public function testHTMLAttributes() {
61+
62+
$attributes = [
63+
'id' => 'react-laravel',
64+
'class' => 'react_laravel_class',
65+
'tag' => 'span'
66+
];
67+
68+
$elementString = $this->react->render('Alert', null, $attributes);
69+
70+
$element = TestHelpers::stringToElement($elementString);
71+
72+
$this->assertTrue($element->hasAttributes());
73+
$this->assertEquals('react-laravel', $element->getAttribute('id'));
74+
$this->assertEquals('react_laravel_class', $element->getAttribute('class'));
75+
$this->assertEquals('', $element->getAttribute('tag'));
76+
$this->assertEquals('', $element->getAttribute('prerender'));
77+
}
5978
}

0 commit comments

Comments
 (0)