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

Added charMap property in Digit. Added Story for additional character… #3

Merged
merged 7 commits into from
Dec 3, 2024
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
*storybook.log
*storybook.log
.idea
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-7-segment-display",
"version": "1.1.2",
"version": "1.1.3.1",
"description": "A react component that simulates a 7-segment display",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
13 changes: 9 additions & 4 deletions src/components/Digit.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import Segment from "./Segment";
import React, { useEffect, useState } from "react";

import charToDigit from "../utils/charToDigit";
import charToDigit, { CharToDigit } from "../utils/charToDigit";

const letters = ["A", "B", "C", "D", "E", "F", "G"] as const;

const DEFAULT_CHAR = "-";

type DigitType = {
char: string;
color: string;
height: number;
skew: boolean;
charMap?: CharToDigit;
};


export const Digit = ({
char = "-",
char = DEFAULT_CHAR,
color = "red",
height = 250,
skew = false,
charMap = charToDigit,
}: DigitType) => {
const style = {
height: `${height}px`,
Expand All @@ -27,11 +32,11 @@ export const Digit = ({
} as React.CSSProperties;

const [activeArray, setActiveArray] = useState(
char ? charToDigit[char] : charToDigit["-"],
char && char in charMap ? charMap[char] : charMap[DEFAULT_CHAR],
);

useEffect(() => {
setActiveArray(char ? charToDigit[char] : charToDigit["-"]);
setActiveArray(char && char in charMap ? charMap[char] : charMap[DEFAULT_CHAR]);
}, [char]);

return (
Expand Down
4 changes: 4 additions & 0 deletions src/components/Display.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import defaultCharMap, { CharToDigit } from "../utils/charToDigit";
import { Digit } from "./Digit";
import React, { useEffect, useState } from "react";

Expand All @@ -8,6 +9,7 @@ type DisplayType = {
color: string;
backgroundColor?: string;
skew: boolean;
charMap?: CharToDigit;
};

export const Display = ({
Expand All @@ -17,6 +19,7 @@ export const Display = ({
color = "red",
backgroundColor,
skew = false,
charMap = defaultCharMap,
}: DisplayType) => {
const [digits, setDigits] = useState([]);

Expand Down Expand Up @@ -70,6 +73,7 @@ export const Display = ({
height={height}
color={color}
skew={skew}
charMap={charMap}
/>
);
})
Expand Down
20 changes: 20 additions & 0 deletions src/stories/Digit.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Meta, StoryObj } from "@storybook/react";

import { Digit } from "../components/Digit";
import CharToDigit from "../utils/charToDigit";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the import should be chatToDigit

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite get it? Do I have a typo? Do you😅?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a typo. Since utils/charToDigit.ts exports charToDigit by default, you can rename it when you import it, that's why this code works anyways. But it should be charToDigit since it is the array that you want and not the type. Don't worry, I'll merge the PR and then fix it.


const meta = {
title: "Example/Digit",
Expand Down Expand Up @@ -44,3 +45,22 @@ export const Default: Story = {
skew: false,
},
};

export const WithCharMap: Story = {
args: {
color: "red",
height: 250,
char: "_",
skew: false,
charMap: {"_": [0,0,0,1,0,0,0], ...CharToDigit}
},
};

export const IllegalChar: Story = {
args: {
color: "red",
height: 250,
char: "^",
skew: false
},
};
16 changes: 16 additions & 0 deletions src/stories/Display.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Meta, StoryObj } from "@storybook/react";

import { Display } from "../components/Display";
import CharToDigit from "../utils/charToDigit";

const meta = {
title: "Example/Display",
Expand Down Expand Up @@ -33,3 +34,18 @@ export const Default: Story = {
skew: false,
},
};

export const WithCharMap: Story = {
args: {
color: "red",
height: 250,
value: "_- _",
count: 4,
backgroundColor: "black",
skew: false,
charMap: {
"_": [0,0,0,1,0,0,0],
" ": [0,0,0,0,0,0,0],
...CharToDigit}
},
};
8 changes: 5 additions & 3 deletions src/utils/charToDigit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const charToDigit = {
export type CharToDigit = Record<string, [number, number, number, number, number, number, number]>;

const defaultCharMap: CharToDigit = {
"0": [1, 1, 1, 1, 1, 1, 0],
"1": [0, 1, 1, 0, 0, 0, 0],
"2": [1, 1, 0, 1, 1, 0, 1],
Expand All @@ -17,6 +19,6 @@ const charToDigit = {
e: [1, 0, 0, 1, 1, 1, 1],
f: [1, 0, 0, 0, 1, 1, 1],
"-": [0, 0, 0, 0, 0, 0, 1],
} as { [key: string]: number[] };
} as CharToDigit;

export default charToDigit;
export default defaultCharMap;