Skip to content

Commit

Permalink
refactor(vue/Field): expose v-model at form element
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerAPfledderer committed Sep 13, 2024
1 parent 2354033 commit 9646f12
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 10 deletions.
6 changes: 6 additions & 0 deletions packages/vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ description: All notable changes will be documented in this file.

## [Unreleased]

### Changed

- **Field**: Expose `v-model` through `FieldInput`, `FieldSelect`, and `FieldTextarea`. Use
`v-model` at these components instead of `FieldRoot` to maintain value type for each form element,
as the type is variable.

### Added

- **Frame (Preview)**: Added `Frame` component for rendering components inside an iframe.
Expand Down
4 changes: 3 additions & 1 deletion packages/vue/src/components/field/examples/input.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<script setup lang="ts">
import { Field } from '../'
const model = defineModel()
</script>

<template>
<Field.Root>
<Field.Label>Label</Field.Label>
<Field.Input />
<Field.Input v-model="model" />
<Field.HelperText>Some additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
Expand Down
4 changes: 3 additions & 1 deletion packages/vue/src/components/field/examples/select.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<script setup lang="ts">
import { Field } from '../'
const model = defineModel<string>()
</script>

<template>
<Field.Root>
<Field.Label>Label</Field.Label>
<Field.Select>
<Field.Select v-model="model">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
Expand Down
4 changes: 3 additions & 1 deletion packages/vue/src/components/field/examples/textarea.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<script setup lang="ts">
import type { TextareaHTMLAttributes } from 'vue'
import { Field } from '../'
const model = defineModel<TextareaHTMLAttributes['value']>()
</script>

<template>
<Field.Root>
<Field.Label>Label</Field.Label>
<Field.Textarea />
<Field.Textarea v-model="model" />
<Field.HelperText>Some additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
Expand Down
13 changes: 11 additions & 2 deletions packages/vue/src/components/field/field-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ export interface FieldInputProps
import { ark } from '../factory'
import { useFieldContext } from './use-field-context'
defineProps<FieldInputProps>()
const props = defineProps<FieldInputProps & { modelValue?: unknown }>()
const field = useFieldContext()
const emit = defineEmits(['update:modelValue'])
</script>

<template>
<ark.input v-bind="field.getInputProps()" :as-child="asChild"><slot /></ark.input>
<ark.input
v-bind="field.getInputProps()"
:as-child="asChild"
:value="props.modelValue"
@input="(event) => emit('update:modelValue', (event.target as HTMLInputElement).value)"
>
<slot />
</ark.input>
</template>
13 changes: 11 additions & 2 deletions packages/vue/src/components/field/field-select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ export interface FieldSelectProps
import { ark } from '../factory'
import { useFieldContext } from './use-field-context'
defineProps<FieldSelectProps>()
const props = defineProps<FieldSelectProps & { modelValue?: string }>()
const field = useFieldContext()
const emit = defineEmits(['update:modelValue'])
</script>

<template>
<ark.select v-bind="field.getSelectProps()" :as-child="asChild"><slot /></ark.select>
<ark.select
v-bind="field.getSelectProps()"
:value="props.modelValue"
@change="(event) => emit('update:modelValue', (event.target as HTMLSelectElement).value)"
:as-child="asChild"
>
<slot />
</ark.select>
</template>
16 changes: 13 additions & 3 deletions packages/vue/src/components/field/field-textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ export interface FieldTextareaProps
/**
* @vue-ignore
*/
TextareaHTMLAttributes {}
Omit<TextareaHTMLAttributes, 'value'> {
modelValue?: TextareaHTMLAttributes['value']
}
</script>

<script setup lang="ts">
import { ark } from '../factory'
import { useFieldContext } from './use-field-context'
defineProps<FieldTextareaProps>()
const props = defineProps<FieldTextareaProps>()
const field = useFieldContext()
const emit = defineEmits(['update:modelValue'])
</script>

<template>
<ark.textarea v-bind="field.getTextareaProps()" :as-child="asChild"><slot /></ark.textarea>
<ark.textarea
v-bind="field.getTextareaProps()"
:value="props.modelValue"
@input="(event) => emit('update:modelValue', (event.target as HTMLTextAreaElement).value)"
:as-child="asChild"
>
<slot />
</ark.textarea>
</template>
16 changes: 16 additions & 0 deletions packages/vue/src/components/field/field.stories.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
<script setup lang="ts">
import { ref } from 'vue'
import Input from './examples/input.vue'
import Select from './examples/select.vue'
import Textarea from './examples/textarea.vue'
const inputModel = ref('This input is controlled')
const selectModel = ref('2')
const textareaModel = ref(['This is some text', 'then more text'])
</script>
<template>
<Story title="Field">
<Variant title="Input">
<Input />
</Variant>
<Variant title="Input Controlled">
<span>Input value: {{ inputModel }}</span>
<Input v-model="inputModel" />
</Variant>
<Variant title="Select">
<Select />
</Variant>
<Variant title="Select Controlled">
<span>Selected: Option {{ selectModel }}</span>
<Select v-model="selectModel" />
</Variant>
<Variant title="Textarea">
<Textarea />
</Variant>
<Variant title="Textarea Controlled">
<span>Textarea value: {{ textareaModel }}</span>
<Textarea v-model="textareaModel" />
</Variant>
</Story>
</template>

0 comments on commit 9646f12

Please sign in to comment.