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

next: Time Field #582

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
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
55 changes: 35 additions & 20 deletions packages/bits-ui/src/lib/shared/date/field/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import {
} from "$lib/internal/index.js";
import { get, type Writable } from "svelte/store";

/**
* Initializes the segment values object which keeps track of the inputs
* for each segment in the field. This is then input into the formatter
* to generate the formatted strings to render in the field.
*/
export function initializeSegmentValues(granularity: Granularity) {
const calendarDateTimeGranularities = ["hour", "minute", "second"];
const initialParts = EDITABLE_SEGMENT_PARTS.map((part) => {
Expand Down Expand Up @@ -71,24 +76,27 @@ type CreateContentArrProps = SharedContentProps & {
contentObj: SegmentContentObj;
};

// TODO: clean me up
function createContentObj(props: CreateContentObjProps) {
const { segmentValues, formatter, locale, dateRef } = props;

const content = Object.keys(segmentValues).reduce((obj, part) => {
if (!isSegmentPart(part)) return obj;
// @ts-expect-error - we're populating the object with the keys in the loop
const content: SegmentContentObj = {};

for (const part of Object.keys(segmentValues)) {
if (!isSegmentPart(part)) continue;

if ("hour" in segmentValues && part === "dayPeriod") {
const value = segmentValues[part];
if (!isNull(value)) {
obj[part] = value;
content[part] = value;
} else {
obj[part] = getPlaceholder(part, "AM", locale);
content[part] = getPlaceholder(part, "AM", locale);
}
} else {
obj[part] = getPartContent(part);
content[part] = getPartContent(part);
}

return obj;
}, {} as SegmentContentObj);
}

function getPartContent(part: DateSegmentPart | TimeSegmentPart) {
if ("hour" in segmentValues) {
Expand Down Expand Up @@ -179,6 +187,10 @@ export function createContent(props: CreateContentProps) {
};
}

/**
* Get the options to use for the `Intl.DateTimeFormat` constructor based on
* the granularity and hour cycle of the field.
*/
function getOptsByGranularity(granularity: Granularity, hourCycle: HourCycle) {
const opts: Intl.DateTimeFormatOptions = {
year: "numeric",
Expand Down Expand Up @@ -208,23 +220,23 @@ function getOptsByGranularity(granularity: Granularity, hourCycle: HourCycle) {
return opts;
}

export function initSegmentStates() {
return EDITABLE_SEGMENT_PARTS.reduce((acc, key) => {
acc[key] = {
/**
* Initializes the segment states object which keeps track of state
* necessary to manage the behavior of the segments.
*/
export function initSegmentStates(): SegmentStateMap {
// @ts-expect-error - we're populating the object with the keys in the loop
const segmentStates: SegmentStateMap = {};

for (const key of EDITABLE_SEGMENT_PARTS) {
segmentStates[key] = {
lastKeyZero: false,
hasLeftFocus: true,
updating: null,
};
return acc;
}, {} as SegmentStateMap);
}
}

export function initSegmentIds() {
return Object.fromEntries(
ALL_SEGMENT_PARTS.map((part) => {
return [part, generateId()];
}).filter(([key]) => key !== "literal")
);
return segmentStates;
}

export function isDateSegmentPart(part: unknown): part is DateSegmentPart {
Expand Down Expand Up @@ -261,6 +273,9 @@ type GetValueFromSegments = {
dateRef: DateValue;
};

/**
* Creates a new `DateValue` object based on the state of the field.
*/
export function getValueFromSegments(props: GetValueFromSegments) {
const { segmentObj, fieldNode, dateRef } = props;
const usedSegments = getUsedSegments(fieldNode);
Expand Down
4 changes: 4 additions & 0 deletions packages/bits-ui/src/tests/date-field/DateField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,10 @@ describe("date field", () => {
expect(day).toHaveFocus();
expect(year).toHaveTextContent("0202");
});

it.todo(
"if a user enters something like `15` into the hour segment and a 12 hour cycle is being used, the value should not be changed to `3` and should instead be `5`"
);
});

// eslint-disable-next-line ts/no-explicit-any
Expand Down
Loading