Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript Rollout Tier 16 - docs cleavejs #405

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/docs/src/pages/extensions/cleavejs/Cleavejs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,28 @@
</div>
</template>

<script>
import ExFormat from './examples/ExFormat'
<script lang="ts">
import { defineComponent, shallowRef } from 'vue'

import { BMessage } from '@ntohq/buefy-next'

import CodeView from '@/components/CodeView.vue'
import Example from '@/components/Example.vue'

import ExFormat from './examples/ExFormat.vue'
import ExFormatCode from './examples/ExFormat.vue?raw'

export default {
export default defineComponent({
components: {
BMessage,
CodeView,
Example
},
data() {
return {
ExFormat,
ExFormat: shallowRef(ExFormat),
ExFormatCode
}
}
}
})
</script>
39 changes: 26 additions & 13 deletions packages/docs/src/pages/extensions/cleavejs/examples/ExFormat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,49 @@
placeholder="Custom"
:value="value"
v-cleave="masks.custom"
@input.native="onInput">
@input="onInput">
</b-input>
<p><b>Formatted value (v-model)</b>: {{ value }}</p>
<p><b>Raw value</b>: {{ rawValue }}</p>
</b-field>
</section>
</template>

<script>
<script lang="ts">
import { defineComponent } from 'vue'
import type { Directive } from 'vue'
// You have to install cleave.js to use it:
// 'npm install cleave.js'
// 'npm install --save-dev @types/cleave.js'
import Cleave from 'cleave.js'
import type { CleaveOptions } from 'cleave.js/options'

import { BField, BInput } from '@ntohq/buefy-next'

interface CleaveContainer extends HTMLInputElement {
_vCleave: Cleave
}

/**
* We add a new instance of Cleave when the element
* is bound and destroy it when it's unbound.
*/
const cleave = {
name: 'cleave',
bind(el, binding) {
const input = el.querySelector('input')
const cleave: Directive<HTMLElement, CleaveOptions> = {
beforeMount(el, binding) {
const input = el.querySelector('input') as CleaveContainer
input._vCleave = new Cleave(input, binding.value)
},
unbind(el) {
const input = el.querySelector('input')
unmounted(el) {
const input = el.querySelector('input') as CleaveContainer
input._vCleave.destroy()
}
}

export default {
export default defineComponent({
components: {
BField,
BInput
},
directives: { cleave },
data() {
return {
Expand All @@ -70,10 +83,10 @@
}
},
methods: {
onInput(event) {
this.rawValue = event.target._vCleave.getRawValue()
this.value = event.target._vCleave.getFormattedValue()
onInput(event: InputEvent) {
this.rawValue = (event.target as unknown as CleaveContainer)._vCleave.getRawValue()
this.value = (event.target as unknown as CleaveContainer)._vCleave.getFormattedValue()
}
}
}
})
</script>
Loading