Skip to content

Commit

Permalink
[Field] Refactor data-field style hook to data-valid/data-invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Oct 16, 2024
1 parent 18256f6 commit d928757
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 43 deletions.
8 changes: 4 additions & 4 deletions docs/data/components/field/UnstyledFieldAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='invalid']:not([data-pending]) {
&[data-invalid]:not([data-pending]) {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
&[data-field='valid']:not([data-pending]) {
&[data-valid]:not([data-pending]) {
border-color: green;
background-color: rgb(0 255 0 / 0.1);
}
Expand All @@ -117,12 +117,12 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='invalid']:not([data-pending]) {
&[data-invalid]:not([data-pending]) {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
&[data-field='valid']:not([data-pending]) {
&[data-valid]:not([data-pending]) {
box-shadow: 0 0 0 3px rgba(100 200 100 / 0.3);
}
}
Expand Down
8 changes: 4 additions & 4 deletions docs/data/components/field/UnstyledFieldAsync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='invalid']:not([data-pending]) {
&[data-invalid]:not([data-pending]) {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
&[data-field='valid']:not([data-pending]) {
&[data-valid]:not([data-pending]) {
border-color: green;
background-color: rgb(0 255 0 / 0.1);
}
Expand All @@ -117,12 +117,12 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='invalid']:not([data-pending]) {
&[data-invalid]:not([data-pending]) {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
&[data-field='valid']:not([data-pending]) {
&[data-valid]:not([data-pending]) {
box-shadow: 0 0 0 3px rgba(100 200 100 / 0.3);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
Expand All @@ -57,7 +57,7 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
Expand All @@ -57,7 +57,7 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
Expand Down
8 changes: 4 additions & 4 deletions docs/data/components/field/UnstyledFieldPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='valid'][data-dirty] {
&[data-valid][data-dirty] {
border-color: green;
background-color: rgb(0 255 0 / 0.1);
}
&[data-field='invalid'][data-touched][data-dirty] {
&[data-invalid][data-touched][data-dirty] {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
Expand All @@ -73,12 +73,12 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='valid'][data-dirty] {
&[data-valid][data-dirty] {
border-color: green;
box-shadow: 0 0 0 3px rgba(100 200 100 / 0.3);
}
&[data-field='invalid'][data-touched][data-dirty] {
&[data-invalid][data-touched][data-dirty] {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
Expand Down
8 changes: 4 additions & 4 deletions docs/data/components/field/UnstyledFieldPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='valid'][data-dirty] {
&[data-valid][data-dirty] {
border-color: green;
background-color: rgb(0 255 0 / 0.1);
}
&[data-field='invalid'][data-touched][data-dirty] {
&[data-invalid][data-touched][data-dirty] {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
Expand All @@ -73,12 +73,12 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='valid'][data-dirty] {
&[data-valid][data-dirty] {
border-color: green;
box-shadow: 0 0 0 3px rgba(100 200 100 / 0.3);
}
&[data-field='invalid'][data-touched][data-dirty] {
&[data-invalid][data-touched][data-dirty] {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
Expand Down
4 changes: 2 additions & 2 deletions docs/data/components/field/UnstyledFieldServerError.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
Expand All @@ -106,7 +106,7 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
Expand Down
4 changes: 2 additions & 2 deletions docs/data/components/field/UnstyledFieldServerError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
Expand All @@ -108,7 +108,7 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
Expand Down
4 changes: 2 additions & 2 deletions docs/data/components/field/field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ The `onChange` validation is debounced by 500ms to avoid firing a network reques

## Styling

The `[data-field="valid"]` and `[data-field="invalid"]` style hooks determine if the field is invalid or not:
The `[data-valid]` and `[data-invalid]` style hooks determine if the field is invalid or not:

```jsx
<Field.Root>
Expand All @@ -229,7 +229,7 @@ The `[data-field="valid"]` and `[data-field="invalid"]` style hooks determine if
```

```css
.FieldControl[data-field='invalid'] {
.FieldControl[data-invalid] {
color: red;
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/data/components/form/FormIntroduction/system/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
Expand All @@ -95,7 +95,7 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
Expand Down
4 changes: 2 additions & 2 deletions docs/data/components/form/FormIntroduction/system/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const FieldControl = styled(Field.Control)`
padding: 6px;
font-size: 100%;
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
background-color: rgb(255 0 0 / 0.1);
}
Expand All @@ -97,7 +97,7 @@ const FieldControl = styled(Field.Control)`
border-color: #0078d4;
box-shadow: 0 0 0 3px rgba(0 100 255 / 0.3);
&[data-field='invalid'] {
&[data-invalid] {
border-color: red;
box-shadow: 0 0 0 3px rgba(255 0 0 / 0.3);
}
Expand Down
24 changes: 12 additions & 12 deletions packages/mui-base/src/Field/Root/FieldRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('<Field.Root />', () => {
});
});

it('should apply [data-field] style hooks to field components', () => {
it('should apply [data-valid] and [data-invalid] style hooks to field components', () => {
render(
<Field.Root>
<Field.Label data-testid="label">Label</Field.Label>
Expand All @@ -106,9 +106,9 @@ describe('<Field.Root />', () => {
const description = screen.getByTestId('description');
let error = screen.queryByTestId('error');

expect(control).not.to.have.attribute('data-field');
expect(label).not.to.have.attribute('data-field');
expect(description).not.to.have.attribute('data-field');
expect(control).not.to.have.attribute('data-valid');
expect(label).not.to.have.attribute('data-valid');
expect(description).not.to.have.attribute('data-valid');
expect(error).to.equal(null);

fireEvent.focus(control);
Expand All @@ -118,10 +118,10 @@ describe('<Field.Root />', () => {

error = screen.getByTestId('error');

expect(control).to.have.attribute('data-field', 'invalid');
expect(label).to.have.attribute('data-field', 'invalid');
expect(description).to.have.attribute('data-field', 'invalid');
expect(error).to.have.attribute('data-field', 'invalid');
expect(control).to.have.attribute('data-invalid', '');
expect(label).to.have.attribute('data-invalid', '');
expect(description).to.have.attribute('data-invalid', '');
expect(error).to.have.attribute('data-invalid', '');

act(() => {
control.value = 'value';
Expand All @@ -131,9 +131,9 @@ describe('<Field.Root />', () => {

error = screen.queryByTestId('error');

expect(control).to.have.attribute('data-field', 'valid');
expect(label).to.have.attribute('data-field', 'valid');
expect(description).to.have.attribute('data-field', 'valid');
expect(control).to.have.attribute('data-valid', '');
expect(label).to.have.attribute('data-valid', '');
expect(description).to.have.attribute('data-valid', '');
expect(error).to.equal(null);
});

Expand Down Expand Up @@ -281,7 +281,7 @@ describe('<Field.Root />', () => {

fireEvent.change(control, { target: { value: 't' } });

expect(control).to.have.attribute('data-field', 'invalid');
expect(control).to.have.attribute('data-invalid', '');
expect(control).to.have.attribute('aria-invalid', 'true');
});
});
Expand Down
7 changes: 6 additions & 1 deletion packages/mui-base/src/Field/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ export const STYLE_HOOK_MAPPING = {
if (value === null) {
return null;
}
if (value) {
return {
'data-valid': '',
};
}
return {
'data-field': value ? 'valid' : 'invalid',
'data-invalid': '',
};
},
};

0 comments on commit d928757

Please sign in to comment.