最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Styled Component For Forms - Stack Overflow

matteradmin8PV0评论

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>
          );
Post a comment

comment list (0)

  1. No comments so far