Skip to content

Commit

Permalink
feat <DynamicWidthInput /> (#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
takurinton authored Sep 25, 2023
1 parent 1366b37 commit 731383d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-houses-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ingred-ui": patch
---

fix DateField and DateRangeField width
3 changes: 2 additions & 1 deletion src/components/DateField/DateField/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useMergeRefs } from "../../../hooks/useMergeRefs";
import { CalendarIcon, InputContainer } from "./styled";
import { Dayjs } from "dayjs";
import { useTheme } from "../../../themes";
import { getInputWidth } from "../internal/utils";

export type DateFieldProps = {
/**
Expand Down Expand Up @@ -42,7 +43,7 @@ const DateField = forwardRef<HTMLInputElement, DateFieldProps>(
propRef,
) {
const theme = useTheme();
const width = useMemo(() => format.length * 10, [format]);
const width = useMemo(() => `${getInputWidth(format) * 10}px`, [format]);
const { ref: inputRef, ...props } = useDateField({ format, ...rest });
const ref = useMergeRefs<HTMLInputElement>(propRef, inputRef);

Expand Down
3 changes: 2 additions & 1 deletion src/components/DateField/DateRangeField/DateRangeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ClickStateType,
} from "../../Calendar/CalendarRange/constants";
import { useTheme } from "../../../themes";
import { getInputWidth } from "../internal/utils";

export type DateRangeFieldProps = {
/**
Expand Down Expand Up @@ -52,7 +53,7 @@ const DateRangeField = forwardRef<HTMLInputElement, DateRangeFieldProps>(
propRef,
) {
const theme = useTheme();
const width = useMemo(() => format.length * 12, [format]);
const width = useMemo(() => `${getInputWidth(format) * 10}px`, [format]);

const handleChange = (t: ClickStateType) => (date: Dayjs) => {
const { startDate, endDate } = rest.date;
Expand Down
27 changes: 27 additions & 0 deletions src/components/DateField/internal/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getInputWidth } from "../utils";

describe("getInputWidth", () => {
it("should return the correct width for a single-byte character", () => {
const input = "a";
const expectedWidth = 1;
expect(getInputWidth(input)).toEqual(expectedWidth);
});

it("should return the correct width for a double-byte character", () => {
const input = "あ";
const expectedWidth = 2;
expect(getInputWidth(input)).toEqual(expectedWidth);
});

it("should return the correct width for a mix of single-byte and double-byte characters", () => {
const input = "aあb";
const expectedWidth = 4;
expect(getInputWidth(input)).toEqual(expectedWidth);
});

it("should return 0 for an empty string", () => {
const input = "";
const expectedWidth = 0;
expect(getInputWidth(input)).toEqual(expectedWidth);
});
});
5 changes: 5 additions & 0 deletions src/components/DateField/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getInputWidth = (input: string) =>
[...input.split("")].reduce((acc, char) => {
const code = char.charCodeAt(0);
return acc + (code >= 0x3000 && code <= 0xff60 ? 2 : 1);
}, 0);

0 comments on commit 731383d

Please sign in to comment.