forked from mui/base-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckboxIndicator.tsx
121 lines (105 loc) · 4.04 KB
/
CheckboxIndicator.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useCheckboxRootContext } from '../root/CheckboxRootContext';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useCustomStyleHookMapping } from '../utils/useCustomStyleHookMapping';
import type { CheckboxRoot } from '../root/CheckboxRoot';
import type { BaseUIComponentProps } from '../../utils/types';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';
import { type TransitionStatus, useTransitionStatus } from '../../utils/useTransitionStatus';
import { useForkRef } from '../../utils/useForkRef';
import type { CustomStyleHookMapping } from '../../utils/getStyleHookProps';
import { transitionStatusMapping } from '../../utils/styleHookMapping';
/**
* Indicates whether the checkbox is ticked.
* Renders a `<span>` element.
*
* Documentation: [Base UI Checkbox](https://base-ui.com/react/components/checkbox)
*/
const CheckboxIndicator = React.forwardRef(function CheckboxIndicator(
props: CheckboxIndicator.Props,
forwardedRef: React.ForwardedRef<HTMLSpanElement>,
) {
const { render, className, keepMounted = false, ...otherProps } = props;
const rootState = useCheckboxRootContext();
const rendered = rootState.checked || rootState.indeterminate;
const { transitionStatus, setMounted } = useTransitionStatus(rendered);
const indicatorRef = React.useRef<HTMLSpanElement | null>(null);
const mergedRef = useForkRef(forwardedRef, indicatorRef);
const state: CheckboxIndicator.State = React.useMemo(
() => ({
...rootState,
transitionStatus,
}),
[rootState, transitionStatus],
);
useAfterExitAnimation({
open: rendered,
animatedElementRef: indicatorRef,
onFinished() {
setMounted(false);
},
});
const baseStyleHookMapping = useCustomStyleHookMapping(rootState);
const customStyleHookMapping: CustomStyleHookMapping<CheckboxIndicator.State> = React.useMemo(
() => ({
...baseStyleHookMapping,
...transitionStatusMapping,
}),
[baseStyleHookMapping],
);
const { renderElement } = useComponentRenderer({
render: render ?? 'span',
ref: mergedRef,
state,
className,
customStyleHookMapping,
extraProps: otherProps,
});
const shouldRender = keepMounted || rendered;
if (!shouldRender) {
return null;
}
return renderElement();
});
namespace CheckboxIndicator {
export interface State extends CheckboxRoot.State {
transitionStatus: TransitionStatus;
}
export interface Props extends BaseUIComponentProps<'span', State> {
/**
* Whether to keep the element in the DOM when the checkbox is not checked.
* @default false
*/
keepMounted?: boolean;
}
}
CheckboxIndicator.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* @ignore
*/
children: PropTypes.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* Whether to keep the element in the DOM when the checkbox is not checked.
* @default false
*/
keepMounted: PropTypes.bool,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
} as any;
export { CheckboxIndicator };