import _ from 'lodash'; import React from 'react'; import {StyleSheet} from 'react-native'; import { Checkbox, Switch, ColorPalette, Colors, RadioButton, RadioGroup, Slider, SegmentedControl, SegmentedControlItemProps, Text, TextProps, View } from 'react-native-ui-lib'; interface StateOptions { state?: string; setState?: React.Dispatch>; } interface RadioGroupBaseOptions { isRow?: boolean; useValueAsLabel?: boolean; } type RadioGroupOptions = | (RadioGroupBaseOptions & { afterValueChanged?: () => void; }) | (RadioGroupBaseOptions & StateOptions); interface BooleanGroupOptions { spread?: boolean; afterValueChanged?: () => void; state?: boolean; setState?: React.Dispatch>; } export function renderHeader(title: string, others?: TextProps) { return ( {title} ); } export function renderBooleanOption(title: string, key: string, {spread, afterValueChanged, state, setState}: BooleanGroupOptions = {spread: true}) { // @ts-ignore const value = state ?? this.state[key]; return ( {title} { if (setState) { setState(value); } else { // @ts-ignore this.setState({[key]: value}, afterValueChanged); } }} /> ); } export function renderBooleanGroup(title: string, options: string[]) { return ( {title} {_.map(options, key => { // @ts-ignore const value = this.state[key]; return ( this.setState({[key]: value})} /> {key} ); })} ); } export function renderRadioGroup(title: string, key: string, options: object, // @ts-ignore {isRow, afterValueChanged, useValueAsLabel, state, setState}: RadioGroupOptions = {}) { // @ts-ignore const value = state ?? this.state[key]; return ( {!_.isUndefined(title) && ( {title} )} { if (setState) { setState(value); if (afterValueChanged) { // eslint-disable-next-line no-restricted-syntax console.error('afterValueChanged is not supported together with the state option'); } } else { // @ts-ignore this.setState({[key]: value}, afterValueChanged); } }} > {_.map(options, (value, key) => { return ( ); })} ); } export function renderColorOption(title: string, key: string, colors = ['transparent', Colors.blue30, Colors.grey10, Colors.yellow30, Colors.green30, Colors.purple30]) { // @ts-ignore const value = this.state[key]; return ( {title} this.setState({[key]: value === 'transparent' ? undefined : value})} /> ); } export function renderSliderOption(title: string, key: string, { min = 0, max = 10, step = 1, initial = 0, sliderText = '', state, setState }: { min?: number; max?: number; step?: number; initial?: number; sliderText?: string; state?: number; setState?: React.Dispatch>; }) { // @ts-ignore const value = state ?? this.state[key] ?? initial; return ( {title} { if (setState) { setState(value); } else { // @ts-ignore this.setState({[key]: value}); } }} /> {sliderText} {value} ); } export function renderMultipleSegmentOptions(title: string, key: string, options: (SegmentedControlItemProps & {value: any})[], {state, setState}: StateOptions = {}) { // @ts-ignore const value = state ?? this.state[key]; const index = _.findIndex(options, {value}); return ( {!!title && ( {title} )} { const value = options[index].value; if (setState) { setState(value); } else { // @ts-ignore this.setState({[key]: value}); } }} /> ); } const styles = StyleSheet.create({ rowWrap: { flexWrap: 'wrap' }, text: { width: 30 } });