-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ columnwidth in gardenmeta (#483)
- Loading branch information
1 parent
24a0422
commit 7356cd6
Showing
7 changed files
with
84 additions
and
85 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
148 changes: 75 additions & 73 deletions
148
packages/garden/src/lib/components/ExpandProvider/ExpandProvider.tsx
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,73 +1,75 @@ | ||
import { createContext, PropsWithChildren, useReducer } from 'react'; | ||
import { defaultGardenPackageWidth } from '../../hooks'; | ||
|
||
type State = { | ||
expandedColumns: number[]; | ||
widths: number[]; | ||
}; | ||
export enum ActionType { | ||
EXPAND_COLUMN, | ||
} | ||
|
||
type ExpandColumn = { | ||
index: number; | ||
type: ActionType.EXPAND_COLUMN; | ||
}; | ||
|
||
type Action = ExpandColumn; | ||
const expandReducer = (state: State, action: Action): State => { | ||
switch (action.type) { | ||
case ActionType.EXPAND_COLUMN: { | ||
if (state.expandedColumns.findIndex((s) => s === action.index) !== -1) { | ||
const newWidths = [...state.widths]; | ||
newWidths[action.index] = defaultGardenPackageWidth; | ||
return { | ||
expandedColumns: state.expandedColumns.filter((s) => s !== action.index), | ||
widths: newWidths, | ||
}; | ||
} else { | ||
const newWidths = [...state.widths]; | ||
newWidths[action.index] = 500; | ||
return { | ||
expandedColumns: [...state.expandedColumns, action.index], | ||
widths: newWidths, | ||
}; | ||
} | ||
} | ||
|
||
default: | ||
return { expandedColumns: [], widths: [] }; | ||
} | ||
}; | ||
|
||
type DispatchAction = (action: Action) => void; | ||
const ExpandContext = createContext<State>({ | ||
expandedColumns: [], | ||
widths: [], | ||
}); | ||
|
||
function createExpandDispatchContext() { | ||
return createContext<DispatchAction | undefined>(undefined); | ||
} | ||
const ExpandDispatchContext = createExpandDispatchContext(); | ||
|
||
type ExpandProviderProps = { | ||
initialWidths: number[]; | ||
}; | ||
|
||
const ExpandProvider = (props: PropsWithChildren<ExpandProviderProps>) => { | ||
const { initialWidths, children } = props; | ||
|
||
const [state, dispatch] = useReducer(expandReducer, { | ||
expandedColumns: [], | ||
widths: initialWidths, | ||
}); | ||
|
||
return ( | ||
<ExpandContext.Provider value={state}> | ||
<ExpandDispatchContext.Provider value={dispatch}>{children}</ExpandDispatchContext.Provider> | ||
</ExpandContext.Provider> | ||
); | ||
}; | ||
|
||
export { ExpandProvider, ExpandContext, ExpandDispatchContext }; | ||
import { createContext, PropsWithChildren, useReducer } from 'react'; | ||
|
||
type State = { | ||
expandedColumns: number[]; | ||
widths: number[]; | ||
}; | ||
export enum ActionType { | ||
EXPAND_COLUMN, | ||
} | ||
|
||
type ExpandColumn = { | ||
index: number; | ||
type: ActionType.EXPAND_COLUMN; | ||
}; | ||
|
||
type Action = ExpandColumn; | ||
const expandReducer = | ||
(defaultColumnWidth: number) => | ||
(state: State, action: Action): State => { | ||
switch (action.type) { | ||
case ActionType.EXPAND_COLUMN: { | ||
if (state.expandedColumns.findIndex((s) => s === action.index) !== -1) { | ||
const newWidths = [...state.widths]; | ||
newWidths[action.index] = defaultColumnWidth; | ||
return { | ||
expandedColumns: state.expandedColumns.filter((s) => s !== action.index), | ||
widths: newWidths, | ||
}; | ||
} else { | ||
const newWidths = [...state.widths]; | ||
newWidths[action.index] = defaultColumnWidth + 200; | ||
return { | ||
expandedColumns: [...state.expandedColumns, action.index], | ||
widths: newWidths, | ||
}; | ||
} | ||
} | ||
|
||
default: | ||
return { expandedColumns: [], widths: [] }; | ||
} | ||
}; | ||
|
||
type DispatchAction = (action: Action) => void; | ||
const ExpandContext = createContext<State>({ | ||
expandedColumns: [], | ||
widths: [], | ||
}); | ||
|
||
function createExpandDispatchContext() { | ||
return createContext<DispatchAction | undefined>(undefined); | ||
} | ||
const ExpandDispatchContext = createExpandDispatchContext(); | ||
|
||
type ExpandProviderProps = { | ||
initialWidths: number[]; | ||
defaultColumnWidth: number; | ||
}; | ||
|
||
const ExpandProvider = (props: PropsWithChildren<ExpandProviderProps>) => { | ||
const { initialWidths, defaultColumnWidth, children } = props; | ||
|
||
const [state, dispatch] = useReducer(expandReducer(defaultColumnWidth), { | ||
expandedColumns: [], | ||
widths: initialWidths, | ||
}); | ||
|
||
return ( | ||
<ExpandContext.Provider value={state}> | ||
<ExpandDispatchContext.Provider value={dispatch}>{children}</ExpandDispatchContext.Provider> | ||
</ExpandContext.Provider> | ||
); | ||
}; | ||
|
||
export { ExpandProvider, ExpandContext, ExpandDispatchContext }; |
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