-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[charts] Make
useChartGradientId
public (#16106)
- Loading branch information
Showing
16 changed files
with
203 additions
and
60 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { createRenderer, screen } from '@mui/internal-test-utils'; | ||
import { useChartGradientId, useChartGradientIdObjectBound } from './useChartGradientId'; | ||
import { ChartDataProvider } from '../context'; | ||
|
||
function UseGradientId() { | ||
const id = useChartGradientId('test-id'); | ||
return <div>{id}</div>; | ||
} | ||
|
||
function UseGradientIdObjectBound() { | ||
const id = useChartGradientIdObjectBound('test-id'); | ||
return <div>{id}</div>; | ||
} | ||
|
||
describe('useChartGradientId', () => { | ||
const { render } = createRenderer(); | ||
|
||
it('should properly generate a correct id', () => { | ||
render( | ||
<ChartDataProvider series={[]} height={100} width={100}> | ||
<UseGradientId /> | ||
</ChartDataProvider>, | ||
); | ||
|
||
expect(screen.getByText(/:\w+:-gradient-test-id/)).toBeVisible(); | ||
}); | ||
|
||
describe('useChartGradientIdObjectBound', () => { | ||
it('should properly generate a correct id', () => { | ||
render( | ||
<ChartDataProvider series={[]} height={100} width={100}> | ||
<UseGradientIdObjectBound /> | ||
</ChartDataProvider>, | ||
); | ||
|
||
expect(screen.getByText(/:\w+:-gradient-test-id-object-bound/)).toBeVisible(); | ||
}); | ||
}); | ||
}); |
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,51 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { useChartId } from './useChartId'; | ||
import { AxisId } from '../models/axis'; | ||
|
||
/** | ||
* Returns a function that generates a gradient id for the given axis id. | ||
*/ | ||
export function useChartGradientIdBuilder() { | ||
const chartId = useChartId(); | ||
return React.useCallback((axisId: AxisId) => `${chartId}-gradient-${axisId}`, [chartId]); | ||
} | ||
|
||
/** | ||
* Returns a function that generates a gradient id for the given axis id. | ||
*/ | ||
export function useChartGradientIdObjectBoundBuilder() { | ||
const chartId = useChartId(); | ||
return React.useCallback( | ||
(axisId: AxisId) => `${chartId}-gradient-${axisId}-object-bound`, | ||
[chartId], | ||
); | ||
} | ||
|
||
/** | ||
* Returns a gradient id for the given axis id. | ||
* | ||
* Can be useful when reusing the same gradient on custom components. | ||
* | ||
* For a gradient that respects the coordinates of the object on which it is applied, use `useChartGradientIdObjectBound` instead. | ||
* | ||
* @param axisId the axis id | ||
* @returns the gradient id | ||
*/ | ||
export function useChartGradientId(axisId: AxisId) { | ||
return useChartGradientIdBuilder()(axisId); | ||
} | ||
|
||
/** | ||
* Returns a gradient id for the given axis id. | ||
* | ||
* Can be useful when reusing the same gradient on custom components. | ||
* | ||
* This gradient differs from `useChartGradientId` in that it respects the coordinates of the object on which it is applied. | ||
* | ||
* @param axisId the axis id | ||
* @returns the gradient id | ||
*/ | ||
export function useChartGradientIdObjectBound(axisId: AxisId) { | ||
return useChartGradientIdObjectBoundBuilder()(axisId); | ||
} |
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
Oops, something went wrong.