|
1 | 1 | # Convert options object |
2 | 2 |
|
3 | | -Здесь будет описано трансформация options. |
| 3 | +For better compatibility with previous solutions, as well as ease of development, the options property can be |
| 4 | +two kinds: |
| 5 | + |
| 6 | +## Object |
| 7 | +This view is an object with **value** on the left and **label** on the right. For example |
| 8 | +country enumeration has been selected: |
| 9 | +```ts |
| 10 | +const options = { |
| 11 | + 'en': 'Great Britain', |
| 12 | + 'us': 'United States', |
| 13 | + 'jp': 'Japan', |
| 14 | + 'ru': 'Russia' |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +:::tip Why Value on left side? |
| 19 | +Primarily because the value must be unique. Because object is used for |
| 20 | +simple variants of this property was the most significant. |
| 21 | +::: |
| 22 | + |
| 23 | +The disadvantage of this approach is that the value can only be a string. **Important** to remember that |
| 24 | +even a numeric property will be converted to a string. |
| 25 | + |
| 26 | +## Array based |
| 27 | +This method is more flexible, because allows you to set any data to the value. In this case, *options* |
| 28 | +takes the following form: |
| 29 | +```ts |
| 30 | +const options = [ |
| 31 | + { value: 'en', label: 'Great Britain' }, |
| 32 | + { value: 'us', label: 'United States' }, |
| 33 | + { value: 'jp', label: 'Japan' }, |
| 34 | + { value: 'ru', label: 'Russia' }, |
| 35 | +] |
| 36 | +``` |
| 37 | +This method is more cumbersome than the first, but more flexible, because now we can use the following: |
| 38 | +```ts |
| 39 | +const options = [ |
| 40 | + { |
| 41 | + value: { code: 1, mark: 'en' }, label: 'Great Britain' |
| 42 | + } |
| 43 | +] |
| 44 | +``` |
| 45 | + |
| 46 | +## Converting |
| 47 | + |
| 48 | +When using **input-field** remember that you will always get options as an output as an array! |
| 49 | +This is easier to show with an example: |
| 50 | + |
| 51 | +```vue |
| 52 | +
|
| 53 | +<template> |
| 54 | + <input-field :options = "data" type = "custom-input"/> |
| 55 | +</template> |
| 56 | +<script setup> |
| 57 | +import {InputField} from "./index"; |
| 58 | +const data = { |
| 59 | + '1': 'M', |
| 60 | + '2': 'W' |
| 61 | +} |
| 62 | +</script> |
| 63 | +``` |
| 64 | +```vue |
| 65 | +<!--custom-input--> |
| 66 | +<div> |
| 67 | + {{options}} |
| 68 | +</div> |
| 69 | +<script setup> |
| 70 | +const props = defineProps({ |
| 71 | + options: Array |
| 72 | +}) |
| 73 | +
|
| 74 | +props.options; // [ { value: '1', label: 'M' }, { value: '2', label: 'W' } ] |
| 75 | +</script> |
| 76 | +``` |
0 commit comments