2021-05-14 08:04:35 +03:00
|
|
|
<script>
|
2021-06-21 19:21:43 +03:00
|
|
|
import PrimaryButton from "../../PrimaryButton/index.svelte";
|
2021-07-04 22:06:28 +03:00
|
|
|
import { VARIANTS } from "../../PrimaryButton/constants";
|
2021-07-03 19:29:41 +00:00
|
|
|
import CodeSnippet from "../../CodeSnippet/index.svelte";
|
2021-06-21 19:21:43 +03:00
|
|
|
import Checkbox from "../../Form/Checkbox.svelte";
|
2021-05-14 08:04:35 +03:00
|
|
|
|
|
|
|
|
export let title = "";
|
2021-07-03 19:29:41 +00:00
|
|
|
export let code;
|
2021-05-14 08:04:35 +03:00
|
|
|
export let name = "";
|
|
|
|
|
export let options = [];
|
2021-07-06 08:39:17 +03:00
|
|
|
export let answers = [];
|
2021-05-14 08:04:35 +03:00
|
|
|
export let onSubmit = () => {};
|
|
|
|
|
export let onPrevious = () => {};
|
2021-06-21 19:21:43 +03:00
|
|
|
export let defaultValue = [];
|
2021-05-14 08:04:35 +03:00
|
|
|
export let disablePreviousButton = false;
|
2021-07-04 22:06:28 +03:00
|
|
|
export let isPreview = false;
|
2021-07-06 08:39:17 +03:00
|
|
|
export let nextButtonProps = {};
|
2021-05-14 08:04:35 +03:00
|
|
|
|
2021-07-06 08:39:17 +03:00
|
|
|
function getVal(form, name) {
|
2021-06-21 19:21:43 +03:00
|
|
|
let values = [];
|
2021-07-06 08:39:17 +03:00
|
|
|
const checkboxEl = form.elements[name];
|
2021-05-14 08:04:35 +03:00
|
|
|
|
2021-07-06 08:39:17 +03:00
|
|
|
for (let i = 0, len = checkboxEl.length; i < len; i++) {
|
|
|
|
|
if (checkboxEl[i].checked) {
|
|
|
|
|
values.push(checkboxEl[i].value);
|
2021-05-14 08:04:35 +03:00
|
|
|
}
|
|
|
|
|
}
|
2021-06-21 19:21:43 +03:00
|
|
|
|
|
|
|
|
return values;
|
2021-05-14 08:04:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleFormSubmit(event) {
|
2021-07-04 22:06:28 +03:00
|
|
|
if (isPreview) return;
|
2021-07-06 08:39:17 +03:00
|
|
|
const values = getVal(event.target, name);
|
2021-06-21 19:21:43 +03:00
|
|
|
onSubmit(name, values);
|
2021-07-06 08:39:17 +03:00
|
|
|
// event.target.reset();
|
2021-05-14 08:04:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handlePrevious(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
onPrevious();
|
|
|
|
|
}
|
2021-07-06 08:39:17 +03:00
|
|
|
|
|
|
|
|
function getValidationClassName(option) {
|
|
|
|
|
if (defaultValue.includes(option.id)) {
|
|
|
|
|
if (answers.includes(option.id)) {
|
|
|
|
|
return "border-green-700";
|
|
|
|
|
} else {
|
|
|
|
|
return "border-red-700";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2021-05-14 08:04:35 +03:00
|
|
|
</script>
|
|
|
|
|
|
2021-05-22 12:32:32 +03:00
|
|
|
<form on:submit|preventDefault={handleFormSubmit}>
|
2021-05-14 08:04:35 +03:00
|
|
|
<h2>{title}</h2>
|
|
|
|
|
|
2021-07-03 19:29:41 +00:00
|
|
|
{#if code}
|
|
|
|
|
<CodeSnippet {code} />
|
|
|
|
|
{/if}
|
|
|
|
|
|
2021-05-14 08:04:35 +03:00
|
|
|
<div class="ml-4">
|
|
|
|
|
{#each options as option}
|
2021-07-03 19:29:41 +00:00
|
|
|
<button
|
2021-07-06 08:39:17 +03:00
|
|
|
class="cursor-pointer text-left my-2 border-2 border-gray-300 p-2 rounded-md cursor-pointer hover:bg-gray-200 w-full {getValidationClassName(
|
|
|
|
|
option
|
|
|
|
|
)}"
|
2021-07-03 19:29:41 +00:00
|
|
|
type="button"
|
|
|
|
|
>
|
2021-06-21 19:21:43 +03:00
|
|
|
<Checkbox
|
2021-05-14 08:04:35 +03:00
|
|
|
{name}
|
2021-07-06 08:39:17 +03:00
|
|
|
value={option.id}
|
|
|
|
|
checked={defaultValue.includes(option.id)}
|
2021-06-26 22:38:48 +03:00
|
|
|
label={option.label || option.value}
|
2021-07-06 08:39:17 +03:00
|
|
|
disabled={nextButtonProps.disableOptionSelect}
|
2021-05-14 08:04:35 +03:00
|
|
|
/>
|
2021-07-03 19:29:41 +00:00
|
|
|
</button>
|
2021-05-14 08:04:35 +03:00
|
|
|
{/each}
|
|
|
|
|
</div>
|
2021-07-04 22:06:28 +03:00
|
|
|
{#if !isPreview}
|
|
|
|
|
<div class="mt-3 flex items-center justify-between w-full">
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
onClick={handlePrevious}
|
|
|
|
|
label="Previous"
|
|
|
|
|
isDisabled={disablePreviousButton}
|
|
|
|
|
variant={VARIANTS.OUTLINED}
|
|
|
|
|
/>
|
|
|
|
|
<PrimaryButton
|
2021-07-06 08:39:17 +03:00
|
|
|
variant={nextButtonProps.isActive
|
|
|
|
|
? VARIANTS.CONTAINED
|
|
|
|
|
: VARIANTS.OUTLINED}
|
2021-07-04 22:06:28 +03:00
|
|
|
type="submit"
|
2021-07-06 08:39:17 +03:00
|
|
|
label={nextButtonProps.label}
|
2021-07-04 22:06:28 +03:00
|
|
|
{name}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
2021-05-14 08:04:35 +03:00
|
|
|
</form>
|