{"id":145,"date":"2023-01-17T11:28:00","date_gmt":"2023-01-17T11:28:00","guid":{"rendered":"https:\/\/mrzebra.co.uk\/code\/?p=145"},"modified":"2023-01-17T11:28:00","modified_gmt":"2023-01-17T11:28:00","slug":"how-to-make-a-compound-input-in-vue3","status":"publish","type":"post","link":"https:\/\/zebra-north.com\/code\/2023\/01\/17\/how-to-make-a-compound-input-in-vue3\/","title":{"rendered":"How to Make a Compound Input in Vue3"},"content":{"rendered":"\n<p>Form inputs often have separate fields that belong together, for example the &#8220;name&#8221; field may be decomposed into &#8220;first name&#8221; and &#8220;last name&#8221;.  This guide shows you how to bundle multiple fields into a single component.  The objective is to have the input&#8217;s value represented as an object:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-json\">{\n    &quot;firstName&quot;: &quot;First name value&quot;,\n    &quot;lastName&quot;: &quot;Last name value&quot;,\n}<\/code><\/pre>\n\n\n\n<!--more-->\n\n\n\n<p>First, create a single page component called &#8220;Name.vue&#8221; containing two inputs:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-markup\">&lt;template&gt;\n  &lt;div&gt;\n    &lt;input type=&quot;text&quot; v-model=&quot;value.firstName&quot; \/&gt;\n    &lt;input type=&quot;text&quot; v-model=&quot;value.lastName&quot; \/&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script setup&gt;\nconst props = defineProps({ modelValue: Object });\nconst value = props.modelValue;\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>The component is used as follows:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-markup\">&lt;template&gt;\n  &lt;div&gt;\n    &lt;name v-model=&quot;name&quot;&gt;&lt;\/name&gt;\n    &lt;br \/&gt;\n    {{ name }}\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script setup&gt;\nimport { ref } from &quot;vue&quot;;\nimport Name from &quot;@\/Name.vue&quot;;\n\nconst name = ref({ firstName: &quot;Test&quot;, lastName: &quot;Person&quot; });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Form inputs often have separate fields that belong together, for example the &#8220;name&#8221; field may be decomposed into &#8220;first name&#8221; and &#8220;last name&#8221;. This guide shows you how to bundle multiple fields into a single component. The objective is to have the input&#8217;s value represented as an object:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-vue3"],"_links":{"self":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":2,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/145\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}