Skip to content

Commit fa53321

Browse files
jpnelsonljharb
authored andcommitted
[New] DirectionProvider: add lang prop
1 parent 3db695c commit fa53321

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,30 @@ Usage example:
5555
import DirectionProvider, { DIRECTIONS } from 'react-with-direction/dist/DirectionProvider';
5656
```
5757

58-
```js
58+
```jsx
5959
<DirectionProvider direction={DIRECTIONS.RTL}>
6060
<div>
6161
<ForwardsLabel />
6262
</div>
6363
</DirectionProvider>
6464
```
6565

66+
To set the `lang` attribute on the wrapping element, provide the `lang` prop to `DirectionProvider`.
67+
68+
Usage example:
69+
70+
```jsx
71+
import DirectionProvider, { DIRECTIONS } from 'react-with-direction/dist/DirectionProvider';
72+
73+
<DirectionProvider direction={DIRECTIONS.RTL} lang="ar">
74+
<div>
75+
<ForwardsLabel />
76+
</div>
77+
</DirectionProvider>
78+
```
79+
80+
Note that `lang` and `direction` are independent – `lang` only sets the attribute on the wrapping element.
81+
6682
## AutoDirectionProvider
6783

6884
Use `AutoDirectionProvider` around, for example, user-generated content where the text direction is unknown or may change. This renders a `DirectionProvider` with the `direction` prop automatically set based on the `text` prop provided.
@@ -83,6 +99,8 @@ import AutoDirectionProvider from 'react-with-direction/dist/AutoDirectionProvid
8399
</AutoDirectionProvider>
84100
```
85101

102+
`AutoDirectionProvider` also supports the `lang` prop in the same way as `DirectionProvider` does.
103+
86104
[package-url]: https://npmjs.org/package/react-with-direction
87105
[npm-version-svg]: http://versionbadg.es/airbnb/react-with-direction.svg
88106
[travis-svg]: https://travis-ci.org/airbnb/react-with-direction.svg

src/AutoDirectionProvider.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const propTypes = forbidExtraProps({
1111
direction: directionPropType.isRequired,
1212
inline: PropTypes.bool,
1313
text: PropTypes.string.isRequired,
14+
lang: PropTypes.string,
1415
});
1516

1617
const defaultProps = {
@@ -22,6 +23,7 @@ function AutoDirectionProvider({
2223
direction,
2324
inline,
2425
text,
26+
lang,
2527
}) {
2628
const textDirection = getDirection(text);
2729
const dir = textDirection === 'neutral' ? direction : textDirection;
@@ -30,6 +32,7 @@ function AutoDirectionProvider({
3032
<DirectionProvider
3133
direction={dir}
3234
inline={inline}
35+
{...(lang && { lang })}
3336
>
3437
{React.Children.only(children)}
3538
</DirectionProvider>

src/DirectionProvider.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const propTypes = forbidExtraProps({
1414
children: PropTypes.node.isRequired,
1515
direction: directionPropType.isRequired,
1616
inline: PropTypes.bool,
17+
lang: PropTypes.string,
1718
});
1819

1920
const defaultProps = {
@@ -45,10 +46,10 @@ export default class DirectionProvider extends React.Component {
4546
}
4647

4748
render() {
48-
const { children, direction, inline } = this.props;
49+
const { children, direction, inline, lang } = this.props;
4950
const Tag = inline ? 'span' : 'div';
5051
return (
51-
<Tag dir={direction}>
52+
<Tag dir={direction} {...(lang && { lang })}>
5253
{React.Children.only(children)}
5354
</Tag>
5455
);

tests/AutoDirectionProvider_test.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,14 @@ describe('<AutoDirectionProvider>', () => {
8787

8888
expect(wrapper.find(DirectionProvider)).to.have.prop('inline', true);
8989
});
90+
91+
it('passes the lang prop to DirectionProvider', () => {
92+
const wrapper = shallow((
93+
<AutoDirectionProvider text="a" lang="en">
94+
<div />
95+
</AutoDirectionProvider>
96+
)).dive();
97+
98+
expect(wrapper.find(DirectionProvider)).to.have.prop('lang', 'en');
99+
});
90100
});

tests/DirectionProvider_test.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ describe('<DirectionProvider>', () => {
3737
expect(wrapper).to.have.prop('dir', direction);
3838
});
3939

40+
it('renders a lang attribute when the lang prop is set', () => {
41+
const direction = DIRECTIONS.RTL;
42+
const wrapper = shallow(
43+
<DirectionProvider direction={direction} lang="ar">{children}</DirectionProvider>,
44+
);
45+
expect(wrapper).to.have.prop('lang', 'ar');
46+
});
47+
4048
it('broadcasts the direction when the direction prop changes', () => {
4149
const direction = DIRECTIONS.LTR;
4250
const nextDirection = DIRECTIONS.RTL;

0 commit comments

Comments
 (0)