switcher.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Field from './_field';
  2. import Switch, { resolveControlSize } from 'app/ui/atoms/switcher'
  3. import { View } from 'app/design/view'
  4. import { Text } from 'app/design/typography'
  5. import { appSetting } from 'app/lib/util'
  6. import { useFormField } from 'app/lib/form/use-form-field';
  7. import { switcherInitialValue } from 'app/lib/form/field-initial-values';
  8. const switcherTheme = appSetting('theme', 'switcher');
  9. export default function FormFieldSwitcher(props) {
  10. const { caption, size, attrs } = props;
  11. const disabled = !!attrs?.disabled;
  12. const resolvedSize = resolveControlSize(size);
  13. const sizeClass = switcherTheme.size?.[resolvedSize] ?? switcherTheme.size?.regular ?? '';
  14. const { field } = useFormField(props, {
  15. defaultValue: switcherInitialValue(props),
  16. syncValue: false,
  17. });
  18. const handleToggle = () => {
  19. field.onChange(field.value ? 0 : 1);
  20. };
  21. return (
  22. <Field {...props}>
  23. <View className={`${switcherTheme['u-controls-switcher-container']} ${sizeClass}`}>
  24. <Switch
  25. onValueChange={handleToggle}
  26. value={!!field.value}
  27. size={resolvedSize}
  28. disabled={disabled}
  29. />
  30. <Text className={switcherTheme['u-controls-switcher-text']}>
  31. {caption}
  32. </Text>
  33. </View>
  34. </Field>
  35. );
  36. }