-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1360151219
committed
Aug 10, 2023
1 parent
3e7627e
commit e45f4ab
Showing
10 changed files
with
157 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { Switch } from 'pivot-design'; | ||
import React, { useState } from 'react'; | ||
import React from 'react'; | ||
|
||
const App: React.FC = () => { | ||
return <Switch />; | ||
return ( | ||
<> | ||
<Switch /> | ||
<Switch defaultValue={true} /> | ||
</> | ||
); | ||
}; | ||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Switch } from 'pivot-design'; | ||
import React, { useState } from 'react'; | ||
|
||
const App: React.FC = () => { | ||
const [value, setValue] = useState(true); | ||
return ( | ||
<> | ||
<Switch value={value} onChange={(checked) => setValue(checked)} /> | ||
</> | ||
); | ||
}; | ||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,38 @@ | ||
@import '../constants.scss'; | ||
|
||
.#{$prefix}-switch { | ||
color: black; | ||
position: relative; | ||
width: 42px; | ||
height: 24px; | ||
border-radius: 12px; | ||
background-color: var(--semi-color-fill-2); | ||
transition: background 200ms ease-in; | ||
|
||
&.checked { | ||
background-color: var(--semi-color-success); | ||
.#{$prefix}-switch-pole { | ||
transform: translateX(20px); | ||
} | ||
} | ||
|
||
&-pole { | ||
position: absolute; | ||
top: 2px; | ||
left: 0; | ||
width: 20px; | ||
height: 20px; | ||
border-radius: 50%; | ||
transform: translateX(2px); | ||
background-color: var(--semi-color-white); | ||
transition: transform 200ms ease-in; | ||
} | ||
&-input { | ||
position: absolute; | ||
left: 0; | ||
top: 0; | ||
bottom: 0; | ||
right: 0; | ||
opacity: 0; | ||
z-index: 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,31 @@ | ||
import classnames from 'classnames'; | ||
import { prefix } from '../constants'; | ||
import './index.scss'; | ||
function Switch(props: any) { | ||
return <div className={`${prefix}-switch`}>switch</div>; | ||
import useControlled from '../hooks/useControlled'; | ||
|
||
interface SwitchProps { | ||
value?: boolean; | ||
defaultValue?: boolean; | ||
onChange?: (e: boolean) => void; | ||
} | ||
|
||
function Switch(props: SwitchProps) { | ||
const [value, setValue] = useControlled(props); | ||
|
||
return ( | ||
<div | ||
className={classnames(`${prefix}-switch`, { | ||
checked: value, | ||
})} | ||
> | ||
<div className={classnames(`${prefix}-switch-pole`)} /> | ||
<input | ||
type="checkbox" | ||
className={classnames(`${prefix}-switch-input`)} | ||
defaultChecked={value} | ||
onChange={(e) => setValue(e.target.checked)} | ||
/> | ||
</div> | ||
); | ||
} | ||
export default Switch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint-disable no-redeclare */ | ||
import { useCallback, useState, SetStateAction } from 'react'; | ||
|
||
export interface Options<T> { | ||
defaultValue?: T; | ||
defaultValuePropName?: string; | ||
valuePropName?: string; | ||
changeName?: string; | ||
} | ||
|
||
export interface StandardProps<T> { | ||
value?: T; | ||
defaultValue?: T; | ||
onChange?: (val: T) => void; | ||
} | ||
|
||
type Props = Record<string, any>; | ||
|
||
function useControlled<T = any>(props: StandardProps<T>): [T, (v: SetStateAction<T>) => void]; | ||
function useControlled<T = any>( | ||
props?: StandardProps<T>, | ||
options?: Options<T> | ||
): [T, (v: SetStateAction<T>, ...args: any[]) => void]; | ||
|
||
function useControlled<T = any>(props: Props = {}, options: Options<T> = {}) { | ||
const { | ||
defaultValue: defaultOptionsValue, | ||
defaultValuePropName = 'defaultValue', | ||
valuePropName = 'value', | ||
changeName = 'onChange', | ||
} = options; | ||
|
||
// 获取外部传入的value,判断是否受控 | ||
const value = props[valuePropName] as T; | ||
const defaultPropsValue = props[defaultValuePropName] ?? defaultOptionsValue; | ||
const propsOnChange = props[changeName]; | ||
const isControlled = Object.hasOwn(props, valuePropName); | ||
|
||
const [state, setState] = useState(isControlled ? value : defaultPropsValue); | ||
|
||
const setValue = useCallback((v: T, ...args: any[]) => { | ||
if (isControlled) { | ||
propsOnChange(v, args); | ||
} | ||
setState(v); | ||
}, []); | ||
|
||
return [state, setValue]; | ||
} | ||
|
||
export default useControlled; |