Compare commits

...

2 Commits

Author SHA1 Message Date
muzam1l
cb25986d72 update transaction tab labels 2022-07-01 17:30:57 +05:30
muzam1l
309ad57173 Skip auto appneding test file extension. 2022-07-01 17:26:10 +05:30
2 changed files with 23 additions and 12 deletions

View File

@@ -31,13 +31,15 @@ interface TabProps {
// TODO customise messages shown // TODO customise messages shown
interface Props { interface Props {
label?: string;
activeIndex?: number; activeIndex?: number;
activeHeader?: string; activeHeader?: string;
headless?: boolean; headless?: boolean;
children: ReactElement<TabProps>[]; children: ReactElement<TabProps>[];
keepAllAlive?: boolean; keepAllAlive?: boolean;
defaultExtension?: string; defaultExtension?: string;
forceDefaultExtension?: boolean; appendDefaultExtension?: boolean;
allowedExtensions?: string[];
onCreateNewTab?: (name: string) => any; onCreateNewTab?: (name: string) => any;
onCloseTab?: (index: number, header?: string) => any; onCloseTab?: (index: number, header?: string) => any;
onChangeActive?: (index: number, header?: string) => any; onChangeActive?: (index: number, header?: string) => any;
@@ -46,6 +48,7 @@ interface Props {
export const Tab = (props: TabProps) => null; export const Tab = (props: TabProps) => null;
export const Tabs = ({ export const Tabs = ({
label = "Tab",
children, children,
activeIndex, activeIndex,
activeHeader, activeHeader,
@@ -55,7 +58,8 @@ export const Tabs = ({
onCloseTab, onCloseTab,
onChangeActive, onChangeActive,
defaultExtension = "", defaultExtension = "",
forceDefaultExtension, appendDefaultExtension = false,
allowedExtensions,
}: Props) => { }: Props) => {
const [active, setActive] = useState(activeIndex || 0); const [active, setActive] = useState(activeIndex || 0);
const tabs: TabProps[] = children.map(elem => elem.props); const tabs: TabProps[] = children.map(elem => elem.props);
@@ -86,9 +90,13 @@ export const Tabs = ({
if (tabs.find(tab => tab.header === tabname)) { if (tabs.find(tab => tab.header === tabname)) {
return { error: "Name already exists." }; return { error: "Name already exists." };
} }
const ext = tabname.split(".").pop() || "";
if (allowedExtensions && !allowedExtensions.includes(ext)) {
return { error: "This file extension is not allowed!" };
}
return { error: null }; return { error: null };
}, },
[tabs] [allowedExtensions, tabs]
); );
const handleActiveChange = useCallback( const handleActiveChange = useCallback(
@@ -101,9 +109,11 @@ export const Tabs = ({
const handleCreateTab = useCallback(() => { const handleCreateTab = useCallback(() => {
// add default extension in case omitted // add default extension in case omitted
let _tabname = tabname.includes(".") ? tabname : tabname + defaultExtension; let _tabname = tabname.includes(".")
if (forceDefaultExtension && !_tabname.endsWith(defaultExtension)) { ? tabname
_tabname = _tabname + defaultExtension; : `${tabname}.${defaultExtension}`;
if (appendDefaultExtension && !_tabname.endsWith(defaultExtension)) {
_tabname = `${_tabname}.${defaultExtension}`;
} }
const chk = validateTabname(_tabname); const chk = validateTabname(_tabname);
@@ -122,7 +132,7 @@ export const Tabs = ({
}, [ }, [
tabname, tabname,
defaultExtension, defaultExtension,
forceDefaultExtension, appendDefaultExtension,
validateTabname, validateTabname,
onCreateNewTab, onCreateNewTab,
handleActiveChange, handleActiveChange,
@@ -206,13 +216,13 @@ export const Tabs = ({
size="sm" size="sm"
css={{ alignItems: "center", px: "$2", mr: "$3" }} css={{ alignItems: "center", px: "$2", mr: "$3" }}
> >
<Plus size="16px" /> {tabs.length === 0 && "Add new tab"} <Plus size="16px" /> {tabs.length === 0 && `Add new ${label.toLocaleLowerCase()}`}
</Button> </Button>
</DialogTrigger> </DialogTrigger>
<DialogContent> <DialogContent>
<DialogTitle>Create new tab</DialogTitle> <DialogTitle>Create new {label.toLocaleLowerCase()}</DialogTitle>
<DialogDescription> <DialogDescription>
<Label>Tabname</Label> <Label>{label} name</Label>
<Input <Input
value={tabname} value={tabname}
onChange={e => setTabname(e.target.value)} onChange={e => setTabname(e.target.value)}

View File

@@ -76,14 +76,15 @@ const Test = () => {
> >
<Box css={{ width: "55%", px: "$2" }}> <Box css={{ width: "55%", px: "$2" }}>
<Tabs <Tabs
label='Transaction'
activeHeader={activeHeader} activeHeader={activeHeader}
// TODO make header a required field // TODO make header a required field
onChangeActive={(idx, header) => { onChangeActive={(idx, header) => {
if (header) transactionsState.activeHeader = header; if (header) transactionsState.activeHeader = header;
}} }}
keepAllAlive keepAllAlive
forceDefaultExtension defaultExtension="json"
defaultExtension=".json" allowedExtensions={['json']}
onCreateNewTab={(header) => modifyTransaction(header, {})} onCreateNewTab={(header) => modifyTransaction(header, {})}
onCloseTab={(idx, header) => onCloseTab={(idx, header) =>
header && modifyTransaction(header, undefined) header && modifyTransaction(header, undefined)