-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add chart size type overrides (#317)
Now the Chart size prop takes multiple types as input: basic string or number, a tuple of string or number or undefined, an object with width and height (string, number or optionals). fix #177
- Loading branch information
Showing
6 changed files
with
165 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
.chart { | ||
position: relative; | ||
width: 100%; | ||
height: 50%; | ||
height: 100%; | ||
} | ||
</style> | ||
</head> | ||
|
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,62 @@ | ||
import { getChartSize } from './chart_size'; | ||
|
||
describe('chart size utilities', () => { | ||
test('array', () => { | ||
expect(getChartSize([100, 100])).toEqual({ | ||
width: 100, | ||
height: 100, | ||
}); | ||
expect(getChartSize([undefined, 100])).toEqual({ | ||
width: '100%', | ||
height: 100, | ||
}); | ||
expect(getChartSize([100, undefined])).toEqual({ | ||
width: 100, | ||
height: '100%', | ||
}); | ||
expect(getChartSize([undefined, undefined])).toEqual({ | ||
width: '100%', | ||
height: '100%', | ||
}); | ||
expect(getChartSize([0, '100em'])).toEqual({ | ||
width: 0, | ||
height: '100em', | ||
}); | ||
}); | ||
test('value', () => { | ||
expect(getChartSize(1)).toEqual({ | ||
width: 1, | ||
height: 1, | ||
}); | ||
expect(getChartSize('100em')).toEqual({ | ||
width: '100em', | ||
height: '100em', | ||
}); | ||
expect(getChartSize(0)).toEqual({ | ||
width: 0, | ||
height: 0, | ||
}); | ||
}); | ||
test('object', () => { | ||
expect(getChartSize({ width: 100, height: 100 })).toEqual({ | ||
width: 100, | ||
height: 100, | ||
}); | ||
expect(getChartSize({ height: 100 })).toEqual({ | ||
width: '100%', | ||
height: 100, | ||
}); | ||
expect(getChartSize({ width: 100 })).toEqual({ | ||
width: 100, | ||
height: '100%', | ||
}); | ||
expect(getChartSize({})).toEqual({ | ||
width: '100%', | ||
height: '100%', | ||
}); | ||
expect(getChartSize({ width: 0, height: '100em' })).toEqual({ | ||
width: 0, | ||
height: '100em', | ||
}); | ||
}); | ||
}); |
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,26 @@ | ||
export type ChartSizeArray = [number | string | undefined, number | string | undefined]; | ||
export interface ChartSizeObject { | ||
width?: number | string; | ||
height?: number | string; | ||
} | ||
|
||
export type ChartSize = number | string | ChartSizeArray | ChartSizeObject; | ||
|
||
export function getChartSize(size: ChartSize) { | ||
if (Array.isArray(size)) { | ||
return { | ||
width: size[0] === undefined ? '100%' : size[0], | ||
height: size[1] === undefined ? '100%' : size[1], | ||
}; | ||
} else if (typeof size === 'object') { | ||
return { | ||
width: size.width === undefined ? '100%' : size.width, | ||
height: size.height === undefined ? '100%' : size.height, | ||
}; | ||
} | ||
const sameSize = size === undefined ? '100%' : size; | ||
return { | ||
width: sameSize, | ||
height: sameSize, | ||
}; | ||
} |
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