input-set.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Field from './_field';
  2. import { useFormContext } from 'react-hook-form';
  3. import { View, Row } from 'app/design/view';
  4. import { getFormFieldByData } from 'app/lib/form/form-helpers';
  5. import Submit from './submit';
  6. function getInputSetChildren(props) {
  7. return Object.keys(props)
  8. .filter((key) => /^\d+$/.test(key))
  9. .sort((a, b) => Number(a) - Number(b))
  10. .map((key) => props[key])
  11. .filter((child) => child && typeof child === 'object' && child.type);
  12. }
  13. export default function FormFieldControls(props) {
  14. // Unconditional: hooks must not follow the early returns below.
  15. const formContext = useFormContext();
  16. if (props[0].type == 'submit')
  17. return <Submit {...props[0]} handleSubmit={props.handleSubmit}/>
  18. if (props[1] && props[1].type == 'submit')
  19. return <Submit {...props[1]} handleSubmit={props.handleSubmit}/>
  20. if (props[0].type == 'button')
  21. return <Submit {...props[0]} handleSubmit={props.handleSubmit}/>
  22. const children = getInputSetChildren(props);
  23. if (!children.length) return null;
  24. const childProps = {
  25. use_caption_as_placeholder: props.use_caption_as_placeholder,
  26. form_layout: props.form_layout,
  27. noPadding: true,
  28. };
  29. return (
  30. <Field {...props} error2={formContext.formState.errors[props.name]}>
  31. <Row className="items-end gap-2 flex-wrap w-full">
  32. {children.map((child, index) => {
  33. const childKey = `${props.name || 'input_set'}_${child.name || child.type || 'field'}_${index}`
  34. return (
  35. <View
  36. key={childKey}
  37. className="flex-1 min-w-16"
  38. >
  39. {getFormFieldByData(child, props.handleSubmit, 'notitle', childProps, childKey)}
  40. </View>
  41. )
  42. })}
  43. </Row>
  44. </Field>
  45. );
  46. }