Skip to content

Commit

Permalink
refactor: 优化&修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fikyair committed Sep 5, 2021
1 parent 3ebd890 commit 11496df
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 52 deletions.
2 changes: 2 additions & 0 deletions src/components/InputDatePicker/README.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
可用性:从 DateView 切换到 MonthYearsView 的时候
需要焦点转移,反之也需要
5 changes: 3 additions & 2 deletions src/components/InputDatePicker/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export interface CalendarProps {
selectedDate: Date;
onSelectDate: (
e: MouseEvent<HTMLElement>,
date: Date | string | number
date: Date
) => void;
}

function Calendar(props: CalendarProps) {
const { selectedDate, onSelectDate } = props;
const [isDateView, setDateView] = useState(true);
const [isDateView, setDateView] = useState(false);
const calendarRef = useRef(null);

const today = new Date();
Expand All @@ -36,6 +36,7 @@ function Calendar(props: CalendarProps) {
const onSetDateView = setDateView.bind(null, true);

useEffect(() => {
// 处理 MonthYearView 展示后聚焦操作
if (calendarRef) {
calendarRef.current.focus();
}
Expand Down
8 changes: 0 additions & 8 deletions src/components/InputDatePicker/dateContext.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/InputDatePicker/datePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { actions } from "@storybook/addon-actions";
const BaseInputDatePicker = (props: InputDatePickerProps) => {
return (
<div style={{ width: 300 }}>
<InputDatePicker onChange={actions("onChange")} />
<InputDatePicker onChange={()=>actions('onChange')} />
</div>
);
};
Expand Down
8 changes: 5 additions & 3 deletions src/components/InputDatePicker/datePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ export const DatePicker: FC<DatePickerProps & CalendarProps> = (props) => {
return (
<table className={classNames(sc("wrapper"))}>
<thead className={classNames(sc("header"))}>
{dayNames.map((dayName, i) => (
<th key={i}>{exchangeDayNames(dayName)}</th>
))}
<tr>
{dayNames.map((dayName, i) => (
<th key={i}>{exchangeDayNames(dayName)}</th>
))}
</tr>
</thead>

<tbody className={classNames("weeks")}>
Expand Down
22 changes: 12 additions & 10 deletions src/components/InputDatePicker/dateView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {MouseEvent, Dispatch, SetStateAction } from "react";
import React, { MouseEvent, Dispatch, SetStateAction } from "react";
import { ViewLayout } from "./viewLayout";
import DatePicker, { CalendarType } from "./datePicker";
import { Button } from "../Button/button";
Expand Down Expand Up @@ -26,7 +26,7 @@ function DateView(props: DateViewProps & CalendarProps) {
onTitleClick,
selectedDate,
onSelectDate,
onClickToday
onClickToday,
} = props;

function incrementMonthIndex(increment: number) {
Expand Down Expand Up @@ -54,17 +54,19 @@ function DateView(props: DateViewProps & CalendarProps) {
header={{
leftElement: <Icon icon="arrow-left" onClick={goToPreviousMonth} />,
middleElement: (
<p>
<HeaderTitle
year={year}
monthIndex={monthIndex}
onTitleClick={onTitleClick}
/>
</p>
<HeaderTitle
year={year}
monthIndex={monthIndex}
onTitleClick={onTitleClick}
/>
),
rightElement: <Icon icon="arrow-right" onClick={goToNextMonth} />,
}}
footerElement={<Button btnType="ghost" onClick={onClickToday}> 今天 </Button>}
footerElement={
<Button btnType="ghost" onClick={onClickToday}>
今天
</Button>
}
></ViewLayout>
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/InputDatePicker/input.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useContext } from "react";
import React, { useContext, useState } from "react";
import { DateContext, IPickerContext } from "./dateManager";
import { Input } from "../inputs/input";

function InputComponent() {
const { value } = useContext<IPickerContext>(DateContext);
return <Input size="sm" value={value.textInput} />;

return <Input size="sm" value={value.textInput}/>;
}

export default InputComponent;
10 changes: 7 additions & 3 deletions src/components/InputDatePicker/inputDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ export const InputDatePicker: FC<InputDatePickerProps> = (props) => {
const [showPicker, setShowPicker] = useState(false);
const openPicker = setShowPicker.bind(null, true);
const closePicker = setShowPicker.bind(null, false);

function onFocus() {
openPicker();
}

function onBlur() {
closePicker();
}

// tabIndex 使 div 可以聚焦,从而可以使用 onFocus/onBlur
return (
<FocusManager onFocus={onFocus} onBlur={onBlur}>
<FocusManager onFocus={onFocus} onBlur={onBlur} tabIndex={0}>
<DateManager onChange={props.onChange}>
<InputComponent />
{showPicker && <Picker />}
{/* {showPicker && <Picker />} */}
{<Picker />}
</DateManager>
</FocusManager>
);
};
InputDatePicker.defaultProps = {};

export default InputDatePicker;
42 changes: 22 additions & 20 deletions src/components/InputDatePicker/monthPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ function MonthPicker(props: MonthPickerProps) {
const { selectedMonthIndex, onSelect } = props;
return (
<table className={sc("wrapper")}>
{months.map((row: MonthOfYear[], i: number) => (
<tr key={i}>
{row.map((month: MonthOfYear, j: number) => {
const isSelected = month.index === selectedMonthIndex;
return (
<td className={sc("cell")} key={j}>
<Button
className={classNames(sc("ghost"), {
[`${sc("is-selected")}`]: isSelected,
})}
btnType="ghost"
onClick={() => onSelect && onSelect(month.index)}
>
{month.name}
</Button>
</td>
);
})}
</tr>
))}
<tbody>
{months.map((row: MonthOfYear[], i: number) => (
<tr key={i}>
{row.map((month: MonthOfYear, j: number) => {
const isSelected = month.index === selectedMonthIndex;
return (
<td className={sc("cell")} key={j}>
<Button
className={classNames(sc("ghost"), {
[`${sc("is-selected")}`]: isSelected,
})}
btnType="ghost"
onClick={() => onSelect && onSelect(month.index)}
>
{month.name}
</Button>
</td>
);
})}
</tr>
))}
</tbody>
</table>
);
}
Expand Down
19 changes: 17 additions & 2 deletions src/components/InputDatePicker/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@

&-header {
text-align: center;

tr>th {
padding-bottom: $picker-padding;
}

&::after {
content: "";
width: 100%;
border-bottom: 0.1rem solid #edeeef;
position: absolute;
left: 0;
top: 2.2rem;
}
}

&-weeks {}
&-weeks {
cursor: pointer;
}

&-weeks-item {
text-align: center;
Expand Down Expand Up @@ -55,7 +70,6 @@
&-container {
display: flex;
flex-direction: column;

}

&-header {
Expand Down Expand Up @@ -110,6 +124,7 @@
&-wrapper {
display: flex;
align-items: center;

>span {
margin-right: $picker-padding;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/InputDatePicker/yearPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function YearPicker(props: YearPickerProps) {
<div>
<Select defaultValue={defaultValue} style={{ width: 100 }} onChange={(value) => onSelectYear(Number(value))}>
{years.map((year: number, i: number) => (
<Option value={year.toString()} >{year}</Option>
<Option value={year.toString()} key={i}>{year}</Option>
))}
</Select>
{/* <ul>
Expand Down
4 changes: 4 additions & 0 deletions src/components/icons/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
.icon-#{$key} {
color: $val;
}
}

.chocolate-icon {
cursor: pointer;
}

0 comments on commit 11496df

Please sign in to comment.