-
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.
Merge branch 'main' of https://github.com/reearth/reearth into refact…
…or(web)/refactor-textInput-component
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
web/src/beta/components/fields/LocationField/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,20 @@ | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import LocationField from "."; | ||
|
||
const meta: Meta<typeof LocationField> = { | ||
component: LocationField, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof LocationField>; | ||
|
||
export const Sample: Story = { | ||
args: { | ||
name: "Location Field", | ||
description: "Location field description", | ||
value: { lat: 87.43, lng: 107.53 }, | ||
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,56 @@ | ||
import { useCallback, useState } from "react"; | ||
|
||
import { LatLng } from "@reearth/beta/utils/value"; | ||
import { styled } from "@reearth/services/theme"; | ||
|
||
import Property from ".."; | ||
import NumberInput from "../common/NumberInput"; | ||
|
||
type Props = { | ||
name?: string; | ||
description?: string; | ||
value?: LatLng; | ||
onChange?: (location: LatLng) => void; | ||
}; | ||
|
||
const LocationField: React.FC<Props> = ({ name, description, value, onChange }) => { | ||
const [location, setLocation] = useState<LatLng>(value || { lat: 0, lng: 0 }); | ||
|
||
const handleChange = useCallback( | ||
(coordination: string, newValue: number | undefined) => { | ||
if (newValue === undefined) return; | ||
|
||
setLocation(prevLocation => ({ | ||
...prevLocation, | ||
[coordination === "Latitude" ? "lat" : "lng"]: newValue, | ||
})); | ||
onChange?.(location); | ||
}, | ||
[location, onChange], | ||
); | ||
|
||
return ( | ||
<Property name={name} description={description}> | ||
<Wrapper> | ||
<NumberInput | ||
value={location.lat} | ||
inputDescription="Latitude" | ||
onChange={newValue => handleChange("Latitude", newValue)} | ||
/> | ||
<NumberInput | ||
value={location.lng} | ||
inputDescription="Longtitude" | ||
onChange={newValue => handleChange("Longtitude", newValue)} | ||
/> | ||
</Wrapper> | ||
</Property> | ||
); | ||
}; | ||
|
||
export default LocationField; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
align-items: flex-start; | ||
gap: 4px; | ||
`; |
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