javascript - Styled Component For Forms - Stack Overflow

I am using this form with material-ui ponents. Instead of writing the inline style that I am currently

I am using this form with material-ui ponents. Instead of writing the inline style that I am currently using for width, I wanted to opt for css-in-js. I have used styled-ponents previously but I don't think there's a form element with that.

The only example I came across was one where they had used built-in styled ponent labels. Since I have implemented validation on these material ui text fields as well so I don't want to change the structure. What would be the suitable way to put the style in css-in-js. I would prefer if there's a solution with styled-ponents.

          return (
            <div className="main-content">
              <form
                style={{ width: '100%' }}
                onSubmit={e => {
                  e.preventDefault();
                  submitForm(email);
                }}>
                <div>
                  <TextField
                    variant="outlined"
                    margin="normal"
                    id="email"
                    name="email"
                    helperText={touched.email ? errors.email : ''}
                    error={touched.email && Boolean(errors.email)}
                    label="Email"
                    value={email}
                    onChange={change.bind(null, 'email')}
                  />
                  <CustomButton
                    disabled={!isValid || !email}
                    text={'Remove User'}
                  />
                </div>
              </form>
            </div>
          );

I am using this form with material-ui ponents. Instead of writing the inline style that I am currently using for width, I wanted to opt for css-in-js. I have used styled-ponents previously but I don't think there's a form element with that.

The only example I came across was one where they had used built-in styled ponent labels. Since I have implemented validation on these material ui text fields as well so I don't want to change the structure. What would be the suitable way to put the style in css-in-js. I would prefer if there's a solution with styled-ponents.

          return (
            <div className="main-content">
              <form
                style={{ width: '100%' }}
                onSubmit={e => {
                  e.preventDefault();
                  submitForm(email);
                }}>
                <div>
                  <TextField
                    variant="outlined"
                    margin="normal"
                    id="email"
                    name="email"
                    helperText={touched.email ? errors.email : ''}
                    error={touched.email && Boolean(errors.email)}
                    label="Email"
                    value={email}
                    onChange={change.bind(null, 'email')}
                  />
                  <CustomButton
                    disabled={!isValid || !email}
                    text={'Remove User'}
                  />
                </div>
              </form>
            </div>
          );
Share Improve this question asked Apr 4, 2020 at 20:36 x89x89 3,50015 gold badges79 silver badges164 bronze badges 2
  • Does this answer your question? stackoverflow./a/61025884/11872246 – keikai Commented Apr 4, 2020 at 20:38
  • const StyledForm = styled.form``;? – Drew Reese Commented Apr 4, 2020 at 22:03
Add a ment  | 

1 Answer 1

Reset to default 2

just make the styled form:

import styled from 'styled-ponents';

const Form = styled.form`
   width: 100%;
`;

and use it:

    return (
            <div className="main-content">
              <Form
                onSubmit={e => {
                  e.preventDefault();
                  submitForm(email);
                }}>
                <div>
                  <TextField
                    variant="outlined"
                    margin="normal"
                    id="email"
                    name="email"
                    helperText={touched.email ? errors.email : ''}
                    error={touched.email && Boolean(errors.email)}
                    label="Email"
                    value={email}
                    onChange={change.bind(null, 'email')}
                  />
                  <CustomButton
                    disabled={!isValid || !email}
                    text={'Remove User'}
                  />
                </div>
              </Form>
            </div>
          );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745633628a4637255.html

相关推荐

  • javascript - Styled Component For Forms - Stack Overflow

    I am using this form with material-ui ponents. Instead of writing the inline style that I am currently

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信