我正在着手开发glamorous,这是一个React组件样式模块。我有两个正在尝试设计样式的按钮:Add和Clear。我尝试将这两个按钮放在同一行,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"添加到按钮组件,如下所示:
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"添加到按钮组件中:
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"
}
});它的效果不是很好。我所有的尝试都没有对按钮做任何事情。如何将按钮放置在窗体上等间距的同一行上?
发布于 2017-08-11 22:04:31
我知道已经有一段时间了,但也许这会对某些人有帮助:
问题出在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
https://stackoverflow.com/questions/44251662
复制相似问题