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

components: Add CharCount #48

Merged
merged 1 commit into from
Feb 23, 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
1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@babel/preset-env": "^7.23.9",
"@babel/preset-react": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@dolthub/web-utils": "^0.1.2",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/CharCount/index.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.charCount {
@apply text-acc-grey;
}

.charCountOver {
@apply text-acc-red;
}
22 changes: 22 additions & 0 deletions packages/components/src/CharCount/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import cx from "classnames";
import React from "react";
import css from "./index.module.css";

export const maxChar = 2048;

type Props = {
desc: string;
className?: string;
};

export default function CharCount(props: Props) {
return (
<span
className={cx(css.charCount, props.className, {
[css.charCountOver]: props.desc.length > maxChar,
})}
>
{props.desc.length}/{maxChar}
</span>
);
}
30 changes: 30 additions & 0 deletions packages/components/src/__tests__/CharCount.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { nTimes } from "@dolthub/web-utils";
import { render, screen } from "@testing-library/react";
import React from "react";
import CharCount from "../CharCount";
import css from "./index.module.css";

const tilde = () => "~";

const mocks = [
{ words: "", over: false },
{ words: nTimes(9, tilde).join(""), over: false },
{ words: nTimes(2050, tilde).join(""), over: true },
];

describe("test CharCount", () => {
mocks.forEach(mock => {
it(`renders CharCount for word length of ${mock.words.length}`, () => {
render(<CharCount desc={mock.words} className="classname" />);

const content = screen.getByText(`${mock.words.length}/2048`);
expect(content).toBeVisible();
expect(content).toHaveClass(css.charCount);
expect(content).toHaveClass("classname");

if (mock.over) {
expect(content).toHaveClass(css.charCountOver);
}
});
});
});
1 change: 1 addition & 0 deletions packages/components/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as CharCount } from "./CharCount";
export { default as ExternalLink } from "./ExternalLink";
export { default as Loader } from "./Loader";
export { default as Popup, PopupProps } from "./Popup";
Expand Down
7 changes: 7 additions & 0 deletions packages/components/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const colors = {
"acc-grey": "#b2c0c4",
"acc-red": "#ff9a99",
};

const config = {
corePlugins: {
preflight: false,
Expand All @@ -6,6 +11,8 @@ const config = {
theme: {
extend: {
transitionProperty: { width: "width" },
gradientColorStops: colors,
colors,
fontFamily: {
sans: ["Source Sans Pro", "sans-serif"],
},
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,7 @@ __metadata:
"@babel/preset-env": "npm:^7.23.9"
"@babel/preset-react": "npm:^7.23.3"
"@babel/preset-typescript": "npm:^7.23.3"
"@dolthub/web-utils": "npm:^0.1.2"
"@rollup/plugin-commonjs": "npm:^25.0.7"
"@rollup/plugin-node-resolve": "npm:^15.2.3"
"@rollup/plugin-typescript": "npm:^11.1.5"
Expand Down
Loading