Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/prevent last dash from overflowing #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Dash.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import { getDashStyle, isStyleRow } from '../util'
const Dash = props => {
const isRow = isStyleRow(props.style)
const length = isRow ? props.width : props.height
const n = Math.ceil(length / (props.dashGap + props.dashLength))
const fullDashesCount = Math.floor(length / (props.dashGap + props.dashLength))
const lastDashSize = length - fullDashesCount * (props.dashGap + props.dashLength);
const calculatedDashStyles = getDashStyle(props)
const calculatedLastDashStyle = getDashStyle(props, lastDashSize)
let dash = []
for (let i = 0; i < n; i++) {
for (let i = 0; i < fullDashesCount; i++) {
dash.push(
<View
key={ i }
Expand All @@ -27,6 +29,17 @@ const Dash = props => {
/>
)
}
if (lastDashSize > 0) {
dash.push(
<View
key={ fullDashesCount }
style={ [
calculatedLastDashStyle,
props.dashStyle,
] }
/>
);
}
return (
<View
onLayout={ props.onLayout }
Expand Down
12 changes: 10 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
var Dash = function Dash(props) {
var isRow = (0, _util.isStyleRow)(props.style);
var length = isRow ? props.width : props.height;
var n = Math.ceil(length / (props.dashGap + props.dashLength));
var fullDashesCount = Math.floor(length / (props.dashGap + props.dashLength));
var lastDashSize = length - fullDashesCount * (props.dashGap + props.dashLength);
var calculatedDashStyles = (0, _util.getDashStyle)(props);
var calculatedLastDashStyle = (0, _util.getDashStyle)(props, lastDashSize);
var dash = [];
for (var i = 0; i < n; i++) {
for (var i = 0; i < fullDashesCount; i++) {
dash.push(_react2.default.createElement(_reactNative.View, {
key: i,
style: [calculatedDashStyles, props.dashStyle]
}));
}
if (lastDashSize > 0) {
dash.push(_react2.default.createElement(_reactNative.View, {
key: fullDashesCount,
style: [calculatedLastDashStyle, props.dashStyle]
}));
}
return _react2.default.createElement(
_reactNative.View,
{
Expand Down
21 changes: 13 additions & 8 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,40 @@ export const isStyleRow = style => {
const getDashStyleId = (
{ dashGap, dashLength, dashThickness, dashColor },
isRow,
isShortened = false
) =>
`${dashGap}-${dashLength}-${dashThickness}-${dashColor}-${
isRow ? 'row' : 'column'
}`
}-${isShortened}`

const createDashStyleSheet = (
{ dashGap, dashLength, dashThickness, dashColor },
isRow,
maxLength,
) => {
const currentDashLength = maxLength === undefined ? dashLength : Math.min(maxLength, dashLength);
const currentGapLength = maxLength === undefined ? dashGap : Math.max(maxLength - dashLength, 0);

const idStyle = StyleSheet.create({
style: {
width: isRow ? dashLength : dashThickness,
height: isRow ? dashThickness : dashLength,
marginRight: isRow ? dashGap : 0,
marginBottom: isRow ? 0 : dashGap,
width: isRow ? currentDashLength : dashThickness,
height: isRow ? dashThickness : currentDashLength,
marginRight: isRow ? currentGapLength : 0,
marginBottom: isRow ? 0 : currentGapLength,
backgroundColor: dashColor,
},
})
return idStyle.style
}

let stylesStore = {}
export const getDashStyle = props => {
export const getDashStyle = (props, maxLength=undefined) => {
const isRow = isStyleRow(props.style)
const id = getDashStyleId(props, isRow)
const id = getDashStyleId(props, isRow, maxLength !== undefined)
if (!stylesStore[id]) {
stylesStore = {
...stylesStore,
[id]: createDashStyleSheet(props, isRow),
[id]: createDashStyleSheet(props, isRow, maxLength),
}
}
return stylesStore[id]
Expand Down