Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AutoDirectionProvider] Add component for automatically detected dire…
…ction Adds the `AutoDirectionProvider` component for rendering a `DirectionProvider` based on the first strong LTR/RTL character in the `text` prop. This provides a clean way to handle user-generated content, including forms where UI changes on the fly to handle RTL/LTR input.
- Loading branch information
1 parent
1339e80
commit ad2095a
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { forbidExtraProps } from 'airbnb-prop-types'; | ||
import getDirection from 'direction'; | ||
import directionPropType from './proptypes/direction'; | ||
import DirectionProvider from './DirectionProvider'; | ||
import withDirection from './withDirection'; | ||
|
||
const propTypes = forbidExtraProps({ | ||
children: PropTypes.node.isRequired, | ||
direction: directionPropType.isRequired, | ||
inline: PropTypes.bool, | ||
text: PropTypes.string.isRequired, | ||
}); | ||
|
||
const defaultProps = { | ||
inline: false, | ||
}; | ||
|
||
function AutoDirectionProvider({ | ||
children, | ||
direction, | ||
inline, | ||
text, | ||
}) { | ||
const textDirection = getDirection(text); | ||
const dir = textDirection === 'neutral' ? direction : textDirection; | ||
|
||
return ( | ||
<DirectionProvider | ||
direction={dir} | ||
inline={inline} | ||
> | ||
{children} | ||
</DirectionProvider> | ||
); | ||
} | ||
|
||
AutoDirectionProvider.propTypes = propTypes; | ||
AutoDirectionProvider.defaultProps = defaultProps; | ||
|
||
export default withDirection(AutoDirectionProvider); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import AutoDirectionProvider from '../src/AutoDirectionProvider'; | ||
import DirectionProvider from '../src/DirectionProvider'; | ||
import { DIRECTIONS, CHANNEL } from '../src/constants'; | ||
import mockBrcast from './mocks/brcast_mock'; | ||
|
||
describe('<AutoDirectionProvider>', () => { | ||
it('renders a DirectionProvider', () => { | ||
const wrapper = shallow(( | ||
<AutoDirectionProvider text="a"> | ||
<div /> | ||
</AutoDirectionProvider> | ||
)).dive(); | ||
|
||
expect(wrapper).to.have.exactly(1).descendants(DirectionProvider); | ||
}); | ||
|
||
describe('direction prop', () => { | ||
it('is LTR correct for LTR strings', () => { | ||
const wrapper = shallow(( | ||
<AutoDirectionProvider text="a"> | ||
<div /> | ||
</AutoDirectionProvider> | ||
)).dive(); | ||
|
||
expect(wrapper.find(DirectionProvider)).to.have.prop('direction', DIRECTIONS.LTR); | ||
}); | ||
|
||
it('is RTL correct for RTL strings', () => { | ||
const wrapper = shallow(( | ||
<AutoDirectionProvider text="א"> | ||
<div /> | ||
</AutoDirectionProvider> | ||
)).dive(); | ||
|
||
expect(wrapper.find(DirectionProvider)).to.have.prop('direction', DIRECTIONS.RTL); | ||
}); | ||
|
||
it('in inherited from context for neutral strings', () => { | ||
const wrapper = shallow( | ||
( | ||
<AutoDirectionProvider text="1"> | ||
<div /> | ||
</AutoDirectionProvider> | ||
), { | ||
context: { | ||
[CHANNEL]: mockBrcast({ | ||
data: DIRECTIONS.RTL, | ||
}), | ||
}, | ||
}, | ||
).dive(); | ||
|
||
expect(wrapper.find(DirectionProvider)).to.have.prop('direction', DIRECTIONS.RTL); | ||
}); | ||
}); | ||
|
||
it('renders its children', () => { | ||
const children = <div>Foo</div>; | ||
|
||
const wrapper = shallow(( | ||
<AutoDirectionProvider text="a"> | ||
{children} | ||
</AutoDirectionProvider> | ||
)).dive(); | ||
|
||
expect(wrapper).to.contain(children); | ||
}); | ||
|
||
it('passes the inline prop to DirectionProvider', () => { | ||
let wrapper = shallow(( | ||
<AutoDirectionProvider text="a"> | ||
<div /> | ||
</AutoDirectionProvider> | ||
)).dive(); | ||
|
||
expect(wrapper.find(DirectionProvider)).to.have.prop('inline', false); | ||
|
||
wrapper = shallow(( | ||
<AutoDirectionProvider text="a" inline> | ||
<div /> | ||
</AutoDirectionProvider> | ||
)).dive(); | ||
|
||
expect(wrapper.find(DirectionProvider)).to.have.prop('inline', true); | ||
}); | ||
}); |