1 import { withFormik } from 'formik';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { Button, Col, Form, Modal, Row } from 'react-bootstrap';
5 import { useTranslation } from 'react-i18next';
7 import yup from '../../schema/yup';
9 const SettingsForm = ({
20 const { t } = useTranslation();
22 return <Form noValidate onSubmit={handleSubmit}>
25 <Form.Group as={Col} sm={3} controlId="snes.proto">
26 <Form.Label>{t('snes.proto')}</Form.Label>
28 isInvalid={!!(touched.proto && errors.proto)}
31 onChange={handleChange}
32 value={values.proto || 'ws'}
34 <option value="ws">ws://</option>
35 <option value="wss">wss://</option>
37 {touched.proto && errors.proto ?
38 <Form.Control.Feedback type="invalid">
40 </Form.Control.Feedback>
43 <Form.Group as={Col} sm={6} controlId="snes.host">
44 <Form.Label>{t('snes.host')}</Form.Label>
46 isInvalid={!!(touched.host && errors.host)}
49 onChange={handleChange}
51 value={values.host || 'localhost'}
53 {touched.host && errors.host ?
54 <Form.Control.Feedback type="invalid">
56 </Form.Control.Feedback>
59 <Form.Group as={Col} sm={3} controlId="snes.port">
60 <Form.Label>{t('snes.port')}</Form.Label>
62 isInvalid={!!(touched.port && errors.port)}
67 onChange={handleChange}
69 value={values.port || 8080}
71 {touched.port && errors.port ?
72 <Form.Control.Feedback type="invalid">
74 </Form.Control.Feedback>
77 <Form.Group as={Col} sm={12} controlId="snes.device">
78 <Form.Label>{t('snes.device')}</Form.Label>
80 isInvalid={!!(touched.device && errors.device)}
83 onChange={handleChange}
84 value={values.device || ''}
86 <option value="">Auto</option>
87 {settings.device && !deviceList.includes(settings.device) ?
88 <option value={settings.device}>{settings.device}</option>
90 {deviceList.map(device =>
91 <option key={device} value={device}>{device}</option>
94 {touched.device && errors.device ?
95 <Form.Control.Feedback type="invalid">
97 </Form.Control.Feedback>
104 <Button onClick={onCancel} variant="secondary">
108 <Button type="submit" variant="primary">
115 SettingsForm.propTypes = {
116 deviceList: PropTypes.arrayOf(PropTypes.string),
117 errors: PropTypes.shape({
118 device: PropTypes.string,
119 host: PropTypes.string,
120 port: PropTypes.string,
121 proto: PropTypes.string,
123 handleBlur: PropTypes.func,
124 handleChange: PropTypes.func,
125 handleSubmit: PropTypes.func,
126 onCancel: PropTypes.func,
127 settings: PropTypes.shape({
128 device: PropTypes.string,
129 host: PropTypes.string,
130 port: PropTypes.number,
131 proto: PropTypes.string,
133 touched: PropTypes.shape({
134 device: PropTypes.bool,
135 host: PropTypes.bool,
136 port: PropTypes.bool,
137 proto: PropTypes.bool,
139 values: PropTypes.shape({
140 device: PropTypes.string,
141 host: PropTypes.string,
142 port: PropTypes.number,
143 proto: PropTypes.string,
147 export default withFormik({
148 displayName: 'SettingsForm',
149 enableReinitialize: true,
150 handleSubmit: async (values, actions) => {
151 const { onSubmit } = actions.props;
154 mapPropsToValues: ({ settings }) => settings,
155 validationSchema: yup.object().shape({
156 device: yup.string(),
158 port: yup.number().min(1).max(65665),