Skip to content

Commit

Permalink
refactor: add useDefinedNameSpace, remove ns param from useCEStates; …
Browse files Browse the repository at this point in the history
…simplify useEdit
  • Loading branch information
lejunyang committed Nov 2, 2024
1 parent 9576fa6 commit 673726e
Show file tree
Hide file tree
Showing 34 changed files with 111 additions and 104 deletions.
10 changes: 8 additions & 2 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ runs:
shell: bash # Must set shell for every step in composite action...
run: pnpm install --frozen-lockfile

- name: Check inputs
shell: bash
run: |
echo "github.event.inputs: ${{ toJSON(github.event.inputs) }}"
echo "inputs: ${{ toJSON(inputs) }}"
- name: Check if needs to init test
shell: bash
if: ${{ github.event.inputs.init-test == true || github.event.inputs.init-test == 'true' }}
if: ${{ inputs.init-test == true || inputs.init-test == 'true' }}
run: pnpm exec playwright install

- name: Check if needs to download build
uses: actions/download-artifact@v4
if: ${{ github.event.inputs.artifact-id }}
if: ${{ inputs.artifact-id }}
with:
name: package-builds
path: ./packages
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup
with:
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/accordion/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Accordion = defineSSRCustomElement({
else return isOpen.value ? val?.open ?? defaultV : val?.close ?? defaultV;
};

const [stateClass] = useCEStates(() => ({ open: isOpen }), ns);
const [stateClass] = useCEStates(() => ({ open: isOpen }));
return () => {
const { iconPosition = 'end', iconName, iconLibrary } = props;
const icon = renderElement('icon', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const AccordionGroup = defineSSRCustomElement({
});

useCEExpose(methods);
const [stateClass] = useCEStates(() => null, ns);
const [stateClass] = useCEStates(() => null);

return () => {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const Button = defineSSRCustomElement({
},
};

const [stateClass] = useCEStates(() => ({ holdShow }), ns);
const [stateClass] = useCEStates(() => ({ holdShow }));

let timer: ReturnType<typeof setInterval>,
countdownTxt = ref<string | null>();
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const Calendar = defineSSRCustomElement({

const { getShortWeekDays, getWeekFirstDay } = createDateLocaleMethods(lang);

const [stateClass] = useCEStates(() => ({ [pickingType.value]: 1 }), ns);
const [stateClass] = useCEStates(() => ({ [pickingType.value]: 1 }));
useCEExpose({ ...methods, ...expose }, toGetterDescriptors(state));

const getCellNodes = ({ value }: ComputedRef<UseDatePanelCells>, bodyClass?: string) => {
Expand Down
15 changes: 6 additions & 9 deletions packages/components/src/components/checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,12 @@ export const Checkbox = defineSSRCustomElement({

const type = computed(() => props.type || checkboxContext?.parent!.props.type);

const [stateClass] = useCEStates(
() => ({
card: type.value === 'card',
checked,
intermediate,
on: [checked, intermediate],
}),
ns,
);
const [stateClass] = useCEStates(() => ({
card: type.value === 'card',
checked,
intermediate,
on: [checked, intermediate],
}));

useExpose(refLikesToGetters({ disabled: () => editComputed.disabled }));
return () => {
Expand Down
9 changes: 3 additions & 6 deletions packages/components/src/components/checkbox/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ export const CheckboxGroup = defineSSRCustomElement({
});
const children = CheckboxCollector.parent({ extraProvide: { radioState } });

const [stateClass] = useCEStates(
() => ({
vertical: props.vertical,
}),
ns,
);
const [stateClass] = useCEStates(() => ({
vertical: props.vertical,
}));

const { render } = useOptions(props, 'checkbox');
return () => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const ColorPicker = defineSSRCustomElement({
},
};

const [stateClass] = useCEStates(() => pick(props, ['panelOnly']), ns);
const [stateClass] = useCEStates(() => pick(props, ['panelOnly']));

const triggers = ['click', 'focus'];
return () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createDefineElement } from 'utils';
import { customRendererProps } from './type';
import { isArray, isHTMLTemplateElement, isNode, isObject, runIfFn } from '@lun-web/utils';
import { generateWithTemplate } from './utils';
import { useCE } from 'hooks';
import { useCE } from '../../hooks/shadowDom';

const name = 'custom-renderer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const DatePicker = defineSSRCustomElement({

const getValue = createGetterForHasRawModel(valueModel);

const [stateClass] = useCEStates(() => null, ns);
const [stateClass] = useCEStates(() => null);
// TODO expose
return () => {
const { popoverProps, inputProps, multiple, ...rest } = props;
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/components/dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ export const Dialog = Object.assign(
noTopLayer: props.noTopLayer || !supportDialog,
confirm: props.isConfirm,
noMask: props.noMask,
}),
ns,
})
);

const [renderHeader] = useSlot('header', () => props.header);
Expand Down
11 changes: 4 additions & 7 deletions packages/components/src/components/form-item/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,10 @@ export const FormItem = defineSSRCustomElement({
),
};
});
const [stateClass, states] = useCEStates(
() => ({
required: formContext ? validateProps.value.required : props.value.required,
...layoutInfo?.value.itemState,
}),
ns,
);
const [stateClass, states] = useCEStates(() => ({
required: formContext ? validateProps.value.required : props.value.required,
...layoutInfo?.value.itemState,
}));

const [elementRef, errorActiveTarget] = useErrorTooltip(() => tips.tooltip, {
isDisabled: () => !tips.hasTooltip,
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const Form = defineSSRCustomElement({
extraProvide: provide,
});

const [stateClass] = useCEStates(() => ({}), ns);
const [stateClass] = useCEStates(() => ({}));

const renderErrorTooltip = provideErrorTooltip({
class: [ns.e('tooltip'), ns.m('error')],
Expand Down
19 changes: 8 additions & 11 deletions packages/components/src/components/input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,14 @@ export const Input = defineSSRCustomElement({
} else valueModel.value = null;
};

const [stateClass, states] = useCEStates(
() => ({
empty: isEmpty(valueModel.value) && !valueForMultiple.value,
multiple: props.multiple,
required: validateProps.value.required,
withPrepend: !prependEmpty.value,
withAppend: !appendEmpty.value,
withRenderer: !rendererEmpty.value,
}),
ns,
);
const [stateClass, states] = useCEStates(() => ({
empty: isEmpty(valueModel.value) && !valueForMultiple.value,
multiple: props.multiple,
required: validateProps.value.required,
withPrepend: !prependEmpty.value,
withAppend: !appendEmpty.value,
withRenderer: !rendererEmpty.value,
}));

const lengthInfo = computed(() => {
const valueLength = props.multiple ? valueForMultiple.value.length : String(valueModel.value ?? '').length;
Expand Down
22 changes: 13 additions & 9 deletions packages/components/src/components/mentions/Mentions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { computed, ref, watchEffect } from 'vue';
import { useSetupEdit, useMentions, VirtualElement, MentionSpan, MentionsTriggerParam, useSetupEvent } from '@lun-web/core';
import {
useSetupEdit,
useMentions,
VirtualElement,
MentionSpan,
MentionsTriggerParam,
useSetupEvent,
} from '@lun-web/core';
import { defineSSRCustomElement } from 'custom';
import { createDefineElement, renderElement } from 'utils';
import { useCEStates, useNamespace, useOptions, usePropsFromFormItem, useValueModel } from 'hooks';
Expand Down Expand Up @@ -108,14 +115,11 @@ export const Mentions = defineSSRCustomElement({
valueModel.value = null as any;
};

const [stateClass, states] = useCEStates(
() => ({
empty: isEmpty(valueModel.value),
required: validateProps.value.required,
withClearIcon: props.showClearIcon && editComputed.editable,
}),
ns,
);
const [stateClass, states] = useCEStates(() => ({
empty: isEmpty(valueModel.value),
required: validateProps.value.required,
withClearIcon: props.showClearIcon && editComputed.editable,
}));

const lengthInfo = computed(() => {
const { maxLength, showLengthInfo } = props;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/radio/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const Radio = defineSSRCustomElement({
start: button && (props.start || radioContext?.isStart),
end: button && (props.end || radioContext?.isEnd),
};
}, ns);
});

return () => {
const { labelPosition, noIndicator } = mergedProps;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/radio/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const RadioGroup = defineSSRCustomElement({

const { render } = useOptions(props, 'radio');

const [stateClass] = useCEStates(() => ({}), ns);
const [stateClass] = useCEStates(() => ({}));

// TODO arrow move to check prev/next active radio
return () => (
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/range/Range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export const Range = defineSSRCustomElement({
});
onUpdated(updateLabelSize);

const [stateClass] = useCEStates(() => null, ns);
const [stateClass] = useCEStates(() => null);

return () => {
const { value } = processedValues,
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const Select = defineSSRCustomElement({
popover: popoverRef,
}),
);
const [stateClass] = useCEStates(() => null, ns);
const [stateClass] = useCEStates(() => null);

const { render, loading, options, renderOptions, renderOption } = useOptions(props, 'select-option', {
groupOptionName: 'select-optgroup',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const SelectOptgroup = defineSSRCustomElement({
const ns = useNamespace(name, { parent: selectContext?.parent });
SelectOptgroupContext.provide();
const [_editComputed] = useSetupEdit();
const [stateClass] = useCEStates(() => null, ns);
const [stateClass] = useCEStates(() => null);

return () => (
<div part={compParts[0]} class={stateClass.value}>
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/select/SelectOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const SelectOption = defineSSRCustomElement({

const disabled = () => editComputed.disabled;
useExpose(refLikesToGetters({ hidden, selected, disabled })); // expose it to Select
const [stateClass] = useCEStates(() => ({ selected, active, underGroup: optgroup }), ns);
const [stateClass] = useCEStates(() => ({ selected, active, underGroup: optgroup }));

const handlers = {
onClick() {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Switch = defineSSRCustomElement({
},
};

const [stateClass] = useCEStates(() => ({ checked: checkedModel.value }), ns);
const [stateClass] = useCEStates(() => ({ checked: checkedModel.value }));

return () => {
const checked = checkedModel.value;
Expand Down
13 changes: 5 additions & 8 deletions packages/components/src/components/textarea/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,11 @@ export const Textarea = defineSSRCustomElement({

useCEExpose(methods, refLikeToDescriptors({ textarea: textareaRef }));

const [stateClass, states] = useCEStates(
() => ({
empty: isEmpty(valueModel.value),
required: validateProps.value.required,
withClearIcon: props.showClearIcon && editComputed.editable,
}),
ns,
);
const [stateClass, states] = useCEStates(() => ({
empty: isEmpty(valueModel.value),
required: validateProps.value.required,
withClearIcon: props.showClearIcon && editComputed.editable,
}));

const lengthInfo = computed(() => {
const { maxLength, showLengthInfo } = props;
Expand Down
11 changes: 4 additions & 7 deletions packages/components/src/components/tour/Tour.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,10 @@ export const Tour = defineSSRCustomElement({
}
});

const [stateClass] = useCEStates(
() => ({
[`placement-${placement.value}`]: 1,
[`side-${placementInfo.value[0]}`]: 1,
}),
ns,
);
const [stateClass] = useCEStates(() => ({
[`placement-${placement.value}`]: 1,
[`side-${placementInfo.value[0]}`]: 1,
}));
const nextStep = () => updateStep(1),
prevStep = () => updateStep(-1);

Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/components/tree/TreeItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ export const TreeItem = defineSSRCustomElement({
checked,
lineSelectable,
labelSelectable,
}),
ns,
})
);

const [renderLabel] = useSlot('label', () => props.label);
Expand Down
11 changes: 1 addition & 10 deletions packages/components/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getCurrentInstance } from 'vue';

export * from './shadowDom';
export * from './transition';
export * from './useBreakpoint';
Expand All @@ -10,11 +8,4 @@ export * from './useSlot';
export * from './useStyles';
export * from './useTooltip';
export * from './useValue';

// vue setup expose can only be used once. But we have manual expose in useNameSpace, need to expose multiple times.
export function useExpose(obj: Record<string, any>, properties?: PropertyDescriptorMap) {
const vm = getCurrentInstance()!;
if (!vm.exposed) vm.exposed = obj;
else Object.defineProperties(vm.exposed, Object.getOwnPropertyDescriptors(obj));
if (properties) Object.defineProperties(vm.exposed, properties);
}
export * from './vue';
9 changes: 5 additions & 4 deletions packages/components/src/hooks/shadowDom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
onMounted,
watchEffect,
} from 'vue';
import { useNamespace } from './useNameSpace';
import { useDefinedNameSpace } from './useNameSpace';
import { GlobalStaticConfig } from '../components/config/config.static';
import { VueElement } from 'custom';
import type { VueElement } from 'custom';

/** get current host custom element */
export const useCE = () => getCurrentInstance()!.ce! as VueElement;
Expand Down Expand Up @@ -44,9 +44,10 @@ export function useCEStates<
editable?: boolean | undefined;
interactive?: boolean | undefined;
},
>(statesGetter?: () => T, ns?: ReturnType<typeof useNamespace>) {
>(statesGetter?: () => T) {
let stop: ReturnType<typeof watchEffect>;
const editComputed = useEdit();
const editComputed = useEdit()!;
const ns = useDefinedNameSpace();
const states = objectComputed(() => ({
...statesGetter?.(),
...pick(editComputed, ['disabled', 'editable', 'interactive', 'loading', 'readonly']),
Expand Down
Loading

0 comments on commit 673726e

Please sign in to comment.