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

Refactor Limits component #361

Merged
merged 1 commit into from
Apr 19, 2022
Merged
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
26 changes: 16 additions & 10 deletions love/src/components/GeneralPurpose/Limits/Limits.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import styles from './Limits.module.css';

export default class Limits extends Component {
static propTypes = {
/** Minimum possible value, start of the limit bar */
lowerLimit: PropTypes.number,
/** Maximum possible value, end of the limit bar */
upperLimit: PropTypes.number,
/** Current value */
currentValue: PropTypes.number,
/** Target value */
targetValue: PropTypes.number,
/** Option to show labels */
displayLabels: PropTypes.bool,
/** Height of the limit bar */
height: PropTypes.number,
/** Size of the warning zone in the same scale as lowerLimit and upperLimit */
limitWarning: PropTypes.number,
};

static defaultProps = {
Expand All @@ -23,16 +31,14 @@ export default class Limits extends Component {
};

render() {
const { lowerLimit, upperLimit, limitWarning } = this.props;
const { currentValue, targetValue } = this.props;
const height = this.props.height;
const { lowerLimit, upperLimit, limitWarning, currentValue, targetValue, height } = this.props;
const barHeight = height / 7;
const xMargin = 5;
const currentValueX = xMargin + ((100 - 2 * xMargin) * (currentValue - lowerLimit)) / (upperLimit - lowerLimit);
const targetValueX = xMargin + ((100 - 2 * xMargin) * (targetValue - lowerLimit)) / (upperLimit - lowerLimit);
const currentValueX = ((100 - 2 * xMargin) * (currentValue - lowerLimit)) / (upperLimit - lowerLimit);
const targetValueX = ((100 - 2 * xMargin) * (targetValue - lowerLimit)) / (upperLimit - lowerLimit);
const yOffset = height / 3;
const limitWarningPixels = (limitWarning / (upperLimit - lowerLimit)) * 100;
const isInWarningZone = currentValue > upperLimit || currentValue < lowerLimit;
const limitWarningPixels = (limitWarning / (upperLimit - lowerLimit)) * (100 - 2 * xMargin);
const isInWarningZone = currentValue > upperLimit - limitWarning || currentValue < lowerLimit + limitWarning;

return (
<div className={styles.container}>
Expand Down Expand Up @@ -63,17 +69,17 @@ export default class Limits extends Component {
/>
<rect
className={styles.currentValue}
x={currentValueX - 0.5}
x={currentValueX + xMargin}
y={height / 3 + yOffset}
height={height / 3}
width={1}
strokeWidth={0}
/>
<line
className={styles.targetValue}
x1={targetValueX}
x1={targetValueX + xMargin}
y1={height / 3 + yOffset}
x2={targetValueX}
x2={targetValueX + xMargin}
y2={(2 * height) / 3 + yOffset}
/>
{this.props.displayLabels && (
Expand Down