ds-form-text.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Form, FormTextProps as BS5FormTextProps } from 'react-bootstrap'
  2. import classnames from 'classnames'
  3. import { MergeAndOverride } from '@ol-types/utils'
  4. import { CheckCircle, WarningCircle } from '@phosphor-icons/react'
  5. type TextType = 'success' | 'error'
  6. export type FormTextProps = MergeAndOverride<
  7. BS5FormTextProps,
  8. {
  9. type?: TextType
  10. marginless?: boolean
  11. }
  12. >
  13. const typeClasses = {
  14. error: 'text-danger',
  15. success: 'text-success',
  16. } as const
  17. export const getFormTextClass = (type?: TextType) =>
  18. type && type in typeClasses
  19. ? typeClasses[type as keyof typeof typeClasses]
  20. : undefined
  21. function FormTextIcon({ type }: { type?: TextType }) {
  22. switch (type) {
  23. case 'success':
  24. return <CheckCircle className="ciam-form-text-icon flex-shrink-0" />
  25. case 'error':
  26. return <WarningCircle className="ciam-form-text-icon flex-shrink-0" />
  27. default:
  28. return null
  29. }
  30. }
  31. function DSFormText({
  32. type,
  33. marginless,
  34. children,
  35. className,
  36. ...rest
  37. }: FormTextProps) {
  38. return (
  39. <Form.Text
  40. className={classnames('form-text-ds', className, getFormTextClass(type), {
  41. marginless,
  42. })}
  43. {...rest}
  44. >
  45. <span className="form-text-inner-ds">
  46. <FormTextIcon type={type} />
  47. <span>{children}</span>
  48. </span>
  49. </Form.Text>
  50. )
  51. }
  52. export default DSFormText