File size: 6,621 Bytes
cf86710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import React from "react";
import type { Locale } from "react-day-picker";
import { DateLib } from "react-day-picker";
import {
  amET,
  getDateLib as getDateLibEthiopic,
} from "react-day-picker/ethiopic";
import * as locales from "react-day-picker/locale";
import {
  faIR,
  getDateLib as getDateLibPersian,
} from "react-day-picker/persian";

import styles from "./styles.module.css";
import type { DayPickerPropsWithCalendar } from "./useQueryStringSync";

interface CustomizationFieldsetProps {
  props: DayPickerPropsWithCalendar;
  setProps: React.Dispatch<React.SetStateAction<DayPickerPropsWithCalendar>>;
  accentColor: string;
  setAccentColor: React.Dispatch<React.SetStateAction<string>>;
}

function resolveDateLib(props: DayPickerPropsWithCalendar) {
  if (props.calendar === "persian") {
    return getDateLibPersian({
      locale: (props.locale as Locale) ?? faIR,
      timeZone: props.timeZone,
      numerals: props.numerals,
    });
  }
  if (props.calendar === "ethiopic") {
    return getDateLibEthiopic({
      locale: (props.locale as Locale) ?? (amET as Locale),
      timeZone: props.timeZone,
      numerals: props.numerals,
    });
  }
  return new DateLib({
    locale: (props.locale as Locale) ?? locales.enUS,
    timeZone: props.timeZone,
  });
}

function resolveReferenceDate(
  props: DayPickerPropsWithCalendar,
  dateLib: DateLib,
) {
  const candidates = [props.month, props.startMonth, props.defaultMonth];
  for (const candidate of candidates) {
    if (candidate instanceof Date) return candidate;
  }
  return dateLib.today();
}

export function CustomizationFieldset({
  props,
  setProps,
  accentColor,
  setAccentColor,
}: CustomizationFieldsetProps) {
  return (
    <fieldset>
      <legend>
        Customization{" "}
        <button
          type="button"
          onClick={() => {
            setProps({
              ...props,
              captionLayout: undefined,
              navLayout: undefined,
              showOutsideDays: false,
              showWeekNumber: false,
              fixedWeeks: false,
              hideWeekdays: false,
              reverseYears: false,
              startMonth: undefined,
              endMonth: undefined,
            });
            setAccentColor("");
          }}
        >
          Reset
        </button>
      </legend>
      <div className={styles.fields}>
        <label>
          Navigation Layout:
          <select
            name="navLayout"
            value={props.navLayout ?? ""}
            onChange={(e) => {
              const newProps: DayPickerPropsWithCalendar = {
                ...props,
                navLayout: (e.target.value ?? undefined) as
                  | "around"
                  | "after"
                  | undefined,
              };
              setProps(newProps);
            }}
          >
            <option value=""></option>
            <option value="around">Around</option>
            <option value="after">After</option>
          </select>
        </label>
        <label>
          Caption Layout:
          <select
            name="captionLayout"
            value={props.captionLayout ?? ""}
            onChange={(e) => {
              const newCaptionLayout = e.target.value as
                | "label"
                | "dropdown"
                | "dropdown-months"
                | "dropdown-years";
              const newProps: DayPickerPropsWithCalendar = {
                ...props,
                captionLayout: newCaptionLayout,
              };
              if (newCaptionLayout === "dropdown") {
                const dateLib = resolveDateLib(newProps);
                const referenceDate = resolveReferenceDate(newProps, dateLib);
                const hundredYearsAgo = dateLib.addYears(referenceDate, -100);
                newProps.startMonth = dateLib.startOfMonth(hundredYearsAgo);
                newProps.endMonth = dateLib.startOfMonth(referenceDate);
              } else if (newCaptionLayout === "label") {
                newProps.startMonth = undefined;
                newProps.endMonth = undefined;
              }
              setProps(newProps);
            }}
          >
            <option></option>
            <option value="label">Label</option>
            <option value="dropdown">Dropdown</option>
            <option value="dropdown-months">Dropdown Months</option>
            <option value="dropdown-years">Dropdown Years</option>
          </select>
        </label>
        {(props.captionLayout === "dropdown" ||
          props.captionLayout === "dropdown-years") && (
          <label>
            <input
              type="checkbox"
              name="reverseYears"
              checked={!!props.reverseYears}
              onChange={(e) =>
                setProps({ ...props, reverseYears: e.target.checked })
              }
              disabled={
                !(
                  props.captionLayout === "dropdown" ||
                  props.captionLayout === "dropdown-years"
                )
              }
            />
            Reverse Dropdown Years
          </label>
        )}
        <label>
          <input
            type="checkbox"
            name="showOutsideDays"
            checked={props.showOutsideDays}
            onChange={(e) =>
              setProps({ ...props, showOutsideDays: e.target.checked })
            }
          />
          Show outside days
        </label>
        <label>
          <input
            type="checkbox"
            name="showWeekNumber"
            checked={props.showWeekNumber}
            onChange={(e) =>
              setProps({ ...props, showWeekNumber: e.target.checked })
            }
          />
          Show week number
        </label>
        <label>
          <input
            type="checkbox"
            name="fixedWeeks"
            checked={props.fixedWeeks}
            onChange={(e) =>
              setProps({ ...props, fixedWeeks: e.target.checked })
            }
          />
          Fixed weeks
        </label>
        <label>
          <input
            type="checkbox"
            name="hideWeekdays"
            checked={props.hideWeekdays}
            onChange={(e) =>
              setProps({ ...props, hideWeekdays: e.target.checked })
            }
          />
          Hide weekdays
        </label>
        <label>
          Accent Color:
          <input
            value={accentColor ?? ""}
            type="color"
            name="accentColor"
            onChange={(e) => setAccentColor(e.target.value)}
          />
        </label>
      </div>
    </fieldset>
  );
}