-
Notifications
You must be signed in to change notification settings - Fork 44
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
1 parent
0eaeed2
commit b13b187
Showing
10 changed files
with
221 additions
and
110 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useArgs } from "@storybook/preview-api"; | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
import { useCallback } from "react"; | ||
|
||
import { styled } from "@reearth/services/theme"; | ||
|
||
import Toggle, { Props } from "."; | ||
|
||
const meta: Meta<typeof Toggle> = { | ||
component: Toggle, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof Toggle>; | ||
|
||
export const Default: Story = (args: Props) => { | ||
const [_, updateArgs] = useArgs(); | ||
|
||
const handleChange = useCallback( | ||
(checked: boolean) => updateArgs({ checked: !checked }), | ||
[updateArgs], | ||
); | ||
|
||
return ( | ||
<Wrapper> | ||
<div> | ||
<Toggle {...args} onChange={handleChange} /> | ||
</div> | ||
<div> | ||
<Toggle {...args} checked={!args.checked} onChange={handleChange} /> | ||
</div> | ||
<div> | ||
<Toggle {...args} checked={!args.checked} disabled={true} onChange={handleChange} /> | ||
</div> | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 10%; | ||
margin-left: 2rem; | ||
margin-top: 2rem; | ||
height: 300px; | ||
`; | ||
|
||
Default.args = { | ||
checked: true, | ||
disabled: false, | ||
onChange: () => console.log("clicked"), | ||
}; |
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,57 @@ | ||
import { useCallback } from "react"; | ||
|
||
import { styled } from "@reearth/services/theme"; | ||
|
||
export type Props = { | ||
checked: boolean; | ||
onChange: (checked: boolean) => void; | ||
disabled?: boolean; | ||
}; | ||
|
||
const Toggle: React.FC<Props> = ({ checked, onChange, disabled = false }) => { | ||
const handleClick = useCallback( | ||
() => !disabled && onChange(checked), | ||
[checked, onChange, disabled], | ||
); | ||
|
||
return ( | ||
<Wrapper> | ||
<Switch checked={checked} disabled={disabled} onClick={handleClick}> | ||
<TopSlider checked={checked} /> | ||
</Switch> | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
export default Toggle; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
`; | ||
|
||
const Switch = styled.label<{ | ||
checked: boolean; | ||
disabled: boolean; | ||
}>` | ||
cursor: ${({ disabled }) => (!disabled ? "pointer" : "not-allowed")}; | ||
width: 24px; | ||
height: 14px; | ||
background: ${({ checked, theme }) => (checked ? theme.select.main : theme.secondary.main)}; | ||
border: 1px solid ${({ checked, theme }) => (checked ? theme.select.main : theme.secondary.main)}; | ||
border-radius: 12px; | ||
opacity: ${({ disabled }) => (!disabled ? 1 : 0.5)}; | ||
transition: 0.4s; | ||
`; | ||
|
||
const TopSlider = styled.div<{ | ||
checked: boolean; | ||
}>` | ||
width: 14px; | ||
height: 14px; | ||
background: ${({ theme }) => theme.content.withBackground}; | ||
transition: 0.4s; | ||
border-radius: 50%; | ||
transform: ${({ checked }) => checked && "translateX(10px)"}; | ||
`; |
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 was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
web/src/beta/components/fields/ToggleField/index.stories.tsx
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,67 @@ | ||
import { useArgs } from "@storybook/preview-api"; | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
import { useCallback } from "react"; | ||
|
||
import { styled } from "@reearth/services/theme"; | ||
|
||
import ToggleField, { Props } from "."; | ||
|
||
const meta: Meta<typeof ToggleField> = { | ||
component: ToggleField, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof ToggleField>; | ||
|
||
export const Default: Story = (args: Props) => { | ||
const [_, updateArgs] = useArgs(); | ||
|
||
const handleChange = useCallback( | ||
(checked: boolean) => updateArgs({ checked: !checked }), | ||
[updateArgs], | ||
); | ||
|
||
return ( | ||
<Wrapper> | ||
<div> | ||
<ToggleField {...args} onChange={handleChange} /> | ||
</div> | ||
<div> | ||
<ToggleField | ||
{...args} | ||
name="Reverse Toggle" | ||
description={"Invesere of above"} | ||
checked={!args.checked} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div> | ||
<ToggleField | ||
{...args} | ||
name="Disabled" | ||
description="Disabled field" | ||
disabled={true} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 10%; | ||
margin-left: 2rem; | ||
margin-top: 2rem; | ||
height: 300px; | ||
`; | ||
|
||
Default.args = { | ||
name: "Toggle Field", | ||
description: "Sample description", | ||
checked: true, | ||
disabled: false, | ||
onChange: () => console.log("clicked"), | ||
}; |
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,17 @@ | ||
import Property from "@reearth/beta/components/fields"; | ||
import Toggle, { Props as ToggleProps } from "@reearth/beta/components/Toggle"; | ||
|
||
export type Props = { | ||
name?: string; | ||
description?: string; | ||
} & ToggleProps; | ||
|
||
const ToggleField: React.FC<Props> = ({ name, description, ...args }: Props) => { | ||
return ( | ||
<Property name={name} description={description}> | ||
<Toggle {...args} /> | ||
</Property> | ||
); | ||
}; | ||
|
||
export default ToggleField; |
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
Oops, something went wrong.