aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/components/CreateWorkspaceForm.tsx
blob: 0c365564a963cf752fabe3269a934babbdb6d46f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { observer } from 'mobx-react';
import { Component, type ReactElement } from 'react';
import {
  type WrappedComponentProps,
  defineMessages,
  injectIntl,
} from 'react-intl';
import withStyles, { type WithStylesProps } from 'react-jss';
import Button from '../../../components/ui/button';
import Input from '../../../components/ui/input/index';
import { required } from '../../../helpers/validation-helpers';
import Form from '../../../lib/Form';
import { workspaceStore } from '../index';

const messages = defineMessages({
  submitButton: {
    id: 'settings.workspace.add.form.submitButton',
    defaultMessage: 'Create workspace',
  },
  name: {
    id: 'settings.workspace.add.form.name',
    defaultMessage: 'Name',
  },
});

const styles = {
  form: {
    display: 'flex',
  },
  input: {
    flexGrow: 1,
    marginRight: '10px',
  },
  submitButton: {
    height: 'inherit',
  },
};

interface IProps extends WithStylesProps<typeof styles>, WrappedComponentProps {
  isSubmitting: boolean;
  onSubmit: (...args: any[]) => void;
}

@observer
class CreateWorkspaceForm extends Component<IProps> {
  form: Form;

  constructor(props: IProps) {
    super(props);

    this.form = new Form({
      fields: {
        name: {
          label: this.props.intl.formatMessage(messages.name),
          placeholder: this.props.intl.formatMessage(messages.name),
          value: '',
          validators: [required],
        },
      },
    });
  }

  submitForm(): void {
    this.form.submit({
      onSuccess: async form => {
        const { onSubmit } = this.props;
        const values = form.values();
        onSubmit(values);
      },
    });
  }

  render(): ReactElement {
    const { classes, isSubmitting, intl } = this.props;
    const { form } = this;

    return (
      <div className={classes.form}>
        <Input
          {...form.$('name').bind()}
          className={classes.input}
          showLabel={false}
          // @ts-expect-error Expected 1 arguments, but got 2.
          onEnterKey={this.submitForm.bind(this, form)}
          focus={workspaceStore.isUserAllowedToUseFeature}
        />
        <Button
          className={`${classes.submitButton} franz-form__button`}
          type="submit"
          label={intl.formatMessage(messages.submitButton)}
          // @ts-expect-error Expected 1 arguments, but got 2.
          onClick={this.submitForm.bind(this, form)}
          busy={isSubmitting}
          buttonType={isSubmitting ? 'secondary' : 'primary'}
        />
      </div>
    );
  }
}

export default injectIntl(
  withStyles(styles, { injectTheme: true })(CreateWorkspaceForm),
);