Skip to content

Commit

Permalink
feat: support groupAppend & groupAppend slots for form-input component
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Feb 11, 2025
1 parent 9e43be3 commit 036ce79
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/form-controls/src/components/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export enum SlotName {
DEFAULT = 'default',
LABEL = 'label',
HINT = 'hint',

GROUP_APPEND = 'groupAppend',
GROUP_PREPEND = 'groupPrepend',

VALIDATION_GROUP = 'validationGroup',
VALIDATION_ITEM = 'validationItem',
}
Expand Down
17 changes: 16 additions & 1 deletion packages/form-controls/src/components/form-input/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { SlotsType } from 'vue';
import { defineComponent } from 'vue';
import { buildFormInput } from './module';
import type { SlotName } from '../constants';

export const VCFormInput = defineComponent({
props: {
Expand All @@ -14,7 +16,18 @@ export const VCFormInput = defineComponent({
},
},
emits: ['update:modelValue'],
setup(props, { attrs, emit }) {
slots: Object as SlotsType<{
[SlotName.GROUP_APPEND]: undefined,
[SlotName.GROUP_PREPEND]: undefined
}>,
setup(
props,
{
attrs,
emit,
slots,
},
) {
return () => buildFormInput({
type: props.type,

Expand All @@ -24,6 +37,8 @@ export const VCFormInput = defineComponent({
emit('update:modelValue', input);
},

slotItems: slots,

props: attrs,
});
},
Expand Down
46 changes: 40 additions & 6 deletions packages/form-controls/src/components/form-input/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { VNodeChild } from 'vue';
import { h, mergeProps, unref } from 'vue';
import { createComponentOptionsManager } from '@vuecs/core';
import { Component } from '../constants';
import { createComponentOptionsManager, hasNormalizedSlot, normalizeSlot } from '@vuecs/core';
import { Component, SlotName } from '../constants';
import { buildFormBaseOptions, handleFormValueChanged } from '../form-base';
import type { FormInputBuildOptions, FormInputBuildOptionsInput } from './type';

Expand Down Expand Up @@ -84,9 +84,25 @@ export function buildFormInputFromOptions(
): VNodeChild {
const children : VNodeChild = [];

if (options.groupPrepend) {
if (hasNormalizedSlot(SlotName.GROUP_PREPEND)) {
children.push(
h('span', { class: options.groupPrependClass }, [options.groupPrependContent]),
h(
'span',
{ class: options.groupPrependClass },
[
normalizeSlot(SlotName.GROUP_PREPEND, {}, options.slotItems),
],
),
);
} else if (options.groupPrepend) {
children.push(
h(
'span',
{ class: options.groupPrependClass },
[
options.groupPrependContent,
],
),
);
}

Expand All @@ -111,9 +127,27 @@ export function buildFormInputFromOptions(
),
));

if (options.groupAppend) {
if (hasNormalizedSlot(SlotName.GROUP_APPEND)) {
children.push(
h('span', { class: options.groupAppendClass }, [options.groupAppendContent]),
h(
'span',
{ class: options.groupAppendClass },
[
normalizeSlot(SlotName.GROUP_APPEND, {}, options.slotItems),
],
),
);
} else if (options.groupAppend) {
children.push(
h(
'span',
{
class: options.groupAppendClass,
},
[
options.groupAppendContent,
],
),
);
}

Expand Down
7 changes: 5 additions & 2 deletions packages/form-controls/src/components/form-input/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import type {
PartialPick,
VNodeClass,
} from '@vuecs/core';
import type { VNodeChild } from 'vue';
import type { Slots, VNodeChild } from 'vue';
import type { ExpectFormBaseOptions, FormBaseOptions, FormBaseOptionsInput } from '../form-base';

export type FormInputBuildOptions = FormBaseOptions & {
slotItems: Slots,

type: string,
group: boolean,
groupClass: VNodeClass,
Expand All @@ -34,6 +36,7 @@ export type FormInputBuildOptionsInput =
'groupPrependContent' |
'groupAppend' |
'groupAppendClass' |
'groupAppendContent'
'groupAppendContent' |
'slotItems'
>>
>;

0 comments on commit 036ce79

Please sign in to comment.