0

我开始接触glamorous,它是一个React 组件样式模块。我有两个要设置样式的按钮:AddClear. 我试图将两个按钮放在同一行,Clear左侧的Add按钮和右侧的按钮。我无法在同一行上制作两个按钮。相反,Clear按钮位于按钮下方Add。这就是我所拥有的:

import * as React from "react";
import glamorous from "glamorous";
import { CSSProperties } from "glamorous/typings/css-properties";
import { graphql, InjectedGraphQLProps } from "react-apollo";
import { reduxForm, Field, FormProps, WrappedFieldProps } from "redux-form";
import { ItemListQuery, AddItemMutation, AddItemMutationVariables } from "../queries/types";

const Form = glamorous.form({
  display: "flex",
  flexFlow: "column nowrap",
  maxWidth: 320,
  "> *": { margin: "1 1 30px" }
});

const Label = glamorous.label({
  display: "flex",
  flexFlow: "column-reverse",
  "> :first-child": { marginTop: 5 }
});

const sharedInputStyles: CSSProperties = {
  backgroundColor: "#e9e9e9",
  border: "none",
  borderBottom: "2px solid #e9e9e9",
  padding: 6,
  outline: "none",
  transition: "all .2s ease-in",
  ":focus": {
    borderBottom: "2px solid #777",
  },
  fontSize: 14,
  fontWeight: 200
};

const ItemInput = glamorous.input(
  sharedInputStyles,
  { height: 24 }
);

const Button = glamorous.button({
  height: 40,
  backgroundColor: "#bababa",
  border: "none",
  outline: "none",
  ":focus": { border: "2px solid #777" },
  ":active": {
    backgroundColor: "#fff",
    border: "2px solid #dcdcdc"
  }
});

const ItemField = ({ input }: WrappedFieldProps<{}>) => (
  <ItemInput {...input} />
);

export interface AddItemProps extends
  InjectedGraphQLProps<{}>,
  FormProps<Partial<Item>, undefined, undefined> {
}

const AddItem = ({ handleSubmit }: AddItemProps) => (
  <Form onSubmit={handleSubmit}>
    <Label>
      Name
      <Field name="name" component={ItemField} />
    </Label>
    <Button type="submit">Submit</Button>
    <Button type="submit">Reset</Button>
  </Form>
);

我尝试display: "inline"像这样添加到 Button 组件:

const Button = glamorous.button({
  height: 40,
  backgroundColor: "#bababa",
  display: "inline",
  border: "none",
  outline: "none",
  ":focus": { border: "2px solid #777" },
  ":active": {
    backgroundColor: "#fff",
    border: "2px solid #dcdcdc"
  }
});

它没有做任何事情。接下来,我尝试将其更改为display: "inline-block"

const Button = glamorous.button({
  height: 40,
  backgroundColor: "#bababa",
  display: "inline-block",
  border: "none",
  outline: "none",
  ":focus": { border: "2px solid #777" },
  ":active": {
    backgroundColor: "#fff",
    border: "2px solid #dcdcdc"
  }
});

最后,我尝试overflow: "auto"在 Button 组件中添加:

const Button = glamorous.button({
  height: 40,
  backgroundColor: "#bababa",
  display: "inline-block",
  overflow: "auto",
  border: "none",
  outline: "none",
  ":focus": { border: "2px solid #777" },
  ":active": {
    backgroundColor: "#fff",
    border: "2px solid #dcdcdc"
  }
});

它也没有奏效。我所有的尝试都没有对按钮做任何事情。如何将按钮放在表单上均匀分布的同一行上?

4

1 回答 1

0

我知道已经有一段时间了,但也许它会帮助某人:

问题出在Form元素上。它具有 flex 列的样式,这就是为什么它将其子项放置在新行上的原因。如果您在按钮周围添加一个包装器,即使是一个简单的 div,它们的行为也会如您所愿:

const AddItem = ({ handleSubmit } ) => (
  <Form onSubmit={handleSubmit}>
    <Label>
      Name
      <ItemField />
    </Label>
    <div>
      <Button type="submit">Submit</Button>
      <Button type="submit">Reset</Button>
    </div>
  </Form>
);

这是一个稍微修改的代码片段: https ://stackblitz.com/edit/react-vgnjjz

于 2017-08-11T14:04:30.790 回答