UNPKG

@progress/sitefinity-nextjs-sdk

Version:

Provides OOB widgets developed using the Next.js framework, which includes an abstraction layer for Sitefinity communication. Additionally, it offers an expanded API, typings, and tools for further development and integration.

67 lines (66 loc) 3.9 kB
'use client'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import React, { useContext, useState } from 'react'; import { VisibilityStyle } from '../../styling/visibility-style'; import { StylingConfig } from '../../styling/styling-config'; import { classNames } from '../../../editor/utils/classNames'; import { FormContext } from '../../form/form-context'; import { getUniqueId } from '../../../editor/utils/getUniqueId'; export function DateTimeFieldClient(props) { const dateTimeFieldUniqueId = props.widgetContext.model.Properties.SfFieldName; const dateTimeFieldErrorMessageId = getUniqueId('DateTimeFieldErrorMessage', props.widgetContext.model.Id); const dateTimeFieldInfoMessageId = getUniqueId('DateTimeFieldInfo', props.widgetContext.model.Id); let ariaDescribedByAttribute = props.hasDescription ? `${dateTimeFieldUniqueId} ${dateTimeFieldErrorMessageId}` : dateTimeFieldErrorMessageId; const inputRef = React.useRef(null); const [inputValue, setInputValue] = React.useState(''); const { formViewProps, sfFormValueChanged, hiddenInputs, skippedInputs, registerFieldValidator } = useContext(FormContext); const isHidden = hiddenInputs[dateTimeFieldUniqueId]; const isSkipped = skippedInputs[dateTimeFieldUniqueId]; const [errorMessageText, setErrorMessageText] = useState(''); let delayTimer; function dispatchValueChanged() { clearTimeout(delayTimer); delayTimer = setTimeout(function () { sfFormValueChanged(); }, 300); } function setErrorMessage(_input, message) { if (message) { setErrorMessageText(message); } } function clearErrorMessage() { setErrorMessageText(''); } const handleDateTimeValidation = () => { const target = inputRef.current; const validationMessages = props.violationRestrictionsMessages; setInputValue(target.value); if (target.required && target.validity.valueMissing) { setErrorMessage(target, validationMessages.required); return false; } else if (!target.validity.valid) { setErrorMessage(target, validationMessages.invalid); return false; } else { clearErrorMessage(); return true; } }; const handleInputEvent = (e) => { setInputValue(e.currentTarget.value); handleDateTimeValidation(); dispatchValueChanged(); }; React.useEffect(() => { registerFieldValidator(dateTimeFieldUniqueId, handleDateTimeValidation); }, []); const rootClass = classNames('mb-3', props.cssClass, isHidden ? StylingConfig.VisibilityClasses[VisibilityStyle.Hidden] : StylingConfig.VisibilityClasses[VisibilityStyle.Visible]); return (_jsxs("div", { className: rootClass, "data-sf-role": "date-time-field-container", children: [_jsx("label", { className: "h6", htmlFor: dateTimeFieldUniqueId, children: props.label }), _jsx("input", { id: dateTimeFieldUniqueId, type: props.inputType, className: classNames('form-control', { [formViewProps.invalidClass]: formViewProps.invalidClass && errorMessageText }), ref: inputRef, name: props.fieldName, value: inputValue, "data-sf-role": "date-time-field-input", disabled: isHidden || isSkipped, "aria-describedby": ariaDescribedByAttribute, onChange: handleDateTimeValidation, onInput: handleInputEvent, onInvalid: handleDateTimeValidation, ...props.validationAttributes }), props.instructionalText && _jsx("div", { id: dateTimeFieldInfoMessageId, className: "form-text w-100", children: props.instructionalText }), errorMessageText && _jsx("div", { id: dateTimeFieldErrorMessageId, "data-sf-role": "error-message", role: "alert", "aria-live": "assertive", className: "invalid-feedback", children: errorMessageText })] })); }