index.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. /* eslint-disable react/prop-types */
  6. import React, { useMemo } from 'react';
  7. import { Input } from '@douyinfe/semi-ui';
  8. import { useTypeManager } from '@/plugins';
  9. import { PropsType, Strategy as ConstantInputStrategy } from './types';
  10. export { type ConstantInputStrategy };
  11. export function ConstantInput(props: PropsType) {
  12. const { value, onChange, schema, strategies, fallbackRenderer, readonly, ...rest } = props;
  13. const typeManager = useTypeManager();
  14. const Renderer = useMemo(() => {
  15. const strategy = (strategies || []).find((_strategy) => _strategy.hit(schema));
  16. if (!strategy) {
  17. return typeManager.getTypeBySchema(schema)?.ConstantRenderer;
  18. }
  19. return strategy?.Renderer;
  20. }, [strategies, schema]);
  21. if (!Renderer) {
  22. if (fallbackRenderer) {
  23. return React.createElement(fallbackRenderer, {
  24. value,
  25. onChange,
  26. readonly,
  27. ...rest,
  28. });
  29. }
  30. return <Input size="small" disabled placeholder="Unsupported type" />;
  31. }
  32. return <Renderer value={value} onChange={onChange} readonly={readonly} {...rest} />;
  33. }