Skip to content

Commit

Permalink
Feat disabled prop (#1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
takurinton committed Aug 21, 2023
1 parent 3d98e32 commit d2999f4
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-ravens-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ingred-ui": patch
---

feat disabled prop
7 changes: 7 additions & 0 deletions src/components/DateField/DateField/DateField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ export const Japanese: StoryObj<DateFieldProps> = {
format: "YYYY月MM月DD日",
},
};

export const Disable: StoryObj<DateFieldProps> = {
...Example,
args: {
disabled: true,
},
};
14 changes: 10 additions & 4 deletions src/components/DateField/DateField/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ import React, { forwardRef, memo } from "react";
import { Icon, Input } from "../..";
import { useDateField } from "../useDateField";
import { useMergeRefs } from "../../../hooks/useMergeRefs";
import { CalendarIcon, InputContainer } from "../styled";
import { CalendarIcon, InputContainer } from "./styled";
import { DateFieldProps } from "../types";

const DateField = forwardRef<HTMLInputElement, DateFieldProps>(
function DateField({ onClick, ...rest }, propRef) {
function DateField({ disabled = false, onClick, ...rest }, propRef) {
const { ref: inputRef, ...props } = useDateField({ ...rest });
const ref = useMergeRefs<HTMLInputElement>(propRef, inputRef);

return (
<InputContainer>
<Input ref={ref} readOnly style={{ border: "none" }} {...props} />
<InputContainer disabled={disabled}>
<Input
ref={ref}
readOnly
disabled={disabled}
style={{ border: "none" }}
{...props}
/>
<CalendarIcon onClick={onClick}>
<Icon name="date_range" />
</CalendarIcon>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from "styled-components";

export const InputContainer = styled.div`
export const InputContainer = styled.div<{ disabled: boolean }>`
display: flex;
align-items: center;
width: 150px;
Expand All @@ -25,13 +25,11 @@ export const InputContainer = styled.div`
&::-ms-input-placeholder {
color: ${({ theme }) => theme.palette.text.hint};
}
&:disabled {
color: ${({ theme }) => theme.palette.text.disabled};
border-color: ${({ theme }) => theme.palette.divider};
box-shadow: "none";
background-color: ${({ theme }) => theme.palette.gray.light};
cursor: not-allowed;
}
color: ${({ theme, disabled }) =>
disabled ? theme.palette.text.disabled : theme.palette.black};
background-color: ${({ theme, disabled }) =>
disabled ? theme.palette.gray.light : "transparent"};
cursor: ${({ disabled }) => (disabled ? "not-allowed" : "text")};
`;

export const CalendarIcon = styled.button`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ export const Japanese: StoryObj<DateRangeFieldProps> = {
format: "YYYY年MM月DD日",
},
};

export const Disable: StoryObj<DateRangeFieldProps> = {
...Example,
args: {
disabled: true,
},
};
11 changes: 9 additions & 2 deletions src/components/DateField/DateRangeField/DateRangeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ type Range = {
export type DateRangeFieldProps = {
date: Range;
format?: string;
/**
* 入力を無効にする
* @default false
*/
disabled?: boolean;
onClickCalendarIcon?: () => void;
onDatesChange?: (date: Range) => void;
};

const DateRangeField = forwardRef<HTMLInputElement, DateRangeFieldProps>(
function DateRangeField(
{ format = "YYYY-MM-DD", onClickCalendarIcon, ...rest },
{ format = "YYYY-MM-DD", disabled = false, onClickCalendarIcon, ...rest },
propRef,
) {
const width = useMemo(() => format.length * 12, [format]);
Expand Down Expand Up @@ -61,11 +66,12 @@ const DateRangeField = forwardRef<HTMLInputElement, DateRangeFieldProps>(
const endRef = useMergeRefs<HTMLInputElement>(propRef, endInputRef);

return (
<InputContainer>
<InputContainer disabled={disabled}>
<>
<Input
ref={startRef}
readOnly
disabled={disabled}
width={width}
style={{ border: "none", textAlign: "center" }}
{...startProps}
Expand All @@ -74,6 +80,7 @@ const DateRangeField = forwardRef<HTMLInputElement, DateRangeFieldProps>(
<Input
ref={endRef}
readOnly
disabled={disabled}
width={width}
style={{ border: "none", textAlign: "center" }}
{...endProps}
Expand Down
14 changes: 6 additions & 8 deletions src/components/DateField/DateRangeField/styled.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from "styled-components";

export const InputContainer = styled.div`
export const InputContainer = styled.div<{ disabled: boolean }>`
display: flex;
align-items: center;
width: fit-content;
Expand All @@ -25,13 +25,11 @@ export const InputContainer = styled.div`
&::-ms-input-placeholder {
color: ${({ theme }) => theme.palette.text.hint};
}
&:disabled {
color: ${({ theme }) => theme.palette.text.disabled};
border-color: ${({ theme }) => theme.palette.divider};
box-shadow: "none";
background-color: ${({ theme }) => theme.palette.gray.light};
cursor: not-allowed;
}
color: ${({ theme, disabled }) =>
disabled ? theme.palette.text.disabled : theme.palette.black};
background-color: ${({ theme, disabled }) =>
disabled ? theme.palette.gray.light : "transparent"};
cursor: ${({ disabled }) => (disabled ? "not-allowed" : "text")};
`;

export const CalendarIcon = styled.button`
Expand Down
5 changes: 5 additions & 0 deletions src/components/DateField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export type DateFieldProps = {
* @default YYYY-MM-DD
*/
format?: string;
/**
* 入力を無効にする
* @default false
*/
disabled?: boolean;
/**
* 日付が変更されたときに呼ばれる関数
*/
Expand Down
7 changes: 7 additions & 0 deletions src/components/NewDatePicker/NewDatePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export const WithActions: StoryObj<NewDatePickerProps> = {
},
};

export const Disabled: StoryObj<NewDatePickerProps> = {
...Example,
args: {
disabled: true,
},
};

export const IsOutsideRange: StoryObj<NewDatePickerProps> = {
render: (args) => {
const [date, setDate] = useState(dayjs());
Expand Down
14 changes: 12 additions & 2 deletions src/components/NewDatePicker/NewDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type NewDatePickerProps = {
date: Dayjs;
actions?: Action[];
format?: string;
disabled?: boolean;
/**
* 選択可能なカレンダーの領域を制限する
* true が返る場合は、選択不可となる
Expand All @@ -29,7 +30,14 @@ export type NewDatePickerProps = {
*/
export const NewDatePicker = forwardRef<HTMLDivElement, NewDatePickerProps>(
function DatePicker(
{ date, format = "YYYY-MM-DD", isOutsideRange, actions, onDateChange },
{
date,
format = "YYYY-MM-DD",
disabled = false,
isOutsideRange,
actions,
onDateChange,
},
ref,
) {
const [open, setOpen] = useState(false);
Expand All @@ -49,8 +57,9 @@ export const NewDatePicker = forwardRef<HTMLDivElement, NewDatePickerProps>(
]);

const handleClickCalendarIcon = useCallback(() => {
if (disabled) return;
setOpen((prev) => !prev);
}, []);
}, [disabled]);

const handleClickDate = useCallback(
(date: Dayjs) => {
Expand All @@ -70,6 +79,7 @@ export const NewDatePicker = forwardRef<HTMLDivElement, NewDatePickerProps>(
<DateField
date={date}
format={format}
disabled={disabled}
onClick={handleClickCalendarIcon}
onDateChange={onDateChange}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ export const WithActions: StoryObj<NewDateRangePickerProps> = {
},
};

export const Disabled: StoryObj<NewDateRangePickerProps> = {
...Example,
args: {
disabled: true,
},
};

export const IsOutsideRange: StoryObj<NewDateRangePickerProps> = {
render: (args) => {
const [date, setDate] = useState({
Expand Down
4 changes: 4 additions & 0 deletions src/components/NewDateRangePicker/NewDateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type NewDateRangePickerProps = {
startDate: Dayjs;
endDate: Dayjs;
actions?: Action[];
disabled?: boolean;
/**
* 選択可能なカレンダーの領域を制限する
* true が返る場合は、選択不可となる
Expand All @@ -39,6 +40,7 @@ export const DateRangePicker = forwardRef<
>(function DateRangePicker({
startDate,
endDate,
disabled = false,
isOutsideRange = () => false,
actions,
onDatesChange,
Expand All @@ -60,6 +62,7 @@ export const DateRangePicker = forwardRef<
]);

const handleClickCalendarIcon = () => {
if (disabled) return;
setOpen((prev) => !prev);
};

Expand All @@ -85,6 +88,7 @@ export const DateRangePicker = forwardRef<
startDate,
endDate,
}}
disabled={disabled}
onDatesChange={onDatesChange}
onClickCalendarIcon={handleClickCalendarIcon}
/>
Expand Down

0 comments on commit d2999f4

Please sign in to comment.