feat: migrate DateField and shared date input group
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
mergeProps,
|
||||
type SlotsType,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import { injectDateFieldRootContext } from "reka-ui";
|
||||
import { dateInputGroupVariants } from "@heroui-vue/styles";
|
||||
import {
|
||||
dateGroupKey,
|
||||
dateSegmentsKey,
|
||||
useDateField,
|
||||
type DateSegment,
|
||||
} from "./context";
|
||||
import DateInputGroupSegment from "./DateInputGroupSegment.vue";
|
||||
export default defineComponent({
|
||||
name: "DateInputGroupInput",
|
||||
inheritAttrs: false,
|
||||
props: { as: { type: String, default: "div" } },
|
||||
slots: Object as SlotsType<{
|
||||
default?: (props: { segment: DateSegment; index: number }) => VNodeChild;
|
||||
}>,
|
||||
setup(props, { attrs, slots }) {
|
||||
const field = useDateField();
|
||||
const base = injectDateFieldRootContext();
|
||||
const styles = inject(dateGroupKey, undefined);
|
||||
const segments = inject(dateSegmentsKey)!;
|
||||
return () =>
|
||||
h(
|
||||
props.as,
|
||||
mergeProps(attrs, {
|
||||
class: (styles?.value ?? dateInputGroupVariants()).input(),
|
||||
"data-slot": "date-input-group-input",
|
||||
}),
|
||||
segments.value.map((part, index) => {
|
||||
const isPlaceholder =
|
||||
part.part in base.segmentValues.value &&
|
||||
base.segmentValues.value[
|
||||
part.part as keyof typeof base.segmentValues.value
|
||||
] === null;
|
||||
let text = part.value;
|
||||
if (
|
||||
!isPlaceholder &&
|
||||
["month", "day", "hour", "minute", "second"].includes(part.part) &&
|
||||
/^\d+$/.test(text)
|
||||
) {
|
||||
if (field.leadingZeros() === true) text = text.padStart(2, "0");
|
||||
else if (field.leadingZeros() === false)
|
||||
text = String(Number(text));
|
||||
}
|
||||
const segment: DateSegment = {
|
||||
...part,
|
||||
type: part.part,
|
||||
text,
|
||||
value: text,
|
||||
isPlaceholder,
|
||||
isEditable: part.part !== "literal" && part.part !== "timeZoneName",
|
||||
};
|
||||
return h(
|
||||
"span",
|
||||
{ key: `${part.part}-${index}`, style: { display: "contents" } },
|
||||
slots.default?.({ segment, index }) ??
|
||||
h(DateInputGroupSegment, { segment }),
|
||||
);
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user