Skip to content

refactor: switch #6236

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

Merged
merged 2 commits into from
Feb 7, 2023
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
2 changes: 1 addition & 1 deletion components/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import './carousel/style';
// import './message/style';
// import './spin/style';
import './select/style';
import './switch/style';
// import './switch/style';
import './auto-complete/style';
// import './affix/style';
import './cascader/style';
Expand Down
86 changes: 46 additions & 40 deletions components/switch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useConfigInject from '../config-provider/hooks/useConfigInject';
import { useInjectFormItemContext } from '../form/FormItemContext';
import omit from '../_util/omit';
import type { FocusEventHandler } from '../_util/EventInterface';

import useStyle from './style';
export const SwitchSizes = tuple('small', 'default');
type CheckedType = boolean | string | number;
export const switchProps = () => ({
Expand Down Expand Up @@ -89,6 +89,7 @@ const Switch = defineComponent({
);

const { prefixCls, direction, size } = useConfigInject('switch', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
const refSwitchNode = ref();
const focus = () => {
refSwitchNode.value?.focus();
Expand Down Expand Up @@ -148,47 +149,52 @@ const Switch = defineComponent({
[`${prefixCls.value}-disabled`]: props.disabled,
[prefixCls.value]: true,
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
[hashId.value]: true,
}));

return () => (
<Wave insertExtraNode>
<button
{...omit(props, [
'prefixCls',
'checkedChildren',
'unCheckedChildren',
'checked',
'autofocus',
'checkedValue',
'unCheckedValue',
'id',
'onChange',
'onUpdate:checked',
])}
{...attrs}
id={props.id ?? formItemContext.id.value}
onKeydown={handleKeyDown}
onClick={handleClick}
onBlur={handleBlur}
onMouseup={handleMouseUp}
type="button"
role="switch"
aria-checked={checked.value as any}
disabled={props.disabled || props.loading}
class={[attrs.class, classNames.value]}
ref={refSwitchNode}
>
<div class={`${prefixCls.value}-handle`}>
{props.loading ? <LoadingOutlined class={`${prefixCls.value}-loading-icon`} /> : null}
</div>
<span class={`${prefixCls.value}-inner`}>
{checkedStatus.value
? getPropsSlot(slots, props, 'checkedChildren')
: getPropsSlot(slots, props, 'unCheckedChildren')}
</span>
</button>
</Wave>
);
return () =>
wrapSSR(
<Wave insertExtraNode>
<button
{...omit(props, [
'prefixCls',
'checkedChildren',
'unCheckedChildren',
'checked',
'autofocus',
'checkedValue',
'unCheckedValue',
'id',
'onChange',
'onUpdate:checked',
])}
{...attrs}
id={props.id ?? formItemContext.id.value}
onKeydown={handleKeyDown}
onClick={handleClick}
onBlur={handleBlur}
onMouseup={handleMouseUp}
type="button"
role="switch"
aria-checked={checked.value as any}
disabled={props.disabled || props.loading}
class={[attrs.class, classNames.value]}
ref={refSwitchNode}
>
<div class={`${prefixCls.value}-handle`}>
{props.loading ? <LoadingOutlined class={`${prefixCls.value}-loading-icon`} /> : null}
</div>
<span class={`${prefixCls.value}-inner`}>
<span class={`${prefixCls.value}-inner-checked`}>
{getPropsSlot(slots, props, 'checkedChildren')}
</span>
<span class={`${prefixCls.value}-inner-unchecked`}>
{getPropsSlot(slots, props, 'unCheckedChildren')}
</span>
</span>
</button>
</Wave>,
);
},
});

Expand Down
155 changes: 0 additions & 155 deletions components/switch/style/index.less

This file was deleted.

Loading