-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/components/forms/controls/TimePicker/TimePicker.module.scss
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,11 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of | ||
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
@use '../../../../styling/defs.scss' as bk; | ||
|
||
@layer baklava.components { | ||
.bk-timepicker { | ||
@include bk.component-base(bk-timepicker); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/components/forms/controls/TimePicker/TimePicker.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,44 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of | ||
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import * as React from 'react'; | ||
|
||
import { TimePicker } from './TimePicker.tsx'; | ||
|
||
|
||
type TimePickerArgs = React.ComponentProps<typeof TimePicker>; | ||
type Story = StoryObj<TimePickerArgs>; | ||
|
||
export default { | ||
component: TimePicker, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
}, | ||
args: {}, | ||
decorators: [ | ||
Story => <form onSubmit={event => { event.preventDefault(); }}><Story/></form>, | ||
], | ||
} satisfies Meta<TimePickerArgs>; | ||
|
||
export const TimePickerStory: Story = { | ||
name: 'Time Picker', | ||
render: (args) => { | ||
const [date, setDate] = React.useState<Date>(new Date()); | ||
|
||
return ( | ||
<div> | ||
<TimePicker date={date} onUpdate={setDate}/> | ||
<p> | ||
The selected time is: | ||
{date.getHours().toString().padStart(2, '0')}:{date.getMinutes().toString().padStart(2, '0')} | ||
</p> | ||
</div> | ||
); | ||
} | ||
}; |
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 @@ | ||
/* Copyright (c) Fortanix, Inc. | ||
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of | ||
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import * as React from 'react'; | ||
|
||
import { classNames as cx, type ClassNameArgument, type ComponentProps } from '../../../../util/componentUtil.ts'; | ||
|
||
import { Input } from '../Input/Input.tsx'; | ||
|
||
import cl from './TimePicker.module.scss'; | ||
|
||
|
||
export type TimePickerProps = ComponentProps<'input'> & { | ||
/** Whether this component should be unstyled. */ | ||
unstyled?: undefined | boolean, | ||
|
||
/** An optional class name to be appended to the class list. */ | ||
className?: ClassNameArgument, | ||
|
||
/** The date object to show / manipulate the time. */ | ||
date: Date, | ||
|
||
/** A callback function to update the time. */ | ||
onUpdate: (date: Date) => void, | ||
}; | ||
|
||
export const TimePicker = ({ unstyled = false, className, date, onUpdate, ...propsRest }: TimePickerProps) => { | ||
const hours = date.getHours().toString().padStart(2, '0'); | ||
const minutes = date.getMinutes().toString().padStart(2, '0'); | ||
const time = `${hours}:${minutes}`; | ||
|
||
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const newTimeString = e.target.value; | ||
const [newHours, newMinutes] = newTimeString.split(':'); | ||
let newDate = new Date(date); | ||
newDate.setHours(Number(newHours)); | ||
newDate.setMinutes(Number(newMinutes)); | ||
onUpdate(newDate); | ||
}; | ||
|
||
return ( | ||
<div className={cx( | ||
'bk', | ||
{ [cl['bk-timepicker']]: !unstyled }, | ||
className, | ||
)}> | ||
<Input type="time" value={time} onChange={onChange} {...propsRest} /> | ||
</div> | ||
); | ||
}; |