-
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.
chore(web): add date time field component (#740)
Co-authored-by: nina992 <[email protected]>
- Loading branch information
Showing
5 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
web/src/beta/components/fields/DateTimeField/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,16 @@ | ||
import { action } from "@storybook/addon-actions"; | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import DateTimeField from "."; | ||
|
||
const meta: Meta<typeof DateTimeField> = { | ||
component: DateTimeField, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof DateTimeField>; | ||
|
||
export const DateTimeFieldInput: Story = { | ||
render: () => <DateTimeField name="Date Time Field" onChange={action("onchange")} />, | ||
}; |
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,63 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
import { styled } from "@reearth/services/theme"; | ||
|
||
import Property from ".."; | ||
import TextInput from "../common/TextInput"; | ||
|
||
export type Props = { | ||
name?: string; | ||
description?: string; | ||
value?: string; | ||
onChange?: (value?: string | undefined) => void; | ||
}; | ||
|
||
const DateTimeField: React.FC<Props> = ({ name, description, value, onChange }) => { | ||
const [time, setTime] = useState<string>(); | ||
const [date, setDate] = useState<string>(); | ||
|
||
const handleTimeChange = useCallback( | ||
(newValue: string | undefined) => { | ||
if (newValue === undefined) return; | ||
|
||
setTime(newValue); | ||
onChange?.(date + " " + newValue); | ||
}, | ||
[date, onChange], | ||
); | ||
|
||
const handleDateChange = useCallback( | ||
(newValue: string | undefined) => { | ||
if (newValue === undefined) return; | ||
|
||
setDate(newValue); | ||
onChange?.(newValue + " " + time); | ||
}, | ||
[time, onChange], | ||
); | ||
|
||
useEffect(() => { | ||
if (value) { | ||
const [dateString, timeString] = value.split(" "); | ||
setTime(timeString); | ||
setDate(dateString); | ||
} | ||
}, [value]); | ||
|
||
return ( | ||
<Property name={name} description={description}> | ||
<Wrapper> | ||
<TextInput type="date" value={value} onChange={handleDateChange} /> | ||
<TextInput type="time" value={value} onChange={handleTimeChange} /> | ||
</Wrapper> | ||
</Property> | ||
); | ||
}; | ||
|
||
export default DateTimeField; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
align-items: stretch; | ||
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
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