-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collection): Add support for string children (#2202)
Add string children support to the collection system via `useListItemAllowChildStrings` elemProps hook. [category:Components]
- Loading branch information
1 parent
b81bd7c
commit 5fd436a
Showing
5 changed files
with
114 additions
and
2 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
37 changes: 37 additions & 0 deletions
37
modules/react/collection/lib/useListItemAllowChildStrings.ts
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {createElemPropsHook} from '@workday/canvas-kit-react/common'; | ||
import {useListModel} from '@workday/canvas-kit-react/collection'; | ||
|
||
/** | ||
* This elemProps hook allows for children values to be considered identifiers if the children | ||
* are strings. This can be useful for autocomplete or select components that allow string values. | ||
* This hook must be defined _before_ {@link useListItemRegister} because it sets the `data-id` | ||
* attribute if one hasn't been defined by the application. | ||
* | ||
* An example might look like: | ||
* ```tsx | ||
* const useMyListItem = composeHooks( | ||
* // any other hooks here | ||
* useListItemSelect, | ||
* useListItemRegister, | ||
* useListItemAllowChildStrings | ||
* ) | ||
* | ||
* | ||
* <MyList onSelect={({id}) => { | ||
* console.log(id) // will be "First" or "Second" | ||
* }}> | ||
* <MyList.Item>First</MyList.Item> | ||
* <MyList.Item>Second</MyList.Item> | ||
* </MyList> | ||
* ``` | ||
*/ | ||
export const useListItemAllowChildStrings = createElemPropsHook(useListModel)( | ||
(_, __, elemProps: {'data-id'?: string; children?: React.ReactNode} = {}) => { | ||
const props: {'data-id'?: string} = {}; | ||
if (!elemProps['data-id'] && typeof elemProps.children === 'string') { | ||
props['data-id'] = elemProps.children; | ||
} | ||
|
||
return props; | ||
} | ||
); |
24 changes: 24 additions & 0 deletions
24
modules/react/collection/spec/useListItemAllowChildStrings.spec.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
|
||
import {renderHook} from '@testing-library/react-hooks'; | ||
import {useListModel, useListItemAllowChildStrings} from '@workday/canvas-kit-react/collection'; | ||
|
||
describe('useListItemAllowChildSiblings', () => { | ||
it('should add a [data-id] attribute equal to the string value', () => { | ||
const {result} = renderHook(() => { | ||
const model = useListModel(); | ||
return useListItemAllowChildStrings(model, {children: 'Foobar'}); | ||
}); | ||
|
||
expect(result.current).toHaveProperty('data-id', 'Foobar'); | ||
}); | ||
|
||
it('should not add a [data-id] attribute if the child is not a string', () => { | ||
const {result} = renderHook(() => { | ||
const model = useListModel(); | ||
return useListItemAllowChildStrings(model, {children: <div>FooBar</div>}); | ||
}); | ||
|
||
expect(result.current).not.toHaveProperty('data-id'); | ||
}); | ||
}); |
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
35 changes: 35 additions & 0 deletions
35
modules/react/collection/stories/examples/StringChildren.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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
|
||
import { | ||
ListBox, | ||
useListItemRegister, | ||
useListItemAllowChildStrings, | ||
useListItemSelect, | ||
useListModel, | ||
} from '@workday/canvas-kit-react/collection'; | ||
import {composeHooks, createSubcomponent} from '@workday/canvas-kit-react/common'; | ||
|
||
const useItem = composeHooks(useListItemSelect, useListItemRegister, useListItemAllowChildStrings); | ||
|
||
const Item = createSubcomponent('button')({ | ||
modelHook: useListModel, | ||
elemPropsHook: useItem, | ||
})((elemProps, Element) => { | ||
return <Element {...elemProps} />; | ||
}); | ||
|
||
export const StringChildren = () => { | ||
const model = useListModel(); | ||
|
||
return ( | ||
<> | ||
<ListBox model={model}> | ||
<Item>First</Item> | ||
<Item>Second</Item> | ||
</ListBox> | ||
<div> | ||
Selected: <span id="selected">{model.state.selectedIds[0] || 'Nothing'}</span> | ||
</div> | ||
</> | ||
); | ||
}; |