| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- import {
- useImperativeHandle,
- forwardRef,
- useEffect,
- useState,
- type Ref,
- useRef
- } from "react";
- import {
- TouchableOpacity,
- View
- } from "react-native";
- import type IDateTimePickerProps from "./type";
- import type {
- DateTimePickerDateRange,
- DateTimePickerType,
- IDateTimePickerRef
- } from "./type";
- import stylesheet, {
- getDateTimePickerType,
- useStyles
- } from "./stylesheet";
- import {
- NCoreUIKitLocalize,
- NCoreUIKitTheme
- } from "../../core/hooks";
- import {
- type NCoreUIKitIcon
- } from "../../types";
- import type {
- IBottomSheetRef
- } from "../bottomSheet/type";
- import type ITextProps from "../text/type";
- import {
- type RRule as RRuleType,
- RRule
- } from "rrule";
- import moment from "moment";
- import {
- BadgeQuestionMarkIcon,
- BadgeSuccessIcon,
- BadgeDangerIcon,
- BadgeAlertIcon,
- BadgeInfoIcon,
- CleanIcon
- } from "../../assets/svg";
- import {
- uuid
- } from "../../utils";
- import Text from "../text";
- import DateTimeSheet from "../dateTimeSheet";
- import {
- parseRRuleConfig
- } from "../../helpers";
- const DateTimePickerTypeIcon: Record<Exclude<DateTimePickerType, "default">, NCoreUIKitIcon> = {
- "question": BadgeQuestionMarkIcon,
- "success": BadgeSuccessIcon,
- "warning": BadgeAlertIcon,
- "danger": BadgeDangerIcon,
- "info": BadgeInfoIcon
- };
- const DateTimePicker = ({
- renderLoadingIcon : LoadingIconComponentProp,
- rightIcon: RightIconComponentProp,
- localeBasedFirstDayOfWeek = true,
- hintTextIcon: HintTextIconProp,
- spreadBehaviour = "baseline",
- customDateTimeSheetLocalize,
- isShowTodayIndicator = true,
- titleFormat = "DD MMM YYYY",
- isShowDateTimeTools = true,
- isShowHintTextIcon = false,
- isWorkWithRealtime = true,
- dayOfWeekLength = "short",
- customDateTimeSheetTheme,
- pickerType = "date-time",
- icon: IconComponentProp,
- hintTextContainerStyle,
- removeSelectValidation,
- isCleanEnabled = false,
- contentContainerStyle,
- subTitle = "Optional",
- isSearchable = false,
- onFocus: onFocusProp,
- isRequired = false,
- isDisabled = false,
- onBlur: onBlurProp,
- dateTimeSheetProps,
- variant = "single",
- hintTextIconStyle,
- initialDateRange,
- type = "default",
- selectValidation,
- initialDateRule,
- dateRangeTitle,
- customLocalize,
- rightIconStyle,
- isShowSubTitle,
- cleanIconStyle,
- maxChoice = -1,
- contentStyle,
- initialDate,
- customTheme,
- placeholder,
- isOptional,
- customKey,
- minChoice,
- iconStyle,
- onChange,
- hintText,
- onCancel,
- maxDate,
- minDate,
- style,
- title,
- onOk
- }: IDateTimePickerProps, ref: Ref<IDateTimePickerRef>) => {
- const {
- inlineSpaces,
- typography,
- radiuses,
- borders,
- spaces,
- colors
- } = NCoreUIKitTheme.useContext(customTheme);
- const {
- rruleConfig,
- localize
- } = NCoreUIKitLocalize.useContext(customLocalize);
- const bottomSheetRef = useRef<IBottomSheetRef>(null);
- const dateTimePickerKey = useRef(customKey ? customKey : uuid());
- const currentType = getDateTimePickerType({
- type
- });
- const styleType = type === "default" ? "neutral" : type === "question" ? "neutral" : type === "danger" ? "error" : type;
- const [
- isActive,
- setIsActive
- ] = useState(false);
- const [
- isLoading,
- setIsLoading
- ] = useState(false);
- const [
- tempDate,
- setTempDate
- ] = useState<Date | undefined>(initialDate);
- const [
- date,
- setDate
- ] = useState<Date | undefined>(initialDate);
- const [
- tempDateRule,
- setTempDateRule
- ] = useState<RRuleType | undefined>(initialDateRule);
- const [
- dateRule,
- setDateRule
- ] = useState<RRuleType | undefined>(initialDateRule);
- const [
- dateRange,
- setDateRange
- ] = useState<DateTimePickerDateRange | undefined>(initialDateRange);
- const [
- tempDateRange,
- setTempDateRange
- ] = useState<DateTimePickerDateRange | undefined>(initialDateRange);
- const mainDateRange = isWorkWithRealtime ? dateRange : tempDateRange;
- const mainDateRule = isWorkWithRealtime ? dateRule : tempDateRule;
- const mainDate = isWorkWithRealtime ? date : tempDate;
- const {
- contentContainer: contentContainerDynamicStyle,
- titleContainer: titleContainerDynamicStyle,
- hintTextIcon: hintTextIconDynamicStyle,
- contentText: contentTextDynamicStyle,
- cleanButton: cleanButtonDynamicStyle,
- rightIcon: rightIconDynamicStyle,
- container: containerDynamicStyle,
- hintText: hintTextDynamicStyle,
- required: requiredDynamicStyle,
- subTitle: subTitleDynamicStyle,
- overlay: overlayDynamicStyle,
- content: contentDynamicStyle,
- title: titleDynamicStyle,
- icon: iconDynamicStyle
- } = useStyles({
- icon: IconComponentProp ? true : false,
- spreadBehaviour,
- inlineSpaces,
- isSearchable,
- currentType,
- isDisabled,
- isActive,
- radiuses,
- borders,
- spaces,
- colors,
- title,
- type
- });
- useImperativeHandle(
- ref,
- () => ({
- setDateRange: (dateRange) => {
- if(isWorkWithRealtime) {
- setDateRange(dateRange);
- } else {
- setTempDateRange(dateRange);
- }
- },
- setDateRule: (dateRule) => {
- if(isWorkWithRealtime) {
- setDateRule(dateRule);
- } else {
- setTempDateRule(dateRule);
- }
- },
- setDate: (date) => {
- if(isWorkWithRealtime) {
- setDate(date);
- } else {
- setTempDate(date);
- }
- },
- getValue: () => {
- return {
- dateRange,
- dateRule,
- date
- };
- },
- cancel,
- clean,
- focus,
- blur,
- ok
- }),
- [
- dateRange,
- dateRule,
- date
- ]
- );
- useEffect(() => {
- if(isActive) {
- if(onFocus) onFocus();
- } else {
- if(!isWorkWithRealtime) {
- setTempDateRange(undefined);
- setTempDateRule(undefined);
- setTempDate(undefined);
- }
- if(onBlur) onBlur();
- }
- }, [
- isActive
- ]);
- useEffect(() => {
- if(onChange) {
- onChange({
- dateRange,
- dateRule,
- date
- });
- }
- }, [
- tempDateRange,
- tempDateRule,
- dateRange,
- dateRule,
- tempDate,
- date
- ]);
- const titleProps: ITextProps = {
- color: currentType.titleColor,
- variant: "bodyLargeSize"
- };
- const iconProps: NCoreUIKit.IconCallbackProps = {
- size: Number(typography.labelLargeSize.fontSize) + 6,
- color: currentType.iconColor
- };
- if(isDisabled) {
- iconProps.color = "disabled";
- }
- const blur = () => {
- setIsActive(false);
- };
- const focus = () => {
- if(!isWorkWithRealtime) {
- setTempDateRange(dateRange);
- setTempDateRule(dateRule);
- setTempDate(date);
- }
- setIsActive(true);
- };
- const cancel = () => {
- if(!isWorkWithRealtime) {
- bottomSheetRef.current?.close(() => {
- blur();
- });
- if(onCancel) {
- onCancel({
- dateRange,
- dateRule,
- date
- });
- }
- }
- };
- const ok = () => {
- if(!isWorkWithRealtime) {
- setDateRange(tempDateRange);
- setDateRule(tempDateRule);
- setDate(tempDate);
- bottomSheetRef.current?.close(() => {
- blur();
- });
- if(onOk) {
- onOk({
- dateRange,
- dateRule,
- date
- });
- }
- }
- };
- const clean = () => {
- setDateRange(undefined);
- setDateRule(undefined);
- setDate(undefined);
- setTempDateRange(undefined);
- setTempDateRule(undefined);
- setTempDate(undefined);
- };
- const selectDateRule = (selectedDateRule?: RRule) => {
- if(variant === "rrule") {
- if(selectedDateRule && selectedDateRule !== dateRule) {
- if(selectValidation) {
- if(selectValidation({
- dateRule: selectedDateRule
- })) {
- if(isWorkWithRealtime) {
- setDateRule(selectedDateRule);
- } else {
- setTempDateRule(selectedDateRule);
- }
- }
- } else {
- if(isWorkWithRealtime) {
- setDateRule(selectedDateRule);
- } else {
- setTempDateRule(selectedDateRule);
- }
- }
- } else {
- if(removeSelectValidation) {
- if(removeSelectValidation({
- dateRule
- })) {
- setTempDateRule(undefined);
- setDateRule(undefined);
- }
- } else {
- setTempDateRule(undefined);
- setDateRule(undefined);
- }
- }
- }
- };
- const selectMultipleObject = (selectedDateRange?: {
- start?: Date;
- end?: Date;
- }) => {
- if(variant === "range") {
- if(selectedDateRange) {
- if(selectValidation) {
- if(selectValidation({
- dateRule
- })) {
- if(isWorkWithRealtime) {
- setDateRange({
- start: selectedDateRange.start,
- end: selectedDateRange.end
- });
- } else {
- setTempDateRange({
- start: selectedDateRange.start,
- end: selectedDateRange.end
- });
- }
- }
- } else {
- if(isWorkWithRealtime) {
- setDateRange({
- start: selectedDateRange.start,
- end: selectedDateRange.end
- });
- } else {
- setTempDateRange({
- start: selectedDateRange.start,
- end: selectedDateRange.end
- });
- }
- }
- } else {
- if(removeSelectValidation) {
- if(removeSelectValidation({
- dateRange: selectedDateRange
- })) {
- setTempDateRange(undefined);
- setDateRange(undefined);
- }
- } else {
- setTempDateRange(undefined);
- setDateRange(undefined);
- }
- }
- }
- };
- const selectObject = (selectedDate?: Date) => {
- if(variant === "single") {
- if(selectedDate === date) {
- if(removeSelectValidation) {
- if(removeSelectValidation({
- date
- })) {
- if(isWorkWithRealtime) {
- setDate(undefined);
- } else {
- setTempDate(undefined);
- }
- }
- } else {
- if(isWorkWithRealtime) {
- setDate(undefined);
- } else {
- setTempDate(undefined);
- }
- }
- } else {
- if(selectValidation) {
- if(selectValidation({
- date
- })) {
- if(isWorkWithRealtime) {
- setDate(selectedDate);
- } else {
- setTempDate(selectedDate);
- }
- }
- } else {
- if(isWorkWithRealtime) {
- setDate(selectedDate);
- } else {
- setTempDate(selectedDate);
- }
- }
- }
- }
- };
- const onFocus = () => {
- if(onFocusProp) onFocusProp();
- };
- const onBlur = () => {
- if(onBlurProp) onBlurProp();
- };
- const renderCleanButton = () => {
- if(isDisabled) {
- return null;
- }
- if(!isCleanEnabled) {
- return null;
- }
- if(variant === "rrule") {
- if(!mainDateRule) return null;
- } else if(variant === "range") {
- if(!dateRange) return null;
- } else {
- if(!date) return null;
- }
- return <TouchableOpacity
- style={[
- cleanIconStyle,
- stylesheet.cleanButton,
- cleanButtonDynamicStyle
- ]}
- onPress={() => {
- setDateRange(undefined);
- setDateRule(undefined);
- setDate(undefined);
- setTempDateRange(undefined);
- setTempDateRule(undefined);
- setTempDate(undefined);
- }}
- >
- <CleanIcon
- color="mid"
- size={20}
- />
- </TouchableOpacity>;
- };
- const renderIcon = () => {
- if (!IconComponentProp) {
- return null;
- }
- return <View
- style={[
- iconStyle,
- stylesheet.icon,
- iconDynamicStyle
- ]}
- >
- <IconComponentProp
- color={iconProps.color}
- size={iconProps.size}
- />
- </View>;
- };
- const renderRightIcon = () => {
- if (!RightIconComponentProp) {
- return null;
- }
- if(isCleanEnabled) {
- return null;
- }
- if(variant === "rrule") {
- if(!mainDateRule) return null;
- } else if(variant === "range") {
- if(!mainDateRange) return null;
- } else {
- if(!mainDate) return null;
- }
- return <View
- style={[
- rightIconStyle,
- stylesheet.rightIcon,
- rightIconDynamicStyle
- ]}
- >
- <RightIconComponentProp
- color={iconProps.color}
- size={iconProps.size}
- />
- </View>;
- };
- const renderHintIcon = () => {
- if(!isShowHintTextIcon) {
- return null;
- }
- if(HintTextIconProp) {
- return <HintTextIconProp
- color={isDisabled ? "disabled" : currentType.hintTextIconColor}
- size={20}
- style={[
- hintTextIconStyle,
- stylesheet.hintTextIcon,
- hintTextIconDynamicStyle
- ]}
- />;
- }
- const CurrentHintIcon = DateTimePickerTypeIcon[type === "default" ? "question" : type];
- return <CurrentHintIcon
- customColor={isDisabled ? colors.system.state.content.disabled[styleType] : undefined}
- color={currentType.hintTextIconColor}
- size={20}
- style={[
- hintTextIconStyle,
- stylesheet.hintTextIcon,
- hintTextIconDynamicStyle
- ]}
- />;
- };
- const renderHintText = () => {
- if (!hintText) {
- return null;
- }
- return <View
- style={[
- hintTextContainerStyle,
- stylesheet.hintText,
- hintTextDynamicStyle
- ]}
- >
- {renderHintIcon()}
- <Text
- customColor={isDisabled ? colors.system.state.content.disabled[styleType] : undefined}
- color={currentType.hintTextColor}
- variant="labelSmallSize"
- >
- {hintText}
- </Text>
- </View>;
- };
- const renderRequired = () => {
- if(!isRequired) {
- return null;
- }
- return <Text
- color="danger"
- style={[
- stylesheet.required,
- requiredDynamicStyle
- ]}
- >*</Text>;
- };
- const renderSubtitle = () => {
- if(!isShowSubTitle && !isOptional) {
- return null;
- }
- return <Text
- variant="labelLargeSize"
- color={titleProps.color}
- style={[
- stylesheet.subTitle,
- subTitleDynamicStyle
- ]}
- >
- ( {isOptional ? localize("is-optional") : subTitle} )
- </Text>;
- };
- const renderTitle = () => {
- if (!title) {
- return null;
- }
- return <TouchableOpacity
- style={[
- stylesheet.titleContainer,
- titleContainerDynamicStyle
- ]}
- onPress={() => {
- if(!isDisabled) {
- focus();
- }
- }}
- >
- {renderRequired()}
- <Text
- {...titleProps}
- variant={titleProps.variant}
- color={titleProps.color}
- style={[
- stylesheet.title,
- titleDynamicStyle
- ]}
- >
- {title}
- </Text>
- {renderSubtitle()}
- </TouchableOpacity>;
- };
- const renderDateValue = () => {
- if(!date) {
- return <Text
- variant="labelLargeSize"
- numberOfLines={1}
- style={[
- stylesheet.contentText,
- contentTextDynamicStyle
- ]}
- >
- {placeholder ? placeholder : localize("select-any-date")}
- </Text>;
- }
- return <Text
- variant="labelLargeSize"
- numberOfLines={1}
- style={[
- stylesheet.contentText,
- contentTextDynamicStyle
- ]}
- >
- {moment(date).format(titleFormat)}
- </Text>;
- };
- const renderDateRangeValue = () => {
- if(!dateRange) {
- return <Text
- variant="labelLargeSize"
- numberOfLines={1}
- style={[
- stylesheet.contentText,
- contentTextDynamicStyle
- ]}
- >
- {placeholder ? placeholder : localize("select-any-date")}
- </Text>;
- }
- if(dateRangeTitle) {
- return dateRangeTitle(dateRange);
- }
- return <Text
- variant="labelLargeSize"
- numberOfLines={1}
- style={[
- stylesheet.contentText,
- contentTextDynamicStyle
- ]}
- >
- {moment(dateRange.start).format(titleFormat)} - {moment(dateRange.end).format(titleFormat)}
- </Text>;
- };
- const renderDateRuleValue = () => {
- if(!dateRule) {
- return <Text
- variant="labelLargeSize"
- numberOfLines={1}
- style={[
- stylesheet.contentText,
- contentTextDynamicStyle
- ]}
- >
- {placeholder ? placeholder : localize("select-any-date")}
- </Text>;
- }
- return <Text
- variant="labelLargeSize"
- numberOfLines={1}
- style={[
- stylesheet.contentText,
- contentTextDynamicStyle
- ]}
- >
- {dateRule.toText(undefined, parseRRuleConfig(rruleConfig))}
- </Text>;
- };
- const renderValue = () => {
- if(variant === "rrule") {
- return renderDateRuleValue();
- }
- if(variant === "range") {
- return renderDateRangeValue();
- }
- return renderDateValue();
- };
- const renderContent = () => {
- return <View
- style={[
- contentStyle,
- stylesheet.content,
- contentDynamicStyle
- ]}
- >
- {renderValue()}
- </View>;
- };
- const renderOverlay = () => {
- return <View
- style={[
- stylesheet.overlay,
- overlayDynamicStyle
- ]}
- />;
- };
- return <View
- style={[
- style,
- stylesheet.container,
- containerDynamicStyle
- ]}
- >
- {renderTitle()}
- <TouchableOpacity
- disabled={isDisabled}
- style={[
- contentContainerStyle,
- stylesheet.contentContainer,
- contentContainerDynamicStyle
- ]}
- onPress={() => {
- if(!isDisabled) {
- focus();
- }
- }}
- >
- {renderOverlay()}
- {renderIcon()}
- {renderContent()}
- {renderCleanButton()}
- {renderRightIcon()}
- </TouchableOpacity>
- {renderHintText()}
- <DateTimeSheet
- localeBasedFirstDayOfWeek={localeBasedFirstDayOfWeek}
- LoadingIconComponentProp={LoadingIconComponentProp}
- dateTimePickerKey={dateTimePickerKey.current}
- customLocalize={customDateTimeSheetLocalize}
- selectMultipleObject={selectMultipleObject}
- isShowTodayIndicator={isShowTodayIndicator}
- isWorkWithRealtime={isWorkWithRealtime}
- customTheme={customDateTimeSheetTheme}
- isShowTools={isShowDateTimeTools}
- dayOfWeekLength={dayOfWeekLength}
- selectDateRule={selectDateRule}
- bottomSheetRef={bottomSheetRef}
- selectObject={selectObject}
- setIsLoading={setIsLoading}
- setDateRange={setDateRange}
- setIsActive={setIsActive}
- setDateRule={setDateRule}
- bottomSheetProps={{
- isAutoHeight: true,
- ...dateTimeSheetProps
- }}
- dateRange={mainDateRange}
- pickerType={pickerType}
- maxChoice={maxChoice}
- minChoice={minChoice}
- isLoading={isLoading}
- dateRule={dateRule}
- isActive={isActive}
- variant={variant}
- maxDate={maxDate}
- minDate={minDate}
- date={mainDate}
- cancel={cancel}
- clean={clean}
- title={title}
- ok={ok}
- />
- </View>;
- };
- export default forwardRef(DateTimePicker);
|