UNPKG

@aokiapp/rjsf-mantine-theme

Version:

Mantine theme, fields and widgets for react-jsonschema-form

1 lines 5.23 kB
{"version":3,"file":"MantineDateWidget.cjs","sources":["../../src/widgets/MantineDateWidget.tsx"],"sourcesContent":["import {\n FormContextType,\n RJSFSchema,\n StrictRJSFSchema,\n WidgetProps,\n labelValue,\n ariaDescribedByIds,\n getUiOptions,\n} from '@rjsf/utils';\nimport { useCallback } from 'react';\n\nimport { DatePickerInput } from '@mantine/dates';\nimport { createErrors } from '../utils/createErrors';\n\n/** The `DateWidget` component uses the `BaseInputTemplate` changing the type to `date` and transforms\n * the value to undefined when it is falsy during the `onChange` handling.\n *\n * @param props - The `WidgetProps` for this component\n */\nexport default function MantineDateWidget<\n T = any,\n S extends StrictRJSFSchema = RJSFSchema,\n F extends FormContextType = any,\n>(props: WidgetProps<T, S, F>) {\n const {\n id,\n placeholder,\n label,\n hideLabel,\n value,\n required,\n readonly,\n disabled,\n onChange,\n onChangeOverride,\n autofocus,\n rawErrors,\n uiSchema,\n hideError,\n schema,\n } = props;\n\n const options = getUiOptions<T, S, F>(uiSchema);\n\n // TODO: options\n const inputValue = value ? fromRawDateStrToUtcTime(value) : null;\n\n const _onChange = useCallback(\n (value: Date | null) => onChange(value ? fromUtcTimeToRawDateStr(value) : undefined),\n [onChange],\n );\n\n const description = options.description || schema.description;\n return (\n <DatePickerInput\n value={inputValue}\n onChange={onChangeOverride || _onChange}\n key={id}\n id={id}\n placeholder={placeholder}\n label={labelValue(label, hideLabel, undefined)}\n required={required}\n clearable={!required}\n disabled={disabled || readonly}\n autoFocus={autofocus}\n error={createErrors<T>(rawErrors, hideError)}\n aria-describedby={ariaDescribedByIds<T>(id)}\n {...(options.props as Record<string, unknown>)}\n className='armt-widget-date-mt'\n description={description}\n />\n );\n}\n/**\n * Eliminate local time information which is converted into UTC and get pure date string\n *\n * Unlike the pair of date and time, the date in solo is generally not associated with any time zone in particular. (e.g. \"Let's meet on Jan 8\" doesn't imply arrival at exactly Jan 8, 00:00 UTC)\nSo the output should be date string, instead of Date object that have time information.\n * So the output should be date string, instead of Date object that have time information.\n * @param value Date in UTC\n * @returns Date string in YYYY-MM-DD format\n */\nfunction fromUtcTimeToRawDateStr(value: Date | null) {\n if (value === null) {\n return null;\n }\n const date = new Date(value);\n date.setMinutes(date.getMinutes() - new Date().getTimezoneOffset());\n return date.toISOString().split('T')[0];\n}\n\n/**\n * Reverse operation of fromUtcTimeToRawDateStr\n * @param value Date string in YYYY-MM-DD format\n * @returns Date in UTC\n */\nfunction fromRawDateStrToUtcTime(value: string | null) {\n if (value === null) {\n return null;\n }\n const date = new Date(value);\n date.setMinutes(date.getMinutes() + new Date().getTimezoneOffset());\n return date;\n}\n"],"names":["getUiOptions","useCallback","jsx","DatePickerInput","labelValue","createErrors","ariaDescribedByIds"],"mappings":";;;;;;;;;;AAUe,SAAS,iBAAiB,CAAC,KAAK,EAAE;AACjD,EAAE,MAAM;AACR,IAAI,EAAE;AACN,IAAI,WAAW;AACf,IAAI,KAAK;AACT,IAAI,SAAS;AACb,IAAI,KAAK;AACT,IAAI,QAAQ;AACZ,IAAI,QAAQ;AACZ,IAAI,QAAQ;AACZ,IAAI,QAAQ;AACZ,IAAI,gBAAgB;AACpB,IAAI,SAAS;AACb,IAAI,SAAS;AACb,IAAI,QAAQ;AACZ,IAAI,SAAS;AACb,IAAI,MAAM;AACV,GAAG,GAAG,KAAK,CAAC;AACZ,EAAE,MAAM,OAAO,GAAGA,kBAAY,CAAC,QAAQ,CAAC,CAAC;AACzC,EAAE,MAAM,UAAU,GAAG,KAAK,GAAG,uBAAuB,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;AACnE,EAAE,MAAM,SAAS,GAAGC,iBAAW;AAC/B,IAAI,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,GAAG,uBAAuB,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC;AAC3E,IAAI,CAAC,QAAQ,CAAC;AACd,GAAG,CAAC;AACJ,EAAE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