-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): indicator data- and aria- properties apply to label (#236)
closes #230
- Loading branch information
Showing
9 changed files
with
115 additions
and
36 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
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
14 changes: 14 additions & 0 deletions
14
packages/react/src/internal/indicator-container/splitContainerProps.spec.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,14 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { splitContainerProps } from './splitContainerProps'; | ||
|
||
describe('splitContainerProps', () => { | ||
it('should split props into [containerProps, inputProps]', () => { | ||
const foo = {}; | ||
const props = { foo, bar: 1, 'data-test': 'bar' }; | ||
|
||
const [containerProps, inputProps] = splitContainerProps(props as any); | ||
|
||
expect(containerProps).toStrictEqual({ 'data-test': 'bar' }); | ||
expect(inputProps).toStrictEqual({ foo, bar: 1 }); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
packages/react/src/internal/indicator-container/splitContainerProps.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,14 @@ | ||
import { partitionObject } from '../../utils'; | ||
|
||
/** | ||
* Splits `Indicator` properties into `label` and `input` props. | ||
*/ | ||
export const splitContainerProps = ( | ||
props: InputElementProps | LabelElementProps | ||
) => partitionObject(props, ([key]) => containerProps.test(key)) as Tuple; | ||
|
||
const containerProps = /^aria-|^data-/; | ||
|
||
type InputElementProps = JSX.IntrinsicElements['input']; | ||
type LabelElementProps = JSX.IntrinsicElements['label']; | ||
type Tuple = [LabelElementProps, InputElementProps]; |
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,2 @@ | ||
export * from './partitionObject/partitionObject'; | ||
export * from './withRef/withRef'; |
13 changes: 13 additions & 0 deletions
13
packages/react/src/utils/partitionObject/partitionObject.spec.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,13 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { partitionObject } from './partitionObject'; | ||
|
||
describe('partitionObject', () => { | ||
it('should split an object like [{ "does" }, { "does not" }] satisfy the predicate', () => { | ||
const obj = { foo: true, bar: false }; | ||
|
||
const [truthy, falsy] = partitionObject(obj, ([, v]) => Boolean(v)); | ||
|
||
expect(truthy).toStrictEqual({ foo: true }); | ||
expect(falsy).toStrictEqual({ bar: false }); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/react/src/utils/partitionObject/partitionObject.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,20 @@ | ||
/** | ||
* Partitions an `object` in two with entries that satisfy the `predicate` then | ||
* those that don't, respectively. | ||
* | ||
* @param object Object to be partitioned. | ||
* @param predicate Function that determines in where to put each entry. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const partitionObject = <T extends Record<string, any>>( | ||
object: T, | ||
predicate: (entry: [keyof T, T[keyof T]], index: number) => boolean | ||
) => | ||
Object.entries(object).reduce( | ||
(tuple, [key, value]: [keyof T, T[keyof T]], index) => { | ||
const which = predicate([key, value], index) ? 0 : 1; | ||
tuple[which][key] = value; | ||
return tuple; | ||
}, | ||
[{}, {}] as [Partial<T>, Partial<T>] | ||
); |