2021-05-14 08:04:35 +03:00
|
|
|
<script>
|
2021-07-17 22:29:12 +03:00
|
|
|
import CodeSnippet from '../../CodeSnippet/index.svelte';
|
|
|
|
|
import PrimaryButton from '../../PrimaryButton/index.svelte';
|
|
|
|
|
import { VARIANTS } from '../../PrimaryButton/constants';
|
|
|
|
|
import RadioItem from '../../Form/RadioItem.svelte';
|
2021-05-14 08:04:35 +03:00
|
|
|
|
2021-07-17 22:29:12 +03:00
|
|
|
export let title = '';
|
2021-07-03 19:29:41 +00:00
|
|
|
export let code;
|
2021-07-17 22:29:12 +03:00
|
|
|
export let name = '';
|
2021-05-14 08:04:35 +03:00
|
|
|
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-07-17 22:29:12 +03:00
|
|
|
export let defaultValue = '';
|
2021-05-14 08:04:35 +03:00
|
|
|
export let disablePreviousButton = false;
|
2021-07-10 15:47:43 +00:00
|
|
|
export let disabled = 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
|
|
|
|
|
|
|
|
function getRadioVal(form, name) {
|
|
|
|
|
let val;
|
|
|
|
|
const radios = form.elements[name];
|
|
|
|
|
|
|
|
|
|
for (let i = 0, len = radios.length; i < len; i++) {
|
|
|
|
|
if (radios[i].checked) {
|
|
|
|
|
val = radios[i].value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleFormSubmit(event) {
|
2021-07-04 22:06:28 +03:00
|
|
|
if (isPreview) return;
|
2021-05-14 08:04:35 +03:00
|
|
|
const value = getRadioVal(event.target, name);
|
2021-07-06 08:39:17 +03:00
|
|
|
onSubmit(name, [value]);
|
|
|
|
|
// 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)) {
|
2021-07-17 22:29:12 +03:00
|
|
|
return 'border-green-700';
|
2021-07-06 08:39:17 +03:00
|
|
|
} else {
|
2021-07-17 22:29:12 +03:00
|
|
|
return 'border-red-700';
|
2021-07-06 08:39:17 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-17 22:29:12 +03:00
|
|
|
return '';
|
2021-07-06 08:39:17 +03:00
|
|
|
}
|
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}
|
2021-07-17 22:29:12 +03:00
|
|
|
<CodeSnippet {code} />
|
2021-07-03 19:29:41 +00:00
|
|
|
{/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-05-22 12:32:32 +03:00
|
|
|
<RadioItem
|
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) &&
|
|
|
|
|
answers.includes(option.id)}
|
2021-07-03 19:29:41 +00:00
|
|
|
label={option.label || option.value}
|
2021-07-10 15:47:43 +00:00
|
|
|
{disabled}
|
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>
|