-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exchange defaultProps for default parameters in function components #1226
base: main
Are you sure you want to change the base?
Changes from all commits
643aa8b
424d980
081fb80
1fe8bc7
52768af
cb4a3e4
b8bced1
71d6502
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It occurred to me that you could do something like this in the component: function CheckboxTree<T>(partialProps: Props<T>) {
const props = { ...defaultProps, ...partialProps };
// ...
} If a prop is defined in |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -163,7 +163,7 @@ export type CheckboxTreeProps<T> = { | |||||||||
isSelectable?: boolean; | ||||||||||
|
||||||||||
/** List of selected nodes as represented by their ids, defaults to [ ] */ | ||||||||||
selectedList: string[]; | ||||||||||
selectedList?: string[]; | ||||||||||
|
||||||||||
/** | ||||||||||
* List of filtered nodes as represented by their ids used to determine isLeafVisible node status. | ||||||||||
|
@@ -183,7 +183,7 @@ export type CheckboxTreeProps<T> = { | |||||||||
|
||||||||||
/** Takes array of ids, thus encapsulates: | ||||||||||
selectAll, clearAll, selectDefault, selectCurrent (i.e. reset) */ | ||||||||||
onSelectionChange: ChangeHandler; | ||||||||||
onSelectionChange?: ChangeHandler; | ||||||||||
|
||||||||||
/** List of “current” ids, if omitted (undefined or null), then don’t display link */ | ||||||||||
currentList?: string[]; | ||||||||||
|
@@ -194,7 +194,7 @@ export type CheckboxTreeProps<T> = { | |||||||||
//%%%%%%%%%%% Properties associated with search %%%%%%%%%%% | ||||||||||
|
||||||||||
/** Indicates whether this is a searchable CBT. If so, then show boxes and respect the optional parameters below, also turn off expansion; default to false */ | ||||||||||
isSearchable: boolean; | ||||||||||
isSearchable?: boolean; | ||||||||||
|
||||||||||
/** Indicates if the search box should have autoFocus set to true */ | ||||||||||
autoFocusSearchBox?: boolean; | ||||||||||
|
@@ -203,7 +203,7 @@ export type CheckboxTreeProps<T> = { | |||||||||
showSearchBox?: boolean; | ||||||||||
|
||||||||||
/** PlaceHolder text; shown in grey if searchTerm is empty */ | ||||||||||
searchBoxPlaceholder: string; | ||||||||||
searchBoxPlaceholder?: string; | ||||||||||
|
||||||||||
/** Name of icon to show in search box */ | ||||||||||
searchIconName?: 'search' | 'filter'; | ||||||||||
|
@@ -215,13 +215,13 @@ export type CheckboxTreeProps<T> = { | |||||||||
searchBoxHelp?: string; | ||||||||||
|
||||||||||
/** Current search term; if non-empty, expandability is disabled */ | ||||||||||
searchTerm: string; | ||||||||||
searchTerm?: string; | ||||||||||
|
||||||||||
/** Takes single arg: the new search text. Called when user types into the search box */ | ||||||||||
onSearchTermChange: (term: string) => void; | ||||||||||
onSearchTermChange?: (term: string) => void; | ||||||||||
|
||||||||||
/** Takes (node, searchTerms) and returns boolean. searchTerms is a list of query terms, parsed from the original input string. This function returns a boolean indicating if a node matches search criteria and should be shown */ | ||||||||||
searchPredicate: (node: T, terms: string[]) => boolean; | ||||||||||
searchPredicate?: (node: T, terms: string[]) => boolean; | ||||||||||
|
||||||||||
renderNoResults?: (searchTerm: string, tree: T) => React.ReactNode; | ||||||||||
|
||||||||||
|
@@ -247,6 +247,29 @@ export type CheckboxTreeProps<T> = { | |||||||||
customTreeNodeCssSelectors?: object; | ||||||||||
}; | ||||||||||
|
||||||||||
// Default values. Used across multiple functions in this file. | ||||||||||
const defaultCheckboxTreeProps = { | ||||||||||
showRoot: false, | ||||||||||
expandedList: null, | ||||||||||
isSelectable: false, | ||||||||||
selectedList: [], | ||||||||||
customCheckboxes: {}, | ||||||||||
isMultiPick: true, | ||||||||||
onSelectionChange: () => { | ||||||||||
/* */ | ||||||||||
}, | ||||||||||
isSearchable: false, | ||||||||||
showSearchBox: true, | ||||||||||
searchBoxPlaceholder: 'Search...', | ||||||||||
searchBoxHelp: '', | ||||||||||
searchTerm: '', | ||||||||||
onSearchTermChange: () => { | ||||||||||
/* */ | ||||||||||
}, | ||||||||||
searchPredicate: () => true, | ||||||||||
linksPosition: LinksPosition.Both, | ||||||||||
}; | ||||||||||
|
||||||||||
type TreeLinkHandler = MouseEventHandler<HTMLButtonElement>; | ||||||||||
|
||||||||||
type TreeLinksProps = { | ||||||||||
|
@@ -464,7 +487,7 @@ function applyPropsToStatefulTree<T>( | |||||||||
isSearchable: CheckboxTreeProps<T>['isSearchable'], | ||||||||||
isMultiPick: CheckboxTreeProps<T>['isMultiPick'], | ||||||||||
searchTerm: CheckboxTreeProps<T>['searchTerm'], | ||||||||||
selectedList: CheckboxTreeProps<T>['selectedList'], | ||||||||||
selectedList: CheckboxTreeProps<T>['selectedList'] = defaultCheckboxTreeProps.selectedList, | ||||||||||
propsExpandedList: CheckboxTreeProps<T>['expandedList'], | ||||||||||
isAdditionalFilterApplied: CheckboxTreeProps<T>['isAdditionalFilterApplied'], | ||||||||||
isLeafVisible: (id: string) => boolean, | ||||||||||
|
@@ -590,8 +613,8 @@ function applyPropsToStatefulTree<T>( | |||||||||
*/ | ||||||||||
function isActiveSearch<T>( | ||||||||||
isAdditionalFilterApplied: CheckboxTreeProps<T>['isAdditionalFilterApplied'], | ||||||||||
isSearchable: CheckboxTreeProps<T>['isSearchable'], | ||||||||||
searchTerm: CheckboxTreeProps<T>['searchTerm'] | ||||||||||
isSearchable: CheckboxTreeProps<T>['isSearchable'] = defaultCheckboxTreeProps.isSearchable, | ||||||||||
searchTerm: CheckboxTreeProps<T>['searchTerm'] = defaultCheckboxTreeProps.searchTerm | ||||||||||
) { | ||||||||||
return isSearchable && isFiltered(searchTerm, isAdditionalFilterApplied); | ||||||||||
} | ||||||||||
|
@@ -626,12 +649,12 @@ function isFiltered(searchTerm: string, isAdditionalFilterApplied?: boolean) { | |||||||||
*/ | ||||||||||
function createIsLeafVisible<T>( | ||||||||||
tree: CheckboxTreeProps<T>['tree'], | ||||||||||
searchTerm: CheckboxTreeProps<T>['searchTerm'], | ||||||||||
searchPredicate: CheckboxTreeProps<T>['searchPredicate'], | ||||||||||
searchTerm: CheckboxTreeProps<T>['searchTerm'] = defaultCheckboxTreeProps.searchTerm, | ||||||||||
searchPredicate: CheckboxTreeProps<T>['searchPredicate'] = defaultCheckboxTreeProps.searchPredicate, | ||||||||||
getNodeId: CheckboxTreeProps<T>['getNodeId'], | ||||||||||
getNodeChildren: CheckboxTreeProps<T>['getNodeChildren'], | ||||||||||
isAdditionalFilterApplied: CheckboxTreeProps<T>['isAdditionalFilterApplied'], | ||||||||||
isSearchable: CheckboxTreeProps<T>['isSearchable'], | ||||||||||
isSearchable: CheckboxTreeProps<T>['isSearchable'] = defaultCheckboxTreeProps.isSearchable, | ||||||||||
filteredList: CheckboxTreeProps<T>['filteredList'] | ||||||||||
) { | ||||||||||
// if not searching, if no additional filters are applied, and if filteredList is undefined, then all nodes are visible | ||||||||||
|
@@ -696,32 +719,38 @@ function CheckboxTree<T>(props: CheckboxTreeProps<T>) { | |||||||||
tree, | ||||||||||
getNodeId, | ||||||||||
getNodeChildren, | ||||||||||
searchTerm, | ||||||||||
selectedList, | ||||||||||
searchTerm = '', | ||||||||||
selectedList = [], | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will create a new array on each render. Use
Suggested change
|
||||||||||
currentList, | ||||||||||
defaultList, | ||||||||||
isSearchable, | ||||||||||
expandedList = null, | ||||||||||
isSearchable = false, | ||||||||||
isAdditionalFilterApplied, | ||||||||||
name, | ||||||||||
shouldExpandDescendantsWithOneChild, | ||||||||||
onExpansionChange, | ||||||||||
isSelectable, | ||||||||||
isMultiPick, | ||||||||||
onSelectionChange, | ||||||||||
showRoot, | ||||||||||
onSelectionChange = () => { | ||||||||||
// | ||||||||||
}, | ||||||||||
Comment on lines
+732
to
+734
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
isSelectable = false, | ||||||||||
isMultiPick = true, | ||||||||||
showRoot = false, | ||||||||||
additionalActions, | ||||||||||
linksPosition = LinksPosition.Both, | ||||||||||
showSearchBox, | ||||||||||
showSearchBox = true, | ||||||||||
autoFocusSearchBox, | ||||||||||
onSearchTermChange, | ||||||||||
searchBoxPlaceholder, | ||||||||||
onSearchTermChange = () => { | ||||||||||
// | ||||||||||
}, | ||||||||||
Comment on lines
+742
to
+744
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
searchBoxPlaceholder = 'Search...', | ||||||||||
searchIconName, | ||||||||||
searchIconPosition, | ||||||||||
searchBoxHelp, | ||||||||||
searchBoxHelp = '', | ||||||||||
searchPredicate = () => true, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
additionalFilters, | ||||||||||
wrapTreeSection, | ||||||||||
shouldExpandOnClick = true, | ||||||||||
customCheckboxes, | ||||||||||
customCheckboxes = {}, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
renderNoResults, | ||||||||||
styleOverrides = {}, | ||||||||||
customTreeNodeCssSelectors = {}, | ||||||||||
|
@@ -765,7 +794,7 @@ function CheckboxTree<T>(props: CheckboxTreeProps<T>) { | |||||||||
* Creates a function that will handle selection-related tree link clicks | ||||||||||
*/ | ||||||||||
function createSelector(listFetcher: ListFetcher) { | ||||||||||
return createLinkHandler(listFetcher, props.onSelectionChange); | ||||||||||
return createLinkHandler(listFetcher, onSelectionChange); | ||||||||||
} | ||||||||||
|
||||||||||
// define event handlers related to expansion | ||||||||||
|
@@ -1071,29 +1100,6 @@ function defaultRenderNoResults() { | |||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
const defaultProps = { | ||||||||||
showRoot: false, | ||||||||||
expandedList: null, | ||||||||||
isSelectable: false, | ||||||||||
selectedList: [], | ||||||||||
customCheckboxes: {}, | ||||||||||
isMultiPick: true, | ||||||||||
onSelectionChange: () => { | ||||||||||
/* */ | ||||||||||
}, | ||||||||||
isSearchable: false, | ||||||||||
showSearchBox: true, | ||||||||||
searchBoxPlaceholder: 'Search...', | ||||||||||
searchBoxHelp: '', | ||||||||||
searchTerm: '', | ||||||||||
onSearchTermChange: () => { | ||||||||||
/* */ | ||||||||||
}, | ||||||||||
searchPredicate: () => true, | ||||||||||
linksPosition: LinksPosition.Both, | ||||||||||
}; | ||||||||||
|
||||||||||
CheckboxTree.defaultProps = defaultProps; | ||||||||||
CheckboxTree.LinkPlacement = LinksPosition; | ||||||||||
export default CheckboxTree; | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this. Isn't
selectedList
an option prop for CheckboxTree?