UNPKG

react-7-segment-display-plus

Version:

A react component that simulates a 7-segment display

55 lines 2.61 kB
import { Digit } from "./Digit"; import React, { useEffect, useState } from "react"; import { Dot } from "./Dot"; export const Display = ({ count = 2, height = 250, value = null, color = "red", backgroundColor, skew = false, }) => { const [digits, setDigits] = useState([]); const [dotIndex, setDotIndex] = useState(-1); const style = { display: "flex", flexDirection: "row", justifyContent: "center", alignItems: "center", height: "fit-content", width: "fit-content", padding: "20px", }; const displayStyle = { display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", height: "fit-content", width: "fit-content", backgroundColor: backgroundColor ? backgroundColor : "transparent", padding: "20px", color: "white", }; useEffect(() => { let dotIndex = value ? value.toString().indexOf(".") - 1 : -1; const newValue = dotIndex > -1 ? value.toString().replace(".", "") : value; let newDigits = newValue && newValue.toString().split(""); if (!newValue || count < newValue.toString().length) { newDigits = null; } if (newValue && count > newValue.toString().length) { for (let i = 0; i < count - newValue.toString().length; i++) { newDigits.unshift("0"); } } setDigits(newDigits); setDotIndex(dotIndex > -1 ? dotIndex + count - newValue.toString().length : -1); }, [count, value]); return (React.createElement("div", { className: "display", style: displayStyle }, React.createElement("div", { className: "display-digits", style: style }, digits ? digits.map((digit, index) => { return (React.createElement(React.Fragment, null, React.createElement(Digit, { key: `digit-${index}`, char: digit, height: height, color: color, skew: skew }), React.createElement(Dot, { key: `dot-${index}`, active: index === dotIndex, color: color, height: height, skew: skew }))); }) : Array.from(Array(count).keys()).map((index) => { return (React.createElement(React.Fragment, null, React.createElement(Digit, { key: `digit-${index}`, char: "-", height: height, color: color, skew: skew }), React.createElement(Dot, { key: `dot-${index}`, active: false, color: color, height: height, skew: skew }))); })))); }; //# sourceMappingURL=Display.js.map