Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b0c1eef5a | |||
| 9cc6e4e048 |
@@ -1,10 +1,269 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, useId } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
import Checkbox from 'primevue/checkbox'
|
|
||||||
const props = defineProps<{ modelValue: boolean; label: string; inputId?: string; disabled?: boolean }>()
|
export interface KsCheckboxProps {
|
||||||
const emit = defineEmits<{ 'update:modelValue': [value: boolean] }>()
|
modelValue: boolean | string[]
|
||||||
const generatedId = useId()
|
label: string
|
||||||
const resolvedId = computed(() => props.inputId ?? `ks-check-${generatedId}`)
|
value?: any
|
||||||
|
disabled?: boolean
|
||||||
|
readonly?: boolean
|
||||||
|
indeterminate?: boolean
|
||||||
|
inputId?: string
|
||||||
|
description?: string
|
||||||
|
error?: string
|
||||||
|
size?: 'sm' | 'md' | 'lg'
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = withDefaults(defineProps<KsCheckboxProps>(), {
|
||||||
|
disabled: false,
|
||||||
|
readonly: false,
|
||||||
|
indeterminate: false,
|
||||||
|
size: 'md',
|
||||||
|
})
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
'update:modelValue': [value: boolean | string[]]
|
||||||
|
change: [value: boolean | string[]]
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const isFocused = ref(false)
|
||||||
|
const id = computed(() => props.inputId || `checkbox-${Math.random().toString(36).substr(2, 9)}`)
|
||||||
|
|
||||||
|
const isChecked = computed(() => {
|
||||||
|
if (Array.isArray(props.modelValue)) {
|
||||||
|
return props.modelValue.includes(props.value)
|
||||||
|
}
|
||||||
|
return props.modelValue === true
|
||||||
|
})
|
||||||
|
|
||||||
|
const containerClass = computed(() => [
|
||||||
|
'ks-checkbox',
|
||||||
|
`ks-checkbox--${props.size}`,
|
||||||
|
{
|
||||||
|
'ks-checkbox--focused': isFocused.value,
|
||||||
|
'ks-checkbox--checked': isChecked.value,
|
||||||
|
'ks-checkbox--indeterminate': props.indeterminate,
|
||||||
|
'ks-checkbox--disabled': props.disabled,
|
||||||
|
'ks-checkbox--error': !!props.error,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const handleChange = (event: Event) => {
|
||||||
|
const target = event.target as HTMLInputElement
|
||||||
|
let newValue
|
||||||
|
|
||||||
|
if (Array.isArray(props.modelValue)) {
|
||||||
|
newValue = target.checked
|
||||||
|
? [...props.modelValue, props.value]
|
||||||
|
: props.modelValue.filter(v => v !== props.value)
|
||||||
|
} else {
|
||||||
|
newValue = target.checked
|
||||||
|
}
|
||||||
|
|
||||||
|
emit('update:modelValue', newValue)
|
||||||
|
emit('change', newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleFocus = () => {
|
||||||
|
isFocused.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleBlur = () => {
|
||||||
|
isFocused.value = false
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
<template><label class="ks-check" :for="resolvedId"><Checkbox class="ks-checkbox" :input-id="resolvedId" :model-value="modelValue" binary :disabled="disabled" @update:model-value="emit('update:modelValue', Boolean($event))" /><span>{{ label }}</span></label></template>
|
|
||||||
<style scoped>.ks-check { display: inline-flex; align-items: center; gap: var(--ks-space-2); cursor: pointer; }</style>
|
<template>
|
||||||
|
<div :class="containerClass">
|
||||||
|
<div class="ks-checkbox__wrapper">
|
||||||
|
<input
|
||||||
|
:id="id"
|
||||||
|
type="checkbox"
|
||||||
|
class="ks-checkbox__input"
|
||||||
|
:checked="isChecked"
|
||||||
|
:disabled="disabled"
|
||||||
|
:readonly="readonly"
|
||||||
|
:value="value"
|
||||||
|
:aria-label="label"
|
||||||
|
:aria-invalid="!!error"
|
||||||
|
:aria-describedby="error || description ? `${id}-hint` : undefined"
|
||||||
|
@change="handleChange"
|
||||||
|
@focus="handleFocus"
|
||||||
|
@blur="handleBlur"
|
||||||
|
/>
|
||||||
|
<div class="ks-checkbox__box">
|
||||||
|
<svg v-if="isChecked && !indeterminate" class="ks-checkbox__check" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
||||||
|
<polyline points="20 6 9 17 4 12" />
|
||||||
|
</svg>
|
||||||
|
<div v-if="indeterminate" class="ks-checkbox__indeterminate" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="ks-checkbox__content">
|
||||||
|
<label :for="id" class="ks-checkbox__label">{{ label }}</label>
|
||||||
|
<div v-if="description || error" :id="`${id}-hint`" :class="{ 'ks-checkbox__error': error, 'ks-checkbox__description': description }">
|
||||||
|
{{ error || description }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.ks-checkbox {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: var(--spacing-3);
|
||||||
|
cursor: pointer;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-checkbox--sm {
|
||||||
|
--box-size: 16px;
|
||||||
|
--font-size: var(--font-size-xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-checkbox--md {
|
||||||
|
--box-size: 20px;
|
||||||
|
--font-size: var(--font-size-sm);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-checkbox--lg {
|
||||||
|
--box-size: 24px;
|
||||||
|
--font-size: var(--font-size-base);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Input wrapper */
|
||||||
|
.ks-checkbox__wrapper {
|
||||||
|
position: relative;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
flex-shrink: 0;
|
||||||
|
margin-top: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Hidden input */
|
||||||
|
.ks-checkbox__input {
|
||||||
|
position: absolute;
|
||||||
|
width: 0;
|
||||||
|
height: 0;
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Checkbox box */
|
||||||
|
.ks-checkbox__box {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: var(--box-size);
|
||||||
|
height: var(--box-size);
|
||||||
|
border: 2px solid var(--color-border-primary);
|
||||||
|
border-radius: var(--border-radius-sm);
|
||||||
|
background-color: var(--color-background-primary);
|
||||||
|
transition: all var(--transition-normal);
|
||||||
|
color: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Hover state */
|
||||||
|
.ks-checkbox:not(.ks-checkbox--disabled) .ks-checkbox__input:hover ~ .ks-checkbox__box {
|
||||||
|
border-color: var(--color-primary-500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Checked state */
|
||||||
|
.ks-checkbox--checked .ks-checkbox__box {
|
||||||
|
background-color: var(--color-primary-500);
|
||||||
|
border-color: var(--color-primary-500);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-checkbox--checked:not(.ks-checkbox--disabled) .ks-checkbox__input:hover ~ .ks-checkbox__box {
|
||||||
|
background-color: var(--color-primary-600);
|
||||||
|
border-color: var(--color-primary-600);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Indeterminate state */
|
||||||
|
.ks-checkbox--indeterminate .ks-checkbox__box {
|
||||||
|
background-color: var(--color-primary-500);
|
||||||
|
border-color: var(--color-primary-500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Focus state */
|
||||||
|
.ks-checkbox--focused .ks-checkbox__box {
|
||||||
|
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Disabled state */
|
||||||
|
.ks-checkbox--disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Error state */
|
||||||
|
.ks-checkbox--error .ks-checkbox__box {
|
||||||
|
border-color: var(--color-danger-500);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-checkbox--error.ks-checkbox--checked .ks-checkbox__box {
|
||||||
|
background-color: var(--color-danger-500);
|
||||||
|
border-color: var(--color-danger-500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check icon */
|
||||||
|
.ks-checkbox__check {
|
||||||
|
width: 12px;
|
||||||
|
height: 12px;
|
||||||
|
stroke-width: 3;
|
||||||
|
stroke-linecap: round;
|
||||||
|
stroke-linejoin: round;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Indeterminate icon */
|
||||||
|
.ks-checkbox__indeterminate {
|
||||||
|
width: 8px;
|
||||||
|
height: 2px;
|
||||||
|
background-color: white;
|
||||||
|
border-radius: 1px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Content */
|
||||||
|
.ks-checkbox__content {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: var(--spacing-1);
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Label */
|
||||||
|
.ks-checkbox__label {
|
||||||
|
font-size: var(--font-size);
|
||||||
|
font-weight: var(--font-weight-medium);
|
||||||
|
color: var(--color-text-primary);
|
||||||
|
cursor: pointer;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-checkbox--disabled .ks-checkbox__label {
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Description/Error text */
|
||||||
|
.ks-checkbox__description {
|
||||||
|
font-size: var(--font-size-xs);
|
||||||
|
color: var(--color-text-secondary);
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-checkbox__error {
|
||||||
|
font-size: var(--font-size-xs);
|
||||||
|
color: var(--color-danger-500);
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Reduced motion */
|
||||||
|
@media (prefers-reduced-motion: reduce) {
|
||||||
|
.ks-checkbox__box {
|
||||||
|
transition: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|||||||
@@ -1,14 +1,224 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import DatePicker from 'primevue/datepicker'
|
import { computed, ref } from 'vue'
|
||||||
import { computed } from 'vue'
|
|
||||||
import FieldShell from './FieldShell.vue'
|
export interface KsDateFieldProps {
|
||||||
const props = defineProps<{ modelValue: string | Date | null; label: string; inputId?: string; disabled?: boolean; required?: boolean; error?: string; help?: string; min?: Date; max?: Date }>()
|
modelValue: string | Date | null
|
||||||
const emit = defineEmits<{ 'update:modelValue': [value: string | Date | null]; blur: [event: FocusEvent] }>()
|
label: string
|
||||||
const dateValue = computed(() => typeof props.modelValue === 'string' ? new Date(props.modelValue) : props.modelValue)
|
type?: 'date' | 'datetime' | 'range'
|
||||||
function handleDateChange(value: Date | Date[] | (Date | null)[] | null | undefined): void {
|
disabled?: boolean
|
||||||
if (value instanceof Date) emit('update:modelValue', value)
|
readonly?: boolean
|
||||||
else if (value == null) emit('update:modelValue', null)
|
required?: boolean
|
||||||
else emit('update:modelValue', value[0] ?? null)
|
error?: string
|
||||||
|
help?: string
|
||||||
|
min?: Date | string
|
||||||
|
max?: Date | string
|
||||||
|
placeholder?: string
|
||||||
|
inputId?: string
|
||||||
|
size?: 'sm' | 'md' | 'lg'
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = withDefaults(defineProps<KsDateFieldProps>(), {
|
||||||
|
type: 'date',
|
||||||
|
disabled: false,
|
||||||
|
readonly: false,
|
||||||
|
required: false,
|
||||||
|
size: 'md',
|
||||||
|
})
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
'update:modelValue': [value: string | Date | null]
|
||||||
|
blur: [event: FocusEvent]
|
||||||
|
focus: [event: FocusEvent]
|
||||||
|
change: [value: string | Date | null]
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const isFocused = ref(false)
|
||||||
|
const id = computed(() => props.inputId || `date-${Math.random().toString(36).substr(2, 9)}`)
|
||||||
|
|
||||||
|
const dateString = computed(() => {
|
||||||
|
if (!props.modelValue) return ''
|
||||||
|
const date = typeof props.modelValue === 'string' ? new Date(props.modelValue) : props.modelValue
|
||||||
|
return date.toISOString().split('T')[0]
|
||||||
|
})
|
||||||
|
|
||||||
|
const containerClass = computed(() => [
|
||||||
|
'ks-date-field',
|
||||||
|
`ks-date-field--${props.size}`,
|
||||||
|
{
|
||||||
|
'ks-date-field--focused': isFocused.value,
|
||||||
|
'ks-date-field--filled': !!props.modelValue,
|
||||||
|
'ks-date-field--disabled': props.disabled,
|
||||||
|
'ks-date-field--error': !!props.error,
|
||||||
|
'ks-date-field--required': props.required,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const handleInput = (event: Event) => {
|
||||||
|
const target = event.target as HTMLInputElement
|
||||||
|
const date = new Date(target.value)
|
||||||
|
emit('update:modelValue', date)
|
||||||
|
emit('change', date)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleFocus = (event: FocusEvent) => {
|
||||||
|
isFocused.value = true
|
||||||
|
emit('focus', event)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleBlur = (event: FocusEvent) => {
|
||||||
|
isFocused.value = false
|
||||||
|
emit('blur', event)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<template><FieldShell :label="label" :input-id="inputId" :required="required" :error="error" :help="help" v-slot="field"><DatePicker :input-id="field.inputId" :model-value="dateValue" :disabled="disabled" :invalid="field.invalid" :min-date="min" :max-date="max" date-format="yy-mm-dd" show-icon :aria-describedby="field.describedBy" :aria-required="field.required || undefined" @update:model-value="handleDateChange" @blur="emit('blur', $event as unknown as FocusEvent)" /></FieldShell></template>
|
|
||||||
|
<template>
|
||||||
|
<div :class="containerClass">
|
||||||
|
<!-- Label -->
|
||||||
|
<label :for="id" class="ks-date-field__label">
|
||||||
|
{{ label }}
|
||||||
|
<span v-if="required" class="ks-date-field__required">*</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<!-- Input -->
|
||||||
|
<div class="ks-date-field__container">
|
||||||
|
<input
|
||||||
|
:id="id"
|
||||||
|
:type="type === 'range' ? 'text' : type"
|
||||||
|
:value="dateString"
|
||||||
|
:disabled="disabled"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="required"
|
||||||
|
:min="typeof min === 'string' ? min : min?.toISOString().split('T')[0]"
|
||||||
|
:max="typeof max === 'string' ? max : max?.toISOString().split('T')[0]"
|
||||||
|
:placeholder="placeholder"
|
||||||
|
class="ks-date-field__input"
|
||||||
|
:aria-invalid="!!error"
|
||||||
|
:aria-describedby="error || help ? `${id}-hint` : undefined"
|
||||||
|
@input="handleInput"
|
||||||
|
@focus="handleFocus"
|
||||||
|
@blur="handleBlur"
|
||||||
|
/>
|
||||||
|
<span class="ks-date-field__icon">📅</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Error or help text -->
|
||||||
|
<div v-if="error || help" :id="`${id}-hint`" :class="{ 'ks-date-field__error': error, 'ks-date-field__help': help }">
|
||||||
|
{{ error || help }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.ks-date-field {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: var(--spacing-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-date-field--sm {
|
||||||
|
--input-height: 32px;
|
||||||
|
--font-size: var(--font-size-sm);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-date-field--md {
|
||||||
|
--input-height: 36px;
|
||||||
|
--font-size: var(--font-size-sm);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-date-field--lg {
|
||||||
|
--input-height: 44px;
|
||||||
|
--font-size: var(--font-size-base);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Label */
|
||||||
|
.ks-date-field__label {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--spacing-1);
|
||||||
|
font-weight: var(--font-weight-medium);
|
||||||
|
color: var(--color-text-primary);
|
||||||
|
font-size: var(--font-size-sm);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-date-field__required {
|
||||||
|
color: var(--color-danger-500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Container */
|
||||||
|
.ks-date-field__container {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
border: 1px solid var(--color-border-primary);
|
||||||
|
border-radius: var(--border-radius-sm);
|
||||||
|
background-color: var(--color-background-primary);
|
||||||
|
transition: all var(--transition-normal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Input */
|
||||||
|
.ks-date-field__input {
|
||||||
|
flex: 1;
|
||||||
|
height: var(--input-height);
|
||||||
|
padding: 0 var(--spacing-3);
|
||||||
|
padding-right: var(--spacing-10);
|
||||||
|
border: none;
|
||||||
|
background: transparent;
|
||||||
|
font-size: var(--font-size);
|
||||||
|
color: var(--color-text-primary);
|
||||||
|
outline: none;
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-date-field__input::placeholder {
|
||||||
|
color: var(--color-text-tertiary);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Icon */
|
||||||
|
.ks-date-field__icon {
|
||||||
|
position: absolute;
|
||||||
|
right: var(--spacing-3);
|
||||||
|
font-size: 1.2rem;
|
||||||
|
pointer-events: none;
|
||||||
|
color: var(--color-text-tertiary);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Focus state */
|
||||||
|
.ks-date-field--focused .ks-date-field__container {
|
||||||
|
border-color: var(--color-primary-500);
|
||||||
|
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Disabled state */
|
||||||
|
.ks-date-field--disabled .ks-date-field__container {
|
||||||
|
background-color: var(--color-background-secondary);
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-date-field--disabled .ks-date-field__input {
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Error state */
|
||||||
|
.ks-date-field--error .ks-date-field__container {
|
||||||
|
border-color: var(--color-danger-500);
|
||||||
|
box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ks-date-field--error .ks-date-field__label {
|
||||||
|
color: var(--color-danger-500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Error text */
|
||||||
|
.ks-date-field__error {
|
||||||
|
font-size: var(--font-size-xs);
|
||||||
|
color: var(--color-danger-500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Help text */
|
||||||
|
.ks-date-field__help {
|
||||||
|
font-size: var(--font-size-xs);
|
||||||
|
color: var(--color-text-secondary);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user