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

[aform] broken story elements + typing docs #229

Merged
merged 2 commits into from
Dec 24, 2024
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
9 changes: 8 additions & 1 deletion aform/src/components/form/ACheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ import { InputHTMLAttributes } from 'vue'

import { ComponentProps } from '../../types'

const { label, required, readonly, uuid, validation = { errorMessage: '&nbsp;' } } = defineProps<ComponentProps>()
const {
schema, // don't remove to allow masking to work
label,
required,
readonly,
uuid,
validation = { errorMessage: '&nbsp;' },
} = defineProps<ComponentProps>()
const checkbox = defineModel<InputHTMLAttributes['checked']>()
</script>

Expand Down
1 change: 1 addition & 0 deletions aform/src/components/form/ADate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useTemplateRef } from 'vue'
import { ComponentProps } from '../../types'

const {
schema, // don't remove to allow masking to work
label = 'Date',
required,
readonly,
Expand Down
2 changes: 2 additions & 0 deletions aform/src/components/form/ADatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ useKeyboardNav([
},
},
])

defineExpose({ currentMonth, currentYear, selectedDate })
</script>

<style scoped>
Expand Down
2 changes: 2 additions & 0 deletions aform/src/components/form/AFieldset.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const toggleCollapse = (event: Event) => {
collapsed.value = !collapsed.value
}
}

defineExpose({ collapsed })
</script>

<style scoped>
Expand Down
9 changes: 8 additions & 1 deletion aform/src/components/form/ANumericInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
<script setup lang="ts">
import { ComponentProps } from '../../types'

const { label, required, readonly, uuid, validation = { errorMessage: '&nbsp;' } } = defineProps<ComponentProps>()
const {
schema, // don't remove to allow masking to work
label,
required,
readonly,
uuid,
validation = { errorMessage: '&nbsp;' },
} = defineProps<ComponentProps>()
const inputNumber = defineModel<number>()
</script>
10 changes: 9 additions & 1 deletion aform/src/components/form/ATextInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ import { /* inject, */ ref } from 'vue'
import { useStringMask as vMask } from '../../directives/mask'
import { ComponentProps } from '../../types'

const { label, mask, required, readonly, uuid, validation = { errorMessage: '&nbsp;' } } = defineProps<ComponentProps>()
const {
schema, // don't remove to allow masking to work
label,
mask,
required,
readonly,
uuid,
validation = { errorMessage: '&nbsp;' },
} = defineProps<ComponentProps>()

// TODO: setup maskFilled as a computed property
const maskFilled = ref(true)
Expand Down
35 changes: 34 additions & 1 deletion aform/src/directives/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { DirectiveBinding } from 'vue'

import type { FormSchema } from '../types'

/**
* Named masks for common input types
*/
const NAMED_MASKS = {
date: '##/##/####',
datetime: '####/##/## ##:##',
Expand All @@ -11,6 +14,11 @@ const NAMED_MASKS = {
card: '#### #### #### ####',
}

/**
* Extracts a mask function from a stringified function
* @param mask - Mask string
* @returns Mask function
*/
function extractMaskFn(mask: string): ((args: any) => string) | void {
try {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
Expand All @@ -22,6 +30,11 @@ function extractMaskFn(mask: string): ((args: any) => string) | void {
}
}

/**
* Gets the mask for a given directive binding
* @param binding - Binding object from directive hook
* @returns Mask string
*/
function getMask(binding: DirectiveBinding<string>) {
let mask = binding.value

Expand All @@ -35,7 +48,7 @@ function getMask(binding: DirectiveBinding<string>) {
}
} else {
// TODO: (state) handle using state management
const schema: FormSchema = binding.instance['schema']
const schema = binding.instance['schema'] as FormSchema
const fieldType: string | undefined = schema?.fieldtype?.toLowerCase()
if (fieldType && NAMED_MASKS[fieldType]) {
mask = NAMED_MASKS[fieldType]
Expand All @@ -45,6 +58,12 @@ function getMask(binding: DirectiveBinding<string>) {
return mask
}

/**
* Unmasks the input string
* @param input - Input string
* @param maskToken - Mask token character
* @returns Unmasked input string
*/
function unmaskInput(input: string, maskToken?: string) {
if (!maskToken) {
maskToken = '#'
Expand All @@ -60,6 +79,13 @@ function unmaskInput(input: string, maskToken?: string) {
return unmaskedInput
}

/**
* Fills the mask with the input string
* @param input - Input string
* @param mask - Mask string
* @param maskToken - Mask token character
* @returns Masked input string
*/
function fillMask(input: string, mask: string, maskToken?: string) {
if (!maskToken) {
maskToken = '#'
Expand All @@ -78,6 +104,13 @@ function fillMask(input: string, mask: string, maskToken?: string) {
return replacement.slice(0, mask.length)
}

/**
* Applies a mask to an input element
* @param el - Input element
* @param binding - Binding object from directive hook
* @returns void
* @public
*/
export function useStringMask(el: HTMLInputElement, binding: DirectiveBinding<string>) {
const mask = getMask(binding)
if (!mask) return
Expand Down
8 changes: 2 additions & 6 deletions aform/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type { CellContext, TableConfig, TableColumn, TableRow } from '@stonecrop/atable'
import { App } from 'vue'
import type { App } from 'vue'

import ACheckbox from './components/form/ACheckbox.vue'
import AComboBox from './components/form/AComboBox.vue'
Expand All @@ -12,9 +12,7 @@ import AForm from './components/AForm.vue'
import ANumericInput from './components/form/ANumericInput.vue'
import ATextInput from './components/form/ATextInput.vue'
import Login from './components/utilities/Login.vue'
export type { BasicSchema, FormSchema, TableSchema, FieldsetSchema, SchemaTypes } from './types'
// import { ACurrency } from './components/form/ACurrency.vue'
// import { AQuantity } from './components/form/AQuantity.vue'
export type { BaseSchema, FieldsetSchema, FormSchema, SchemaTypes, TableSchema } from './types'

/**
* Install all AForm components
Expand All @@ -32,8 +30,6 @@ function install(app: App /* options */) {
app.component('AForm', AForm)
app.component('ANumericInput', ANumericInput)
app.component('ATextInput', ATextInput)
// app.component('ACurrency', ACurrency)
// app.component('AQuantity', AQuantity)
}

export {
Expand Down
Loading
Loading