@10x-media/form-builder 0.1.0-beta.9 → 4.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +173 -0
- package/README.md +11 -3
- package/dist/actions/assertNoBlockCollision.js +18 -0
- package/dist/actions/assertNoBlockCollision.js.map +1 -0
- package/dist/actions/body/serializeBody.d.ts +16 -14
- package/dist/actions/body/serializeBody.js +5 -8
- package/dist/actions/body/serializeBody.js.map +1 -1
- package/dist/actions/builtin/confirmation.js +2 -1
- package/dist/actions/builtin/confirmation.js.map +1 -1
- package/dist/actions/builtin/emailAction.d.ts +15 -3
- package/dist/actions/builtin/emailAction.js +72 -20
- package/dist/actions/builtin/emailAction.js.map +1 -1
- package/dist/actions/builtin/emailTeam.js +11 -5
- package/dist/actions/builtin/emailTeam.js.map +1 -1
- package/dist/actions/defineAction.d.ts +37 -6
- package/dist/actions/defineAction.js +14 -1
- package/dist/actions/defineAction.js.map +1 -1
- package/dist/actions/dispatch.d.ts +12 -0
- package/dist/actions/dispatch.js +103 -30
- package/dist/actions/dispatch.js.map +1 -1
- package/dist/actions/dispatchContext.js +19 -0
- package/dist/actions/dispatchContext.js.map +1 -0
- package/dist/actions/emailRecipients.js +38 -8
- package/dist/actions/emailRecipients.js.map +1 -1
- package/dist/actions/emailRender.d.ts +27 -0
- package/dist/actions/fromAddresses.d.ts +19 -1
- package/dist/actions/fromAddresses.js +66 -9
- package/dist/actions/fromAddresses.js.map +1 -1
- package/dist/actions/recipientSources.d.ts +23 -0
- package/dist/actions/recipientSources.js +11 -0
- package/dist/actions/recipientSources.js.map +1 -0
- package/dist/actions/registry.js +3 -1
- package/dist/actions/registry.js.map +1 -1
- package/dist/actions/runActions.d.ts +2 -0
- package/dist/actions/runActions.js +13 -8
- package/dist/actions/runActions.js.map +1 -1
- package/dist/actions/sign.js.map +1 -1
- package/dist/actions/submissionContext.d.ts +34 -0
- package/dist/actions/task.js +43 -16
- package/dist/actions/task.js.map +1 -1
- package/dist/aggregation/aggregateResponses.js +3 -4
- package/dist/aggregation/aggregateResponses.js.map +1 -1
- package/dist/aggregation/resolveResultsRequest.d.ts +6 -0
- package/dist/aggregation/resolveResultsRequest.js +69 -26
- package/dist/aggregation/resolveResultsRequest.js.map +1 -1
- package/dist/calc/computeCalcFields.d.ts +5 -3
- package/dist/calc/computeCalcFields.js +4 -3
- package/dist/calc/computeCalcFields.js.map +1 -1
- package/dist/calc/evaluate.d.ts +26 -3
- package/dist/calc/evaluate.js +33 -11
- package/dist/calc/evaluate.js.map +1 -1
- package/dist/calc/formatCalc.d.ts +11 -0
- package/dist/calc/formatCalc.js +35 -0
- package/dist/calc/formatCalc.js.map +1 -0
- package/dist/calc/formatCalcValue.d.ts +17 -0
- package/dist/calc/formatCalcValue.js +17 -0
- package/dist/calc/formatCalcValue.js.map +1 -0
- package/dist/calc/normalizeCalc.d.ts +17 -3
- package/dist/calc/normalizeCalc.js +39 -17
- package/dist/calc/normalizeCalc.js.map +1 -1
- package/dist/calc/registry.d.ts +43 -0
- package/dist/calc/registry.js +27 -0
- package/dist/calc/registry.js.map +1 -0
- package/dist/calc/resolveCalcContext.d.ts +36 -0
- package/dist/calc/resolveCalcContext.js +87 -0
- package/dist/calc/resolveCalcContext.js.map +1 -0
- package/dist/calc/types.d.ts +19 -4
- package/dist/calc/types.js +3 -2
- package/dist/calc/types.js.map +1 -1
- package/dist/client/ByteSizeField.js +11 -8
- package/dist/client/ByteSizeField.js.map +1 -1
- package/dist/client/CalcExpressionBuilder.d.ts +20 -0
- package/dist/client/CalcExpressionBuilder.js +888 -0
- package/dist/client/CalcExpressionBuilder.js.map +1 -0
- package/dist/client/ClosePollButton.js +3 -3
- package/dist/client/ClosePollButton.js.map +1 -1
- package/dist/client/CondensedArray.d.ts +2 -2
- package/dist/client/CondensedArray.js +5 -7
- package/dist/client/CondensedArray.js.map +1 -1
- package/dist/client/ConditionBuilder.js +5 -7
- package/dist/client/ConditionBuilder.js.map +1 -1
- package/dist/client/ConditionRow.js +3 -3
- package/dist/client/ConditionRow.js.map +1 -1
- package/dist/client/ConsentBlockLabel.js +2 -2
- package/dist/client/ConsentBlockLabel.js.map +1 -1
- package/dist/client/ConsentRetentionNotice.d.ts +11 -0
- package/dist/client/ConsentRetentionNotice.js +23 -0
- package/dist/client/ConsentRetentionNotice.js.map +1 -0
- package/dist/client/EndpointOptionsSelect.d.ts +17 -7
- package/dist/client/EndpointOptionsSelect.js +18 -14
- package/dist/client/EndpointOptionsSelect.js.map +1 -1
- package/dist/client/FieldBlockLabel.js +2 -2
- package/dist/client/FieldBlockLabel.js.map +1 -1
- package/dist/client/FieldNameSelect.d.ts +1 -0
- package/dist/client/FieldNameSelect.js +2 -4
- package/dist/client/FieldNameSelect.js.map +1 -1
- package/dist/client/FlowBuilder.js +31 -23
- package/dist/client/FlowBuilder.js.map +1 -1
- package/dist/client/FormConditionField.js +0 -1
- package/dist/client/FormConditionField.js.map +1 -1
- package/dist/client/RecipientsSelect.d.ts +9 -2
- package/dist/client/RecipientsSelect.js +31 -10
- package/dist/client/RecipientsSelect.js.map +1 -1
- package/dist/client/admin.css +345 -72
- package/dist/client/endpointOptions.d.ts +11 -0
- package/dist/client/endpointOptions.js +17 -8
- package/dist/client/endpointOptions.js.map +1 -1
- package/dist/collections/formSubmissions.js +145 -51
- package/dist/collections/formSubmissions.js.map +1 -1
- package/dist/collections/forms.js +170 -65
- package/dist/collections/forms.js.map +1 -1
- package/dist/collections/formsEndpoints.js +31 -26
- package/dist/collections/formsEndpoints.js.map +1 -1
- package/dist/collections/settingsFields.d.ts +32 -0
- package/dist/collections/settingsFields.js +55 -0
- package/dist/collections/settingsFields.js.map +1 -0
- package/dist/consent/captureConsent.d.ts +2 -1
- package/dist/consent/captureConsent.js +4 -1
- package/dist/consent/captureConsent.js.map +1 -1
- package/dist/consent/consentSourcesField.d.ts +7 -1
- package/dist/consent/consentSourcesField.js +15 -1
- package/dist/consent/consentSourcesField.js.map +1 -1
- package/dist/consent/effectiveStatement.js +16 -0
- package/dist/consent/effectiveStatement.js.map +1 -0
- package/dist/consent/resolveConsentSourcesRequest.js +2 -2
- package/dist/consent/resolveConsentSourcesRequest.js.map +1 -1
- package/dist/consent/resolveConsentStatements.js +3 -1
- package/dist/consent/resolveConsentStatements.js.map +1 -1
- package/dist/consent/resolvePublishedVersionRef.js +1 -0
- package/dist/consent/resolvePublishedVersionRef.js.map +1 -1
- package/dist/consent/types.d.ts +8 -0
- package/dist/context/formContext.d.ts +30 -0
- package/dist/context/formContext.js +54 -0
- package/dist/context/formContext.js.map +1 -0
- package/dist/email/departmentsField.d.ts +6 -1
- package/dist/email/departmentsField.js +6 -1
- package/dist/email/departmentsField.js.map +1 -1
- package/dist/exports/client.d.ts +3 -1
- package/dist/exports/client.js +3 -1
- package/dist/exports/i18n.d.ts +14 -2
- package/dist/exports/i18n.js +14 -2
- package/dist/exports/react.d.ts +23 -5
- package/dist/exports/react.js +17 -1
- package/dist/exports/rsc.d.ts +4 -1
- package/dist/exports/rsc.js +7 -4
- package/dist/exports/types.d.ts +4 -3
- package/dist/fields/buildFieldBlocks.js +2 -1
- package/dist/fields/buildFieldBlocks.js.map +1 -1
- package/dist/fields/builtin/calculation.d.ts +17 -0
- package/dist/fields/builtin/calculation.js +70 -127
- package/dist/fields/builtin/calculation.js.map +1 -1
- package/dist/fields/builtin/checkbox.js +15 -0
- package/dist/fields/builtin/checkbox.js.map +1 -1
- package/dist/fields/builtin/consent.js +21 -1
- package/dist/fields/builtin/consent.js.map +1 -1
- package/dist/fields/builtin/email.js +7 -0
- package/dist/fields/builtin/email.js.map +1 -1
- package/dist/fields/builtin/index.d.ts +6 -2
- package/dist/fields/builtin/index.js +9 -4
- package/dist/fields/builtin/index.js.map +1 -1
- package/dist/fields/builtin/select.js +20 -1
- package/dist/fields/builtin/select.js.map +1 -1
- package/dist/fields/builtin/text.js +7 -0
- package/dist/fields/builtin/text.js.map +1 -1
- package/dist/fields/instanceOptions.js +1 -1
- package/dist/fields/instanceOptions.js.map +1 -1
- package/dist/fields/sharedConfig.js +15 -11
- package/dist/fields/sharedConfig.js.map +1 -1
- package/dist/fields/types.d.ts +8 -2
- package/dist/form/findFormAtLocale.d.ts +30 -0
- package/dist/form/findFormAtLocale.js +88 -0
- package/dist/form/findFormAtLocale.js.map +1 -0
- package/dist/form/pollState.d.ts +2 -1
- package/dist/form/pollState.js.map +1 -1
- package/dist/form/toFormDocument.d.ts +8 -0
- package/dist/form/toFormDocument.js +3 -1
- package/dist/form/toFormDocument.js.map +1 -1
- package/dist/form/types.d.ts +9 -1
- package/dist/index.d.ts +22 -6
- package/dist/index.js +63 -17
- package/dist/index.js.map +1 -1
- package/dist/options.d.ts +113 -4
- package/dist/plugin/collectionSlugs.d.ts +28 -0
- package/dist/plugin/collectionSlugs.js +65 -0
- package/dist/plugin/collectionSlugs.js.map +1 -0
- package/dist/plugin/customState.js +1 -1
- package/dist/plugin/customState.js.map +1 -1
- package/dist/plugin/registerCollections.js +38 -21
- package/dist/plugin/registerCollections.js.map +1 -1
- package/dist/poll/closeJob.d.ts +4 -3
- package/dist/poll/closeJob.js +13 -10
- package/dist/poll/closeJob.js.map +1 -1
- package/dist/poll/outcomeBeforeChange.js +2 -2
- package/dist/poll/outcomeBeforeChange.js.map +1 -1
- package/dist/poll/pollTypeRegistry.js +1 -1
- package/dist/poll/resolvePollCloseRequest.js +5 -4
- package/dist/poll/resolvePollCloseRequest.js.map +1 -1
- package/dist/poll/resolvePollOptionsRequest.js +2 -2
- package/dist/poll/resolvePollOptionsRequest.js.map +1 -1
- package/dist/poll/resolvePollOutcome.d.ts +6 -0
- package/dist/poll/resolvePollOutcome.js +20 -6
- package/dist/poll/resolvePollOutcome.js.map +1 -1
- package/dist/poll/votes/aggregateFromVotes.d.ts +27 -0
- package/dist/poll/votes/aggregateFromVotes.js +55 -0
- package/dist/poll/votes/aggregateFromVotes.js.map +1 -0
- package/dist/poll/votes/answerValues.js +16 -0
- package/dist/poll/votes/answerValues.js.map +1 -0
- package/dist/poll/votes/bumpPollVote.js +91 -0
- package/dist/poll/votes/bumpPollVote.js.map +1 -0
- package/dist/poll/votes/recountPollVotes.d.ts +20 -0
- package/dist/poll/votes/recountPollVotes.js +63 -0
- package/dist/poll/votes/recountPollVotes.js.map +1 -0
- package/dist/poll/votes/voteTallyHook.js +76 -0
- package/dist/poll/votes/voteTallyHook.js.map +1 -0
- package/dist/poll/votes/votesCollection.d.ts +23 -0
- package/dist/poll/votes/votesCollection.js +96 -0
- package/dist/poll/votes/votesCollection.js.map +1 -0
- package/dist/react/Form.d.ts +49 -7
- package/dist/react/Form.js +48 -13
- package/dist/react/Form.js.map +1 -1
- package/dist/react/FormContext.d.ts +8 -1
- package/dist/react/FormContext.js.map +1 -1
- package/dist/react/FormResults.d.ts +4 -2
- package/dist/react/FormResults.js +21 -8
- package/dist/react/FormResults.js.map +1 -1
- package/dist/react/Poll.d.ts +25 -2
- package/dist/react/Poll.js +76 -25
- package/dist/react/Poll.js.map +1 -1
- package/dist/react/adapters.d.ts +35 -0
- package/dist/react/adapters.js +11 -0
- package/dist/react/adapters.js.map +1 -0
- package/dist/react/captcha/HcaptchaCaptcha.d.ts +4 -1
- package/dist/react/captcha/HcaptchaCaptcha.js +2 -2
- package/dist/react/captcha/HcaptchaCaptcha.js.map +1 -1
- package/dist/react/captcha/RecaptchaCaptcha.d.ts +4 -1
- package/dist/react/captcha/RecaptchaCaptcha.js +2 -2
- package/dist/react/captcha/RecaptchaCaptcha.js.map +1 -1
- package/dist/react/captcha/TurnstileCaptcha.d.ts +4 -1
- package/dist/react/captcha/TurnstileCaptcha.js +2 -2
- package/dist/react/captcha/TurnstileCaptcha.js.map +1 -1
- package/dist/react/captcha/useCaptchaScript.js +23 -5
- package/dist/react/captcha/useCaptchaScript.js.map +1 -1
- package/dist/react/fetchResults.d.ts +6 -3
- package/dist/react/fetchResults.js +10 -5
- package/dist/react/fetchResults.js.map +1 -1
- package/dist/react/primitives/Checkbox.d.ts +4 -2
- package/dist/react/primitives/Checkbox.js +3 -2
- package/dist/react/primitives/Checkbox.js.map +1 -1
- package/dist/react/primitives/ChoiceGroup.d.ts +40 -0
- package/dist/react/primitives/ChoiceGroup.js +33 -0
- package/dist/react/primitives/ChoiceGroup.js.map +1 -0
- package/dist/react/primitives/FieldShell.d.ts +11 -1
- package/dist/react/primitives/FieldShell.js +43 -33
- package/dist/react/primitives/FieldShell.js.map +1 -1
- package/dist/react/primitives/Input.d.ts +3 -1
- package/dist/react/primitives/Input.js +2 -1
- package/dist/react/primitives/Input.js.map +1 -1
- package/dist/react/renderers/calculation.js +6 -1
- package/dist/react/renderers/calculation.js.map +1 -1
- package/dist/react/renderers/checkbox.js +2 -1
- package/dist/react/renderers/checkbox.js.map +1 -1
- package/dist/react/renderers/consent.js +8 -3
- package/dist/react/renderers/consent.js.map +1 -1
- package/dist/react/renderers/email.js +3 -1
- package/dist/react/renderers/email.js.map +1 -1
- package/dist/react/renderers/repeater.js +5 -0
- package/dist/react/renderers/repeater.js.map +1 -1
- package/dist/react/renderers/select.js +20 -2
- package/dist/react/renderers/select.js.map +1 -1
- package/dist/react/renderers/text.js +3 -1
- package/dist/react/renderers/text.js.map +1 -1
- package/dist/react/state.d.ts +10 -0
- package/dist/react/state.js +45 -1
- package/dist/react/state.js.map +1 -1
- package/dist/react/submitForm.d.ts +20 -3
- package/dist/react/submitForm.js +8 -4
- package/dist/react/submitForm.js.map +1 -1
- package/dist/react/useCalcValues.d.ts +10 -0
- package/dist/react/useCalcValues.js +13 -0
- package/dist/react/useCalcValues.js.map +1 -0
- package/dist/react/useField.js +2 -1
- package/dist/react/useField.js.map +1 -1
- package/dist/recall/resolver.js +1 -1
- package/dist/recall/resolver.js.map +1 -1
- package/dist/spam/constants.js +7 -1
- package/dist/spam/constants.js.map +1 -1
- package/dist/spam/spamGuard.js +2 -1
- package/dist/spam/spamGuard.js.map +1 -1
- package/dist/submissions/SubmissionAnswersClient.js +14 -14
- package/dist/submissions/SubmissionAnswersClient.js.map +1 -1
- package/dist/submissions/createSubmission.d.ts +7 -1
- package/dist/submissions/createSubmission.js +10 -5
- package/dist/submissions/createSubmission.js.map +1 -1
- package/dist/submissions/formIdOf.js +13 -0
- package/dist/submissions/formIdOf.js.map +1 -0
- package/dist/submissions/resolveVotedSubmission.d.ts +27 -0
- package/dist/submissions/resolveVotedSubmission.js +51 -0
- package/dist/submissions/resolveVotedSubmission.js.map +1 -0
- package/dist/submissions/runSubmission.js +6 -5
- package/dist/submissions/runSubmission.js.map +1 -1
- package/dist/submissions/submissionLocale.js +39 -0
- package/dist/submissions/submissionLocale.js.map +1 -0
- package/dist/submissions/validateSubmission.js +125 -79
- package/dist/submissions/validateSubmission.js.map +1 -1
- package/dist/submissions/verifyContext.js +35 -0
- package/dist/submissions/verifyContext.js.map +1 -0
- package/dist/submissions/voteChange.js +21 -0
- package/dist/submissions/voteChange.js.map +1 -0
- package/dist/submissions/voteChangeEndpoint.js +110 -0
- package/dist/submissions/voteChangeEndpoint.js.map +1 -0
- package/dist/submissions/votedCookie.d.ts +10 -4
- package/dist/submissions/votedCookie.js +49 -11
- package/dist/submissions/votedCookie.js.map +1 -1
- package/dist/translations/ar.d.ts +7 -0
- package/dist/translations/ar.js +379 -0
- package/dist/translations/ar.js.map +1 -0
- package/dist/translations/clientRuntimeKeys.d.ts +17 -0
- package/dist/translations/clientRuntimeKeys.js +48 -0
- package/dist/translations/clientRuntimeKeys.js.map +1 -0
- package/dist/translations/de.d.ts +12 -0
- package/dist/translations/de.js +78 -3
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +78 -3
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.d.ts +7 -0
- package/dist/translations/es.js +379 -0
- package/dist/translations/es.js.map +1 -0
- package/dist/translations/fr.d.ts +7 -0
- package/dist/translations/fr.js +379 -0
- package/dist/translations/fr.js.map +1 -0
- package/dist/translations/id.d.ts +7 -0
- package/dist/translations/id.js +379 -0
- package/dist/translations/id.js.map +1 -0
- package/dist/translations/index.d.ts +23 -6
- package/dist/translations/index.js +31 -6
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +76 -1
- package/dist/translations/keys.js +76 -1
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.d.ts +7 -0
- package/dist/translations/ko.js +379 -0
- package/dist/translations/ko.js.map +1 -0
- package/dist/translations/makeTranslate.d.ts +7 -3
- package/dist/translations/makeTranslate.js +12 -3
- package/dist/translations/makeTranslate.js.map +1 -1
- package/dist/translations/pt.d.ts +7 -0
- package/dist/translations/pt.js +379 -0
- package/dist/translations/pt.js.map +1 -0
- package/dist/translations/ru.d.ts +7 -0
- package/dist/translations/ru.js +379 -0
- package/dist/translations/ru.js.map +1 -0
- package/dist/translations/uk.d.ts +7 -0
- package/dist/translations/uk.js +379 -0
- package/dist/translations/uk.js.map +1 -0
- package/dist/translations/zh.d.ts +7 -0
- package/dist/translations/zh.js +379 -0
- package/dist/translations/zh.js.map +1 -0
- package/dist/validation/builtin/index.js +2 -0
- package/dist/validation/builtin/index.js.map +1 -1
- package/dist/validation/builtin/integer.js +15 -0
- package/dist/validation/builtin/integer.js.map +1 -0
- package/dist/validation/builtin/notAlreadySubmitted.js +5 -3
- package/dist/validation/builtin/notAlreadySubmitted.js.map +1 -1
- package/package.json +24 -16
- package/styles/form-builder.css +84 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { pluginSlugsOf } from "../../plugin/collectionSlugs.js";
|
|
2
|
+
import { pollConfigOf } from "../../form/pollState.js";
|
|
3
|
+
import { formIdOf } from "../../submissions/formIdOf.js";
|
|
4
|
+
import { answerValues } from "./answerValues.js";
|
|
5
|
+
import "./votesCollection.js";
|
|
6
|
+
import { bumpPollVote } from "./bumpPollVote.js";
|
|
7
|
+
//#region src/poll/votes/voteTallyHook.ts
|
|
8
|
+
const isComplete = (doc) => doc.status == null || doc.status === "complete";
|
|
9
|
+
/** Narrow a request's loosely typed `transactionID` to the value `bumpPollVote` accepts. */
|
|
10
|
+
const transactionIDOf = (transactionID) => typeof transactionID === "number" || typeof transactionID === "string" ? transactionID : void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Vote counting: a submission entering `complete` bumps one tally per answer value for the poll
|
|
13
|
+
* results field, plus the shared respondents row. Votes are permanent by default: no decrement on
|
|
14
|
+
* delete, edit, or prune, which is what lets a pruned submission's vote survive. The one
|
|
15
|
+
* exception is an `allowChange` poll, where an update of an already-complete submission moves the
|
|
16
|
+
* tally with the changed answer (decrement removed values, increment added ones, respondents
|
|
17
|
+
* untouched); delete and prune stay permanent even then. Two concurrent changes of the same
|
|
18
|
+
* submission can each read the same previous answer and double-adjust; the client's double-submit
|
|
19
|
+
* guard makes that rare and `recountPollVotes` is the healer, matching `bumpPollVote`'s residual
|
|
20
|
+
* drift note. Bump failures propagate so the enclosing transaction rolls the submission back
|
|
21
|
+
* rather than silently undercounting; registered before the swallow-all dispatch hook.
|
|
22
|
+
*/
|
|
23
|
+
const makeVoteTallyHook = () => async ({ doc, previousDoc, operation, req }) => {
|
|
24
|
+
if (operation !== "create" && operation !== "update") return doc;
|
|
25
|
+
const entering = operation === "create" && isComplete(doc) || operation === "update" && isComplete(doc) && !isComplete(previousDoc ?? {});
|
|
26
|
+
const changing = operation === "update" && isComplete(doc) && isComplete(previousDoc ?? {}) && !entering;
|
|
27
|
+
if (!entering && !changing) return doc;
|
|
28
|
+
const formId = formIdOf(doc.form);
|
|
29
|
+
if (formId == null) return doc;
|
|
30
|
+
const form = await req.payload.findByID({
|
|
31
|
+
collection: pluginSlugsOf(req.payload).forms,
|
|
32
|
+
id: formId,
|
|
33
|
+
depth: 0,
|
|
34
|
+
overrideAccess: true,
|
|
35
|
+
req
|
|
36
|
+
}).catch(() => null);
|
|
37
|
+
const poll = form?.pollEnabled === true ? pollConfigOf(form.poll) : void 0;
|
|
38
|
+
const resultsField = typeof poll?.resultsField === "string" && poll.resultsField.length > 0 ? poll.resultsField : void 0;
|
|
39
|
+
if (!resultsField) return doc;
|
|
40
|
+
const transactionID = transactionIDOf(req.transactionID);
|
|
41
|
+
const base = {
|
|
42
|
+
form: String(formId),
|
|
43
|
+
field: resultsField
|
|
44
|
+
};
|
|
45
|
+
if (changing) {
|
|
46
|
+
if (poll?.allowChange !== true) return doc;
|
|
47
|
+
const previous = answerValues(previousDoc?.values, resultsField);
|
|
48
|
+
const next = answerValues(doc.values, resultsField);
|
|
49
|
+
const removed = previous.filter((value) => !next.includes(value));
|
|
50
|
+
const added = next.filter((value) => !previous.includes(value));
|
|
51
|
+
for (const value of removed) await bumpPollVote(req.payload, {
|
|
52
|
+
...base,
|
|
53
|
+
value
|
|
54
|
+
}, -1, transactionID);
|
|
55
|
+
for (const value of added) await bumpPollVote(req.payload, {
|
|
56
|
+
...base,
|
|
57
|
+
value
|
|
58
|
+
}, 1, transactionID);
|
|
59
|
+
return doc;
|
|
60
|
+
}
|
|
61
|
+
const votes = answerValues(doc.values, resultsField);
|
|
62
|
+
if (votes.length === 0) return doc;
|
|
63
|
+
for (const value of votes) await bumpPollVote(req.payload, {
|
|
64
|
+
...base,
|
|
65
|
+
value
|
|
66
|
+
}, 1, transactionID);
|
|
67
|
+
await bumpPollVote(req.payload, {
|
|
68
|
+
...base,
|
|
69
|
+
value: ""
|
|
70
|
+
}, 1, transactionID);
|
|
71
|
+
return doc;
|
|
72
|
+
};
|
|
73
|
+
//#endregion
|
|
74
|
+
export { makeVoteTallyHook, transactionIDOf };
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=voteTallyHook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"voteTallyHook.js","names":[],"sources":["../../../src/poll/votes/voteTallyHook.ts"],"sourcesContent":["import type { CollectionAfterChangeHook } from 'payload'\nimport { pollConfigOf } from '../../form/pollState'\nimport { pluginSlugsOf } from '../../plugin/collectionSlugs'\nimport { formIdOf } from '../../submissions/formIdOf'\nimport { answerValues } from './answerValues'\nimport { bumpPollVote } from './bumpPollVote'\nimport { RESPONDENTS_VALUE } from './votesCollection'\n\nconst isComplete = (doc: { status?: unknown }): boolean =>\n\tdoc.status == null || doc.status === 'complete'\n\n/** Narrow a request's loosely typed `transactionID` to the value `bumpPollVote` accepts. */\nexport const transactionIDOf = (transactionID: unknown): number | string | undefined =>\n\ttypeof transactionID === 'number' || typeof transactionID === 'string' ? transactionID : undefined\n\n/**\n * Vote counting: a submission entering `complete` bumps one tally per answer value for the poll\n * results field, plus the shared respondents row. Votes are permanent by default: no decrement on\n * delete, edit, or prune, which is what lets a pruned submission's vote survive. The one\n * exception is an `allowChange` poll, where an update of an already-complete submission moves the\n * tally with the changed answer (decrement removed values, increment added ones, respondents\n * untouched); delete and prune stay permanent even then. Two concurrent changes of the same\n * submission can each read the same previous answer and double-adjust; the client's double-submit\n * guard makes that rare and `recountPollVotes` is the healer, matching `bumpPollVote`'s residual\n * drift note. Bump failures propagate so the enclosing transaction rolls the submission back\n * rather than silently undercounting; registered before the swallow-all dispatch hook.\n */\nexport const makeVoteTallyHook =\n\t(): CollectionAfterChangeHook =>\n\tasync ({ doc, previousDoc, operation, req }) => {\n\t\tif (operation !== 'create' && operation !== 'update') return doc\n\t\tconst entering =\n\t\t\t(operation === 'create' && isComplete(doc)) ||\n\t\t\t(operation === 'update' && isComplete(doc) && !isComplete(previousDoc ?? {}))\n\t\tconst changing =\n\t\t\toperation === 'update' && isComplete(doc) && isComplete(previousDoc ?? {}) && !entering\n\t\tif (!entering && !changing) return doc\n\t\tconst formId = formIdOf(doc.form)\n\t\tif (formId == null) return doc\n\t\tconst form = await req.payload\n\t\t\t.findByID({\n\t\t\t\tcollection: pluginSlugsOf(req.payload).forms,\n\t\t\t\tid: formId,\n\t\t\t\tdepth: 0,\n\t\t\t\toverrideAccess: true,\n\t\t\t\treq,\n\t\t\t})\n\t\t\t.catch(() => null)\n\t\tconst poll = form?.pollEnabled === true ? pollConfigOf(form.poll) : undefined\n\t\tconst resultsField =\n\t\t\ttypeof poll?.resultsField === 'string' && poll.resultsField.length > 0\n\t\t\t\t? poll.resultsField\n\t\t\t\t: undefined\n\t\tif (!resultsField) return doc\n\t\tconst transactionID = transactionIDOf(req.transactionID)\n\t\tconst base = { form: String(formId), field: resultsField }\n\t\tif (changing) {\n\t\t\tif (poll?.allowChange !== true) return doc\n\t\t\tconst previous = answerValues(previousDoc?.values, resultsField)\n\t\t\tconst next = answerValues(doc.values, resultsField)\n\t\t\tconst removed = previous.filter((value) => !next.includes(value))\n\t\t\tconst added = next.filter((value) => !previous.includes(value))\n\t\t\tfor (const value of removed) {\n\t\t\t\tawait bumpPollVote(req.payload, { ...base, value }, -1, transactionID)\n\t\t\t}\n\t\t\tfor (const value of added) {\n\t\t\t\tawait bumpPollVote(req.payload, { ...base, value }, 1, transactionID)\n\t\t\t}\n\t\t\treturn doc\n\t\t}\n\t\tconst votes = answerValues(doc.values, resultsField)\n\t\tif (votes.length === 0) return doc\n\t\tfor (const value of votes) {\n\t\t\tawait bumpPollVote(req.payload, { ...base, value }, 1, transactionID)\n\t\t}\n\t\tawait bumpPollVote(req.payload, { ...base, value: RESPONDENTS_VALUE }, 1, transactionID)\n\t\treturn doc\n\t}\n"],"mappings":";;;;;;;AAQA,MAAM,cAAc,QACnB,IAAI,UAAU,QAAQ,IAAI,WAAW;;AAGtC,MAAa,mBAAmB,kBAC/B,OAAO,kBAAkB,YAAY,OAAO,kBAAkB,WAAW,gBAAgB,KAAA;;;;;;;;;;;;;AAc1F,MAAa,0BAEZ,OAAO,EAAE,KAAK,aAAa,WAAW,UAAU;CAC/C,IAAI,cAAc,YAAY,cAAc,UAAU,OAAO;CAC7D,MAAM,WACJ,cAAc,YAAY,WAAW,GAAG,KACxC,cAAc,YAAY,WAAW,GAAG,KAAK,CAAC,WAAW,eAAe,CAAC,CAAC;CAC5E,MAAM,WACL,cAAc,YAAY,WAAW,GAAG,KAAK,WAAW,eAAe,CAAC,CAAC,KAAK,CAAC;CAChF,IAAI,CAAC,YAAY,CAAC,UAAU,OAAO;CACnC,MAAM,SAAS,SAAS,IAAI,IAAI;CAChC,IAAI,UAAU,MAAM,OAAO;CAC3B,MAAM,OAAO,MAAM,IAAI,QACrB,SAAS;EACT,YAAY,cAAc,IAAI,OAAO,EAAE;EACvC,IAAI;EACJ,OAAO;EACP,gBAAgB;EAChB;CACD,CAAC,EACA,YAAY,IAAI;CAClB,MAAM,OAAO,MAAM,gBAAgB,OAAO,aAAa,KAAK,IAAI,IAAI,KAAA;CACpE,MAAM,eACL,OAAO,MAAM,iBAAiB,YAAY,KAAK,aAAa,SAAS,IAClE,KAAK,eACL,KAAA;CACJ,IAAI,CAAC,cAAc,OAAO;CAC1B,MAAM,gBAAgB,gBAAgB,IAAI,aAAa;CACvD,MAAM,OAAO;EAAE,MAAM,OAAO,MAAM;EAAG,OAAO;CAAa;CACzD,IAAI,UAAU;EACb,IAAI,MAAM,gBAAgB,MAAM,OAAO;EACvC,MAAM,WAAW,aAAa,aAAa,QAAQ,YAAY;EAC/D,MAAM,OAAO,aAAa,IAAI,QAAQ,YAAY;EAClD,MAAM,UAAU,SAAS,QAAQ,UAAU,CAAC,KAAK,SAAS,KAAK,CAAC;EAChE,MAAM,QAAQ,KAAK,QAAQ,UAAU,CAAC,SAAS,SAAS,KAAK,CAAC;EAC9D,KAAK,MAAM,SAAS,SACnB,MAAM,aAAa,IAAI,SAAS;GAAE,GAAG;GAAM;EAAM,GAAG,IAAI,aAAa;EAEtE,KAAK,MAAM,SAAS,OACnB,MAAM,aAAa,IAAI,SAAS;GAAE,GAAG;GAAM;EAAM,GAAG,GAAG,aAAa;EAErE,OAAO;CACR;CACA,MAAM,QAAQ,aAAa,IAAI,QAAQ,YAAY;CACnD,IAAI,MAAM,WAAW,GAAG,OAAO;CAC/B,KAAK,MAAM,SAAS,OACnB,MAAM,aAAa,IAAI,SAAS;EAAE,GAAG;EAAM;CAAM,GAAG,GAAG,aAAa;CAErE,MAAM,aAAa,IAAI,SAAS;EAAE,GAAG;EAAM,OAAA;CAAyB,GAAG,GAAG,aAAa;CACvF,OAAO;AACR"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CollectionConfig } from "payload";
|
|
2
|
+
|
|
3
|
+
//#region src/poll/votes/votesCollection.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Default slug of the tally collection. A host may rename it via `poll.votes.overrides.slug`;
|
|
6
|
+
* read the live slug with `collectionSlugsOf(payload).pollVotes`.
|
|
7
|
+
*/
|
|
8
|
+
declare const POLL_VOTES_SLUG: "form-poll-votes";
|
|
9
|
+
/** Reserved tally `value` marking the per-field respondents counter (empty answers are never counted, so '' is free). */
|
|
10
|
+
declare const RESPONDENTS_VALUE = "";
|
|
11
|
+
/**
|
|
12
|
+
* Shard count for hot tally counters. Mongo aborts one of two transactions that write the same
|
|
13
|
+
* document (WriteConflict/TransientTransactionError), so concurrent transactional votes for the
|
|
14
|
+
* same (form, field, value) would deterministically fail one submission. Spreading each
|
|
15
|
+
* transactional bump across VOTE_SHARDS rows makes concurrent bumps land on different shards
|
|
16
|
+
* with high probability, so transactions stop contending. Postgres row locks serialize
|
|
17
|
+
* ON CONFLICT updates without aborting and non-transactional Mongo $inc is conflict-free, so
|
|
18
|
+
* both always use shard 0. Reads sum counts across shards.
|
|
19
|
+
*/
|
|
20
|
+
declare const VOTE_SHARDS = 8;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { POLL_VOTES_SLUG, RESPONDENTS_VALUE, VOTE_SHARDS };
|
|
23
|
+
//# sourceMappingURL=votesCollection.d.ts.map
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { keys } from "../../translations/keys.js";
|
|
2
|
+
import { labelForKey } from "../../translations/server.js";
|
|
3
|
+
import { DEFAULT_COLLECTION_SLUGS } from "../../plugin/collectionSlugs.js";
|
|
4
|
+
import { isLoggedIn } from "../../plugin/access.js";
|
|
5
|
+
//#region src/poll/votes/votesCollection.ts
|
|
6
|
+
/**
|
|
7
|
+
* Default slug of the tally collection. A host may rename it via `poll.votes.overrides.slug`;
|
|
8
|
+
* read the live slug with `collectionSlugsOf(payload).pollVotes`.
|
|
9
|
+
*/
|
|
10
|
+
const POLL_VOTES_SLUG = DEFAULT_COLLECTION_SLUGS.pollVotes;
|
|
11
|
+
/** Reserved tally `value` marking the per-field respondents counter (empty answers are never counted, so '' is free). */
|
|
12
|
+
const RESPONDENTS_VALUE = "";
|
|
13
|
+
/**
|
|
14
|
+
* Shard count for hot tally counters. Mongo aborts one of two transactions that write the same
|
|
15
|
+
* document (WriteConflict/TransientTransactionError), so concurrent transactional votes for the
|
|
16
|
+
* same (form, field, value) would deterministically fail one submission. Spreading each
|
|
17
|
+
* transactional bump across VOTE_SHARDS rows makes concurrent bumps land on different shards
|
|
18
|
+
* with high probability, so transactions stop contending. Postgres row locks serialize
|
|
19
|
+
* ON CONFLICT updates without aborting and non-transactional Mongo $inc is conflict-free, so
|
|
20
|
+
* both always use shard 0. Reads sum counts across shards.
|
|
21
|
+
*/
|
|
22
|
+
const VOTE_SHARDS = 8;
|
|
23
|
+
/**
|
|
24
|
+
* Append-only aggregate tallies: up to VOTE_SHARDS rows per (form, field, value) with running
|
|
25
|
+
* counts that readers sum, plus respondents rows per (form, field) under RESPONDENTS_VALUE.
|
|
26
|
+
* Rows carry no per-voter data and are written via atomic upsert-increments (see bumpPollVote),
|
|
27
|
+
* made concurrency-safe by the unique compound index. Ids are stored as strings for cross-DB
|
|
28
|
+
* parity.
|
|
29
|
+
*
|
|
30
|
+
* `indexes` is intentionally not part of the override surface: the unique compound index is
|
|
31
|
+
* load-bearing for the atomic upsert-increment, so it is fixed rather than spread with the rest.
|
|
32
|
+
*/
|
|
33
|
+
const buildPollVotesCollection = (args) => {
|
|
34
|
+
const defaultFields = [
|
|
35
|
+
{
|
|
36
|
+
name: "form",
|
|
37
|
+
type: "text",
|
|
38
|
+
required: true,
|
|
39
|
+
index: true
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "field",
|
|
43
|
+
type: "text",
|
|
44
|
+
required: true
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "value",
|
|
48
|
+
type: "text"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: "shard",
|
|
52
|
+
type: "number",
|
|
53
|
+
required: true,
|
|
54
|
+
defaultValue: 0
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: "count",
|
|
58
|
+
type: "number",
|
|
59
|
+
required: true,
|
|
60
|
+
defaultValue: 0
|
|
61
|
+
}
|
|
62
|
+
];
|
|
63
|
+
return {
|
|
64
|
+
versions: false,
|
|
65
|
+
authorship: false,
|
|
66
|
+
...args.overrides ?? {},
|
|
67
|
+
slug: args.slug ?? POLL_VOTES_SLUG,
|
|
68
|
+
labels: {
|
|
69
|
+
singular: labelForKey(keys.collectionPollVoteSingular),
|
|
70
|
+
plural: labelForKey(keys.collectionPollVotePlural),
|
|
71
|
+
...args.overrides?.labels ?? {}
|
|
72
|
+
},
|
|
73
|
+
admin: {
|
|
74
|
+
hidden: true,
|
|
75
|
+
...args.overrides?.admin ?? {}
|
|
76
|
+
},
|
|
77
|
+
access: {
|
|
78
|
+
read: isLoggedIn,
|
|
79
|
+
...args.overrides?.access ?? {}
|
|
80
|
+
},
|
|
81
|
+
indexes: [{
|
|
82
|
+
fields: [
|
|
83
|
+
"form",
|
|
84
|
+
"field",
|
|
85
|
+
"value",
|
|
86
|
+
"shard"
|
|
87
|
+
],
|
|
88
|
+
unique: true
|
|
89
|
+
}],
|
|
90
|
+
fields: args.overrides?.fields ? args.overrides.fields({ defaultFields }) : defaultFields
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
//#endregion
|
|
94
|
+
export { POLL_VOTES_SLUG, RESPONDENTS_VALUE, VOTE_SHARDS, buildPollVotesCollection };
|
|
95
|
+
|
|
96
|
+
//# sourceMappingURL=votesCollection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"votesCollection.js","names":[],"sources":["../../../src/poll/votes/votesCollection.ts"],"sourcesContent":["import type { CollectionConfig } from 'payload'\nimport { isLoggedIn } from '../../plugin/access'\nimport type { CollectionOverrides } from '../../plugin/collectionOverrides'\nimport { DEFAULT_COLLECTION_SLUGS } from '../../plugin/collectionSlugs'\nimport { keys } from '../../translations/keys'\nimport { labelForKey } from '../../translations/server'\n\n/**\n * Default slug of the tally collection. A host may rename it via `poll.votes.overrides.slug`;\n * read the live slug with `collectionSlugsOf(payload).pollVotes`.\n */\nexport const POLL_VOTES_SLUG = DEFAULT_COLLECTION_SLUGS.pollVotes\n\n/** Reserved tally `value` marking the per-field respondents counter (empty answers are never counted, so '' is free). */\nexport const RESPONDENTS_VALUE = ''\n\n/**\n * Shard count for hot tally counters. Mongo aborts one of two transactions that write the same\n * document (WriteConflict/TransientTransactionError), so concurrent transactional votes for the\n * same (form, field, value) would deterministically fail one submission. Spreading each\n * transactional bump across VOTE_SHARDS rows makes concurrent bumps land on different shards\n * with high probability, so transactions stop contending. Postgres row locks serialize\n * ON CONFLICT updates without aborting and non-transactional Mongo $inc is conflict-free, so\n * both always use shard 0. Reads sum counts across shards.\n */\nexport const VOTE_SHARDS = 8\n\n/**\n * Append-only aggregate tallies: up to VOTE_SHARDS rows per (form, field, value) with running\n * counts that readers sum, plus respondents rows per (form, field) under RESPONDENTS_VALUE.\n * Rows carry no per-voter data and are written via atomic upsert-increments (see bumpPollVote),\n * made concurrency-safe by the unique compound index. Ids are stored as strings for cross-DB\n * parity.\n *\n * `indexes` is intentionally not part of the override surface: the unique compound index is\n * load-bearing for the atomic upsert-increment, so it is fixed rather than spread with the rest.\n */\nexport const buildPollVotesCollection = (args: {\n\toverrides?: CollectionOverrides\n\t/** The resolved tally slug (`poll.votes.overrides.slug`); the default when omitted. */\n\tslug?: string\n}): CollectionConfig => {\n\tconst defaultFields: CollectionConfig['fields'] = [\n\t\t{ name: 'form', type: 'text', required: true, index: true },\n\t\t{ name: 'field', type: 'text', required: true },\n\t\t{ name: 'value', type: 'text' },\n\t\t{ name: 'shard', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'count', type: 'number', required: true, defaultValue: 0 },\n\t]\n\n\treturn {\n\t\t// A system-maintained tally: no history, no core authorship (both default on in Payload 4).\n\t\tversions: false,\n\t\tauthorship: false,\n\t\t...(args.overrides ?? {}),\n\t\tslug: args.slug ?? POLL_VOTES_SLUG,\n\t\tlabels: {\n\t\t\tsingular: labelForKey(keys.collectionPollVoteSingular),\n\t\t\tplural: labelForKey(keys.collectionPollVotePlural),\n\t\t\t...(args.overrides?.labels ?? {}),\n\t\t},\n\t\tadmin: { hidden: true, ...(args.overrides?.admin ?? {}) },\n\t\taccess: { read: isLoggedIn, ...(args.overrides?.access ?? {}) },\n\t\tindexes: [{ fields: ['form', 'field', 'value', 'shard'], unique: true }],\n\t\tfields: args.overrides?.fields ? args.overrides.fields({ defaultFields }) : defaultFields,\n\t}\n}\n"],"mappings":";;;;;;;;;AAWA,MAAa,kBAAkB,yBAAyB;;AAGxD,MAAa,oBAAoB;;;;;;;;;;AAWjC,MAAa,cAAc;;;;;;;;;;;AAY3B,MAAa,4BAA4B,SAIjB;CACvB,MAAM,gBAA4C;EACjD;GAAE,MAAM;GAAQ,MAAM;GAAQ,UAAU;GAAM,OAAO;EAAK;EAC1D;GAAE,MAAM;GAAS,MAAM;GAAQ,UAAU;EAAK;EAC9C;GAAE,MAAM;GAAS,MAAM;EAAO;EAC9B;GAAE,MAAM;GAAS,MAAM;GAAU,UAAU;GAAM,cAAc;EAAE;EACjE;GAAE,MAAM;GAAS,MAAM;GAAU,UAAU;GAAM,cAAc;EAAE;CAClE;CAEA,OAAO;EAEN,UAAU;EACV,YAAY;EACZ,GAAI,KAAK,aAAa,CAAC;EACvB,MAAM,KAAK,QAAQ;EACnB,QAAQ;GACP,UAAU,YAAY,KAAK,0BAA0B;GACrD,QAAQ,YAAY,KAAK,wBAAwB;GACjD,GAAI,KAAK,WAAW,UAAU,CAAC;EAChC;EACA,OAAO;GAAE,QAAQ;GAAM,GAAI,KAAK,WAAW,SAAS,CAAC;EAAG;EACxD,QAAQ;GAAE,MAAM;GAAY,GAAI,KAAK,WAAW,UAAU,CAAC;EAAG;EAC9D,SAAS,CAAC;GAAE,QAAQ;IAAC;IAAQ;IAAS;IAAS;GAAO;GAAG,QAAQ;EAAK,CAAC;EACvE,QAAQ,KAAK,WAAW,SAAS,KAAK,UAAU,OAAO,EAAE,cAAc,CAAC,IAAI;CAC7E;AACD"}
|
package/dist/react/Form.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { SubmissionValue } from "../submissions/types.js";
|
|
1
2
|
import { AnyFormFieldDefinition } from "../fields/types.js";
|
|
2
3
|
import { BodyConverter } from "../actions/body/converters.js";
|
|
3
4
|
import { FormButtonSettings, FormDocument, FormPollSettings, FormResponseSettings } from "../form/types.js";
|
|
5
|
+
import { FormAdapters } from "./adapters.js";
|
|
4
6
|
import { RendererTranslate } from "./contract.js";
|
|
5
7
|
import { FormEventSink } from "../events/types.js";
|
|
8
|
+
import { FormBuilderCollectionSlugs } from "../plugin/collectionSlugs.js";
|
|
6
9
|
import { AnyValidationRuleDefinition } from "../validation/types.js";
|
|
7
10
|
import { BackButtonRenderProps, NextButtonRenderProps, SubmitButtonRenderProps } from "./FormControls.js";
|
|
8
11
|
import { FormPresentation } from "./presentation/types.js";
|
|
@@ -25,14 +28,22 @@ type FormSuccessResponse = {
|
|
|
25
28
|
};
|
|
26
29
|
/** The second argument to `onSuccess`: the resolved success response (an object, so it can grow). */
|
|
27
30
|
type FormSuccessResult = {
|
|
28
|
-
response?: FormSuccessResponse;
|
|
31
|
+
response?: FormSuccessResponse; /** The submitted answers (reserved keys like the honeypot and captcha token excluded), e.g. for `<Poll>` to track the voter's pick. */
|
|
32
|
+
values?: SubmissionValue[];
|
|
29
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* The plugin's collection slugs as the browser needs them, for a host that renamed them through
|
|
36
|
+
* `overrides.forms.slug` / `overrides.formSubmissions.slug`. Pass the same values the server was
|
|
37
|
+
* given; each omitted key keeps its default.
|
|
38
|
+
*/
|
|
39
|
+
type FormCollections = Partial<Pick<FormBuilderCollectionSlugs, 'forms' | 'formSubmissions'>>;
|
|
30
40
|
type FormProps = {
|
|
31
41
|
form: FormDocument;
|
|
32
42
|
fieldTypes?: AnyFormFieldDefinition[];
|
|
33
43
|
rules?: AnyValidationRuleDefinition[];
|
|
34
44
|
renderers?: RenderersConfig;
|
|
35
|
-
apiRoute?: string;
|
|
45
|
+
apiRoute?: string; /** Renamed plugin collections: submits post to `formSubmissions`, `<Poll>` reads results from `forms`. */
|
|
46
|
+
collections?: FormCollections;
|
|
36
47
|
onSubmit?: SubmitHandler;
|
|
37
48
|
/**
|
|
38
49
|
* Called after a successful submission with the submission id and the resolved success response
|
|
@@ -52,9 +63,23 @@ type FormProps = {
|
|
|
52
63
|
* and keep the form usable.
|
|
53
64
|
*/
|
|
54
65
|
successBehavior?: 'replace' | 'reset';
|
|
66
|
+
/**
|
|
67
|
+
* Client halves of the plugin's registered `calc.functions` (same keys), used for the live calc
|
|
68
|
+
* preview only; the server always recomputes authoritatively at submit with the registry's own
|
|
69
|
+
* functions.
|
|
70
|
+
*/
|
|
71
|
+
calcFunctions?: Record<string, (args: number[]) => number>;
|
|
55
72
|
events?: FormEventSink;
|
|
56
|
-
t?: RendererTranslate;
|
|
73
|
+
t?: RendererTranslate; /** The visitor's locale for renderer strings and value formatting, e.g. `en-US` (`'en'` when absent). */
|
|
57
74
|
locale?: string;
|
|
75
|
+
/**
|
|
76
|
+
* The Payload content locale to submit under, one of the host's `localization` codes (`en`, `de`),
|
|
77
|
+
* sent as `?locale=` so the server stores it on the submission and the post-submit actions
|
|
78
|
+
* (confirmation emails included) render in it. A `<Poll>` also reads its results in it. Separate
|
|
79
|
+
* from `locale` because a formatting tag like `en-US` is usually not a content locale code.
|
|
80
|
+
* Absent, the submission takes the host's default locale.
|
|
81
|
+
*/
|
|
82
|
+
submissionLocale?: string;
|
|
58
83
|
layout?: boolean; /** Submit button label. Precedence: this prop, then the form's `buttons.submitLabel`, then the translated default. */
|
|
59
84
|
submitLabel?: string; /** "Next" button label for multi-step forms. Precedence: this prop, then the form's `buttons.nextLabel`, then the translated default. */
|
|
60
85
|
nextLabel?: string; /** "Back" button label for multi-step forms. Precedence: this prop, then the form's `buttons.prevLabel`, then the translated default. */
|
|
@@ -74,7 +99,13 @@ type FormProps = {
|
|
|
74
99
|
honeypot?: false | {
|
|
75
100
|
name?: string;
|
|
76
101
|
}; /** A token from your captcha widget; verified server-side when a captcha provider is configured. */
|
|
77
|
-
captchaToken?: string;
|
|
102
|
+
captchaToken?: string;
|
|
103
|
+
/**
|
|
104
|
+
* A signed form-context token from `signFormContext` (server/RSC), tying this render to a document
|
|
105
|
+
* (e.g. the profile page it is on). Carried alongside the answers, verified server-side, and
|
|
106
|
+
* available to `email.recipientSources`; it is never stored as an answer.
|
|
107
|
+
*/
|
|
108
|
+
context?: string; /** Custom layout: render fields with `useField`/`useFormState` instead of the auto-rendered field loop. */
|
|
78
109
|
children?: ReactNode; /** Chrome rendered inside the form, above the fields, in default mode (e.g. `<FormSteps />`). */
|
|
79
110
|
header?: ReactNode; /** Additional CSS class names applied to the root `<form>` element (and the success node). */
|
|
80
111
|
className?: string; /** Replace the default submit button entirely. Receives the resolved label and submitting state. */
|
|
@@ -84,6 +115,12 @@ type FormProps = {
|
|
|
84
115
|
submitButtonClassName?: string; /** CSS class forwarded to the default "Next" `<button>`. Ignored when `renderNext` is provided. */
|
|
85
116
|
nextButtonClassName?: string; /** CSS class forwarded to the default "Back" `<button>`. Ignored when `renderBack` is provided. */
|
|
86
117
|
backButtonClassName?: string;
|
|
118
|
+
/**
|
|
119
|
+
* Host-owned effects (navigation, vote persistence, script loading). Each member defaults to
|
|
120
|
+
* the built-in DOM behavior; see {@link FormAdapters}. Pass a stable reference (module scope or
|
|
121
|
+
* `useMemo`), matching the other injectable props.
|
|
122
|
+
*/
|
|
123
|
+
adapters?: FormAdapters;
|
|
87
124
|
};
|
|
88
125
|
/** The headless form controller: state, progressive client validation, conditional visibility, submission, events. */
|
|
89
126
|
declare const Form: ({
|
|
@@ -92,14 +129,17 @@ declare const Form: ({
|
|
|
92
129
|
rules,
|
|
93
130
|
renderers,
|
|
94
131
|
apiRoute,
|
|
132
|
+
collections,
|
|
95
133
|
onSubmit,
|
|
96
134
|
onSuccess,
|
|
97
135
|
onError,
|
|
98
136
|
converters,
|
|
99
137
|
successBehavior,
|
|
138
|
+
calcFunctions,
|
|
100
139
|
events,
|
|
101
140
|
t,
|
|
102
|
-
locale,
|
|
141
|
+
locale: localeProp,
|
|
142
|
+
submissionLocale,
|
|
103
143
|
layout,
|
|
104
144
|
submitLabel,
|
|
105
145
|
nextLabel,
|
|
@@ -113,6 +153,7 @@ declare const Form: ({
|
|
|
113
153
|
initialValues,
|
|
114
154
|
honeypot,
|
|
115
155
|
captchaToken,
|
|
156
|
+
context,
|
|
116
157
|
children,
|
|
117
158
|
header,
|
|
118
159
|
className,
|
|
@@ -121,8 +162,9 @@ declare const Form: ({
|
|
|
121
162
|
renderBack,
|
|
122
163
|
submitButtonClassName,
|
|
123
164
|
nextButtonClassName,
|
|
124
|
-
backButtonClassName
|
|
165
|
+
backButtonClassName,
|
|
166
|
+
adapters
|
|
125
167
|
}: FormProps) => import("react/jsx-runtime").JSX.Element;
|
|
126
168
|
//#endregion
|
|
127
|
-
export { Form, FormProps, FormSuccessResponse, FormSuccessResult };
|
|
169
|
+
export { Form, FormCollections, FormProps, FormSuccessResponse, FormSuccessResult };
|
|
128
170
|
//# sourceMappingURL=Form.d.ts.map
|
package/dist/react/Form.js
CHANGED
|
@@ -3,7 +3,7 @@ import { keys } from "../translations/keys.js";
|
|
|
3
3
|
import { interpolate } from "../recall/interpolate.js";
|
|
4
4
|
import { serializeBody } from "../actions/body/serializeBody.js";
|
|
5
5
|
import { noopEventSink } from "../events/noopSink.js";
|
|
6
|
-
import { CAPTCHA_TOKEN_KEY, HONEYPOT_VALUE_KEY } from "../spam/constants.js";
|
|
6
|
+
import { CAPTCHA_TOKEN_KEY, CONTEXT_KEY, HONEYPOT_VALUE_KEY } from "../spam/constants.js";
|
|
7
7
|
import { fieldKey, isNamedField } from "../fields/fieldKey.js";
|
|
8
8
|
import { calcExpressionOf, computeCalcFields } from "../calc/computeCalcFields.js";
|
|
9
9
|
import { evaluateCondition } from "../conditions/evaluate.js";
|
|
@@ -14,6 +14,7 @@ import { buildRecallResolver, descriptorsFor } from "../recall/resolver.js";
|
|
|
14
14
|
import { firstStepId, isTerminalStepId, resolveNextStepId, stepFieldNames } from "../flow/engine.js";
|
|
15
15
|
import { cn } from "./cn.js";
|
|
16
16
|
import { makeTranslate } from "../translations/makeTranslate.js";
|
|
17
|
+
import { defaultNavigate } from "./adapters.js";
|
|
17
18
|
import { emitFormEvent } from "./events.js";
|
|
18
19
|
import { FormContext } from "./FormContext.js";
|
|
19
20
|
import { FormControls } from "./FormControls.js";
|
|
@@ -60,7 +61,8 @@ const focusFirstIn = (root, selector) => {
|
|
|
60
61
|
/** A stored button label counts only when it is a non-empty string; anything else falls through. */
|
|
61
62
|
const storedLabel = (value) => typeof value === "string" && value.length > 0 ? value : void 0;
|
|
62
63
|
/** The headless form controller: state, progressive client validation, conditional visibility, submission, events. */
|
|
63
|
-
const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSuccess, onError, converters, successBehavior = "replace", events, t, locale
|
|
64
|
+
const Form = ({ form, fieldTypes, rules, renderers, apiRoute, collections, onSubmit, onSuccess, onError, converters, successBehavior = "replace", calcFunctions, events, t, locale: localeProp, submissionLocale, layout, submitLabel, nextLabel, prevLabel, closeLabel, successMessage, presentation, presentations, onClose, title, initialValues, honeypot, captchaToken, context, children, header, className, renderSubmit, renderNext, renderBack, submitButtonClassName, nextButtonClassName, backButtonClassName, adapters }) => {
|
|
65
|
+
const locale = localeProp ?? "en";
|
|
64
66
|
const honeypotName = honeypot === false ? null : honeypot?.name ?? "website";
|
|
65
67
|
const honeypotRef = useRef(null);
|
|
66
68
|
const registry = useMemo(() => buildFieldTypeRegistry(fieldTypes), [fieldTypes]);
|
|
@@ -92,7 +94,23 @@ const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSucces
|
|
|
92
94
|
...seedFieldValues(fields),
|
|
93
95
|
...initialValues ?? {}
|
|
94
96
|
}));
|
|
95
|
-
const effectiveValues = useMemo(() => computeCalcFields(form.fields, state.values
|
|
97
|
+
const effectiveValues = useMemo(() => computeCalcFields(form.fields, state.values, {
|
|
98
|
+
...form.calcResolved ?? {},
|
|
99
|
+
...calcFunctions ? { functions: calcFunctions } : {}
|
|
100
|
+
}), [
|
|
101
|
+
form.fields,
|
|
102
|
+
state.values,
|
|
103
|
+
form.calcResolved,
|
|
104
|
+
calcFunctions
|
|
105
|
+
]);
|
|
106
|
+
const calcValues = useMemo(() => {
|
|
107
|
+
const values = {};
|
|
108
|
+
for (const field of form.fields) if (calcExpressionOf(field) && isNamedField(field)) {
|
|
109
|
+
const computed = effectiveValues[field.name];
|
|
110
|
+
values[field.name] = typeof computed === "number" ? computed : 0;
|
|
111
|
+
}
|
|
112
|
+
return values;
|
|
113
|
+
}, [form.fields, effectiveValues]);
|
|
96
114
|
const recall = useMemo(() => buildRecallResolver({
|
|
97
115
|
fields: form.fields,
|
|
98
116
|
values: effectiveValues,
|
|
@@ -123,10 +141,15 @@ const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSucces
|
|
|
123
141
|
const allStepIds = useMemo(() => flow ? flow.steps.map((flowStep) => flowStep.id) : [DEFAULT_STEP_ID], [flow]);
|
|
124
142
|
const formRef = useRef(null);
|
|
125
143
|
const pendingFocusRef = useRef(null);
|
|
144
|
+
const focusSeqRef = useRef(0);
|
|
126
145
|
const [focusNonce, setFocusNonce] = useState(0);
|
|
127
146
|
const requestFocus = (intent) => {
|
|
128
|
-
|
|
129
|
-
|
|
147
|
+
focusSeqRef.current += 1;
|
|
148
|
+
pendingFocusRef.current = {
|
|
149
|
+
intent,
|
|
150
|
+
nonce: focusSeqRef.current
|
|
151
|
+
};
|
|
152
|
+
setFocusNonce(focusSeqRef.current);
|
|
130
153
|
};
|
|
131
154
|
const dispatch = useCallback((action) => {
|
|
132
155
|
if (action.type === "SET_VALUE" && !startedRef.current) {
|
|
@@ -332,9 +355,10 @@ const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSucces
|
|
|
332
355
|
});
|
|
333
356
|
};
|
|
334
357
|
useEffect(() => {
|
|
335
|
-
const
|
|
336
|
-
if (!
|
|
358
|
+
const pending = pendingFocusRef.current;
|
|
359
|
+
if (!pending || pending.nonce !== focusNonce) return;
|
|
337
360
|
pendingFocusRef.current = null;
|
|
361
|
+
const { intent } = pending;
|
|
338
362
|
if (intent === "firstInvalid") {
|
|
339
363
|
focusFirstIn(formRef.current, "[aria-invalid=\"true\"]");
|
|
340
364
|
return;
|
|
@@ -405,6 +429,7 @@ const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSucces
|
|
|
405
429
|
}
|
|
406
430
|
rawDispatch({ type: "SUBMIT_START" });
|
|
407
431
|
const values = answeredValues();
|
|
432
|
+
const answers = [...values];
|
|
408
433
|
if (honeypotName) {
|
|
409
434
|
const decoy = honeypotRef.current?.value ?? "";
|
|
410
435
|
if (decoy !== "") values.push({
|
|
@@ -416,12 +441,18 @@ const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSucces
|
|
|
416
441
|
field: CAPTCHA_TOKEN_KEY,
|
|
417
442
|
value: captchaToken
|
|
418
443
|
});
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
})
|
|
444
|
+
if (context) values.push({
|
|
445
|
+
field: CONTEXT_KEY,
|
|
446
|
+
value: context
|
|
447
|
+
});
|
|
448
|
+
const input = {
|
|
423
449
|
formId: form.id,
|
|
424
450
|
values,
|
|
451
|
+
...submissionLocale ? { locale: submissionLocale } : {},
|
|
452
|
+
...collections?.formSubmissions ? { collection: collections.formSubmissions } : {}
|
|
453
|
+
};
|
|
454
|
+
const result = onSubmit ? await onSubmit(input) : await submitForm({
|
|
455
|
+
...input,
|
|
425
456
|
apiRoute
|
|
426
457
|
});
|
|
427
458
|
submittingRef.current = false;
|
|
@@ -431,7 +462,10 @@ const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSucces
|
|
|
431
462
|
type: "submission.created",
|
|
432
463
|
submissionId: result.submissionId
|
|
433
464
|
});
|
|
434
|
-
onSuccess?.(result.submissionId, {
|
|
465
|
+
onSuccess?.(result.submissionId, {
|
|
466
|
+
response: resolveSuccessResponse(),
|
|
467
|
+
values: answers
|
|
468
|
+
});
|
|
435
469
|
if (successBehavior === "reset") {
|
|
436
470
|
rawDispatch({
|
|
437
471
|
type: "RESET",
|
|
@@ -450,7 +484,7 @@ const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSucces
|
|
|
450
484
|
if (activePresentation.dismissOnSuccess) handleClose();
|
|
451
485
|
}
|
|
452
486
|
const redirectUrl = form.response?.type === "redirect" ? form.response.redirect?.url : void 0;
|
|
453
|
-
if (typeof redirectUrl === "string" && redirectUrl.length > 0
|
|
487
|
+
if (typeof redirectUrl === "string" && redirectUrl.length > 0) (adapters?.navigate ?? defaultNavigate)(redirectUrl, { replace: false });
|
|
454
488
|
} else {
|
|
455
489
|
if (result.fieldErrors) rawDispatch({
|
|
456
490
|
type: "SET_ALL_ISSUES",
|
|
@@ -511,6 +545,7 @@ const Form = ({ form, fieldTypes, rules, renderers, apiRoute, onSubmit, onSucces
|
|
|
511
545
|
labels,
|
|
512
546
|
t: translate,
|
|
513
547
|
effectiveValues,
|
|
548
|
+
calcValues,
|
|
514
549
|
recall,
|
|
515
550
|
renderedFields: (flow ? stepVisible : visible).filter((field) => field.hidden !== true && field.calcDisplay !== false),
|
|
516
551
|
converters,
|