-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to new API, update dependencies
- Loading branch information
Showing
48 changed files
with
9,808 additions
and
6,068 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
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 |
---|---|---|
|
@@ -22,3 +22,4 @@ npm-debug.log* | |
yarn-debug.log* | ||
yarn-error.log* | ||
.vscode | ||
.eslintcache |
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 +1 @@ | ||
10 | ||
12 |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,113 @@ | ||
import React from "react"; | ||
import React, { useMemo } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import clsx from "clsx"; | ||
import { useState } from "react"; | ||
|
||
export interface CheckboxItem { | ||
export interface CheckboxItem<T> { | ||
key: string; | ||
label: string; | ||
value: T; | ||
} | ||
|
||
type CommonGroupParams = { | ||
items: CheckboxItem[]; | ||
type GroupParams<T> = { | ||
type: "checkbox" | "radio"; | ||
items: CheckboxItem<T>[]; | ||
groupKey: string; | ||
selectedItems: Iterable<string | CheckboxItem<T>> | null; | ||
onChange: (selectedItems: CheckboxItem<T>[]) => void; | ||
}; | ||
type CheckboxGroupParams = CommonGroupParams & { | ||
type: "checkbox"; | ||
selectedItemKeys: Set<string> | null; | ||
onChange: (selectedItemKeys: Set<string>) => void; | ||
}; | ||
type RadioGroupParams = CommonGroupParams & { | ||
type: "radio"; | ||
selectedItemKey: string; | ||
onChange: (selectedItemKey: string) => void; | ||
}; | ||
|
||
export function CheckboxGroup( | ||
props: CheckboxGroupParams | RadioGroupParams = { | ||
export function CheckboxGroup<T>( | ||
props: GroupParams<T> = { | ||
type: "checkbox", | ||
items: [], | ||
groupKey: "default", | ||
selectedItemKeys: null, | ||
selectedItems: null, | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
onChange: () => {} | ||
onChange: () => {}, | ||
} | ||
) { | ||
const { t } = useTranslation(); | ||
const { type, items, groupKey } = props; | ||
const { setSelected, isChecked } = (() => { | ||
const [activeKey, setActiveKey] = useState(null as string | null); | ||
const selectedItemKeys = useMemo(() => { | ||
const ret = new Set<string>(); | ||
for (const item of props.selectedItems || []) { | ||
if (typeof item === "string") { | ||
ret.add(item); | ||
} else { | ||
ret.add(item.key); | ||
} | ||
} | ||
return ret; | ||
}, [props.selectedItems]); | ||
const { type, items, groupKey, onChange } = props; | ||
const { setSelected, selectSingle } = useMemo(() => { | ||
if (props.type === "checkbox") { | ||
const { selectedItemKeys, onChange } = props; | ||
return { | ||
setSelected(key: string, isSelected: boolean) { | ||
if (isSelected && (!selectedItemKeys || selectedItemKeys.has(key))) { | ||
if (isSelected && selectedItemKeys.has(key)) { | ||
return; | ||
} | ||
if (!isSelected && selectedItemKeys && !selectedItemKeys.has(key)) { | ||
if (!isSelected && !selectedItemKeys.has(key)) { | ||
return; | ||
} | ||
const newSet = new Set(selectedItemKeys || items.map(x => x.key)); | ||
const newSet = new Set(selectedItemKeys || items.map((x) => x.key)); | ||
if (isSelected) { | ||
newSet.add(key); | ||
} else { | ||
newSet.delete(key); | ||
} | ||
onChange(newSet); | ||
onChange(items.filter((x) => newSet.has(x.key))); | ||
}, | ||
selectSingle(key: string) { | ||
if (selectedItemKeys.size === 1 && selectedItemKeys.has(key)) { | ||
return; | ||
} | ||
onChange(items.filter((x) => x.key === key)); | ||
}, | ||
isChecked: (key: string) => !selectedItemKeys || selectedItemKeys.has(key) | ||
}; | ||
} else { | ||
const { selectedItemKey, onChange } = props; | ||
return { | ||
setSelected(key: string, isSelected: boolean) { | ||
if (!isSelected) { | ||
return; | ||
} | ||
onChange(key); | ||
const item = items.find((x) => x.key === key); | ||
onChange(item ? [item] : []); | ||
}, | ||
selectSingle(key: string) { | ||
setSelected(key, true); | ||
}, | ||
isChecked: (key: string) => selectedItemKey === key | ||
}; | ||
} | ||
})(); | ||
}, [items, onChange, props.type, selectedItemKeys]); | ||
return ( | ||
<React.Fragment> | ||
{items.map(item => ( | ||
<div className="form-check form-check-inline" key={item.key}> | ||
<div className={clsx("checkbox-group", "checkbox-group-" + type, activeKey && "checkbox-group-has-active")}> | ||
{items.map((item) => ( | ||
<div className={clsx("form-check form-check-inline", activeKey === item.key && "active")} key={item.key}> | ||
<input | ||
className="form-check-input" | ||
type={type} | ||
id={`CG_${groupKey}_${item.key}`} | ||
name={`CG_${groupKey}_${item.key}`} | ||
value={item.key} | ||
checked={isChecked(item.key)} | ||
onChange={event => setSelected(item.key, event.currentTarget.checked)} | ||
checked={selectedItemKeys.has(item.key)} | ||
onChange={(event) => setSelected(item.key, event.currentTarget.checked)} | ||
/> | ||
<label className="form-check-label" htmlFor={`CG_${groupKey}_${item.key}`}> | ||
<label | ||
className="form-check-label" | ||
htmlFor={`CG_${groupKey}_${item.key}`} | ||
onMouseEnter={() => setActiveKey(item.key)} | ||
onMouseLeave={() => (activeKey === item.key ? setActiveKey(null) : undefined)} | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
selectSingle(item.key); | ||
}} | ||
> | ||
{t(item.label)} | ||
</label> | ||
</div> | ||
))} | ||
</React.Fragment> | ||
</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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
import React from "react"; | ||
|
||
import dayjs from "dayjs"; | ||
|
Oops, something went wrong.