feat: establish shared color channel and text input protocols
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
computed,
|
||||
reactive,
|
||||
shallowRef,
|
||||
toRefs,
|
||||
useAttrs,
|
||||
watchEffect,
|
||||
} from "vue";
|
||||
import {
|
||||
useColorState,
|
||||
type ColorModelProps,
|
||||
} from "../../src/components/color/model";
|
||||
import {
|
||||
useColorText,
|
||||
type ColorTextOptions,
|
||||
} from "../../src/components/color/text";
|
||||
import { useColorNativeChannel } from "../../src/components/color/native-channel";
|
||||
import {
|
||||
useChoiceField,
|
||||
type ChoiceFieldProps,
|
||||
} from "../../src/components/choice/shared";
|
||||
import { type Color } from "../../src/components/color/value";
|
||||
import type { ValidateResult } from "../../src/components/field/context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = defineProps<
|
||||
ColorModelProps &
|
||||
ColorTextOptions &
|
||||
ChoiceFieldProps & {
|
||||
mode?: "text" | "range";
|
||||
dir?: "ltr" | "rtl";
|
||||
validate?: (value: Color | null) => ValidateResult;
|
||||
}
|
||||
>();
|
||||
const emit = defineEmits<{
|
||||
"update:modelValue": [Color | null];
|
||||
change: [Color | null];
|
||||
changeEnd: [Color | null];
|
||||
}>();
|
||||
const attrs = useAttrs();
|
||||
const state = useColorState(props, {
|
||||
fallback: () => null,
|
||||
inherit: true,
|
||||
change: (value) => {
|
||||
emit("update:modelValue", value);
|
||||
emit("change", value);
|
||||
},
|
||||
changeEnd: (value) => emit("changeEnd", value),
|
||||
});
|
||||
const fieldProps = reactive({
|
||||
...toRefs(props),
|
||||
isDisabled: state.disabled,
|
||||
isReadOnly: state.readOnly,
|
||||
});
|
||||
const field = useChoiceField(fieldProps, attrs, {
|
||||
kind: "group",
|
||||
value: state.value,
|
||||
validate: (value) => props.validate?.(value),
|
||||
satisfied: () => state.value.value !== null,
|
||||
requiredMessage: "请选择颜色。",
|
||||
});
|
||||
const input = shallowRef<HTMLInputElement>();
|
||||
const text = useColorText(
|
||||
state,
|
||||
props,
|
||||
computed(() => (props.mode === "range" ? undefined : input.value)),
|
||||
{
|
||||
edit: field.edit,
|
||||
reset: field.reset,
|
||||
},
|
||||
);
|
||||
const scalar = useColorNativeChannel(
|
||||
state,
|
||||
reactive({
|
||||
channel: computed(() => props.channel ?? "hue"),
|
||||
colorSpace: computed(() => props.colorSpace),
|
||||
locale: computed(() => props.locale),
|
||||
dir: computed(() => props.dir),
|
||||
}),
|
||||
computed(() => (props.mode === "range" ? input.value : undefined)),
|
||||
);
|
||||
const value = computed(() =>
|
||||
props.mode === "range" ? scalar.value.value : text.draft.value,
|
||||
);
|
||||
const disabled = state.disabled,
|
||||
readonly = state.readOnly;
|
||||
const invalid = field.field.invalid;
|
||||
watchEffect(() => field.validity(input.value));
|
||||
function inputEvent(event: Event) {
|
||||
if (props.mode === "range") {
|
||||
if (
|
||||
!event.defaultPrevented &&
|
||||
!state.disabled.value &&
|
||||
!state.readOnly.value
|
||||
)
|
||||
field.edit();
|
||||
scalar.input(event);
|
||||
} else text.input(event);
|
||||
}
|
||||
function keydown(event: KeyboardEvent) {
|
||||
if (props.mode === "range") scalar.keydown(event);
|
||||
else text.keydown(event);
|
||||
}
|
||||
function blur(event: FocusEvent) {
|
||||
if (props.mode === "range") scalar.commit();
|
||||
else text.blur(event);
|
||||
}
|
||||
const submission = computed(() =>
|
||||
props.mode === "range" ? String(scalar.value.value) : text.submission.value,
|
||||
);
|
||||
defineExpose({ state });
|
||||
</script>
|
||||
<template>
|
||||
<div data-color-input-foundation :data-invalid="invalid || undefined">
|
||||
<input
|
||||
ref="input"
|
||||
data-color-input
|
||||
v-bind="attrs"
|
||||
:id="field.field.controlId.value"
|
||||
:type="mode === 'range' ? 'range' : 'text'"
|
||||
:value="value"
|
||||
:disabled="disabled"
|
||||
:readonly="mode !== 'range' && readonly"
|
||||
:aria-readonly="readonly || undefined"
|
||||
:aria-invalid="invalid || undefined"
|
||||
:aria-required="isRequired || undefined"
|
||||
:aria-describedby="field.describedBy()"
|
||||
:aria-labelledby="field.labelledBy()"
|
||||
:form="form"
|
||||
:min="mode === 'range' ? scalar.range.value.minValue : undefined"
|
||||
:max="mode === 'range' ? scalar.range.value.maxValue : undefined"
|
||||
:step="mode === 'range' ? 'any' : undefined"
|
||||
:aria-valuetext="mode === 'range' ? scalar.label.value : undefined"
|
||||
:dir="dir"
|
||||
@input="inputEvent"
|
||||
@keydown="keydown"
|
||||
@keyup="mode === 'range' && scalar.keyup($event)"
|
||||
@change="mode === 'range' && scalar.commit()"
|
||||
@blur="blur"
|
||||
@pointercancel="mode === 'range' && scalar.cancel()"
|
||||
@compositionstart="text.compositionstart"
|
||||
@compositionend="text.compositionend"
|
||||
@invalid="field.invalidEvent($event, () => input?.focus())"
|
||||
/>
|
||||
<input
|
||||
type="hidden"
|
||||
:name="name"
|
||||
:form="form"
|
||||
:value="state.value.value ? submission : ''"
|
||||
:disabled="disabled"
|
||||
/>
|
||||
<output data-field-error v-if="invalid">
|
||||
{{ field.field.errors.value.join(" ") }}
|
||||
</output>
|
||||
</div>
|
||||
</template>
|
||||
+5
@@ -1,5 +1,10 @@
|
||||
// Installed-version probes use only public exports, resolved from the component package.
|
||||
export {
|
||||
ColorFieldRoot,
|
||||
ColorFieldInput,
|
||||
ColorSliderRoot,
|
||||
ColorSliderTrack,
|
||||
ColorSliderThumb,
|
||||
ColorAreaRoot,
|
||||
ColorAreaArea,
|
||||
ColorAreaThumb,
|
||||
|
||||
Reference in New Issue
Block a user