Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
556 changes: 390 additions & 166 deletions README.md

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions examples/branched-and-merged-flow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
Question 1 splits the flow into two branches that merge back into question 4.
You can have as many branches as you want.

- **Branch A:** question 2 is a `RANGE_SLIDER`. Its next question is defined in `rangeOption.nextItemId`.
- **Branch B:** question 3 is an `INPUT`. Its next question is defined in `inputOption.nextItemId`.
- **Branch A:** question 2 is a `RANGE` with a default value.
- **Branch B:** question 3 is an `INPUT` with a default value.

Only the answers of the chosen branch are in the result.
When the user goes back and chooses the other branch, the answers of the old branch are removed.

```
┌────────────┐
Expand Down
27 changes: 27 additions & 0 deletions examples/conditional-branching/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Conditional branching

The next question depends on the answers, not only on the selected option:

```
How old are you?
├── under 18 ──► Do your parents agree?
│ ├── yes ──► What are you interested in?
│ └── no ──► end
└── 18 or older ──► What do you do?
├── employed, self-employed ──► What is your yearly income? ──► What are you interested in?
└── student ──────────────────────────────────────────────► What are you interested in?

What are you interested in?
├── contains "travel" ──► Where do you want to travel next?
└── other ──► end
```

This example shows:

- `next` with a list of conditions. The first match wins, an entry without `when` is the fallback.
- Comparisons like `{ age: { lt: 18 } }`.
- Multi-select conditions: `{ interests: 'travel' }` matches when the answer contains `'travel'`.
- A flow that ends early when no condition matches.
- A variant with a function condition, for rules that are not possible with data.

See [data.ts](./data.ts) for the data and [conditional-branching.spec.ts](./conditional-branching.spec.ts) for the behavior step by step.
24 changes: 12 additions & 12 deletions examples/custom-component-types/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Custom component types

This example extends `Quaire` with two new component types:
This example adds a `RATING` component type with a `stars` property.

- `MULTI_SELECT`: the answer is an array of option values
- `BOOLEAN`: the answer is `true` or `false`
[rating.ts](./rating.ts) defines the type and its behavior:

[MyQuaire.ts](./MyQuaire.ts) overrides these protected properties and methods:
- `RatingDefinition` describes the properties of a rating question.
- `MyQuestionDefinition` combines the built-in types with the rating type.
- The `rating` component validates the answer and shows "4 / 5" in the navigation.

| Override | Why |
| ------------------------------------------- | ------------------------------------------------------------------------------------------ |
| `_selectComponentTypes` | `MULTI_SELECT` uses `selectOptions` like `SINGLE_SELECT` |
| `_alwaysPossibleFollowUpQuestionComponents` | a `BOOLEAN` question that depends on former answers always stays in the flow |
| `_validateSelectComponent` | a `MULTI_SELECT` answer is valid if at least one value is a valid option |
| `_getNextItemIdFromSelectComponents` | find the next question for an array of values |
| `_getDependencyPath` | a `MULTI_SELECT` answer is sorted and joined with `_` to one key, e.g. `option 1_option 2` |
| `_getNavigationValue` | show the labels of all selected options in the navigation |
The component is passed to quaire with the `components` option:

```ts
new Quaire<object, MyQuestionDefinition>({ questions, components: { RATING: rating } });
```

The flow uses a condition on the rating: a rating of 2 or less leads to a `MULTI_SELECT` question about improvements.

See [data.ts](./data.ts) for the data and [custom-component-types.spec.ts](./custom-component-types.spec.ts) for the behavior step by step.
85 changes: 17 additions & 68 deletions examples/dependencies-between-questions/README.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,36 @@
# Dependencies between questions and validation
# Dependencies between questions

The options of a question can depend on the answers of former questions:

- Question 2 has different options, based on the answer to question 1.
- Question 3 has different options, based on the answers to question 1 and question 2.

The options are nested by result property and answer:
Each question has a list of `variants`. The first variant whose condition matches is applied:

```ts
{
resultProperty: 'baz',
dependsOnResultProperties: ['foo', 'bar'],
selectOptions: {
foo: {
'option 1': {
bar: {
'option 1.1': [{ label: 'Option 1.1.1', value: 'option 1.1.1' }],
// ...
},
},
},
},
id: 3,
type: 'SINGLE_SELECT',
key: 'baz',
title: 'Question 3',
options: [],
variants: [
{ when: { foo: 'option 1', bar: 'option 1.1' }, options: [...] },
{ when: { foo: 'option 1', bar: 'option 1.2' }, options: [...] },
// ...
],
}
```

When the user changes the answer to question 1, other variants apply to question 2 and 3.
quaire resets their answers to `null`, and they get the error `REQUIRED` until the user answers again.

```

When the user changes the answer to question 1, the answers to question 2 and 3 are no longer valid.
quaire resets the answer to question 2 to `null`, removes the answer to question 3,
and adds a `REQUIRED` validation error until the user answers again.

```
┌────────────┐
│ │
│ Question 1 │
│ │
└─────┬──────┘
┌──────────┐ │ ┌──────────┐
│ Option 1 ├─────┴──────┤ Option 2 │
└─────┬────┘ └─────┬────┘
┌────────────┐ │ │ ┌────────────┐
│ │ │ │ │ │
│ Question 2 │◄────┘ └───►│ Question 2 │
│ │ │ │
└──────┬─────┘ └──────┬─────┘
┌────────────┐ │ ┌────────────┐ ┌────────────┐ │ ┌────────────┐
│ Option 1.1 ├─────┴──────┤ Option 1.2 │ │ Option 2.1 ├─────┴──────┤ Option 2.2 │
└─────┬──────┘ └──────┬─────┘ └─────┬──────┘ └─────┬──────┘
┌────────────┐ │ │ │ │
│ │ │ │ │ │
│ Question 3 │◄────┘ │ │ │
│ │ │ │ │
└──────┬─────┘ │ │ │
┌──────────────┐ │ ┌──────────────┐ │ │ │
│ Option 1.1.1 ├─────┴──────┤ Option 1.1.2 │ │ │ │
└──────────────┘ └──────────────┘ │ │ │
┌────────────┐ │ │ │
│ │ │ │ │
│ Question 3 │◄────┘ │ │
│ │ │ │
└──────┬─────┘ │ │
│ │ │
┌──────────────┐ │ ┌──────────────┐ │ │
│ Option 1.2.1 ├─────┴──────┤ Option 1.2.2 │ │ │
└──────────────┘ └──────────────┘ │ │
┌────────────┐ │ │
│ │ │ │
│ Question 3 │◄────┘ │
│ │ │
└──────┬─────┘ │
│ │
│ │
┌──────────────┐ │ ┌──────────────┐ │
│ Option 2.1.1 ├─────┴──────┤ Option 2.1.2 │ │
└──────────────┘ └──────────────┘ │
┌────────────┐ │
│ │ │
│ Question 3 │◄────┘
│ │
└──────┬─────┘
│
│
┌──────────────┐ │ ┌──────────────┐
│ Option 2.2.1 ├─────┴──────┤ Option 2.2.2 │
└──────────────┘ └──────────────┘
```

See [data.ts](./data.ts) for the data and [dependencies-between-questions.spec.ts](./dependencies-between-questions.spec.ts) for the behavior step by step.
19 changes: 11 additions & 8 deletions examples/extending-data-definition/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Extending the data definition

This example adds custom properties to the data, for example to show a progress bar or a unit in your view.
This example adds custom properties to the data, for example to show a progress bar, a unit, or an icon in your view.

- `MyItem` adds `progress` to the questions and `divisor` and `unit` to `rangeOption`.
- `MyQuestion` and `MyNavigationItem` add the same properties to the output.
- `MyQuestionDefinition` adds `progress` and `unit` to every question.
- `MyNavigationDefinition` adds `color` to every navigation item.
- Select options can have custom properties without a new type, e.g. `icon`.

[MyQuaire.ts](./MyQuaire.ts) passes the types as type parameters (`Quaire<QuaireResult, MyItem, MyQuestion, MyNavigationItem>`)
and overrides two protected methods to copy the properties:
Pass the types as type parameters. quaire keeps all custom properties in the questions and navigation items:

- `_getQuestionObject`: adds `progress` to the active question
- `_getNavigationItemObject`: adds `progress` and `unit` to the navigation items
```ts
const quaire = new Quaire<object, MyQuestionDefinition, MyNavigationDefinition>({ questions, navigation });

Custom properties in `rangeOption`, `selectOptions`, and `inputOption` are passed to the question without any override.
quaire.getActiveQuestion().progress; // 50
quaire.getNavigation()[0].color; // 'blue'
quaire.getNavigation()[0].question.unit; // '%'
```

See [data.ts](./data.ts) for the data and [extending-data-definition.spec.ts](./extending-data-definition.spec.ts) for the behavior step by step.
8 changes: 5 additions & 3 deletions examples/linear-flow-skip-question/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Linear flow with an optional question

Question 2 is optional (`required: false`). This example shows both ways to skip it:
Question 2 is optional. This example shows two ways to skip it:

- **Branch around it:** Option 2 of question 1 leads directly to question 3.
- **Answer with `NO_VALUE`:** Question 2 has an option with the value `NO_VALUE`.
Question 2 is not on the path, so the navigation shows it as not `reachable`.
- **Answer with `NO_VALUE`:** Question 2 has a "Skip" option with the value `NO_VALUE`.
The question counts as answered, and the navigation shows `NO_VALUE` as its value.
Use this for a "skip" or "I don't know" button.

An optional question can also be skipped with `saveAnswer(null)`.

```
┌────────────┐
Expand Down
5 changes: 4 additions & 1 deletion examples/linear-flow-with-loop/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Linear flow with a loop

Option 2 of the last question leads back to the first question.
A `nextItemId` can point to any question, also to a former one.
A `next` can point to any question, also to a former one.

The answers stay in the result when the loop starts again, so the user sees their former answers.
A flow in a loop is not complete. Choose option 1 of the last question to complete it.

```
┌────────────┐
Expand Down
5 changes: 3 additions & 2 deletions examples/linear-flow/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Linear flow

A simple flow with three single-select questions. Every option leads to the next question.
A simple flow with three single-select questions. Every option leads to the next question with `next`.

After the last question, the active question stays the same, because its options have no `nextItemId`.
The options of the last question have no `next`, so the flow ends there and `isComplete()` is `true`.
An answer that is not an option gets the error `INVALID_OPTION`, and the question stays active.

```
┌────────────┐
Expand Down
5 changes: 3 additions & 2 deletions examples/navigation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ Contact How should we contact you?

This example shows:

- A category without its own question ("Personal"). Its `hasValue` and `isValid` come from its children.
Selecting it with `setActiveQuestionByNavigationItemId` jumps to its first subcategory.
- A category without its own question ("Personal"). Its state comes from its children.
`goToNavigationItem()` jumps to its first subcategory.
- A category with its own question and subcategories ("Contact").
- The navigation `value` of a select question is the label of the selected option ("E-Mail").
- `reachable` shows which items are on the path.
- Jumping back to a question and changing the answer keeps the other answers and continues with the next question.

See [data.ts](./data.ts) for the data and [navigation.spec.ts](./navigation.spec.ts) for the behavior step by step.
13 changes: 8 additions & 5 deletions examples/restore-state/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Restore state

Pass an existing result to restore a questionnaire, for example after a page reload
Pass an existing result to restore a flow, for example after a page reload
or when the user continues later:

```ts
const q = new Quaire({ items, navigationItems, result: { foo: 'option 1', bar: 'option 1.2' } });
const quaire = new Quaire({ questions, navigation, result: { foo: 'option 1', bar: 'option 1.2' } });
```

quaire replays the answers in the order of the flow. It continues with the first question that has no answer
and shows the options that match the former answers.
quaire follows the path of the answers and continues with the first open question.
This is the first question without an answer or with an invalid answer.
The questions before it are in the history, so `back()` works as expected.

The result that you pass in is not changed.

This example uses the data of [dependencies-between-questions](../dependencies-between-questions/data.ts).
See [restore-state.spec.ts](./restore-state.spec.ts) for a complete, a partial, and an empty result.
See [restore-state.spec.ts](./restore-state.spec.ts) for a complete, a partial, an invalid, and an empty result.
15 changes: 15 additions & 0 deletions examples/validation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Validation

This example shows the built-in validation rules and a custom validator:

| Question | Rules |
| -------- | ------------------------------------------------------- |
| E-mail | `required`, `pattern` |
| Username | `required`, `minLength`, `maxLength`, custom `validate` |
| Age | `min`, `max` (optional question) |
| Topics | `required`, `minSelected`, `maxSelected` |

An invalid answer is saved, but the question stays active and `question.error` contains the error code.
The view maps the codes to messages, see `messages` in [data.ts](./data.ts).

See [validation.spec.ts](./validation.spec.ts) for the behavior step by step.
14 changes: 14 additions & 0 deletions examples/wizard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Wizard: back, progress, and completion

A small checkout wizard. The gift message is only asked when the order is a gift.

This example shows:

- `getProgress()`: the number of answered questions and the number of questions on the path.
The total changes when the user takes another branch.
- `back()` and `canGoBack()` for a back button.
- `isComplete()` to enable a submit button.
- `reset()` to start again.
- `subscribe()` to update the view after every change.

See [data.ts](./data.ts) for the data and [wizard.spec.ts](./wizard.spec.ts) for the behavior step by step.
Loading