ant-design/components/form/hooks/useVariants.ts
叶枫 502dac12aa
docs: format code (#48309)
* docs: fix code

* feat: lint

* feat: prettier

* feat: test

* feat: review

* feat: format html

* feat: format html
2024-04-08 14:04:08 +08:00

31 lines
807 B
TypeScript

import { useContext } from 'react';
import { VariantContext } from '../context';
export const Variants = ['outlined', 'borderless', 'filled'] as const;
export type Variant = (typeof Variants)[number];
/**
* Compatible for legacy `bordered` prop.
*/
const useVariant = (
variant: Variant | undefined,
legacyBordered: boolean | undefined = undefined,
): [Variant, boolean] => {
const ctxVariant = useContext(VariantContext);
let mergedVariant: Variant;
if (typeof variant !== 'undefined') {
mergedVariant = variant;
} else if (legacyBordered === false) {
mergedVariant = 'borderless';
} else {
mergedVariant = ctxVariant ?? 'outlined';
}
const enableVariantCls = Variants.includes(mergedVariant);
return [mergedVariant, enableVariantCls];
};
export default useVariant;