-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix * fixed * screen * fixed deps * optimized
- Loading branch information
1 parent
9cdc596
commit 1022a61
Showing
8 changed files
with
181 additions
and
135 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
} | ||
|
||
.days { | ||
width: min-content; | ||
margin-bottom: 15px; | ||
display: flex; | ||
align-items: center; | ||
|
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,138 +1,141 @@ | ||
import * as React from "react"; | ||
import React, { useState, useMemo, FC } from "react"; | ||
import { Input } from "@skbkontur/react-ui/components/Input"; | ||
import { Radio } from "@skbkontur/react-ui/components/Radio"; | ||
import { Checkbox } from "@skbkontur/react-ui/components/Checkbox"; | ||
import { Schedule } from "../../Domain/Schedule"; | ||
import HelpTooltip from "../HelpTooltip/HelpTooltip"; | ||
import classNames from "classnames/bind"; | ||
import { Schedule, defaultSchedule } from "../../Domain/Schedule"; | ||
|
||
import styles from "./ScheduleEdit.less"; | ||
|
||
const cn = classNames.bind(styles); | ||
|
||
type Props = { | ||
schedule: Schedule; | ||
interface IProps { | ||
schedule?: Schedule; | ||
error?: boolean; | ||
onBlur?: React.FocusEventHandler<HTMLDivElement>; | ||
onChange: (schedule: Schedule) => void; | ||
}; | ||
} | ||
|
||
type State = { | ||
allDay: boolean; | ||
}; | ||
const ScheduleEdit: FC<IProps> = React.forwardRef<HTMLDivElement, IProps>(function ScheduleEdit( | ||
{ schedule, error, onChange, onBlur }, | ||
validationRef | ||
) { | ||
const defaultSched = useMemo(() => defaultSchedule(schedule), [schedule]); | ||
const [allDay, setAllDay] = useState( | ||
defaultSched.startOffset === 0 && defaultSched.endOffset === 1439 | ||
); | ||
|
||
export default class ScheduleEdit extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
const { schedule } = props; | ||
const { startOffset, endOffset } = schedule; | ||
this.state = { | ||
allDay: startOffset === 0 && endOffset === 1439, | ||
}; | ||
} | ||
const time = useMemo(() => { | ||
const start = formatTime(defaultSched.startOffset); | ||
const end = formatTime(defaultSched.endOffset); | ||
return { start, end }; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<div onFocus={onBlur} ref={validationRef} className={cn("days")}> | ||
{defaultSched.days.map(({ name, enabled }, i) => ( | ||
<Checkbox | ||
error={error} | ||
key={name} | ||
checked={enabled} | ||
onValueChange={(checked) => | ||
onChange({ | ||
...defaultSched, | ||
days: [ | ||
...defaultSched.days.slice(0, i), | ||
{ name, enabled: checked }, | ||
...defaultSched.days.slice(i + 1), | ||
], | ||
}) | ||
} | ||
> | ||
{name} | ||
</Checkbox> | ||
))} | ||
</div> | ||
|
||
static formatTime(time: number): string { | ||
const HOUR_IN_DAY = 24; | ||
const MIN_IN_HOUR = 60; | ||
const hours = | ||
Math.floor(time / MIN_IN_HOUR) < HOUR_IN_DAY ? Math.floor(time / MIN_IN_HOUR) : 0; | ||
const minutes = time % MIN_IN_HOUR < MIN_IN_HOUR ? time % MIN_IN_HOUR : 0; | ||
return `${hours > 9 ? hours : `0${hours}`}:${minutes > 9 ? minutes : `0${minutes}`}`; | ||
} | ||
<div className={cn("group")}> | ||
<span className={cn("radio")}> | ||
<Radio | ||
checked={allDay} | ||
onValueChange={() => { | ||
onChange({ | ||
...defaultSched, | ||
startOffset: 0, | ||
endOffset: 1439, | ||
}); | ||
setAllDay(true); | ||
}} | ||
value="all_day" | ||
> | ||
All day | ||
</Radio> | ||
</span> | ||
<span className={cn("radio")}> | ||
<Radio | ||
checked={!allDay} | ||
value="specific_interval" | ||
onValueChange={() => setAllDay(false)} | ||
> | ||
At specific interval | ||
</Radio> | ||
<Input | ||
value={time.start} | ||
width={60} | ||
mask="99:99" | ||
disabled={allDay} | ||
onValueChange={(value) => | ||
onChange({ | ||
...defaultSched, | ||
startOffset: parseTime(value), | ||
}) | ||
} | ||
/> | ||
<span>—</span> | ||
<Input | ||
value={time.end} | ||
width={60} | ||
mask="99:99" | ||
disabled={allDay} | ||
onValueChange={(value) => | ||
onChange({ | ||
...defaultSched, | ||
endOffset: parseTime(value), | ||
}) | ||
} | ||
/> | ||
<HelpTooltip> | ||
<div className={cn("time-range-description-title")}> | ||
Either negative and positive intervals are allowed. | ||
</div> | ||
<div> | ||
For example: 23:00 - 06:00 specifies interval between 23:00 <br /> | ||
of the current day to the 06:00 of the next day. | ||
</div> | ||
</HelpTooltip> | ||
</span> | ||
</div> | ||
</> | ||
); | ||
}); | ||
|
||
static parseTime(time: string): number { | ||
const HOUR_IN_DAY = 24; | ||
const MIN_IN_HOUR = 60; | ||
const [hours, minutes] = time.split(":"); | ||
const parsedHours = parseInt(hours, 10) < HOUR_IN_DAY ? parseInt(hours, 10) : 0; | ||
const parsedMinutes = parseInt(minutes, 10) < MIN_IN_HOUR ? parseInt(minutes, 10) : 0; | ||
return parsedHours * MIN_IN_HOUR + parsedMinutes; | ||
} | ||
const formatTime = (time: number): string => { | ||
const HOUR_IN_DAY = 24; | ||
const MIN_IN_HOUR = 60; | ||
const hours = Math.floor(time / MIN_IN_HOUR) < HOUR_IN_DAY ? Math.floor(time / MIN_IN_HOUR) : 0; | ||
const minutes = time % MIN_IN_HOUR < MIN_IN_HOUR ? time % MIN_IN_HOUR : 0; | ||
return `${hours > 9 ? hours : `0${hours}`}:${minutes > 9 ? minutes : `0${minutes}`}`; | ||
}; | ||
|
||
render(): React.ReactNode { | ||
const { allDay } = this.state; | ||
const { schedule, onChange } = this.props; | ||
const { days, startOffset, endOffset } = schedule; | ||
const parseTime = (time: string): number => { | ||
const HOUR_IN_DAY = 24; | ||
const MIN_IN_HOUR = 60; | ||
const [hours, minutes] = time.split(":"); | ||
const parsedHours = parseInt(hours, 10) < HOUR_IN_DAY ? parseInt(hours, 10) : 0; | ||
const parsedMinutes = parseInt(minutes, 10) < MIN_IN_HOUR ? parseInt(minutes, 10) : 0; | ||
return parsedHours * MIN_IN_HOUR + parsedMinutes; | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div className={cn("days")}> | ||
{days.map(({ name, enabled }, i) => ( | ||
<Checkbox | ||
key={name} | ||
checked={enabled} | ||
onValueChange={(checked) => | ||
onChange({ | ||
...schedule, | ||
days: [ | ||
...days.slice(0, i), | ||
{ name, enabled: checked }, | ||
...days.slice(i + 1), | ||
], | ||
}) | ||
} | ||
> | ||
{name} | ||
</Checkbox> | ||
))} | ||
</div> | ||
<div className={cn("group")}> | ||
<span className={cn("radio")}> | ||
<Radio | ||
checked={allDay} | ||
onValueChange={() => { | ||
onChange({ | ||
...schedule, | ||
startOffset: 0, | ||
endOffset: 1439, | ||
}); | ||
this.setState({ allDay: true }); | ||
}} | ||
value="all_day" | ||
> | ||
All day | ||
</Radio> | ||
</span> | ||
<span className={cn("radio")}> | ||
<Radio | ||
checked={!allDay} | ||
value="specific_interval" | ||
onValueChange={() => this.setState({ allDay: false })} | ||
> | ||
At specific interval | ||
</Radio> | ||
<Input | ||
value={ScheduleEdit.formatTime(startOffset)} | ||
width={60} | ||
mask="99:99" | ||
disabled={allDay} | ||
onValueChange={(value) => | ||
onChange({ | ||
...schedule, | ||
startOffset: ScheduleEdit.parseTime(value), | ||
}) | ||
} | ||
/> | ||
<span>—</span> | ||
<Input | ||
value={ScheduleEdit.formatTime(endOffset)} | ||
width={60} | ||
mask="99:99" | ||
disabled={allDay} | ||
onValueChange={(value) => | ||
onChange({ ...schedule, endOffset: ScheduleEdit.parseTime(value) }) | ||
} | ||
/> | ||
<HelpTooltip> | ||
<div className={cn("time-range-description-title")}> | ||
Either negative and positive intervals are allowed. | ||
</div> | ||
<div> | ||
For example: 23:00 - 06:00 specifies interval between 23:00 <br /> | ||
of the current day to the 06:00 of the next day. | ||
</div> | ||
</HelpTooltip> | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
export default ScheduleEdit; |
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
Oops, something went wrong.