Skip to content

Commit d31dd79

Browse files
committed
fix(PinInput): display placeholders after resetting pin value programmatically
1 parent ad43ecf commit d31dd79

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

packages/radix-vue/src/PinInput/PinInput.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ describe('given default PinInput', () => {
144144
expect(wrapper.emitted('complete')?.[0]?.[0]).toStrictEqual(['a', 'p', 'p', 'l', 'e'])
145145
})
146146
})
147+
148+
describe('after resetting value', async () => {
149+
beforeEach(async () => {
150+
await userEvent.keyboard('apple')
151+
const button = wrapper.find('button').element
152+
button.focus()
153+
button.click()
154+
})
155+
156+
it('should display input placeholders', () => {
157+
expect(inputs[0].element.placeholder).toBe('*')
158+
expect(inputs[1].element.placeholder).toBe('*')
159+
expect(inputs[2].element.placeholder).toBe('*')
160+
expect(inputs[3].element.placeholder).toBe('*')
161+
expect(inputs[4].element.placeholder).toBe('*')
162+
})
163+
})
147164
})
148165

149166
describe('give PinInput type=number', async () => {

packages/radix-vue/src/PinInput/PinInputInput.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface PinInputInputProps extends PrimitiveProps {
1212
</script>
1313

1414
<script setup lang="ts">
15-
import { computed, nextTick, onMounted, onUnmounted } from 'vue'
15+
import { computed, nextTick, onMounted, onUnmounted, watch } from 'vue'
1616
1717
const props = withDefaults(defineProps<PinInputInputProps>(), {
1818
as: 'input',
@@ -98,6 +98,13 @@ function handleBlur(event: FocusEvent) {
9898
})
9999
}
100100
101+
watch(context.modelValue, async () => {
102+
await nextTick()
103+
const target = currentElement.value as HTMLInputElement
104+
if (!target.value && target !== document.activeElement)
105+
target.placeholder = context.placeholder.value
106+
})
107+
101108
function handlePaste(event: ClipboardEvent) {
102109
event.preventDefault()
103110
const clipboardData = event.clipboardData

packages/radix-vue/src/PinInput/story/PinInput.story.vue

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { PinInputInput, PinInputRoot } from '..'
44
import { Label } from '@/Label'
55
66
const value = ref<string[]>([])
7+
const button = [
8+
'mt-4 text-violet11 shadow-blackA7 hover:bg-mauve3 inline-flex h-[35px] items-center justify-center rounded-[4px]',
9+
'bg-white px-[15px] font-medium leading-none shadow-[0_2px_10px] focus:shadow-[0_0_0_2px] focus:shadow-black focus:outline-none',
10+
]
711
</script>
812

913
<template>
@@ -18,6 +22,7 @@ const value = ref<string[]>([])
1822
<PinInputRoot
1923
id="otp"
2024
v-model="value"
25+
placeholder=""
2126
class="flex gap-2 items-center"
2227
@complete="e => console.log(e.join(''))"
2328
>
@@ -28,6 +33,13 @@ const value = ref<string[]>([])
2833
:index="index"
2934
/>
3035
</PinInputRoot>
36+
37+
<button
38+
:class="button"
39+
@click="value = []"
40+
>
41+
Reset value
42+
</button>
3143
</div>
3244
</Variant>
3345
</Story>

packages/radix-vue/src/PinInput/story/_PinInput.vue

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const value = ref<string[]>([])
1515
<PinInputRoot
1616
v-bind="{ ...props, ...useEmitAsProps(emits) }"
1717
v-model="value"
18+
placeholder="*"
1819
class="flex gap-2 items-center"
1920
>
2021
<PinInputInput
@@ -24,4 +25,11 @@ const value = ref<string[]>([])
2425
:index="index"
2526
/>
2627
</PinInputRoot>
28+
29+
<button
30+
class="reset-button"
31+
@click="value = []"
32+
>
33+
Reset value
34+
</button>
2735
</template>

0 commit comments

Comments
 (0)