UNPKG

@hhgtech/hhg-components

Version:
143 lines (136 loc) • 11.6 kB
import { _ as __rest } from './tslib.es6-ea4dfe68.js'; import React__default, { createContext, useContext, useMemo } from 'react'; import { useMantineTheme } from '@mantine/core'; import { useForm, isNotEmpty } from '@mantine/form'; import { u as useHealthToolsCache } from './useHealthToolCache-bf165bb8.js'; import { u as useTranslations } from './index-9d21b711.js'; import { H as HEALTH_TOOL_TYPE } from './constants-f4091ce6.js'; import { I as Input } from './index-5d405c0d.js'; import '@mantine/dates'; import './index-5e947517.js'; import './index-90813715.js'; import './index-ebe66e27.js'; import './useMantineLocale-0c6bea99.js'; import './index-9f5659e8.js'; import './index-c68a0fa7.js'; import './index.styles-770020ac.js'; import '@mantine/hooks'; import './utils-cb7242c7.js'; import './other-4ccb5568.js'; import './index-c2190f6e.js'; import { H as HealthToolFormWrapper } from './index-8a080703.js'; import { I as INPUT_RANGE } from './constants-1cff32f1.js'; import { domainLocales } from './constantsDomainLocales.js'; const PWGFormContext = createContext({}); const DefaultForm = (_a) => { var props = __rest(_a, []); const { t, locale } = useTranslations(); const { linkTool, form, submitProps } = useContext(PWGFormContext); const theme = useMantineTheme(); const isMarryBaby = theme.other.template === 'mb'; return (React__default.createElement(HealthToolFormWrapper, Object.assign({ formLayout: "default" }, props), React__default.createElement(Input.Number, Object.assign({ label: t('pregnancyWeightGain.weekInput'), min: INPUT_RANGE.pregnantWeek.min, max: INPUT_RANGE.pregnantWeek.max, placeholder: `${INPUT_RANGE.pregnantWeek.min} - ${INPUT_RANGE.pregnantWeek.max}` }, form.getInputProps('pregnantWeek'))), React__default.createElement(Input.Number, Object.assign({ label: t('pregnancyWeightGain.weightBeforeInput'), min: INPUT_RANGE.weight.min, max: INPUT_RANGE.weight.max, placeholder: t('tools.inputPlaceholder', { content: '50', }) }, form.getInputProps('lastWeight'))), React__default.createElement(Input.Number, Object.assign({ label: t('pregnancyWeightGain.heightInput'), min: INPUT_RANGE.height.min, max: INPUT_RANGE.height.max, placeholder: t('tools.inputPlaceholder', { content: '160', }) }, form.getInputProps('height'))), React__default.createElement(Input.Number, Object.assign({ label: t('pregnancyWeightGain.weightInput'), min: INPUT_RANGE.weight.min, max: INPUT_RANGE.weight.max, placeholder: t('tools.inputPlaceholder', { content: '50', }) }, form.getInputProps('weight'))), React__default.createElement(HealthToolFormWrapper.Switching, Object.assign({ label: t('pregnancyWeightGain.havingTwins') }, form.getInputProps('hasTwin'))), React__default.createElement(HealthToolFormWrapper.Submit, Object.assign({ type: "submit", dataEventCategory: "Health Tool", dataEventAction: "Embedded Pregnancy Weight Gain", dataEventLabel: `https://${domainLocales[isMarryBaby ? 'vi-VN_MB' : locale]}/${linkTool}`, className: "due-date-calculate-button" }, submitProps), t('dueDatePage.lastPeriod.calculate')))); }; const InlineForm = (_a) => { var props = __rest(_a, []); const { t, locale } = useTranslations(); const { linkTool, form, submitProps } = useContext(PWGFormContext); const theme = useMantineTheme(); const isMarryBaby = theme.other.template === 'mb'; return (React__default.createElement(HealthToolFormWrapper, Object.assign({ formLayout: "inline" }, props), React__default.createElement(HealthToolFormWrapper.BorderInlineControl, { label: t('pregnancyWeightGain.weekInput') }, React__default.createElement(Input.Number, Object.assign({ min: INPUT_RANGE.pregnantWeek.min, max: INPUT_RANGE.pregnantWeek.max, placeholder: `${INPUT_RANGE.pregnantWeek.min} - ${INPUT_RANGE.pregnantWeek.max}`, rightSection: React__default.createElement(HealthToolFormWrapper.Unit, null, t('pwgForm.week')) }, form.getInputProps('pregnantWeek')))), React__default.createElement(HealthToolFormWrapper.BorderInlineControl, { label: t('pregnancyWeightGain.weightBeforeInput') }, React__default.createElement(Input.Number, Object.assign({}, form.getInputProps('lastWeight'), { min: INPUT_RANGE.weight.min, max: INPUT_RANGE.weight.max, placeholder: t('tools.inputPlaceholder', { content: '50', }), rightSection: React__default.createElement(HealthToolFormWrapper.Unit, null, t('babyGrowth.kg')) }))), React__default.createElement(HealthToolFormWrapper.BorderInlineControl, { label: t('pregnancyWeightGain.heightInput') }, React__default.createElement(Input.Number, Object.assign({}, form.getInputProps('height'), { min: INPUT_RANGE.height.min, max: INPUT_RANGE.height.max, placeholder: t('tools.inputPlaceholder', { content: '160', }), rightSection: React__default.createElement(HealthToolFormWrapper.Unit, null, t('babyGrowth.cm')) }))), React__default.createElement(HealthToolFormWrapper.BorderInlineControl, { label: t('pregnancyWeightGain.weightInput') }, React__default.createElement(Input.Number, Object.assign({}, form.getInputProps('weight'), { min: INPUT_RANGE.weight.min, max: INPUT_RANGE.weight.max, placeholder: t('tools.inputPlaceholder', { content: '50', }), rightSection: React__default.createElement(HealthToolFormWrapper.Unit, null, t('babyGrowth.kg')) }))), React__default.createElement(HealthToolFormWrapper.BorderInlineControl, { label: t('pregnancyWeightGain.havingTwins'), width: 'none' }, React__default.createElement(HealthToolFormWrapper.Switching, Object.assign({}, form.getInputProps('hasTwin')))), React__default.createElement(HealthToolFormWrapper.Submit, Object.assign({ type: "submit", dataEventCategory: "Health Tool", dataEventAction: "Embedded Pregnancy Weight Gain", dataEventLabel: `https://${domainLocales[isMarryBaby ? 'vi-VN_MB' : locale]}/${linkTool}`, className: "due-date-calculate-button" }, submitProps), t('dueDatePage.lastPeriod.calculate')))); }; const MbLayout = (_a) => { var props = __rest(_a, []); const { t, locale } = useTranslations(); const { linkTool, form, submitProps } = useContext(PWGFormContext); const theme = useMantineTheme(); const isMarryBaby = theme.other.template === 'mb'; return (React__default.createElement(HealthToolFormWrapper, Object.assign({ formLayout: "inline" }, props), React__default.createElement(HealthToolFormWrapper.FormRow, null, React__default.createElement(HealthToolFormWrapper.BorderBoxControl, { label: t('pregnancyWeightGain.weekInput'), rightSection: t('pregnancy.week') }, React__default.createElement(Input.Number, Object.assign({ min: INPUT_RANGE.pregnantWeek.min, max: INPUT_RANGE.pregnantWeek.max, placeholder: `${INPUT_RANGE.pregnantWeek.min} - ${INPUT_RANGE.pregnantWeek.max}` }, form.getInputProps('pregnantWeek')))), form.errors.pregnantWeek && (React__default.createElement(HealthToolFormWrapper.Error, null, form.errors.pregnantWeek))), React__default.createElement(HealthToolFormWrapper.FormRow, null, React__default.createElement(HealthToolFormWrapper.BorderBoxControl, { label: t('pregnancyWeightGain.weightBeforeInput'), rightSection: "kg" }, React__default.createElement(Input.Number, Object.assign({ min: INPUT_RANGE.weight.min, max: INPUT_RANGE.weight.max }, form.getInputProps('lastWeight'), { placeholder: t('tools.inputPlaceholder', { content: '50', }) }))), form.errors.lastWeight && (React__default.createElement(HealthToolFormWrapper.Error, null, form.errors.lastWeight))), React__default.createElement(HealthToolFormWrapper.FormRow, null, React__default.createElement(HealthToolFormWrapper.BorderBoxControl, { label: t('pregnancyWeightGain.heightInput'), rightSection: "cm" }, React__default.createElement(Input.Number, Object.assign({ min: INPUT_RANGE.height.min, max: INPUT_RANGE.height.max }, form.getInputProps('height'), { placeholder: t('tools.inputPlaceholder', { content: '160', }) }))), form.errors.height && (React__default.createElement(HealthToolFormWrapper.Error, null, form.errors.height))), React__default.createElement(HealthToolFormWrapper.FormRow, null, React__default.createElement(HealthToolFormWrapper.BorderBoxControl, { label: t('pregnancyWeightGain.weightInput'), rightSection: "kg" }, React__default.createElement(Input.Number, Object.assign({ min: INPUT_RANGE.weight.min, max: INPUT_RANGE.weight.max }, form.getInputProps('weight'), { placeholder: t('tools.inputPlaceholder', { content: '50', }) }))), form.errors.weight && (React__default.createElement(HealthToolFormWrapper.Error, null, form.errors.weight))), React__default.createElement(HealthToolFormWrapper.Switching, Object.assign({ label: t('pregnancyWeightGain.havingTwins') }, form.getInputProps('hasTwin'))), React__default.createElement(HealthToolFormWrapper.Submit, Object.assign({ type: "submit", dataEventCategory: "Health Tool", dataEventAction: "Embedded Pregnancy Weight Gain", dataEventLabel: `https://${domainLocales[isMarryBaby ? 'vi-VN_MB' : locale]}/${linkTool}`, className: "due-date-calculate-button" }, submitProps), t('dueDatePage.lastPeriod.calculate')))); }; const PWGForm = (_a) => { var { formLayout = 'default', submitProps, onSubmit } = _a, rest = __rest(_a, ["formLayout", "submitProps", "onSubmit"]); const { t, locale } = useTranslations(); const theme = useMantineTheme(); const isMarryBaby = theme.other.template === 'mb'; const form = useForm({ validate: { pregnantWeek: isNotEmpty(t('validation.error.requiredField')), height: isNotEmpty(t('validation.error.requiredField')), weight: (value, values) => { return !value ? t('validation.error.requiredField') : Number(value) >= Number(values.lastWeight) ? null : t('pregnancyWeightGain.weightInput.error'); }, lastWeight: (value, values) => { return !value ? t('validation.error.requiredField') : Number(value) < Number(values.weight) ? null : t('pregnancyWeightGain.weightInput.error'); }, }, }); const { getHealthToolLink } = useHealthToolsCache(locale); const linkTool = useMemo(() => getHealthToolLink(HEALTH_TOOL_TYPE.PWG), [getHealthToolLink]); const commonProps = Object.assign({ onSubmit: form.onSubmit(onSubmit) }, rest); return (React__default.createElement(PWGFormContext.Provider, { value: { linkTool, form, submitProps, } }, formLayout === 'inline' ? (React__default.createElement(InlineForm, Object.assign({}, commonProps))) : isMarryBaby ? (React__default.createElement(MbLayout, Object.assign({ formLayout: formLayout }, commonProps))) : (React__default.createElement(DefaultForm, Object.assign({}, commonProps))))); }; export { PWGForm as P };