[{"data":1,"prerenderedAt":1923},["ShallowReactive",2],{"article-20220714-vue3-v-model-binding":3},{"article":4,"surround":1912},{"id":5,"title":6,"authorKey":7,"body":8,"codeLinks":1874,"createdAt":1875,"description":1876,"excerpt":1877,"extension":1900,"image":1901,"meta":1902,"navigation":318,"path":1903,"published":1904,"readingTime":355,"revisedAt":1874,"seo":1905,"stem":1906,"tags":1907,"__hash__":1911},"articles\u002Farticles\u002F20220714-vue3-v-model-binding.md","Vue.js 3 v-model two-way data binding for Dummies","philip",{"type":9,"value":10,"toc":1864},"minimark",[11,24,30,50,57,68,176,181,203,206,219,222,236,247,254,269,419,425,531,542,568,575,596,647,679,692,752,758,1082,1087,1125,1224,1229,1250,1259,1266,1273,1287,1294,1415,1439,1461,1852,1857,1860],[12,13,14,15,19,20,23],"p",{},"Vue ",[16,17,18],"code",{},"v-model"," is a directive that creates a two-way data binding between a value in our template and a value in our data properties, while ",[16,21,22],{},"v-bind"," only binds data one way. A common use case for using v-model is when designing forms and inputs.",[12,25,26,27,29],{},"While the core concept of ",[16,28,18],{}," is quite straightforward, you might struggle with it when building advanced custom form components. Personally, this was my main pain point when learning Vue, especially since there are so many different ways this can be handled and tutorials diverge quite a bit.",[12,31,32,33,42,43,45,46],{},"Vue's official ",[34,35,39,40],"a",{"href":36,"rel":37},"https:\u002F\u002Fvuejs.org\u002Fguide\u002Fcomponents\u002Fevents.html#usage-with-v-model",[38],"nofollow","Components In-Depth > Events > Usage with ",[16,41,18],{}," documentation only covers the most basic usage without providing any advanced examples. So I put together some use cases for you to learn more about ",[16,44,18],{},". The main question is: ",[47,48,49],"strong",{},"How can I make that custom component reusable so that it can be used as a form component with two-way data binding?",[51,52,54,56],"h2",{"id":53},"v-model-directive-explained",[16,55,18],{}," directive explained",[12,58,59,60,67],{},"Vue's ",[34,61,64,66],{"href":62,"rel":63},"https:\u002F\u002Fvuejs.org\u002Fapi\u002Fbuilt-in-directives.html#v-model",[38],[16,65,18],{}," directive"," does the following under the hood:",[69,70,75],"pre",{"className":71,"code":72,"language":73,"meta":74,"style":74},"language-vue shiki shiki-themes github-light-default github-light-default github-dark-default","\u003CMyChild v-model=\"someRef\" \u002F>\n\u003C!-- same as -->\n\u003CMyChild\n    :modelValue=\"someRef\"\n    @update:modelValue=\"someRef = $event\"\n\u002F>\n","vue","",[16,76,77,108,115,123,141,170],{"__ignoreMap":74},[78,79,82,86,90,94,97,100,103,105],"span",{"class":80,"line":81},"line",1,[78,83,85],{"class":84},"s_bOF","\u003C",[78,87,89],{"class":88},"sfEsd","MyChild",[78,91,93],{"class":92},"sDkKp"," v-model",[78,95,96],{"class":84},"=",[78,98,99],{"class":84},"\"",[78,101,102],{"class":84},"someRef",[78,104,99],{"class":84},[78,106,107],{"class":84}," \u002F>\n",[78,109,111],{"class":80,"line":110},2,[78,112,114],{"class":113},"s9BHP","\u003C!-- same as -->\n",[78,116,118,120],{"class":80,"line":117},3,[78,119,85],{"class":84},[78,121,122],{"class":88},"MyChild\n",[78,124,126,129,132,134,136,138],{"class":80,"line":125},4,[78,127,128],{"class":84},"    :",[78,130,131],{"class":92},"modelValue",[78,133,96],{"class":84},[78,135,99],{"class":84},[78,137,102],{"class":84},[78,139,140],{"class":84},"\"\n",[78,142,144,147,150,153,155,157,159,162,165,168],{"class":80,"line":143},5,[78,145,146],{"class":84},"    @",[78,148,149],{"class":92},"update",[78,151,152],{"class":84},":",[78,154,131],{"class":92},[78,156,96],{"class":84},[78,158,99],{"class":84},[78,160,161],{"class":84},"someRef ",[78,163,96],{"class":164},"s5nWI",[78,166,167],{"class":84}," $event",[78,169,140],{"class":84},[78,171,173],{"class":80,"line":172},6,[78,174,175],{"class":84},"\u002F>\n",[12,177,178,179,152],{},"By default, the following applies to ",[16,180,18],{},[182,183,184,194],"ul",{},[185,186,187,188,191,192],"li",{},"default ",[47,189,190],{},"prop"," name is ",[16,193,131],{},[185,195,187,196,199,200],{},[47,197,198],{},"event"," is ",[16,201,202],{},"update:modelValue",[12,204,205],{},"This prop and event name will then be available in your child component.",[207,208,209],"blockquote",{},[12,210,211,212,215,216,218],{},"The default prop and event name could be changed by appending the name with a colon: ",[16,213,214],{},"v-model:name",", but let's stick with the default! Changing the name would only be needed if you would like to assign multiple independent ",[16,217,18],{}," directives on the same component tag, which is very rarely used.",[12,220,221],{},"So, in every custom component which should support two-way data binding you need to implement the following:",[182,223,224,230],{},[185,225,226,227,229],{},"declare ",[16,228,131],{}," as a prop",[185,231,232,233,235],{},"emit ",[16,234,202],{}," event when the input changes",[12,237,238,239,246],{},"Let's dive into a selection of 3 use cases which all use the recommended Vue 3 Composition API ",[34,240,243],{"href":241,"rel":242},"https:\u002F\u002Fvuejs.org\u002Fapi\u002Fsfc-script-setup.html",[38],[16,244,245],{},"\u003Cscript setup>"," syntax.",[51,248,250,253],{"id":249},"custominput-form-component",[16,251,252],{},"CustomInput"," form component",[12,255,256,257,259,260,268],{},"In this example, we are building a ",[16,258,252],{}," form component that wraps the native ",[34,261,264,267],{"href":262,"rel":263},"https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FHTML\u002FElement\u002FInput",[38],[16,265,266],{},"\u003Cinput>"," HTML element",". It could then be used like this from our parent component:",[69,270,272],{"className":71,"code":271,"language":73,"meta":74,"style":74},"\u003Cscript setup>\nimport { ref } from 'vue'\nimport CustomInput from '.\u002FCustomInput.vue'\n\nconst fullname = ref('')\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n    \u003Cform>\n        \u003CCustomInput v-model=\"fullname\" \u002F>\n    \u003C\u002Fform>\n\u003C\u002Ftemplate>\n",[16,273,274,287,302,314,320,344,353,358,368,379,400,410],{"__ignoreMap":74},[78,275,276,278,281,284],{"class":80,"line":81},[78,277,85],{"class":84},[78,279,280],{"class":88},"script",[78,282,283],{"class":92}," setup",[78,285,286],{"class":84},">\n",[78,288,289,292,295,298],{"class":80,"line":110},[78,290,291],{"class":164},"import",[78,293,294],{"class":84}," { ref } ",[78,296,297],{"class":164},"from",[78,299,301],{"class":300},"s-BGO"," 'vue'\n",[78,303,304,306,309,311],{"class":80,"line":117},[78,305,291],{"class":164},[78,307,308],{"class":84}," CustomInput ",[78,310,297],{"class":164},[78,312,313],{"class":300}," '.\u002FCustomInput.vue'\n",[78,315,316],{"class":80,"line":125},[78,317,319],{"emptyLinePlaceholder":318},true,"\n",[78,321,322,325,328,331,335,338,341],{"class":80,"line":143},[78,323,324],{"class":164},"const",[78,326,327],{"class":92}," fullname",[78,329,330],{"class":164}," =",[78,332,334],{"class":333},"sd69k"," ref",[78,336,337],{"class":84},"(",[78,339,340],{"class":300},"''",[78,342,343],{"class":84},")\n",[78,345,346,349,351],{"class":80,"line":172},[78,347,348],{"class":84},"\u003C\u002F",[78,350,280],{"class":88},[78,352,286],{"class":84},[78,354,356],{"class":80,"line":355},7,[78,357,319],{"emptyLinePlaceholder":318},[78,359,361,363,366],{"class":80,"line":360},8,[78,362,85],{"class":84},[78,364,365],{"class":88},"template",[78,367,286],{"class":84},[78,369,371,374,377],{"class":80,"line":370},9,[78,372,373],{"class":84},"    \u003C",[78,375,376],{"class":88},"form",[78,378,286],{"class":84},[78,380,382,385,387,389,391,393,396,398],{"class":80,"line":381},10,[78,383,384],{"class":84},"        \u003C",[78,386,252],{"class":88},[78,388,93],{"class":92},[78,390,96],{"class":84},[78,392,99],{"class":84},[78,394,395],{"class":84},"fullname",[78,397,99],{"class":84},[78,399,107],{"class":84},[78,401,403,406,408],{"class":80,"line":402},11,[78,404,405],{"class":84},"    \u003C\u002F",[78,407,376],{"class":88},[78,409,286],{"class":84},[78,411,413,415,417],{"class":80,"line":412},12,[78,414,348],{"class":84},[78,416,365],{"class":88},[78,418,286],{"class":84},[12,420,421,422,424],{},"Our child component (basic ",[16,423,252],{}," form component) looks like this:",[69,426,428],{"className":71,"code":427,"language":73,"meta":74,"style":74},"\u003C!-- CustomInput.vue -->\n\u003Cscript setup>\ndefineProps(['modelValue'])\ndefineEmits(['update:modelValue'])\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n    \u003Cinput\n        :value=\"modelValue\"\n        @input=\"$emit('update:modelValue', $event.target.value)\"\n    \u002F>\n\u003C\u002Ftemplate>\n",[16,429,430,435,445,459,471,479,483,491,498,508,518,523],{"__ignoreMap":74},[78,431,432],{"class":80,"line":81},[78,433,434],{"class":113},"\u003C!-- CustomInput.vue -->\n",[78,436,437,439,441,443],{"class":80,"line":110},[78,438,85],{"class":84},[78,440,280],{"class":88},[78,442,283],{"class":92},[78,444,286],{"class":84},[78,446,447,450,453,456],{"class":80,"line":117},[78,448,449],{"class":333},"defineProps",[78,451,452],{"class":84},"([",[78,454,455],{"class":300},"'modelValue'",[78,457,458],{"class":84},"])\n",[78,460,461,464,466,469],{"class":80,"line":125},[78,462,463],{"class":333},"defineEmits",[78,465,452],{"class":84},[78,467,468],{"class":300},"'update:modelValue'",[78,470,458],{"class":84},[78,472,473,475,477],{"class":80,"line":143},[78,474,348],{"class":84},[78,476,280],{"class":88},[78,478,286],{"class":84},[78,480,481],{"class":80,"line":172},[78,482,319],{"emptyLinePlaceholder":318},[78,484,485,487,489],{"class":80,"line":355},[78,486,85],{"class":84},[78,488,365],{"class":88},[78,490,286],{"class":84},[78,492,493,495],{"class":80,"line":360},[78,494,373],{"class":84},[78,496,497],{"class":88},"input\n",[78,499,500,503,505],{"class":80,"line":370},[78,501,502],{"class":92},"        :value",[78,504,96],{"class":84},[78,506,507],{"class":300},"\"modelValue\"\n",[78,509,510,513,515],{"class":80,"line":381},[78,511,512],{"class":92},"        @input",[78,514,96],{"class":84},[78,516,517],{"class":300},"\"$emit('update:modelValue', $event.target.value)\"\n",[78,519,520],{"class":80,"line":402},[78,521,522],{"class":84},"    \u002F>\n",[78,524,525,527,529],{"class":80,"line":412},[78,526,348],{"class":84},[78,528,365],{"class":88},[78,530,286],{"class":84},[12,532,533,534,537,538,541],{},"In this example, the data we are binding to our event is ",[16,535,536],{},"$event.target.value"," which is the actual ",[16,539,540],{},"String"," value of this input field.",[12,543,544,547,548,553,554,557,558,560,561,564,565,567],{},[47,545,546],{},"Remember:"," Native HTML elements always fire ",[34,549,552],{"href":550,"rel":551},"https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FLearn\u002FJavaScript\u002FBuilding_blocks\u002FEvents",[38],"Events",", so you'll never get a primitive (like e.g. a ",[16,555,556],{},"Boolean"," or a ",[16,559,540],{},") as ",[16,562,563],{},"$event",". That's why you need to retrieve the effective input element's value with ",[16,566,536],{}," in this case.",[51,569,571,574],{"id":570},"customswitch-toggle-form-component",[16,572,573],{},"CustomSwitch"," (Toggle) form component",[12,576,577,578,581,582,591,592,595],{},"Let's create a slightly more sophisticated custom ",[47,579,580],{},"Switch (Toggle)"," form component, using ",[34,583,586,587,590],{"href":584,"rel":585},"https:\u002F\u002Fheadlessui.com\u002Fvue\u002Fswitch",[38],"Headless UI ",[16,588,589],{},"Switch"," component",". Please note that the official Headless UI documentation provides us with a basic example where they use an ",[16,593,594],{},"enabled"," ref to store the current state:",[69,597,599],{"className":71,"code":598,"language":73,"meta":74,"style":74},"\u003Cscript setup>\nimport { ref } from 'vue'\nconst enabled = ref(false)\n\u003C\u002Fscript>\n",[16,600,601,611,621,639],{"__ignoreMap":74},[78,602,603,605,607,609],{"class":80,"line":81},[78,604,85],{"class":84},[78,606,280],{"class":88},[78,608,283],{"class":92},[78,610,286],{"class":84},[78,612,613,615,617,619],{"class":80,"line":110},[78,614,291],{"class":164},[78,616,294],{"class":84},[78,618,297],{"class":164},[78,620,301],{"class":300},[78,622,623,625,628,630,632,634,637],{"class":80,"line":117},[78,624,324],{"class":164},[78,626,627],{"class":92}," enabled",[78,629,330],{"class":164},[78,631,334],{"class":333},[78,633,337],{"class":84},[78,635,636],{"class":92},"false",[78,638,343],{"class":84},[78,640,641,643,645],{"class":80,"line":125},[78,642,348],{"class":84},[78,644,280],{"class":88},[78,646,286],{"class":84},[12,648,649,650,656,657,659,660,662,663,665,666,668,669,671,672,674,675,678],{},"This extra ref is not needed inside our custom component, as the ",[34,651,654],{"href":652,"rel":653},"https:\u002F\u002Fheadlessui.com\u002Fvue\u002Fswitch#switch",[38],[16,655,589],{}," component itself fires an event with a primitive ",[16,658,556],{}," that represents the current on\u002Foff state (see ",[16,661,589],{}," Component API documentation, where ",[16,664,18],{}," supports a ",[16,667,556],{},"). So, ",[16,670,563],{}," is simply the ",[16,673,556],{}," and we can pass it directly to our parent component with ",[16,676,677],{},"@update:modelValue=\"$emit('update:modelValue', $event)\"",".",[12,680,681,682,684,685,691],{},"Side-note: In the example below, we are going to use the newer syntax without depending on the \"magic\" ",[16,683,563],{}," variable, so this is exactly the same in Vue 3 (see ",[34,686,688,689],{"href":36,"rel":687},[38],"Component Events > Usage with ",[16,690,18],{},"):",[69,693,695],{"className":71,"code":694,"language":73,"meta":74,"style":74},"\u003CSwitch\n    @update:modelValue=\"$emit('update:modelValue', $event)\"\n\u002F>\n\u003C!-- same as -->\n\u003CSwitch\n    @update:modelValue=\"(newValue) => $emit('update:modelValue', newValue)\"\n\u002F>\n",[16,696,697,704,730,734,738,743,748],{"__ignoreMap":74},[78,698,699,701],{"class":80,"line":81},[78,700,85],{"class":84},[78,702,703],{"class":88},"Switch\n",[78,705,706,708,710,712,714,716,718,721,723,725,728],{"class":80,"line":110},[78,707,146],{"class":84},[78,709,149],{"class":92},[78,711,152],{"class":84},[78,713,131],{"class":92},[78,715,96],{"class":84},[78,717,99],{"class":84},[78,719,720],{"class":333},"$emit",[78,722,337],{"class":84},[78,724,468],{"class":300},[78,726,727],{"class":84},", $event)",[78,729,140],{"class":84},[78,731,732],{"class":80,"line":117},[78,733,175],{"class":84},[78,735,736],{"class":80,"line":125},[78,737,114],{"class":84},[78,739,740],{"class":80,"line":143},[78,741,742],{"class":84},"\u003CSwitch\n",[78,744,745],{"class":80,"line":172},[78,746,747],{"class":84},"    @update:modelValue=\"(newValue) => $emit('update:modelValue', newValue)\"\n",[78,749,750],{"class":80,"line":355},[78,751,175],{"class":84},[12,753,754,755,757],{},"So our fully working (and minimally styled) ",[16,756,573],{}," component looks like this:",[69,759,761],{"className":71,"code":760,"language":73,"meta":74,"style":74},"\u003C!-- CustomSwitch.vue -->\n\u003Cscript setup>\nimport { Switch } from '@headlessui\u002Fvue'\n\ndefineProps({\n    modelValue: {\n        type: Boolean,\n        default: false,\n    },\n    label: {\n        type: String,\n        default: '',\n    },\n})\n\ndefineEmits(['update:modelValue'])\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n    \u003CSwitch\n        :class=\"modelValue ? 'bg-primary-600' : 'bg-gray-300'\"\n        class=\"relative inline-flex h-6 w-11 items-center rounded-full\"\n        :modelValue=\"modelValue\"\n        @update:modelValue=\"(newValue) => $emit('update:modelValue', newValue)\"\n    >\n        \u003Cspan class=\"sr-only\">{{ label }}\u003C\u002Fspan>\n        \u003Cspan\n            :class=\"modelValue ? 'translate-x-5' : 'translate-x-0'\"\n            class=\"inline-block h-5 w-5 transform rounded-full bg-white\"\n        \u002F>\n    \u003C\u002FSwitch>\n\u003C\u002Ftemplate>\n",[16,762,763,768,778,790,794,801,806,811,821,826,831,836,844,849,855,860,871,880,885,894,901,931,942,957,997,1003,1025,1033,1044,1055,1064,1073],{"__ignoreMap":74},[78,764,765],{"class":80,"line":81},[78,766,767],{"class":113},"\u003C!-- CustomSwitch.vue -->\n",[78,769,770,772,774,776],{"class":80,"line":110},[78,771,85],{"class":84},[78,773,280],{"class":88},[78,775,283],{"class":92},[78,777,286],{"class":84},[78,779,780,782,785,787],{"class":80,"line":117},[78,781,291],{"class":164},[78,783,784],{"class":84}," { Switch } ",[78,786,297],{"class":164},[78,788,789],{"class":300}," '@headlessui\u002Fvue'\n",[78,791,792],{"class":80,"line":125},[78,793,319],{"emptyLinePlaceholder":318},[78,795,796,798],{"class":80,"line":143},[78,797,449],{"class":333},[78,799,800],{"class":84},"({\n",[78,802,803],{"class":80,"line":172},[78,804,805],{"class":84},"    modelValue: {\n",[78,807,808],{"class":80,"line":355},[78,809,810],{"class":84},"        type: Boolean,\n",[78,812,813,816,818],{"class":80,"line":360},[78,814,815],{"class":84},"        default: ",[78,817,636],{"class":92},[78,819,820],{"class":84},",\n",[78,822,823],{"class":80,"line":370},[78,824,825],{"class":84},"    },\n",[78,827,828],{"class":80,"line":381},[78,829,830],{"class":84},"    label: {\n",[78,832,833],{"class":80,"line":402},[78,834,835],{"class":84},"        type: String,\n",[78,837,838,840,842],{"class":80,"line":412},[78,839,815],{"class":84},[78,841,340],{"class":300},[78,843,820],{"class":84},[78,845,847],{"class":80,"line":846},13,[78,848,825],{"class":84},[78,850,852],{"class":80,"line":851},14,[78,853,854],{"class":84},"})\n",[78,856,858],{"class":80,"line":857},15,[78,859,319],{"emptyLinePlaceholder":318},[78,861,863,865,867,869],{"class":80,"line":862},16,[78,864,463],{"class":333},[78,866,452],{"class":84},[78,868,468],{"class":300},[78,870,458],{"class":84},[78,872,874,876,878],{"class":80,"line":873},17,[78,875,348],{"class":84},[78,877,280],{"class":88},[78,879,286],{"class":84},[78,881,883],{"class":80,"line":882},18,[78,884,319],{"emptyLinePlaceholder":318},[78,886,888,890,892],{"class":80,"line":887},19,[78,889,85],{"class":84},[78,891,365],{"class":88},[78,893,286],{"class":84},[78,895,897,899],{"class":80,"line":896},20,[78,898,373],{"class":84},[78,900,703],{"class":88},[78,902,904,907,910,912,914,917,920,923,926,929],{"class":80,"line":903},21,[78,905,906],{"class":84},"        :",[78,908,909],{"class":92},"class",[78,911,96],{"class":84},[78,913,99],{"class":84},[78,915,916],{"class":84},"modelValue ",[78,918,919],{"class":164},"?",[78,921,922],{"class":300}," 'bg-primary-600'",[78,924,925],{"class":164}," :",[78,927,928],{"class":300}," 'bg-gray-300'",[78,930,140],{"class":84},[78,932,934,937,939],{"class":80,"line":933},22,[78,935,936],{"class":92},"        class",[78,938,96],{"class":84},[78,940,941],{"class":300},"\"relative inline-flex h-6 w-11 items-center rounded-full\"\n",[78,943,945,947,949,951,953,955],{"class":80,"line":944},23,[78,946,906],{"class":84},[78,948,131],{"class":92},[78,950,96],{"class":84},[78,952,99],{"class":84},[78,954,131],{"class":84},[78,956,140],{"class":84},[78,958,960,963,965,967,969,971,973,975,979,982,985,988,990,992,995],{"class":80,"line":959},24,[78,961,962],{"class":84},"        @",[78,964,149],{"class":92},[78,966,152],{"class":84},[78,968,131],{"class":92},[78,970,96],{"class":84},[78,972,99],{"class":84},[78,974,337],{"class":84},[78,976,978],{"class":977},"sQJ0q","newValue",[78,980,981],{"class":84},") ",[78,983,984],{"class":164},"=>",[78,986,987],{"class":333}," $emit",[78,989,337],{"class":84},[78,991,468],{"class":300},[78,993,994],{"class":84},", newValue)",[78,996,140],{"class":84},[78,998,1000],{"class":80,"line":999},25,[78,1001,1002],{"class":84},"    >\n",[78,1004,1006,1008,1010,1013,1015,1018,1021,1023],{"class":80,"line":1005},26,[78,1007,384],{"class":84},[78,1009,78],{"class":88},[78,1011,1012],{"class":92}," class",[78,1014,96],{"class":84},[78,1016,1017],{"class":300},"\"sr-only\"",[78,1019,1020],{"class":84},">{{ label }}\u003C\u002F",[78,1022,78],{"class":88},[78,1024,286],{"class":84},[78,1026,1028,1030],{"class":80,"line":1027},27,[78,1029,384],{"class":84},[78,1031,1032],{"class":88},"span\n",[78,1034,1036,1039,1041],{"class":80,"line":1035},28,[78,1037,1038],{"class":92},"            :class",[78,1040,96],{"class":84},[78,1042,1043],{"class":300},"\"modelValue ? 'translate-x-5' : 'translate-x-0'\"\n",[78,1045,1047,1050,1052],{"class":80,"line":1046},29,[78,1048,1049],{"class":92},"            class",[78,1051,96],{"class":84},[78,1053,1054],{"class":300},"\"inline-block h-5 w-5 transform rounded-full bg-white\"\n",[78,1056,1058,1062],{"class":80,"line":1057},30,[78,1059,1061],{"class":1060},"s2sOu","        \u002F",[78,1063,286],{"class":84},[78,1065,1067,1069,1071],{"class":80,"line":1066},31,[78,1068,405],{"class":84},[78,1070,589],{"class":88},[78,1072,286],{"class":84},[78,1074,1076,1078,1080],{"class":80,"line":1075},32,[78,1077,348],{"class":84},[78,1079,365],{"class":88},[78,1081,286],{"class":84},[12,1083,1084],{},[47,1085,1086],{},"Slot props:",[12,1088,1089,1090,1092,1093,1099,1100,1103,1104,1107,1108,1110,1111,1113,1114,1116,1117,1119,1120,1122,1123,152],{},"Please note, in this example, we use the ",[16,1091,131],{}," prop directly for the current state of the Switch\u002FToggle. The ",[34,1094,1096,1097,590],{"href":584,"rel":1095},[38],"Headless UI  ",[16,1098,589],{}," would offer a ",[16,1101,1102],{},"checked"," slot prop for that as well, but this would ",[47,1105,1106],{},"only"," be available inside the ",[16,1109,589],{}," component tag and not in e.g. any direct property of the ",[16,1112,589],{}," tag itself (e.g. ",[16,1115,909],{},", where we would still depend on either ",[16,1118,131],{}," or some other ref that represents the current state). The following could be done, but just makes our example more complex, as we would use ",[16,1121,1102],{}," slot prop in addition to the actual ",[16,1124,131],{},[69,1126,1128],{"className":71,"code":1127,"language":73,"meta":74,"style":74},"\u003Ctemplate>\n    \u003CSwitch\n        v-slot=\"{ checked }\"\n        :class=\"modelValue ? 'bg-primary-600' : 'bg-gray-300'\"\n        ...\n    >\n        \u003Cspan :class=\"checked ? 'translate-x-5' : 'translate-x-0'\" \u002F>\n    \u003C\u002FSwitch>\n\u003C\u002Ftemplate>\n",[16,1129,1130,1138,1144,1158,1180,1185,1189,1208,1216],{"__ignoreMap":74},[78,1131,1132,1134,1136],{"class":80,"line":81},[78,1133,85],{"class":84},[78,1135,365],{"class":88},[78,1137,286],{"class":84},[78,1139,1140,1142],{"class":80,"line":110},[78,1141,373],{"class":84},[78,1143,703],{"class":88},[78,1145,1146,1149,1151,1153,1156],{"class":80,"line":117},[78,1147,1148],{"class":92},"        v-slot",[78,1150,96],{"class":84},[78,1152,99],{"class":84},[78,1154,1155],{"class":84},"{ checked }",[78,1157,140],{"class":84},[78,1159,1160,1162,1164,1166,1168,1170,1172,1174,1176,1178],{"class":80,"line":125},[78,1161,906],{"class":84},[78,1163,909],{"class":92},[78,1165,96],{"class":84},[78,1167,99],{"class":84},[78,1169,916],{"class":84},[78,1171,919],{"class":164},[78,1173,922],{"class":300},[78,1175,925],{"class":164},[78,1177,928],{"class":300},[78,1179,140],{"class":84},[78,1181,1182],{"class":80,"line":143},[78,1183,1184],{"class":84},"        ...\n",[78,1186,1187],{"class":80,"line":172},[78,1188,1002],{"class":84},[78,1190,1191,1193,1195,1198,1200,1203,1206],{"class":80,"line":355},[78,1192,384],{"class":84},[78,1194,78],{"class":88},[78,1196,1197],{"class":92}," :class",[78,1199,96],{"class":84},[78,1201,1202],{"class":300},"\"checked ? 'translate-x-5' : 'translate-x-0'\"",[78,1204,1205],{"class":1060}," \u002F",[78,1207,286],{"class":84},[78,1209,1210,1212,1214],{"class":80,"line":360},[78,1211,405],{"class":84},[78,1213,589],{"class":88},[78,1215,286],{"class":84},[78,1217,1218,1220,1222],{"class":80,"line":370},[78,1219,348],{"class":84},[78,1221,365],{"class":88},[78,1223,286],{"class":84},[12,1225,1226],{},[47,1227,1228],{},"Immutable props:",[12,1230,1231,1232,1234,1235,1237,1238,1241,1242,1244,1245],{},"And why can't we directly bind the ",[16,1233,131],{}," to the ",[16,1236,589],{}," component using ",[16,1239,1240],{},"v-model=\"modelValue\""," inside our ",[16,1243,573],{}," component? Read this: ",[34,1246,1249],{"href":1247,"rel":1248},"https:\u002F\u002Fvuejs.org\u002Fguide\u002Fcomponents\u002Fprops.html#one-way-data-flow",[38],"Props: One-Way Data Flow",[207,1251,1252],{},[12,1253,1254,1255,1258],{},"All props form a one-way-down binding between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. This prevents child components from accidentally mutating the parent's  state, which can make your app's data flow harder to understand.\n(...)\nThis means you should ",[47,1256,1257],{},"not"," attempt to mutate a prop inside a child component. If you do, Vue will warn you in the console",[12,1260,1261,1262,1265],{},"So, ",[47,1263,1264],{},"never try to mutate a property"," in your custom component! Always work around it by only reading the prop value and firing events to pass the changed value back to the parent component, as we did in above example.",[51,1267,1269,1272],{"id":1268},"customlistbox-select-for-component",[16,1270,1271],{},"CustomListbox"," (Select) for component",[12,1274,1275,1276,1278,1279,1286],{},"Let's dive into another slightly more complex example of ",[16,1277,18],{}," two-way data binding, using ",[34,1280,586,1283,590],{"href":1281,"rel":1282},"https:\u002F\u002Fheadlessui.com\u002Fvue\u002Flistbox",[38],[16,1284,1285],{},"Listbox",". We can use this component to build custom, accessible select menus.",[12,1288,1289,1290,1293],{},"Options are passed in an object notation as ",[16,1291,1292],{},"options"," prop to our custom component:",[69,1295,1297],{"className":71,"code":1296,"language":73,"meta":74,"style":74},"\u003Ctemplate>\n    \u003CListboxSelect\n        v-model=\"form.person\"\n        label=\"Person\"\n        :options=\"[\n            { id: 1, name: 'Danny MacAskill' },\n            { id: 2, name: 'Stefano Meloni' },\n            { id: 3, name: 'Toni Peperoni' },\n        ]\"\n    \u002F>\n\u003C\u002Ftemplate>\n",[16,1298,1299,1307,1314,1328,1338,1351,1368,1382,1396,1403,1407],{"__ignoreMap":74},[78,1300,1301,1303,1305],{"class":80,"line":81},[78,1302,85],{"class":84},[78,1304,365],{"class":88},[78,1306,286],{"class":84},[78,1308,1309,1311],{"class":80,"line":110},[78,1310,373],{"class":84},[78,1312,1313],{"class":88},"ListboxSelect\n",[78,1315,1316,1319,1321,1323,1326],{"class":80,"line":117},[78,1317,1318],{"class":92},"        v-model",[78,1320,96],{"class":84},[78,1322,99],{"class":84},[78,1324,1325],{"class":84},"form.person",[78,1327,140],{"class":84},[78,1329,1330,1333,1335],{"class":80,"line":125},[78,1331,1332],{"class":92},"        label",[78,1334,96],{"class":84},[78,1336,1337],{"class":300},"\"Person\"\n",[78,1339,1340,1342,1344,1346,1348],{"class":80,"line":143},[78,1341,906],{"class":84},[78,1343,1292],{"class":92},[78,1345,96],{"class":84},[78,1347,99],{"class":84},[78,1349,1350],{"class":84},"[\n",[78,1352,1353,1356,1359,1362,1365],{"class":80,"line":172},[78,1354,1355],{"class":84},"            { id: ",[78,1357,1358],{"class":92},"1",[78,1360,1361],{"class":84},", name: ",[78,1363,1364],{"class":300},"'Danny MacAskill'",[78,1366,1367],{"class":84}," },\n",[78,1369,1370,1372,1375,1377,1380],{"class":80,"line":355},[78,1371,1355],{"class":84},[78,1373,1374],{"class":92},"2",[78,1376,1361],{"class":84},[78,1378,1379],{"class":300},"'Stefano Meloni'",[78,1381,1367],{"class":84},[78,1383,1384,1386,1389,1391,1394],{"class":80,"line":360},[78,1385,1355],{"class":84},[78,1387,1388],{"class":92},"3",[78,1390,1361],{"class":84},[78,1392,1393],{"class":300},"'Toni Peperoni'",[78,1395,1367],{"class":84},[78,1397,1398,1401],{"class":80,"line":370},[78,1399,1400],{"class":84},"        ]",[78,1402,140],{"class":84},[78,1404,1405],{"class":80,"line":381},[78,1406,522],{"class":84},[78,1408,1409,1411,1413],{"class":80,"line":402},[78,1410,348],{"class":84},[78,1412,365],{"class":88},[78,1414,286],{"class":84},[12,1416,1417,1418,1420,1421,1423,1424,1427,1428,1430,1431,1434,1435,1438],{},"In our ",[16,1419,1271],{}," component, on the top level ",[16,1422,1285],{}," component, we store the current option in a ",[16,1425,1426],{},"selectedOption"," ref, using ",[16,1429,18],{},". As the current option is an object (e.g. ",[16,1432,1433],{},"{id: 1, name: 'Danny MacAskill'}","), we need to extract the id from it and only pass this ",[16,1436,1437],{},"Number"," to the parent component.",[12,1440,1441,1442,1444,1445,1447,1448,1450,1451,1453,1454,1457,1458,1460],{},"So, in below example, we need to store the currently selected option anyway in a new ref ",[16,1443,1426],{},". Like this, we can make use of ",[16,1446,18],{}," on ",[16,1449,1285],{}," component and then watch that ref for changes. On every change event, we emit the ",[16,1452,202],{}," event and only extract the ",[16,1455,1456],{},"id"," from the selected option. Here's our fully working (yet unstyled) ",[16,1459,1271],{}," component:",[69,1462,1464],{"className":71,"code":1463,"language":73,"meta":74,"style":74},"\u003C!-- CustomListbox.vue -->\n\u003Cscript setup>\nimport { ref, watch } from 'vue'\nimport { Listbox, ListboxLabel, ListboxButton, ListboxOptions, ListboxOption } from '@headlessui\u002Fvue'\n\nconst props = defineProps({\n    modelValue: {\n        type: Number,\n        default: null,\n    },\n    label: {\n        type: String,\n        default: '',\n    },\n    options: {\n        type: Array,\n        required: true,\n    },\n})\n\nconst emit = defineEmits(['update:modelValue'])\n\nconst selectedOption = ref(props.options[0] ?? null)\n\nwatch(selectedOption, (option) => emit('update:modelValue', option.id), { immediate: true })\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n    \u003CListbox v-model=\"selectedOption\">\n        \u003CListboxLabel>{{ label }}:\u003C\u002FListboxLabel>\n        \u003CListboxButton>{{ selectedOption.name }}\u003C\u002FListboxButton>\n        \u003CListboxOptions>\n            \u003CListboxOption v-for=\"option in options\" :key=\"option\" :value=\"option\">\n                {{ option.name }}\n            \u003C\u002FListboxOption>\n        \u003C\u002FListboxOptions>\n    \u003C\u002FListbox>\n\u003C\u002Ftemplate>\n",[16,1465,1466,1471,1481,1492,1503,1507,1521,1525,1530,1539,1543,1547,1551,1559,1563,1568,1573,1583,1587,1591,1595,1613,1617,1645,1649,1678,1686,1690,1698,1716,1730,1744,1753,1808,1814,1824,1834,1843],{"__ignoreMap":74},[78,1467,1468],{"class":80,"line":81},[78,1469,1470],{"class":113},"\u003C!-- CustomListbox.vue -->\n",[78,1472,1473,1475,1477,1479],{"class":80,"line":110},[78,1474,85],{"class":84},[78,1476,280],{"class":88},[78,1478,283],{"class":92},[78,1480,286],{"class":84},[78,1482,1483,1485,1488,1490],{"class":80,"line":117},[78,1484,291],{"class":164},[78,1486,1487],{"class":84}," { ref, watch } ",[78,1489,297],{"class":164},[78,1491,301],{"class":300},[78,1493,1494,1496,1499,1501],{"class":80,"line":125},[78,1495,291],{"class":164},[78,1497,1498],{"class":84}," { Listbox, ListboxLabel, ListboxButton, ListboxOptions, ListboxOption } ",[78,1500,297],{"class":164},[78,1502,789],{"class":300},[78,1504,1505],{"class":80,"line":143},[78,1506,319],{"emptyLinePlaceholder":318},[78,1508,1509,1511,1514,1516,1519],{"class":80,"line":172},[78,1510,324],{"class":164},[78,1512,1513],{"class":92}," props",[78,1515,330],{"class":164},[78,1517,1518],{"class":333}," defineProps",[78,1520,800],{"class":84},[78,1522,1523],{"class":80,"line":355},[78,1524,805],{"class":84},[78,1526,1527],{"class":80,"line":360},[78,1528,1529],{"class":84},"        type: Number,\n",[78,1531,1532,1534,1537],{"class":80,"line":370},[78,1533,815],{"class":84},[78,1535,1536],{"class":92},"null",[78,1538,820],{"class":84},[78,1540,1541],{"class":80,"line":381},[78,1542,825],{"class":84},[78,1544,1545],{"class":80,"line":402},[78,1546,830],{"class":84},[78,1548,1549],{"class":80,"line":412},[78,1550,835],{"class":84},[78,1552,1553,1555,1557],{"class":80,"line":846},[78,1554,815],{"class":84},[78,1556,340],{"class":300},[78,1558,820],{"class":84},[78,1560,1561],{"class":80,"line":851},[78,1562,825],{"class":84},[78,1564,1565],{"class":80,"line":857},[78,1566,1567],{"class":84},"    options: {\n",[78,1569,1570],{"class":80,"line":862},[78,1571,1572],{"class":84},"        type: Array,\n",[78,1574,1575,1578,1581],{"class":80,"line":873},[78,1576,1577],{"class":84},"        required: ",[78,1579,1580],{"class":92},"true",[78,1582,820],{"class":84},[78,1584,1585],{"class":80,"line":882},[78,1586,825],{"class":84},[78,1588,1589],{"class":80,"line":887},[78,1590,854],{"class":84},[78,1592,1593],{"class":80,"line":896},[78,1594,319],{"emptyLinePlaceholder":318},[78,1596,1597,1599,1602,1604,1607,1609,1611],{"class":80,"line":903},[78,1598,324],{"class":164},[78,1600,1601],{"class":92}," emit",[78,1603,330],{"class":164},[78,1605,1606],{"class":333}," defineEmits",[78,1608,452],{"class":84},[78,1610,468],{"class":300},[78,1612,458],{"class":84},[78,1614,1615],{"class":80,"line":933},[78,1616,319],{"emptyLinePlaceholder":318},[78,1618,1619,1621,1624,1626,1628,1631,1634,1637,1640,1643],{"class":80,"line":944},[78,1620,324],{"class":164},[78,1622,1623],{"class":92}," selectedOption",[78,1625,330],{"class":164},[78,1627,334],{"class":333},[78,1629,1630],{"class":84},"(props.options[",[78,1632,1633],{"class":92},"0",[78,1635,1636],{"class":84},"] ",[78,1638,1639],{"class":164},"??",[78,1641,1642],{"class":92}," null",[78,1644,343],{"class":84},[78,1646,1647],{"class":80,"line":959},[78,1648,319],{"emptyLinePlaceholder":318},[78,1650,1651,1654,1657,1660,1662,1664,1666,1668,1670,1673,1675],{"class":80,"line":999},[78,1652,1653],{"class":333},"watch",[78,1655,1656],{"class":84},"(selectedOption, (",[78,1658,1659],{"class":977},"option",[78,1661,981],{"class":84},[78,1663,984],{"class":164},[78,1665,1601],{"class":333},[78,1667,337],{"class":84},[78,1669,468],{"class":300},[78,1671,1672],{"class":84},", option.id), { immediate: ",[78,1674,1580],{"class":92},[78,1676,1677],{"class":84}," })\n",[78,1679,1680,1682,1684],{"class":80,"line":1005},[78,1681,348],{"class":84},[78,1683,280],{"class":88},[78,1685,286],{"class":84},[78,1687,1688],{"class":80,"line":1027},[78,1689,319],{"emptyLinePlaceholder":318},[78,1691,1692,1694,1696],{"class":80,"line":1035},[78,1693,85],{"class":84},[78,1695,365],{"class":88},[78,1697,286],{"class":84},[78,1699,1700,1702,1704,1706,1708,1710,1712,1714],{"class":80,"line":1046},[78,1701,373],{"class":84},[78,1703,1285],{"class":88},[78,1705,93],{"class":92},[78,1707,96],{"class":84},[78,1709,99],{"class":84},[78,1711,1426],{"class":84},[78,1713,99],{"class":84},[78,1715,286],{"class":84},[78,1717,1718,1720,1723,1726,1728],{"class":80,"line":1057},[78,1719,384],{"class":84},[78,1721,1722],{"class":88},"ListboxLabel",[78,1724,1725],{"class":84},">{{ label }}:\u003C\u002F",[78,1727,1722],{"class":88},[78,1729,286],{"class":84},[78,1731,1732,1734,1737,1740,1742],{"class":80,"line":1066},[78,1733,384],{"class":84},[78,1735,1736],{"class":88},"ListboxButton",[78,1738,1739],{"class":84},">{{ selectedOption.name }}\u003C\u002F",[78,1741,1736],{"class":88},[78,1743,286],{"class":84},[78,1745,1746,1748,1751],{"class":80,"line":1075},[78,1747,384],{"class":84},[78,1749,1750],{"class":88},"ListboxOptions",[78,1752,286],{"class":84},[78,1754,1756,1759,1762,1765,1767,1769,1772,1775,1778,1780,1782,1785,1787,1789,1791,1793,1795,1798,1800,1802,1804,1806],{"class":80,"line":1755},33,[78,1757,1758],{"class":84},"            \u003C",[78,1760,1761],{"class":88},"ListboxOption",[78,1763,1764],{"class":164}," v-for",[78,1766,96],{"class":84},[78,1768,99],{"class":84},[78,1770,1771],{"class":84},"option ",[78,1773,1774],{"class":164},"in",[78,1776,1777],{"class":84}," options",[78,1779,99],{"class":84},[78,1781,925],{"class":84},[78,1783,1784],{"class":92},"key",[78,1786,96],{"class":84},[78,1788,99],{"class":84},[78,1790,1659],{"class":84},[78,1792,99],{"class":84},[78,1794,925],{"class":84},[78,1796,1797],{"class":92},"value",[78,1799,96],{"class":84},[78,1801,99],{"class":84},[78,1803,1659],{"class":84},[78,1805,99],{"class":84},[78,1807,286],{"class":84},[78,1809,1811],{"class":80,"line":1810},34,[78,1812,1813],{"class":84},"                {{ option.name }}\n",[78,1815,1817,1820,1822],{"class":80,"line":1816},35,[78,1818,1819],{"class":84},"            \u003C\u002F",[78,1821,1761],{"class":88},[78,1823,286],{"class":84},[78,1825,1827,1830,1832],{"class":80,"line":1826},36,[78,1828,1829],{"class":84},"        \u003C\u002F",[78,1831,1750],{"class":88},[78,1833,286],{"class":84},[78,1835,1837,1839,1841],{"class":80,"line":1836},37,[78,1838,405],{"class":84},[78,1840,1285],{"class":88},[78,1842,286],{"class":84},[78,1844,1846,1848,1850],{"class":80,"line":1845},38,[78,1847,348],{"class":84},[78,1849,365],{"class":88},[78,1851,286],{"class":84},[12,1853,1854,1855,678],{},"I hope these examples gave you a better feeling for ",[16,1856,18],{},[12,1858,1859],{},"Now, enjoy the power of magic reactivity by two-way data binding. Happy coding!",[1861,1862,1863],"style",{},"html pre.shiki code .s_bOF, html code.shiki .s_bOF{--shiki-light:#1F2328;--shiki-default:#1F2328;--shiki-dark:#E6EDF3}html pre.shiki code .sfEsd, html code.shiki .sfEsd{--shiki-light:#116329;--shiki-default:#116329;--shiki-dark:#7EE787}html pre.shiki code .sDkKp, html code.shiki .sDkKp{--shiki-light:#0550AE;--shiki-default:#0550AE;--shiki-dark:#79C0FF}html pre.shiki code .s9BHP, html code.shiki .s9BHP{--shiki-light:#6E7781;--shiki-default:#6E7781;--shiki-dark:#8B949E}html pre.shiki code .s5nWI, html code.shiki .s5nWI{--shiki-light:#CF222E;--shiki-default:#CF222E;--shiki-dark:#FF7B72}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s-BGO, html code.shiki .s-BGO{--shiki-light:#0A3069;--shiki-default:#0A3069;--shiki-dark:#A5D6FF}html pre.shiki code .sd69k, html code.shiki .sd69k{--shiki-light:#8250DF;--shiki-default:#8250DF;--shiki-dark:#D2A8FF}html pre.shiki code .sQJ0q, html code.shiki .sQJ0q{--shiki-light:#953800;--shiki-default:#953800;--shiki-dark:#FFA657}html pre.shiki code .s2sOu, html code.shiki .s2sOu{--shiki-light:#82071E;--shiki-light-font-style:italic;--shiki-default:#82071E;--shiki-default-font-style:italic;--shiki-dark:#FFA198;--shiki-dark-font-style:italic}",{"title":74,"searchDepth":110,"depth":117,"links":1865},[1866,1868,1870,1872],{"id":53,"depth":110,"text":1867},"v-model directive explained",{"id":249,"depth":110,"text":1869},"CustomInput form component",{"id":570,"depth":110,"text":1871},"CustomSwitch (Toggle) form component",{"id":1268,"depth":110,"text":1873},"CustomListbox (Select) for component",null,"2022-07-14 12:00","How to make your custom Vue 3 form components v-model capable.",{"type":9,"value":1878},[1879,1885,1889],[12,1880,14,1881,19,1883,23],{},[16,1882,18],{},[16,1884,22],{},[12,1886,26,1887,29],{},[16,1888,18],{},[12,1890,32,1891,42,1896,45,1898],{},[34,1892,39,1894],{"href":36,"rel":1893},[38],[16,1895,18],{},[16,1897,18],{},[47,1899,49],{},"md","\u002Fimages\u002Fcovers\u002Fvue3-v-model-binding.png",{},"\u002Farticles\u002F20220714-vue3-v-model-binding",false,{"title":6,"description":1876},"articles\u002F20220714-vue3-v-model-binding",[1908,1909,1910],"frontend","vue.js","js","nsqA3SHhHKoy6tdb5UVGTSebGnZxYiLxOd2TX31IiGA",[1913,1918],{"title":1914,"path":1915,"stem":1916,"createdAt":1917,"children":-1},"Configure ESLint and Prettier for Vue.js 3 in VS Code","\u002Farticles\u002F20220103-eslint-prettier-vue3","articles\u002F20220103-eslint-prettier-vue3","2022-01-03 17:30",{"title":1919,"path":1920,"stem":1921,"createdAt":1922,"children":-1},"Vue3-Toastify in a Laravel\u002FInertia\u002FVue project","\u002Farticles\u002F20230404-vue3-toastify-inertia","articles\u002F20230404-vue3-toastify-inertia","2023-04-04 00:35",1794961390515]